engineer khan
Member level 3
Hello every body!
please help me in producing exact time delay in c18 compiler, 1msec or 1sec
please help me in producing exact time delay in c18 compiler, 1msec or 1sec
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
// if Fosc=16MHz
void Tempo_1mS()
{
asm{
MOVLW 210 ;
MOVWF _N1; // 8 bit counter
ici:
nop ; //1
nop ;
nop ;
nop ;
nop ;
nop ;
nop ;
nop ;
nop ;
nop ; //10
nop ;
nop ;
nop ;
nop ;
nop ;
nop ; //16
decfsz _N1 ,F ; //decr counter, skip over next instruction if equal to zero
bra ici ;
nop;
nop;
nop;
nop; // add 4x0.25=1µS to round tempo pile poil to 500µS
} ;
}
// PIC18F serie : il faut timer0 sur 16 bits
// delais pour FOSC=16Mhz
void Delay_uS(unsigned int x);
void Delay_mS(unsigned int cnt);
void Delay_1S(void);
void Delay_uS(unsigned int x)
{unsigned int y;
y=x;
while(--y != 0);
}
void Delay_mS(unsigned int cnt)
{
#ifdef DEBUG
return;
#else
INTCONbits.TMR0IE=0; // no interrupt
T0CON=0b00000001; // Timer OFF, 16bits, internal, prescaler=1:4
TMR0H=0xFC; //hi(64537); // 1.0mS avec 1:4
TMR0L=0x19; //lo (65537);
while(cnt>0)
{
T0CONbits.TMR0ON=1;
while(INTCONbits.TMR0IF == 0); // wait over
INTCONbits.T0IF=0;
cnt--;
TMR0H=0xFC; //hi(64537); // 1.0mS avec 1:4
TMR0L=0x19; //lo (65537);
}
T0CON=0;
#endif
}
void Delay_1S(void)
{
INTCONbits.TMR0IE=0; // no interrupt
T0CON=0b00000111; // Timer OFF, 16bits, internal, prescaler=1:256
TMR0H=0xC2; //hi(49911); // 1.0 S avec 1:256
TMR0L=0xF7; //lo (49911);
T0CONbits.TMR0ON=1;
while(INTCONbits.TMR0IF == 0); // wait over
INTCONbits.T0IF=0;
T0CON=0;
TMR0H=0xC2; //hi(49911); // 1.0 S avec 1:256
TMR0L=0xF7; //lo (49911);
}