0- 15 seconds counter using 16f877a

Discussion about projects that used PIC Microcontroller, Hardware Interface, Programming Algorithm and etc......

Re: 0- 15 seconds counter using 16f877a

Postby Kromuald13 » Sat Oct 29, 2011 2:33 pm

Brian Griffin WROTE:Do not depend too much on the simulation. Interrupts are not simulated properly in software. Things can be wrongly simulated and this may produce even more misleading results. Test them on that chip.

For the Timer0, did you set it to 16-bit? All set up at the start? Double check the values.

If you want a one second interrupt from Timer0, you may need to double check the TMR0H and TMR0L values. The int_count variable is not probably neccessary at the first place.

Get a pencil and a paper and calculate this again, and come back to us if you have problems calculating it. :)

Hint: [ (Oscillator Freq.) / 4 ] / Prescaler = Timer0 Frequency.


Hello brian , nice to hear from you again ..
actually for pic 16f877a , TIMER0 is set to 8 bit .. , it is timer1 or timer2 which are of 16 bits ...
concerning the calculation . Among my readings , i did refer to this site : http://www.microcontrollerboard.com/pic ... orial.html
and from there , the Timer frquency calculation is different that ours .. very confused :? now

But based on your formula ,
4 Mhz (internal Oscillator)
the Timer Frequency , will be = -------------------------------------------------
4 * 256(maximun prescaler value)

Timer Frequency = 3.906KHz

so the delay brought in , will be of 25.6ms ....
Kromuald13
Novice
 
Posts: 26
Joined: Wed Feb 09, 2011 2:03 am

Re: 0- 15 seconds counter using 16f877a

Postby Brian Griffin » Sat Oct 29, 2011 3:11 pm

Kromuald13 WROTE:
Brian Griffin WROTE:Do not depend too much on the simulation. Interrupts are not simulated properly in software. Things can be wrongly simulated and this may produce even more misleading results. Test them on that chip.

For the Timer0, did you set it to 16-bit? All set up at the start? Double check the values.

If you want a one second interrupt from Timer0, you may need to double check the TMR0H and TMR0L values. The int_count variable is not probably neccessary at the first place.

Get a pencil and a paper and calculate this again, and come back to us if you have problems calculating it. :)

Hint: [ (Oscillator Freq.) / 4 ] / Prescaler = Timer0 Frequency.


Hello brian , nice to hear from you again ..
actually for pic 16f877a , TIMER0 is set to 8 bit .. , it is timer1 or timer2 which are of 16 bits ...
concerning the calculation . Among my readings , i did refer to this site : http://www.microcontrollerboard.com/pic ... orial.html
and from there , the Timer frquency calculation is different that ours .. very confused :? now

But based on your formula ,
4 Mhz (internal Oscillator)
the Timer Frequency , will be = -------------------------------------------------
4 * 256(maximun prescaler value)

Timer Frequency = 3.906KHz

so the delay brought in , will be of 25.6ms ....


The Timer0 can be also set at 16-bits. Read the datasheet thoroughly. The calculation and that website is the same anyway.

As I told you there are two methods of doing the Timer0 interrupts. One is you poll (check) whether the Timer0's flag is already up. Another one is the thing automatically triggers everytime the flag is up. In the website, they poll the timer flag continuously, leaving the system tied to only that job.

For the program in the website, the Timer0 register is set to 0, and 8-bits, so the flag will be up every 65.536ms. So on every 65.536ms, the counter is incremented by one until it reaches 15. When it hits 16, the counter restarts to zero.

The flag will be up every 65.536ms because the TMR0 had to count up to 256, so 256us * 256 = 65.536ms. Therefore when it hits 256 in the TMR0 register, the flag is raised.
PIC - UIC00B from Cytron (replacement for my broken PICKit 2), Pickit 3, MikroC for PIC
dsPIC - MikroC for dsPIC, mikromedia board (dsPIC33)
AVR - AVR Dragon
Parallax - Prop tool
User avatar
Brian Griffin
Enthusiast
 
Posts: 403
Joined: Mon Jan 17, 2011 9:36 am

Re: 0- 15 seconds counter using 16f877a

Postby robosang » Sat Oct 29, 2011 11:02 pm

Brian Griffin WROTE:The Timer0 can be also set at 16-bits. Read the datasheet thoroughly. The calculation and that website is the same anyway.


Actually as far as I use PIC16F877A, Timer0 is only 8-bit, there is no TMR0L or TMR0H, only TMR0 :) Anyway, you can still make it 16-bit by manipulating it in interrupt, adding an 8-bit TMR0H yourself, every interrupt of TMR0, increase TMR0H yourself in ISR further check whether the TMR0H is 00 (overflow), then you get yourself 16-bit TMR0 :mrgreen:

It confuse me a few thing. You uses #define _XTAL_FREQ 4000000 which mean you are using Fosc at 4MHz and since PIC16F877A does not have internal oscillator, you must be using external crystal or oscillator at 4MHz. Please do not use internal oscillator because internal oscillator refer to internal clock in PIC MCU, this PIC does not have one 8-)

If you are using 4MHz at XTAL_FREQ, frequency to either TMR0, TMR1 or TMR2 will be Fosc/4 which will offer 1MHz clock, not 4MHz. Using timer and pre-scaler and this frequency of oscillator, is not possible to get exact 1 second timing, you will have at least a few micro or milli seconds offset.
robosang
Expert
 
