Let's Make Robots!

timer timer timer, why can't i get you right?

So i set up the forum topic on pwm only to realise that i skipped the setting up of the timer! This is such a pain. So here is some code that i wrote to get the timer0 working. Please tell me what i need to add and what is wrong. I'm really frustrated with this part of PIC, very confusing. I think this is a much more apt question to begin with. 

Here's the code:


LIST P=18F252





  CLRF TMR0; this should start the timer accodring to a book i got. but i get an error saying that it is not defined

LOOPA MOVF TMR0,W; basically movign tmr0 val in w, again tmr0 is not defined

SUBLW .15625; -15625 from tmr0 with prescaler 32


ZEROBIT; this is supposed to check if the result is 0 but zerobit is not defined.

GOTO LOOPA; if result is still not 0

RETLW 0; if result is 0 go back to program where this loop was called(e.g. main)


Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

for example

main bsf portc,rc4

call pause1

bcf rc1

goto main

something like this like a blinking thing

While I can't write your code for you, here are some things to point you in the right direction:
• With a 20MHz oscillator your timers will increment once every 0.2μs. If you have a 1:32 prescaler they increment once every 6.4μs. If you have a 1:256 prescaler they increment once every 51.2μs, which means that the timers will advance by 19,531 every second.
• When a timer reaches its maximum value it overflows, and sets a special interrupt flag bit (even if interrupts are disabled). You can test this flag bit to see if the timer has filled up yet. Don't forget to clear the flag bit when you reset the timer.
• If you move a value into the timer instead of just clearing it you can shorten the time it takes for it to overflow. The max value for a 16b timer is 65,535. If you put the value (65,535 - 19,531) = 46,004 into a 16b timer, it will take 19,531 cycles, or 1 second, to reach overflow and set the interrupt flag bit.

oK shit, i dont understand this at all. damn....

If you want to stick with ASM then there are 2 resources that will tell you everything:
• The datasheet for your micro (PIC18Fxx2).
• Microchip ASM help file (In MPLAB goto: Help > Topics... > MPASM Assembler).

Get familiar with these two documents - I've never needed any other resources since starting with PICs in ASM.

I've seemed to get the timer working but at the same time have not, if u take a look at my blog, u may get more info on this. I can't seem to get a real 1 second value

What code are you using then? You haven't posted this new code here or in your blog, unless you're using the source from extremeelectronics.co.in, which was written in C anyway.

gimme a sec, i'll post it up

Well i thought about programming with C, but I don't know what program to use and i only have the pickit2. From what you have given me, how do i make a pause for say 1 second?

MPLAB has a C compiler... two of them by default, but more are available. There are also plenty of 3rd party compilers and IDEs for other languages like BASIC.

When you say pause, do you just want the program to stop for 1s, or do you want to trigger some routine to run after 1s?

• ORG 0 causes your program to start writing to the Reset Vector address. Normally you want something like:
ORG        0x000000
ORG        0x000020
START     ;(start initialisation routines here)

• You need to initialise Timer0 - it's disabled by default to save power. T0CON is the Timer0 Control Register:
MOVLW   b'10000100'
This will enable Timer0 in 16b mode, running off the instruction cycle clock, with a 1:32 prescaler assigned to it.

• Timer0 is a 16b timer, so you need to clear TMR0H and TMR0L to empty the whole thing. Make sure you clear TMR0H first, as it will only update when you clear TMR0L, allowing both bytes to change at the same time thus avoiding any errors.

• The Working Register (W/WREG) is only 1byte, and SUBLW only accepts 1byte arguments anyway, so however you look at it 15625 is too big.

• The zero bit of the STATUS register is normally just defined as Z, but you can check your include file definitions if you want to be certain.

• If this is your whole code so far then RETLW won't work, since there isn't an address in the stack to return to. You should probably comment this out for testing.


Have you thought about programming this in C instead?