# Creating TMR0 delay for PIC16F84 with rotating sequence for 4 LED's

#### SJC1981

Joined Jul 14, 2018
1
Hi all, I am trying to create a very basic delay using a TMR0 (timer) function as part of a rotating sequence for 4 LED’s with the option to reverse direction. I know I’m almost there but I could do with a little guidance to be honest. I’m using PIC16F84 and datasheet can be downloaded from microchip website where full instruction set is on pg56 down. Also is CLRF TMR0 in right place as this resets the prescaler? Full code is below with annotations:-

Code:
; U19A2T1-4.asm

; blinks LEDs on outputs in a rotating pattern, with input option to reverse direction

; Set microprocessor as 16F84

INCLUDE <P16F84.INC>

; Setup processor configuration

__CONFIG _RC_OSC & _WDT_OFF & _PWRTE_ON

SETUP:ORG 0 ; The next instruction should go to address ‘0’ in the program memory (telling the assembler where to start)

BANKSEL TRISA ; Select register bank ‘TRISA’, which is the register that controls which pins in ‘PORTA’ are an input or an output

CLRF TRISA ; Clearing the ‘TRISA’ register sets all of ‘PORTA’ as outputs

BCF STATUS , RP0 ; Clearing the register bank select bit in the status register, and resets ‘RP0’ back to page ‘0’

BCF STATUS , RP1 ; Clearing the register bank select bit in the status register, and resets ‘RP1’ back to page ‘0’

MOVLW 0X01 ; Puts a literal binary value of ‘0001’ in to the working register

MOVWF PORTB ; Moves the binary value of ‘0001’ from the working register in to the ‘PORTB’ register

BCF STATUS,C ; Clear the carry flag or bit to prevent the arithmetic borrow or carry out of the most significant ALU bit position, i.e. rotating in a ’1’ value, and preventing unwanted bits from being introduced before a rotation

ROTATE:BTFSS PORTA,0 ; Bit test in file and execute next instruction if set at ‘0’, and skip next instruction if set at ‘1’

GOTO RR ; Jump or branch to a 'rotate right' file register

RLF PORTB,F ; Rotate data left stored in register ‘PORTB,F’ through the carry flag, in effect shifting all data one space to the left, and where the existing data in the carry flag will be shifted in to the right most bit. The ‘f’ stands for storing the answer back in ‘PORTB'

GOTO DELAY ; Go to the DELAY register address; repeat loop if delay is not ‘0’

RR:RRF PORTB,F ; This instruction is almost exactly the same as ‘RLF PORTB,F’ above, except that the data moves in the opposite way, i.e. the right most bit will shift in to the carry flag and then back in at the left most bit

DELAY ;

BANKSEL OPTION_REG ; Select register bank ‘OPTION_REG’

MOVLW 0X07 ; Configure timer0 with a prescaler of ‘0000111’

MOVWF OPTION_REG ; Set maximum prescaler increment at 256

TIMER ;

BANKSEL TMR0 ; Select register bank ‘TMR0’

CLRF TMR0 ; Clear the timer register

CHECK:BTFSS INTCON,T0IF ; Check timer flag and wait until TMR0 count rolls over. If '0' execute next instruction, if '1' then skip next instruction

GOTO CHECK ; Loop until T0IF flag is set

BCF INTCON,T0IF ; T0IF flag must be cleared ready for next count

DIRECTION:MOVLW 0X02 ; Move the hexadecimal code value (0X02) and binary equivalent in to the working file register

XORWF PORTA,0 ; Performs ‘XOR’ logic with contents of the working register and contents of the ‘PORTA’ file register address. Answers from both are put back in to the ‘PORTA’ register, toggle bit ‘0’. This overwrites the original data and possibly changes the LED direction

GOTO ROTATE ; Branch back to rotate program instructions as part of the main loop

END ; End of program
Moderators note : used code tags

Last edited by a moderator:

#### dl324

Joined Mar 30, 2015
13,490
Is this really homework? You also posted in Embedded Systems and Microcontrollers.

It would be better if you used a code block for your code.

#### AlbertHall

Joined Jun 4, 2014
11,587
I know I’m almost there but I could do with a little guidance to be honest.
You could give us a bit of a clue by telling us what it does or doesn't do wrong.

Joined Jul 18, 2013
23,897
Moderators note : used code tags
How come the MPASM is not an option in the code options.
It has to be edited in, or am I missing something?
Max.

#### JohnInTX

Joined Jun 26, 2012
4,594
How come the MPASM is not an option in the code options.
It has to be edited in, or am I missing something?
Max.
I don't know why it's not listed either. A lot of them aren't. I just edit the code tag.
FWIW it's slow to paste code into the box in the Insert Code Tag dialog. I just insert null tags, fix up the language spec then paste the code between the code tags.

Last edited:

#### jpanhalt

Joined Jan 18, 2008
11,088
MPASM works for me:

#### takao21203

Joined Apr 28, 2012
3,702
const unsigned char lights[]={0b0001,0b0010,0b0100,0b1000,0b0100,0b0010};