Timer0 in PIC16F72

Status
Not open for further replies.

koolguy

Active Member
Hi again,

I was reading data sheet and want to learn Timer0 in assembly so, i have confusion as the interrupt addr is 0x04 so, how to set for external clock...here is the code which need help.
The aim of code is to change the output high when timer is overflow..

Code:
 LIST p=16f72
#include "p16f72.inc"
__config b'00000000011111'

org 0x00

bsf STATUS,RP0 ; bank 1
movlw b'01000000' ;internal inst cycle showing error at option?
; movwf OPTION
bcf STATUS,RP0; bank 0
movlw b'10100100'
movwf INTCON;  enable interrupt
bsf STATUS,RP0 ; bank 1
movlw 0x00
movwf TRISB; output direction
bcf STATUS,RP0
loop
  movlw 0x00
  movwf PORTB
goto loop

org 0x04
goto loop
retlw 0x00



end
 
hi Ritesh,
You must enable the interrupts and also a context save is a good idea.
Look at this pdf App for timer0 interrupts. Ref Page #5
E
 

Attachments

  • PIC_Mid_A_6.pdf
    481.4 KB · Views: 1,136
Code:
loop
  movlw 0x00
  movwf PORTB
goto loop

org 0x04
goto loop   ; <----- No No No and a big NO
retlw 0x00 ; should be retfie

Look at the above code...

You have the interrupt at 0x04.... When your timer fires the code in the interrupt is run... The micro already takes care of the return automatically ( the reason for the command RETFIE... return from interrupt and enable )

So if you place a goto in the interrupt to outside the interrupt... the intrrrupt never ends and you get a stack underflow..

This is the code you need
Code:
loop
  movlw 0x00
  movwf PORTB
goto loop

org 0x04
   ; <-----do interrupt stuff
retfie
 
What is this can't find in Google..?
clrf DFLAG
bsf DFLAG, DFL0 ;Set flag bit

Its part of a program code definition header, in what context did you use/find it. E
; DFlag
dmx_recieve equ 0 ; Dmx recieve display
dmx_msb equ 1 ; Displaying DMX MSB
dmx_csb equ 2 ; Displaying DMX CSB
dmx_lsb equ 3 ; Displaying DMX LSB
preset_intro equ 4 ; Displaying Preset Intro
 
What is this can't find in Google..?
clrf DFLAG
bsf DFLAG, DFL0 ;Set flag bit
What Eric is saying is
DFLAG is a variable.
DFL0 is a bit within that variable.

It's the same as "bsf PORTB.RB0"

Somewhere in your code DFLAG will be defined somewhere...
 
The code for led blink at portB is working in simulator but not in real u can u see any problem in it?

Code:
LIST    p=16f72
#include "p16f72.inc"
__config b'00000000011111'

cblock 0x20
        count
endc
org 0x00

bsf STATUS,RP0
movlw 0x00
movwf TRISB
bcf STATUS,RP0

LOOP
movlw 0xff
movwf PORTB
CALL delay
movlw 0x00
movwf PORTB
CALL delay
goto LOOP



delay
movlw 0x255
movwf count

delay_1
decfsz count,1
goto delay_1
retlw 0x00
end
 
0x255 is not an 8-bit number. It is decimal 597. It won't fit in the register named "count." Try .255 (decimal) or 0xFF

I am not sure that is the whole problem. Can you show the complete code?

John
 
At what frequency are you running? Have you confirmed it? When you say the led's aren't blinking, have you looked at a port pin with your oscilloscope?

John
 
Also you have RC oscillator selected.... Is this what you want.... Simulators usually ignore OSC settings that's why they work on sim but not for real.
 
Ok... Then you need to change your CONFIG statement..

If your micro is running at 20Mhz.. Can I assume that this is a crystal? If so the CONFIG needs to be

__config b'00000000011110'
 
hi ritesh,
What are the R/C values of your RC Oscillator circuit.?

Look at this CONFIG reg value.
Code:
    list    p=16f72
    #include "p16f72.inc"
    errorlevel -302, -207

;;    __config b'00000000011111'
   
    __CONFIG 0x3FB3
 

Attachments

  • AAesp01.gif
    15.7 KB · Views: 217
  • AAesp02.gif
    7.9 KB · Views: 248
Please post a schematic. Do you have MCLR tied high? If in fact you are anywhere near 20 MHz, or even 1 MHz, it is unlikely the short delay* you have inserted will be visible as a flashing LED. Did you test with an oscilloscope as I asked? Did you change the delay to 0xFF?

John

Edit: 0x255 is read as 0x55 or just decimal 85 counts.
 
Last edited:
If you have an R/C externally running at 20Mhz, then I strongly recommend a crystal instead, especially if you are working with timers!!!...

P.S. I didn't think you could get that fast anyway!!
 
From the datasheet:


I wonder if the oscillator is even running.

Ritesh: Do you have a resonator or crystal to use?

John
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…