Creating a TMR0 delay using PIC16F84?

  • Thread starter SJC1981
  • Start date
  • Tags
    Delay
In summary, this person is trying to create a rotating light show using a timer function and a PIC16F84 microcontroller. They are having difficulty understanding the code and need some help.
  • #1
SJC1981
4
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
  • #2
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

  • Proto.asm.txt
    16.1 KB · Views: 497
  • #3
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
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.
 

1. What is a TMR0 delay and why is it important in PIC16F84 programming?

A TMR0 delay is a delay routine that uses the Timer 0 module in the PIC16F84 microcontroller to create a specific delay in the program. It is important in PIC16F84 programming because it allows for precise timing and synchronization of events, which is necessary for many applications such as controlling motors or sensors.

2. How do I set up and initialize the TMR0 module for a delay in PIC16F84?

To set up and initialize the TMR0 module for a delay, you first need to configure the Timer 0 control register (TMR0). This involves setting the prescaler, enabling interrupts and selecting the appropriate clock source. Then, you need to load the timer with a starting value and enable the timer. Finally, you can use a loop to check the value of the timer until the desired delay is achieved.

3. Can I use a TMR0 delay in both interrupt and polling modes?

Yes, you can use a TMR0 delay in both interrupt and polling modes. In interrupt mode, the program will continue to run while the timer counts in the background, and an interrupt will be triggered when the timer reaches its target value. In polling mode, the program will actively check the timer value until the desired delay is achieved, and then continue with the rest of the code.

4. What are some common pitfalls to avoid when using a TMR0 delay in PIC16F84 programming?

One common pitfall to avoid is not properly configuring the TMR0 module, which can lead to unexpected delays or errors in the program. Another pitfall is not accounting for the time it takes for the program to check the timer value, which can result in a longer delay than intended. Additionally, it is important to handle any interrupts that may be triggered during the delay to prevent errors in the program.

5. Are there any alternative methods for creating a delay in PIC16F84 programming?

Yes, there are alternative methods for creating a delay in PIC16F84 programming. One alternative is to use a software delay loop, which involves using a loop to execute a certain number of instructions to create a delay. Another alternative is to use the built-in delay functions provided by the compiler or library, which can simplify the process but may not be as precise as using the TMR0 delay method.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Programming and Computer Science
Replies
4
Views
2K
Replies
9
Views
2K
  • Programming and Computer Science
Replies
2
Views
5K
  • Electrical Engineering
Replies
2
Views
2K
  • Electrical Engineering
Replies
10
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
7K
  • Electrical Engineering
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
6K
  • General Engineering
Replies
2
Views
3K
Back
Top