Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Timer0 Question

Status
Not open for further replies.

cne

New Member
Hi,

I am using flowcode to create a program that counts minuites on a seven segment display. I am trying to figure out how it works, but I can't seem to figure out how to count minuites.
I also don't understand the prescaler rate.
How to I convert this value into a useful time unit?

Thanks,
Cameron
 
I'm using Timer1 to count my clock but I'll reply anyway:

The prescaler is like an external counter for the timer to allow you to count longer time periods. For example, if the prescaler is set to 1:4, the Timer will only increment every 4 instruction cycles.


In my example I am using Timer1 (16-bit), I have the prescaler set to 1:1 and am using a 20MHz crystal and I want to update a register every 10 milliseconds in order to use that as my timebase.
With a 20MHz clock, one second will take 5,000,000 instruction cycles. That means to have a delay of 10 milliseconds I need to call an interrupt every 50,000 instruction cycles.
Because Timer1 is a 16 bit timer I can just reset Timer1 to about* d'15,536' every interrupt. d'15,536' is 65,536 (Timer1 overflow) minus 50,000.

With Timer1 set up like that I will have an interrupt called every 10 milliseconds.

So to count seconds I simply set a register to 100 and decrement it every interrupt. When it reaches 0 I update the seconds register and reset the first register to 100.

I hope that helped.






* You also need to take into account the amount of instruction cycles it takes to reset the Timer. For example, if it takes 15 instruction cycles from when Timer1 overflow occured until the timer has been set to your new value, then add that to the adusted Timer value.
 
Hi,

I am using flowcode to create a program that counts minuites on a seven segment display. I am trying to figure out how it works, but I can't seem to figure out how to count minuites.
I also don't understand the prescaler rate.
How to I convert this value into a useful time unit?

Thanks,
Cameron

hi,
Look at this attached file.
 

Attachments

  • PicMultiCalc..exe
    340 KB · Views: 393
Here is what I have so far...

Flowcode.jpg

Preload TMR0=4
Each intterupt increments int_count
If int_count=61, 1 second passed
Increment min_count
If min_count=60, 1 minuite passed
Decrement Digit
Some math regarding 10's overflow

After about 40min the timer looses accuracy to about 1.5sec

Cameron
 
After about 40min the timer looses accuracy to about 1.5sec
Cameron

That's could be because the interrupt frequency is 61.035 Hz and you can only count 61 per second.

That means that you will lose a second every 61/0.035 counts.

So every time your counter (which counts to 61) does (61/0.035) 1743 counts, you will lose a second. That's about every (1743/60) 29 minutes.

That equals about 1 - 1.5 seconds every 40 minutes ;)
 
Google "Roman Black". Mr RB (who posts here), has code on his web site that will fix this.
 
Haven't been here in a while, (big workload this year) but here's the link for simple accurate 1 second clock generation from any XTAL;


Thanks,

But how do I enter this C code into my flowchart.
I have never used C before.
 
Last edited:
I have found an icon that C code can be entered into, but when I pasted the code, it showed errors, such as "bres" term unknown.
 
I also want to add a function to the program where, at a certain point, a variable is stored in the eeprom. At another point in the program, that data is retrieved and stored in another variable.
This is so when the power is turned off, the PIC remembers the preset time.
For example...

Set Macro
1.Get keypad number for time preset
2.Store number in eeprom

Read Switch
Start Timer Macro
1. Get data in eeprom
2. Start TMR0
3. Count down from that number
 
hi everyone ı want to stop timer0.there are two signal and first signal is applied pic16f877 re01 pin timer will begin to count and then when the second signal is come it will stop.ı will calculate cos fi.could u help me please
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top