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.

Timer1 help

Status
Not open for further replies.
I've never used any timers and am about to try.

The idea:
Im using an LCD for a system, the details of the system are of no importance.

On power up I have the title of the system on the LCD.
after 2 seconds, I want to clear the screen.

I also have a keypad that I wish to scan.

Instead of writing characters to the display, execting a delay of 2 seconds, then clearing the screen; can i use a timer to interrupt the program and clear the screen.
this way i dont miss out on anytime scaning the keypad.


I am using a PIC16F72 which has Timer0 Timer 1 and Timer2.

With Timer1
If I set the Pre-Scaler to 1:8 does this mean the timer will increment every 8 cycles?
So its a 16bit timer, therefore it will count up to 65536.
For a for a delay of .5 seconds I will need to use 500,000 cycles (at 4MHz).
If I load the timer register with D'3036', It has to count 62500 cycles to reach 65536 for an overflow.
At a 1:8 pre-scaler (62500 x 8) I will get an overflow/interrupt every 500,000 cycles
which will give me an interurrpt every half second.

then I can count to 4 to get 2 seconds. or close to.

Am I On The Right Track?
Or Way Off??????
 
Last edited:
It is normally better to interrupt at a higher frequency like 50Hz and count interrupts to make longer delays. A free running 50Hz interrupt can be easily achieved with Timer2 - PR2=125 postscaler=10 prescaler=16. Calling a routine to clear the screen from the interrupt is a bad idea as your main routine may be accessing the LCD. A better idea would be to do your keyscan on interrupt and the LCD in the main code.

Mike.
 
Calling a routine to clear the screen from the interrupt is a bad idea as your main routine may be accessing the LCD.
Good thinking.
I totally looked over that.

Alright I'll give the keypad interrupt a go.

Thanks again Mike.
 
I've done this recently...

I'm using a PIC24 (this is my first microcontroller project, so don't take this as gospel).

I have a main processing loop that checks for "flags" and does something when a flag is set, e.g. update display. The main processing loop is a never ending while loop. I set flags and do minimal processing from the interrupts. Some of my flags include display update, sleep commands, anything to do with peripherals, etc.

My button processing is done in a 1ms interrupt, it does the debounce and any menu routines that I have to update . These routines are very short/fast. These routines then set the "display flag" for the main processing routine.

I'm currently using three timers, one for a wait routine (it's separate because I need it enabled before the others), one at 1 second for longer delay items and one at 1ms for button processing. I keep counters to keep track of what needs to be done when. For example, my device sleeps after three minutes of no button presses. I increment a counter in the 1 second timer, when it reaches 180, I set the processing flag for the sleep command. When the interrupt exits, it goes back to the main processing routine which checks for that flag -- if set, it sleeps.

Brad
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top