Timers and TMR0

Electronics Computer Programming Q&A
Post Reply
cammhifi
Posts: 10
Joined: Mon Sep 06, 2004 1:01 am
Location: South Africa
Contact:

Timers and TMR0

Post by cammhifi »

Hi
I am trying to learn about TMR0 in a pic 12C509A.
I would like to detect a high on one of the pis then start the timer. When a low is detected the timer must be stopped and the valued stored in a file.
Any help would be appreciated
Mac
User avatar
philba
Posts: 2050
Joined: Tue Nov 30, 2004 1:01 am
Location: Seattle
Contact:

Re: Timers and TMR0

Post by philba »

This is mostly pretty easy if you know a programming language that is supported on the PIC.<p>However, you will need to understand what kind of a device is the hooked up to the pins. a mechanical switch or an eletronic device. If its electronic, you'll need to know if its "open collector" or not. This will determine if you have a "pull-up" resistor on the pin or not (open collector or switches will need a 10K resistor from +V to the pin. The switch will short the pin to ground (0V) when closed. when the switch is open, the pull-up will cause the pin to see +5 (or 3.3V or what ever your supply voltage is). <p>The simplest way to do this is to write a loop in software that repetitively reads the pin. If voltage at the pin is +5V, you will read a 1 and if 0V is at the pin, you will read a 0. The datasheet will tell you at what voltage it goes from 1 to 0. Anyway, when you see the pin gor from 0 to 1, you write the special function register (SFR) for the timer (OPTION) to set the timer pre-scale values and then set the timer SFR to FF. When you see it go from 1 to 0, you read the value of the timer SFR, negate it (timer0 counts down) and then store it. You don't really stop the timer.<p>On more advanced chips like the 12F629, you could use interrupt-on-change for the pin and then there is no need to sit an loop looking at the port. When the pin changes you read it and do the appropriate thing. Also, it has a 16 bit timer which would allow you to measure longer duration events (or use a faster clock rate).<p>I'm not sure I understand why you are using the 12C509A. You can only program it once and if that's not right, you throw it away and start with a new chip. For learning, the F (flash) series is 1000X better - you can program it over and over..
cammhifi
Posts: 10
Joined: Mon Sep 06, 2004 1:01 am
Location: South Africa
Contact:

Re: Timers and TMR0

Post by cammhifi »

Hi Philba
I use an 84F to do my developement on and by using the files common to the 509A I can translate from one pic to the other. When my program works I burn it into the 509A. If I understand you right is it just a matter of Clearing TMR0 to start it and then copying TMR0 to W then storing it into a file? I haven't yet tried the flash 12Fxx devices.
Regards
Mac
User avatar
philba
Posts: 2050
Joined: Tue Nov 30, 2004 1:01 am
Location: Seattle
Contact:

Re: Timers and TMR0

Post by philba »

<blockquote><font size="1" face="Verdana, Helvetica, sans-serif">quote:</font><hr>Originally posted by Mac:
Hi Philba
I use an 84F to do my developement on and by using the files common to the 509A I can translate from one pic to the other. When my program works I burn it into the 509A. If I understand you right is it just a matter of Clearing TMR0 to start it and then copying TMR0 to W then storing it into a file? I haven't yet tried the flash 12Fxx devices.
Regards
Mac
<hr></blockquote><p>basically, yes. It appears on the 509 that you can't actually stop the timer. <p>I guess you must have a cheap source of the C509s.<p>On the flashing 12F devices, it depends on your programmer software. <p>By the way, I generally do in-circuit programming (ICSP) which allows me to test in the target environment. I have found that to be invaluable.
cammhifi
Posts: 10
Joined: Mon Sep 06, 2004 1:01 am
Location: South Africa
Contact:

Re: Timers and TMR0

Post by cammhifi »

Hi
The 509A is about a third of the price of an 84A and for most small projects they are ok, I think it's a waste using larger chips in small projects. Obviously it depends on the number of pins one needs. I find that the timer is the most difficult to learn and come to grips with. Unfortunately I keep comparing it to stop watch where one just presses the button to start and pressing it again to stop it. With the pic it's just not clear enough on the code needed to operate it, I was hoping to have a routine that one could add into ones code....if only it was that simole but I'll persevere.<p>Cheers
Mac
User avatar
philba
Posts: 2050
Joined: Tue Nov 30, 2004 1:01 am
Location: Seattle
Contact:

Re: Timers and TMR0

Post by philba »

The F84A is a pretty old chip. There are newer and faster 18 pin chips that are much cheaper. have you checked out the price of the 16F628A? It is about 1/3 the price of the F84A ($3.96 vs $1.73 from Mouser). It's a pretty nice chip (3 timers, 2 comparators, serial I/O, 18 pins, 20 mhz). It's pin compatible with the F84A. The one I really like is the 16F88 which has all that and 7 10 bit ADCs. But the really big win is it supports boot-loading - no programmer required once you've burned the boot loader in. Totally awesome for field upgrading the firmware. My one gripe is they moved the serial I/O pins around so it's not pin compatible with the 628A.<p>Have you looked around on the internet? there is a ton of free PIC code. piclist.com is worth a look.
Post Reply

Who is online

Users browsing this forum: No registered users and 56 guests