Creating a TMR0 delay using PIC16F84?

  • Thread starter Thread starter SJC1981
  • Start date Start date
  • Tags Tags
    Delay
Click For Summary
The discussion focuses on creating a delay using the TMR0 timer function in a PIC16F84 microcontroller to control a rotating sequence of four LEDs with an option to reverse direction. The user shares their assembly code and seeks guidance, noting the complexity of the task without a real-time operating system (RTOS). A suggestion is made to utilize a micro-sized RTOS to manage delays and tasks more efficiently. Additionally, there is a reminder to post code using proper formatting and a note that expertise in PIC16F84 assembly may be limited among forum members. The conversation emphasizes the need for clarity in code sharing and understanding the specific challenges faced.
SJC1981
Messages
4
Reaction score
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. 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’ into the working register     
MOVWF PORTB ; Moves the binary value of ‘0001’ from the working register into 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 into 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 into 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 into 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 into 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:
Technology news on Phys.org
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

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.

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:
Mark44 said:
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.
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 10 ·
Replies
10
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
7K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
7K