Posts: 1239
Joined: Wed Jun 10, 2009 5:37 pm

Re: 0- 15 seconds counter using 16f877a

Postby Brian Griffin » Sat Oct 29, 2011 11:08 pm

robosang WROTE:
Brian Griffin WROTE:The Timer0 can be also set at 16-bits. Read the datasheet thoroughly. The calculation and that website is the same anyway.


Actually as far as I use PIC16F877A, Timer0 is only 8-bit, there is no TMR0L or TMR0H, only TMR0 :) Anyway, you can still make it 16-bit by manipulating it in interrupt, adding an 8-bit TMR0H yourself, every interrupt of TMR0, increase TMR0H yourself in ISR further check whether the TMR0H is 00 (overflow), then you get yourself 16-bit TMR0 :mrgreen:

It confuse me a few thing. You uses #define _XTAL_FREQ 4000000 which mean you are using Fosc at 4MHz and since PIC16F877A does not have internal oscillator, you must be using external crystal or oscillator at 4MHz. Please do not use internal oscillator because internal oscillator refer to internal clock in PIC MCU, this PIC does not have one 8-)

If you are using 4MHz at XTAL_FREQ, frequency to either TMR0, TMR1 or TMR2 will be Fosc/4 which will offer 1MHz clock, not 4MHz. Using timer and pre-scaler and this frequency of oscillator, is not possible to get exact 1 second timing, you will have at least a few micro or milli seconds offset.


Oops. I may have written the wrong thing about the Timer0. Maybe I had confused it with another datasheet. You are correct that the PIC16F877A has Timer0 which is in the 8-bit form. Apologies for the confusion.

For that application then, it should be 256uS * 256 = 65.536ms per interrupt.

The thing will be off by a few miliseconds for sure due to rounding errors and interrupt latencies. The actual 'one second' if interrupt latency is not included is 0.98304 seconds. :)
PIC - UIC00B from Cytron (replacement for my broken PICKit 2), Pickit 3, MikroC for PIC
dsPIC - MikroC for dsPIC, mikromedia board (dsPIC33)
AVR - AVR Dragon
Parallax - Prop tool
User avatar
Brian Griffin
Enthusiast
 
Posts: 403
Joined: Mon Jan 17, 2011 9:36 am

Re: 0- 15 seconds counter using 16f877a

Postby Kromuald13 » Sun Oct 30, 2011 1:47 pm

hey greatings Brain &Robosang ..
thx guys for your replies .. humm so for , i haven't tried the programm pratically yet , due to some material limitations .
Humm , i would like to ask you guys , besides using interrupt , is ther anyother way of doing a 15 secinds countdown ??
Kromuald13
Novice
 
Posts: 26
Joined: Wed Feb 09, 2011 2:03 am

Re: 0- 15 seconds counter using 16f877a

Postby ober » Tue Nov 01, 2011 10:44 pm

You can try using delay, normal routine to delay until 15 seconds. That is the easier I think. Can use very large variable such as long long to get 64-bit variable and delay 1ms seconds in a for loop. It will not be exactly 1 second to 15 seconds due to latency of calling and exact routine for looping and comparing variable, it is the simplest. Not to forget, you cannot do anything during delay.
Ober Choo
Cytron Technologies Sdn Bhd
www.cytron.com.my
User avatar
ober
Moderator
 
Posts: 1486
Joined: Wed Apr 15, 2009 1:03 pm

Re: 0- 15 seconds counter using 16f877a

Postby demonboy » Tue Nov 01, 2011 11:19 pm

Hi, i'm trying in using interrupt as well. I heard interrupt is better in delaying 15s because you can perform next command. Otherwise,in normal delay we can't perform next program before the delay completed .
I trying to use tutorial as shown in timer1 of this tutorial. http://www.microcontrollerboard.com/pic ... orial.html
I've calculated what Count value should be which is 289.
But i have no ide how to insert it in program, because if i insert the exact program in those link, it will keep looping because of while (1).
Can someone help me on it??
demonboy
Freshie
 
Posts: 5
Joined: Sun Oct 30, 2011 10:18 pm

Re: 0- 15 seconds counter using 16f877a

Postby demonboy » Mon Nov 07, 2011 1:38 am

Hy guys,
I really confused in using timer of delaying a program, this timer will canceled if there is input
Can some one help me??
I've tried everything but still can produce correct program.:(
demonboy
Freshie
 
Posts: 5
Joined: Sun Oct 30, 2011 10:18 pm

Re: 0- 15 seconds counter using 16f877a

Postby robosang » Wed Nov 09, 2011 8:56 pm

Interrupt is interrupt, it can apply in many cases.

Timer is timer, basically it is use for counting pulses. If the pulses come from constant frequency, you can count time 8-) .

By using timer, it can be polling method or interrupt method. If you are using comparison method, basically that is polling as you need to get the data and compare it and you know the status, else timer will keep increase over time and you don know it.

Interrupt mean once you configure correctly, when certain period has pass, the program will be interrupt and need to serve it.

Maybe is not helpful. But you need to understand what is the function of timer, interrupt and polling.
robosang
Expert
 
Posts: 1239
Joined: Wed Jun 10, 2009 5:37 pm

Previous

Return to PIC Microcontroller

Who is online

Users browsing this forum: No registered users and 7 guests