Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

how to make exact delay in c18 compiler

Status
Not open for further replies.

engineer khan

Member level 3
Joined
Aug 31, 2012
Messages
66
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Activity points
1,833
Hello every body!
please help me in producing exact time delay in c18 compiler, 1msec or 1sec
 

configure timer for more accurate delay.
 

hello,

if you don't use interrupt , you can count number of elementary MCU Cycles.
1 cycle duration= 4/ (Fosc in MHz) in µsec

Code:
// 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
 } ;
 }


if you want to use Interrupt timer
initial value depend of your FOSC (or Quartz) .. prescaler value ...timer 8 bit or 16 bits ..
ans if you don't have an exact timer value ,you can add some "NOP" asm code to
get an accurate time .. (depend also of accuacy of Quartz (<20ppm ?)


any example :

Code:
// 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);
}



have a look on This
even it is not C18 !
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top