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.

TMR0 gives incorrect timing

Status
Not open for further replies.

skyrock

New Member
i've worked a test code using TMR0 to produce a 1sec interval but when I switched different power (9v ~ 15v) supplied to 7805 gives different results.. I notice there's no resistor connected from 7805 to the IC, does that gives problem?

Can anyone explain a little how internal oscillator/TMR0 works?

///////// My Code ///////////////////////////

ORG 0x00
goto MAIN
ORG 0x04
goto ISR
MAIN
bsf STATUS, RP0 ; Sets up PORTB for OUTPUT to LED
clrf TRISB
movlw b'10000101' ; PRESCALLER to 1:64
movwf OPTION_REG
bcf STATUS, RP0
clrf PORTB
bsf INTCON, T0IE ; sets up interrupt... I guess
movlw .178 ;(256-178)=78*64us = 4.992ms tick
movwf TMR0
bsf INTCON,GIE
clrf CNT ; CNT is defined in cblock, just omitted
LOOP ; here to save space
goto LOOP
ISR
movlw .178
movwf TMR0
bcf INTCON, T0IF
incf CNT, F
movlw .200 ;200 * 4.992ms = 1sec.
subwf CNT,W
btfss STATUS, Z
retfie
comf PORTB, f
clrf CNT
retfie
END

p.s. i've used the main board from Nigel's tutorial.. thanks Nigel, nice job.

Regards,
SKY
 
Can you post all your code and put
Code:
 before it and [/co[COLOR="Black"]d[/COLOR]e] after it so that it formats correctly.

Mike.
 
Last edited:
well.. pls forgive my first post.. didnt know the "/code" command

here's a repost of the code:

Code:
 LIST p=16F628  ;tell assembler what chip we are using
 include "P16F628.inc"  ;include the defaults for the chip
 ERRORLEVEL 0, -302 ;suppress bank selection messages
 __config 0x3D18 

              cblock
              CNT
              endc
ORG 0x00
              goto MAIN
ORG 0x04
              goto ISR
MAIN
              bsf STATUS, RP0               ; Sets up PORTB for OUTPUT to LED
              clrf TRISB
              movlw b'10000101'               ; PRESCALLER to 1:64
              movwf OPTION_REG
              bcf STATUS, RP0
              clrf PORTB 
              bsf INTCON, T0IE               ; sets up interrupt... I guess
              movlw .178                        ;(256-178)=78*64us = 4.992ms tick
              movwf TMR0
              bsf INTCON,GIE
              clrf CNT                             
LOOP                                               
              goto LOOP
ISR
              movlw .178
              movwf TMR0
              bcf INTCON, T0IF
              incf CNT, F
              movlw .200                       ;200 * 4.992ms = 1sec.
              subwf CNT,W
              btfss STATUS, Z
              retfie
              comf PORTB, f
              clrf CNT
              retfie
END
 
Last edited:
Your config indicates that you are using the internal oscillator. This is only accurate to 1% and varies with supply voltage. If you want it more accurate you need to use an external crystal.

Mike.
 
Last edited:
thanks for the reply Mike,

in this case, is it possible that I add a resistor, say 50ohm from the 7805 to IC 5+v, so that it will limit only 100ma for the IC. Does this makes it giving constant timing? (i tried to make the circuit simple without using external crystal)
 
Last edited:
skyrock said:
thanks for the reply Mike,

in this case, is it possible that I add a resistor, say 50ohm from the 7805 to IC 5+v, so that it will limit only 100ma for the IC. Does this makes it giving constant timing? (i tried to make the circuit simple without using external crystal)
No, don't put _any_ resistor between the regulator and the processor. This drops the Vcc to the chip, and slows the transfer of current just as the processor needs it. DO install capacitors in the Vcc circuit liberally. These act as filters and storage devices. Put a 0.1 and 10 microFarad cap at the regulator's input (depends on circuit's need and regulator manufacturere's recommendations), the same (depends) at the output, and a 0.1 uF cap close to the processor, between the Vcc and ground. This is _probably_ overkill, but too much is not as bad as too little in this case.

Why varying the supply 9 - 15V should give you timing variations is strange. As Pommie says, varying the Vcc may cause timing changes, but the regulator should take all this in stride and put out a steady 5V, give or take a very small amount. That's why I talk about the caps, they can make a difference. That and load. What else, if anything, do you have hooked to the supply?

Later!
kenjj
 
As long as you have the required capacitors around the 7805 then it should be fairly stable. If it is too far out the simplest solution is to adjust it in software, increase/decrease the 200 value etc. There is a table in the data sheet on page 147 showing the oscillator characteristics.

Mike.
 
Also, if you are putting a heavy load on the port pins this can cause an increase in temperature and an increase in the clock speed. Are you sourcing much current from the port pins, lighting LEDs, driving relays etc.

Mike.
 
i'm using a tutorial board from Nigel's.. it does have capacitors between +5v and 0v... 1uF cap..



for my project, the 8 x LEDs uses power from PORTB(8 ports supplying each). It doesnt use from the regulator directly.

the supply is from a wallplug adapter(able to switch voltages), into the regulater.

is it possible that when it draws power for the 8 Leds affected the IC? I assumed that the power from adapter should be supplying enough power?

Mike: YEP, i turns all LEDS on each blinks.. 1sec on, 1sec off... or should I draw power from the regulator for the LEDs so that it wont affect the IC?
 
Last edited:
Do you have the 1k resistors that Nigel uses on his LED board?

The maximum current you are allowed to source from port B is 100mA. If you have 20mA per LED then you will be drawing 160mA from the port and this will cause excessive heating. With the 1K resistors it will draw a total of 24mA. Does your Pic feel hot?

How far out is your timing?

Mike.
 
Yes, I have 1k resistors for each LED.. it was Nigel's LED board. (and the LED doesn't shine much :p )

The IC doesnt feel hot though.. seems like I havent overloaded it.. yet.

and with the deviation, i didnt have the exact figure, but it is noticibly different when I switch the voltages.. I'll get the exact figure later..

and I'll change to turn on 1 LED for testing later..

thanks guys..
 
if your wallplug adapter is not of good quality, The mains frequency will apear in your circuit. try using 9v battery instead.
 
I've tested the voltage change and timed it with a stopwatch but seems it has the same timing.. maybe I didnt read carefully yesterday.

however, it has a 5% different to real world clock with the code above.. So I added 10 to the counter 200 and it is corrected. Does the IC varies alot with one another? If so, then I have to test them out individually if I make multiple of them?
 
my calculations is:

prescaller 1:64
TMR0 = 256-178 = 78 * 0.000064s = 0.4992ms

0.4992ms * 200 = 0.9982s (very close to 1sec)

but it seems I have to add 5% to 200 and become 210 to get accurate 1sec. (at least accurate for a few minutes, I've waited for 60seconds to find out the deviation.)
 
this is my first timer program.. so, has to start somewhere.. i choose TMR0 with internal clock. I thought it's easier both software and hardware...

I'm using MPLAB IDE.. i'm not very familiar with it but i manage to debug program with it, simulate the whole program with it and using stimulus to inject input signals.. the simulation part is just stepping in and out to look for problems.

The test code using TMR0 is actually from the web which I borrowed and tested.. the calculation is supposed to be correct.. theoritically.. that's why I was wondering if it would deviate as much as 5% with it IC itself? and will different piece of 16f628 gives different results as well?
 
Check my links page for Gooligum Electronics, Elmer & Talking Electronics
**broken link removed**

Gooligum tutorials are excellent, the talking electronics ones could use some updating. (IE always use the include file)
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top