CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Problem using Timer0 & Timer1 on PIC 16F610

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
davefromnj



Joined: 03 May 2007
Posts: 42

View user's profile Send private message

Problem using Timer0 & Timer1 on PIC 16F610
PostPosted: Tue Jan 27, 2009 8:25 am     Reply with quote

Hello All,

A friend built a program around a PIC 16F887 / PICKIT2 demo board that worked properly.

He used Timer0 to flash an LED at 2kHz, and Timer1 to flash an LED at a slower 800Hz. Timer0 was setup with an interrupt and within the interrupt, the LED toggles states and Timer0 gets reloaded with the same value that translates into a 2kHz flashrate.

Timer1 was setup in the same way as Timer0, with an interrupt and within the interrupt the other LED gets toggled and Timer1 gets reloaded with a value that will translate into an 800kHz flashrate.

This worked great, but he only needed like 6 I/O pins for his project and the 16F887 has like 44pins.




I suggested he try it out on a PIC16F610 since it's the smallest packaged 16F PIC that advertises having a Timer0 and Timer1.



He built his project up again with the LEDs, however now Timer0 and Timer1 appear to be interacting with each other. When Timer 1 expires, it appears to turn off the LED that is normally controlled by Timer0.


he's using port C on the chip to control the LEDs, and has used the setup_comparator(NC_NC_NC_NC) function to turn off the comparators in his main routine.



What could be causing this?


There is nothing in the while loop, the toggling is only controlled in the interrupts.




When he emails me the code I'll paste it here.
Ttelmah
Guest







PostPosted: Tue Jan 27, 2009 8:58 am     Reply with quote

The obvious 'danger', is in 'toggling' LED's. Don't. Toggle an internal bit in a variable, and output this to the LED's. The most likely problem is that the resistors in series with the LEDs, are not quite large enough.
You have to remember that with any 'toggle' operation, you are performing a RMW cycle on the port. So the voltages on the port pins are read (R), modified (toggling the bit - M), and then written back to the port (W). If a line driving an LED, doesn't have a big enough pull up resistor, it may 'read' as a low, and get written back as a low. Now haven't checked the data sheet, and you don't say which pins are involved, but (for example), if the older chip, had TTL input thresholds on the pins, and the new chip has Schotty thresholds on the pins you are now using, the voltage required to be seen as a 'high', could change from perhaps 2.4v, to 4v.
So avoid the 'toggle', and write the pins directly, from an internally held value.

Best Wishes
davefromnj



Joined: 03 May 2007
Posts: 42

View user's profile Send private message

PostPosted: Tue Jan 27, 2009 9:53 am     Reply with quote

Ttelmah wrote:
The obvious 'danger', is in 'toggling' LED's. Don't. Toggle an internal bit in a variable, and output this to the LED's. The most likely problem is that the resistors in series with the LEDs, are not quite large enough.
You have to remember that with any 'toggle' operation, you are performing a RMW cycle on the port. So the voltages on the port pins are read (R), modified (toggling the bit - M), and then written back to the port (W). If a line driving an LED, doesn't have a big enough pull up resistor, it may 'read' as a low, and get written back as a low. Now haven't checked the data sheet, and you don't say which pins are involved, but (for example), if the older chip, had TTL input thresholds on the pins, and the new chip has Schotty thresholds on the pins you are now using, the voltage required to be seen as a 'high', could change from perhaps 2.4v, to 4v.
So avoid the 'toggle', and write the pins directly, from an internally held value.

Best Wishes



Thanks, I'll ask him what his series resistor values are.



He did use a variable to drive the outputs like this:


within the timer0 interrupt routine:
var1 = ~var1;
output_bit(PIN_C4, var1);




and

within the timer1 interrupt routine:
var2 = ~var2;
output_bit(PIN_C3, var2);



For some reason when timer 1 expires it turns off / on its LED and the one on the other pin.

One thing to note, if only one timer interrupt is used, either timer0 or timer1 the leds will flash at their appropriate rate, its only when they are both used simultaneously that the LEDs almost have what looks like jitter.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group