[SOLVED] CRO VERIFIED ACCURATE 1usec C-CODE DELAY... Please

Status
Not open for further replies.

abdul991

Junior Member level 1
Joined
Feb 8, 2013
Messages
16
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Visit site
Activity points
1,402
hi, all...

Can anyone.. give me C-program function for a perfect 1usec or 2usec Delay using 12Mhz-PIC18f ,MPLAB.. Which is "verified using CRO.."
any other less or equal to 20Mhz Xtal is also OK....
Please..
 

The 18F4550 is running at 20MHz oscillator frequency, but the internal frequency is 48MHz

4/48 MHz = 0.0833333333333333 us

1 instruction cycle is 0.0833333333333333 us

0.0833333333333333 us * x = 1 us

x = 12

0.0833333333333333 us * y = 2 us

y = 24

1 nop instruction is 0.0833333333333333 us

so call 12 nop instructions for 1 us delay and 24 nop instructions for 2 us delay.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void delay_1us(){
    _asm{
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
    }
}
 
//call delay_1us once for is and twice for 2 us delay.

 
Last edited:

Thanks jayanth... i didn;t understand the part where you mentioned internal frequency is 48MHz although you are using 20MHz xtal.

But lately after much struggle i was allowed to use a CRO to check my delay program. Here is the 'verified' o/p. I use 12Mhz and 18f452
mplab

Code:
void main(void)
{
 int i;
         TRISBbits.TRISB7 = 0;

		 while(1){
         
         PORTBbits.RB7=0;
         //for(i=0;i<3;i++);
         DS1820_DelayUs(3);
         PORTBbits.RB7=1;
         //for(i=0;i<3;i++);
         DS1820_DelayUs(3);
       }
}


void  Delay(unsigned char a)
{
   int i,j;
   for(i = 0; i < a; i++)
   for( j = 0; j < 3; j++);
} 
     
void DS1820_DelayUs(int val)
{
int k;
for(k=0;k<val;k++); 
} 



/*******  

 
         Delay(1);       52usec LOW  AND  52USEC HIGH
         
         Delay(2);       88USEC LOW  88USEC HIGH
         Delay(3);       125USEC LOW  125USEC HIGH
 36 USEC INRCM FOR EACH SINGLE DELAY WITH FUNCTION CALLING TIME 16USEC
 **********************************
  PORTBbits.RB7=0;
         i=7;
         PORTBbits.RB7=1;
         i=5;    //     1.4usec low and 2.1usec high.       

 **********************************

for(i=0;i<1;i++); 13usec LOW  AND  14USEC HIGH
for(i=0;i<2;i++); 19usec LOW  AND  20USEC HIGH
for(i=0;i<3;i++); 26usec LOW  AND  26USEC HIGH

EACH FOR LOOP COUNT IS 7USEC DELAY

**********************************

DS1820_DelayUs(1);  25usec low and 26usec high
DS1820_DelayUs(2);  35usec low and 36usec high
DS1820_DelayUs(3);  44usec low and 45usec high

            ******/
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…