Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Timer 0 pic16f877

Status
Not open for further replies.

jmshoti

New Member
I was wondering if I can ask anyone for some help. I seem to get stuck and don't know
where to start with regards to actually typing the actual syntax that needed for my program to work.

In class, we are using PIC16F877. And its about using timer 0. Would you be able to give me a head start on what to do?

I need to run a program showing the binary count on the LED's of how long the button is pressed (PORTB, bit 0). When the button is released and later pressed again, the count picks up from where it left off. When the count reaches 255, the next count should roll over to 0. My outputs would be PORTC. I need to be able to configure TIMER 0 to be able to determine when 10ms have passed. Should I use the 1:256 prescalar for this?

The second part is to change the counter so that the count should be displayed until the button is pressed again, but the count should start from 0 each time. The count should also stop at 255 and display 255 for as long as the button is pressed after this time.

I desperately need some help on these problems.
 
Should I use the 1:256 prescalar for this?
Really, the best place to look that would solve all your issues is the data sheet...

That depends. What clock frequency are you using? For example a 16F pic at 4Mhz runs at 1us instruction time so each bit would be incremented every 1us on a 1:1 scale, so if you need 10ms just figure out your osc frequency and the scale amount to get 10 ms....(although you may not get exactly 10ms as the overflow number because the scalar numbers are powers of 2) you may get close to it though... Unless you wana use delay loops)..

As for your second part.. you could just pol the button and start a count when it is pressed (assuming you denounce the button ) then just keep counting... kinda like ..
Code:
while(1)
  {
  while(!buttonpin); // wait for a press
          starttimer(); // start the TMR0  function

  while(!buttonpin); // wait for another press to reset timer
         stoptimer();  // stop and show result and await another button press
  }
but yea, the data sheet should solve most of tmr0 issues...
 
Last edited:
Code:
;;;;;;; Variables ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
       cblock  Bank0RAM
       T0Count
       Y1
       endc

;;;;;;; Vectors ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

       org     H'000'          ;Reset vector
       goto    Mainline        ;Branch past tables
       org     H'004'          ;Unused interrupt vector
Stop
       goto    Stop            ;Stop if an interrupt accidently occurs

;;;;;;; Tables ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; No tables needed

;;;;;;; End of Tables ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;; Mainline program ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Mainline
       call    Initial         ;Initialize everything
;;;;;;; Initial subroutine ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; This subroutine performs all initializations of variables and registers.

Initial
       bsf     STATUS,RP0      ;Set register access to bank 1
       clrf    TRISC           ;Set up all bits of PORTC as outputs
       bcf     STATUS,RP0      ;Set register access back to bank 0
       movlw   000
       movwf   T0Count
       movlw   100
       bcf     STATUS,RP0      ;change to bank 0
       movwf   TMR0
       bsf     STATUS,RP0      ;change to bank 1
       bsf     OPTION_REG,0
       bcf     OPTION_REG,1
       bsf     OPTION_REG,2    ;set to 5
       bcf     OPTION_REG,3
       bcf     OPTION_REG,5
       bcf     STATUS,RP0      ;change to bank 0
loop1  btfss   PORTB,0         ;Check if button is pressed
       clrf    T0Count
       movf    T0Count,w
       movwf   PORTC
       btfss   PORTB,0
       goto    loop2
       goto    loop1
;Check the T0IE, if it is set, then increment T0Count
loop2  btfsc   INTCON,T0IF
       incf    T0Count
       call    keeplight
       movf    T0Count,w
       movwf   PORTC
       btfsc   INTCON,T0IF
       bcf     INTCON,T0IF
       btfsc   PORTB,0
       goto    loop1
       goto    loop2
       return
;;;;;;; keeplight subroutine;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
;test each bit of T0 to check if it equals 1, if any bit
;is not 1, then T0 is not 255, then exit this subroutine

keeplight
       btfss   T0Count,0
       return
       btfss   T0Count,1
       return
       btfss   T0Count,2
       return
       btfss   T0Count,3
       return
       btfss   T0Count,4
       return
       btfss   T0Count,5
       return
       btfss   T0Count,6
       return
       btfss   T0Count,7
       return
       call    lightson
       return

lightson
       movlw   B'11111111'
       clrf    T0Count
       movwf   PORTC
       btfss   PORTB,0
       call    lightson
       goto    loop1
       return
       end

I got the part B to work, can somebody help me to try and modify this for part A?? Also, How can I do both parts with interrupts? I had to do it without interrupts at frist and then also with interrupts. Any help would be appreciated.
 
Last edited by a moderator:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top