Creating a TMR0 delay using PIC16F84?

  • Thread starter SJC1981
  • Start date
  • #1
4
1

Main Question or Discussion Point

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. Full code is below with annotations:-
Mentor note: Code below with code tags copied from a thread posted in a different forum section.
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
 
Last edited by a moderator:

Answers and Replies

  • #2
Baluncore
Science Advisor
2019 Award
7,457
2,475
It can get very complex quickly without a real time operating system.
Attached is a micro-sized RTOS. It generates almost no unnecessary code.
You can program delays, tasks and more without getting your interupts tangled.
It is over documented. Read the code, then ask questions if needed.
You will need to change the Chip type from PIC16C84 to PIC16F84.
Also check the polarity of the read protect bit which I think changed.
Once you have written code to turn LEDs on and off, simply load their downcounters to schedue the later event.
 

Attachments

  • #3
33,314
5,006
What is the problem you're trying to solve?

Also, please post your code using code tags. There's a sticky on how to use them at the top of this forum section. In summary, they would look like this:

[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)
etc.
[/code]

Edit: I copied the code from the other thread you started, and pasted it into the starting post of this thread.

Finally, I don't believe anyone at this site is knowledgeable about PIC16F84 assembly. You should not expect us to go to some website and download the instruction set to try to figure out what your code is doing.
 
Last edited:
  • #4
Baluncore
Science Advisor
2019 Award
7,457
2,475
Finally, I don't believe anyone at this site is knowledgeable about PIC16F84 assembly.
I was writing PIC16F84 assembly code 20 years ago. It will come back to me if I need it.
The box file of documentation is still there on the shelf.
 

Related Threads on Creating a TMR0 delay using PIC16F84?

  • Last Post
Replies
7
Views
62K
Replies
1
Views
2K
Replies
6
Views
3K
  • Last Post
Replies
4
Views
4K
  • Last Post
Replies
18
Views
185K
  • Last Post
Replies
2
Views
4K
Top