[SOLVED] Programming PIC18f4520 to generate 2500Hz Square wave with timer 1

Status
Not open for further replies.

pravin b

Member level 5
Joined
May 20, 2012
Messages
85
Helped
2
Reputation
4
Reaction score
1
Trophy points
1,288
Location
Mumbai, India
Visit site
Activity points
2,083
Hello Friends,
I am Programming PIC18f4520 to generate 2500Hz Square wave with timer 1, no prescaler.
However I have achieved this using following piece of code;
Code:
void delay()
{
T1CON=0x80;
TMR1L=0x0C;
TMR1H=0xFE;
TMR1ON=1;
while(TMR1IF==0);
TMR1ON=0;
TMR1IF=0;
}

but when I change the above code like;
Code:
void delay()
{
T1CON=0x80;
TMR1L=0x0C;
TMR1H=0xFE;
TMR1ON=1;
while(TMR1IF==1)
{
TMR1ON=0;
TMR1IF=0;
}
}

some random value of delay is generated; though according to me both the code are same! So I am expecting same output for both the codes i.e. 0.2mses delay or 2500Hz square wave; which is not happening...why?
Please let me know if I am missing something.
Thanks & Regards,
Pravin
 

Look more carefully. The two functions behave very differently. Both codes are NOT the same.

Maybe format your code better and you will see the difference more clearly.

.... or maybe draw a flow chart for each?
 
Notice how the second piece of code doesn't WAIT for the flag to be raised. It checks if the flag is raised and if not, just skips the while loop and returns back to the main code. The first code waits for the flag to be raised - this is the delay you want: to wait until the flag is raised.

Hope this helps.
Tahmid.
 
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…