Haberler:

Foruma Resim Yükleme ve Boyut Sınırlaması ( ! )  https://bit.ly/2GMFb8H

Ana Menü

Home made delay.c

Başlatan mihri, 11 Ekim 2008, 00:06:46

mihri

Hazırladığım delay kütüphanesini paylaşıyorum. Şimdilik 18f serisi için sadece MelayMs() fonksiyonu var ama DelayUs() fonksiyonuda eklenecek. Kullandığım derleyici Hi-Tech 9+

//#define PIC_CLK 4000000
//#define DLY_CLR_WDT
    
/* HI-TECH PICC / PICC-Lite compiler */
#if	defined(__PICC__) || defined(__PICCLITE__)


    #if defined PIC_CLK

        #define _XTAL_FREQ PIC_CLK
    
  
        #define DelayCl __delay
        #define DelayUs __delay_us 
        #define DelayMs __delay_ms

    #else
	    #error delay.c - please define PIC_CLK correctly
    #endif

#endif


/* HI-TECH PICC-18 compiler */
#if	defined(__PICC18__)

    unsigned char var1;
    unsigned char var2;
    

#if (PIC_CLK == 4000000)

	#define dly1u asm("nop")
	#define dly2u dly1u;dly1u
	
	#define cnt1 0xc5
    #define cnt2 0x01
					
	
#elif (PIC_CLK == 8000000)

    #define dly500n asm("nop")
	#define dly1u dly500n;dly500n
	#define dly2u dly1u;dly1u
	
	#define cnt1 0x8f
    #define cnt2 0x02


	
#elif (PIC_CLK == 10000000)

	#define dly400n asm("nop")
	
	#define cnt1 0xf3
    #define cnt2 0x02
	
		
#elif (PIC_CLK == 12000000)

	#define dly400n asm("nop")
	
	#define cnt1 0x57
    #define cnt2 0x03
	
	
#elif (PIC_CLK == 16000000)

	#define dly250n asm("nop")
	#define dly500n dly250n;dly250n
	#define dly1u dly500n;dly500n
	#define dly2u dly1u;dly1u
	
	#define cnt1 0x1f
    #define cnt2 0x04
	
	
#elif (PIC_CLK == 20000000)

	#define dly200n asm("nop")
	#define dly400n dly200n;dly200n
	#define dly1u dly400n;dly400n;dly200n
	#define dly2u dly1u;dly1u
	
	#define cnt1 0xe7
    #define cnt2 0x04
	
	
#elif (PIC_CLK == 32000000)

    #define cnt1 0x3f
    #define cnt2 0x07

	#define dly125n asm("nop")
	#define dly250n dly125n;dly125n
	#define dly500n dly250n;dly250n
	#define dly1u dly500n;dly500n
	#define dly2u dly1u;dly1u
	
#elif (PIC_CLK == 40000000)

#define cnt1 0xcf
#define cnt2 0x08


	#define dly100n asm("nop")
	#define dly200n dly100n;dly100n
	#define dly400n dly200n;dly200n
	#define dly1u dly400n;dly400n;dly200n
	#define dly2u dly1u;dly1u
	
		
	
#else
	#error delay.c - please define PIC_CLK correctly

	
#endif

	#define __dly() { \
		var1=cnt1;\
		var2=cnt2;\
		asm("decfsz	_var1,f");\
		asm("bra	$+4");\
		asm("decfsz	_var2, f");\
		asm("bra	$-6");}\
		

void DelayMs(unsigned int cnt)
{
	
    do 
    {
	    __dly();
	    #if defined DLY_CLR_WDT
		    CLRWDT();
		#endif
	} while(--cnt);
}
#endif
"Eppur si muove!"

zafzaf82

paylaşım için çok sağol
 mihri.
Mikroişlemci bi araçtır, amaç değil.....

mihri

delay.h
#define _XTAL_FREQ 40000000
#define DelayClrWdt
extern void DelayMs(unsigned char cnt);


delay.c
//Delay Rutini V1.1
// >Mihri<

#include "htc.h"
#include "delay.h"
/* HI-TECH PICC compiler */
	#if (_XTAL_FREQ == 4000000)
		#define dly1u _delay(1)
		#define dly2u _delay(2)
	#elif (_XTAL_FREQ == 8000000)
		#define dly1u _delay(2)
		#define dly2u _delay(4)
	#elif (_XTAL_FREQ == 10000000)
		#define dly1u _delay(2)
		#define dly2u _delay(5)	
	#elif (_XTAL_FREQ == 16000000)
		#define dly1u _delay(4)
		#define dly2u _delay(8)
	#elif (_XTAL_FREQ == 20000000)
		#define dly1u _delay(5)
		#define dly2u _delay(10)
	#elif (_XTAL_FREQ == 32000000)
		#define dly1u _delay(8)
		#define dly2u _delay(16)
	#elif (_XTAL_FREQ == 40000000)
		#define dly1u _delay(10);
		#define dly2u _delay(20);
	#else
		#error delay.c - please define _XTAL_FREQ correctly
	#endif

#if	defined(__PICC__)

	#define DelayMs __delay_ms
	#define DelayUs __delay_us

#endif

/* HI-TECH PICC-18 compiler */

#if	defined(__PICC18__)

	
	#if _XTAL_FREQ == 4000000
		#define DelayUs(x) _delay(x)
	#elif _XTAL_FREQ == 8000000
		#define DelayUs(x) _delay(x); _delay(x)
	#elif _XTAL_FREQ == 10000000
		#define DelayUs(x) _delay(x);_delay(x);_delay(x/2)
	#elif _XTAL_FREQ == 16000000
		#define DelayUs(x) _delay(x);_delay(x);_delay(x);_delay(x)
	#elif _XTAL_FREQ == 20000000
		#define DelayUs(x) _delay(x);_delay(x);_delay(x);_delay(x);_delay(x)
	#elif _XTAL_FREQ == 32000000
		#define DelayUs(x) _delay(x);_delay(x);_delay(x);_delay(x);_delay(x);_delay(x);_delay(x);_delay(x)
	#elif _XTAL_FREQ == 40000000
		#define DelayUs(x)  _delay(x);_delay(x);_delay(x);_delay(x);_delay(x);_delay(x);_delay(x);_delay(x);_delay(x);_delay(x)
	#else
		#error delay.c - please define _XTAL_FREQ correctly
	#endif

	
	void DelayMs(unsigned char cnt)
	{
		unsigned char	i;
		do {
			i = 4;
			do {
				DelayUs(250);
				#if	defined(DelayClrWdt)
					CLRWDT();
				#endif
			} while(--i);
		} while(--cnt);
	}

#endif
"Eppur si muove!"