Programming help ...!!

Discussion in 'Programmer's Corner' started by RRITESH KAKKAR, Apr 1, 2012.

  1. RRITESH KAKKAR

    Thread Starter Senior Member

    Jun 29, 2010
    2,831
    89
    Hello everyone,

    I want to learn PIC assembly coding, i have completed few testing with LED blinking!!
    I need help in Timer of PIC16f676 how to count the input pulse from a switch..
    I have seen its data-sheet but i am not getting how to use it, please help me to use TIMER register.
    or suggest a nice tutorial for learning it and more of PIC16f676.


    Thanks
     
  2. Pencil

    Active Member

    Dec 8, 2009
    271
    38
    Maybe this will help.

    The examples are in C language, but the general
    idea of how to set up the various registers are of course
    the same for Assembly.
     
    RRITESH KAKKAR likes this.
  3. RRITESH KAKKAR

    Thread Starter Senior Member

    Jun 29, 2010
    2,831
    89
    Hi,

    I have seen that tutorial and it help also. So, i need to make a counter which will read external switch upto some digit.
    so, I know a instruction for testing bit of uC called BTFSS/C PORTA/C, BIT ...so, how to set the timer with this instruction or something similar to this??
     
  4. MMcLaren

    Well-Known Member

    Feb 14, 2010
    759
    116
    Hi Ritesh,

    May I ask how you want to display the count? Will you use discrete LEDs for a binary count of 0 to 7 (three LEDs) or 0 to 15 (four LEDs), or will you use a 7-segment display for a decimal count of 0 to 9? Also, are you using two switches ('up' and 'down'), and if so, what pins are they connected to and are they "active low" or "active high"?

    You could use a delay subroutine or delay subsystem instead of TMR0 or TMR1 but if you really want to try and use one of the timers, we can probably help you figure out a strategy or method for using them.

    Regards, Mike
     
    RRITESH KAKKAR likes this.
  5. be80be

    Senior Member

    Jul 5, 2008
    431
    57
    The timer can count on T0CKI pin you can gate the pin and count a period of time witch can be used for lots of things.

    Or just count a pin and save in a variable using a loop.
     
    RRITESH KAKKAR likes this.
  6. RRITESH KAKKAR

    Thread Starter Senior Member

    Jun 29, 2010
    2,831
    89
    please provide a simple program to do this counting function from external switch(pin)...
     
  7. be80be

    Senior Member

    Jul 5, 2008
    431
    57
  8. RRITESH KAKKAR

    Thread Starter Senior Member

    Jun 29, 2010
    2,831
    89
    HI, i have seen and read about timer set bit but there a one doubt how to increase timer register value by external switch or input pin??
     
  9. RRITESH KAKKAR

    Thread Starter Senior Member

    Jun 29, 2010
    2,831
    89
    Hi again,
    While using counter from option register setting by TMR0...where that counter value is stored??
     
  10. Pencil

    Active Member

    Dec 8, 2009
    271
    38

    Could the answer be File address 01h?

    See datasheet here PIC16F630/676. Pages 10, 11 and 31-33.
     
    RRITESH KAKKAR likes this.
  11. Felo

    Member

    Feb 20, 2012
    91
    13
    Hi, do you still need this? I have a couple of minutes to write this down if you are still interested.
     
    RRITESH KAKKAR likes this.
  12. RRITESH KAKKAR

    Thread Starter Senior Member

    Jun 29, 2010
    2,831
    89
    OK, you can ...
     
  13. Felo

    Member

    Feb 20, 2012
    91
    13
    Sorry, I'm late I was making myself some lunch there it goes

    This is the core file

    Code ( (Unknown Language)):
    1.  
    2. LIST P=16F676
    3. #INCLUDE P16F676.INC
    4. RADIX HEX
    5. __CONFIG 3F84H      ;OSC
    6. ERRORLEVEL -302
    7. ERRORLEVEL -305
    8.  
    9. #INCLUDE 16F676 TMR0 COUNT EXT.INC
    10.  
    11. ;INICIO CODIGO
    12. ORG 0X000
    13. GOTO    SETUP
    14. ORG 0X004 ;MANEJADOR DE INTERRUPCION
    15. GOTO ISR
    16.  
    17. SETUP                   ; GOTO LABEL
    18. ;------------------
    19. ;DESHABILITAR ANALOGOS, SETUP MODO FUNCIONAMIENTO
    20.     BANCO0
    21.     MOVLW   0X07
    22.     MOVWF   CMCON       ;APAGA COMPS, RA0,1,2,3 DIG I/O PINS.                   ;
    23. ;SET PORTS Y REGS PROGRAMADOR
    24.     BANCO1
    25.     MOVLW   B'00000100'
    26.     MOVWF   TRISA
    27.     MOVLW   B'00000000'
    28.     MOVWF   TRISC
    29.     MOVWF   ANSEL
    30.     MOVLW   B'11000000'
    31.     MOVWF   OPTION_REG
    32.     MOVLW   B'10010000' ;ENABLE INTE AND GIE
    33.     MOVWF   INTCON
    34.     BANCO0
    35.     CLRF    PORTA
    36.     CLRF    PORTC
    37. ;HABILITA INTERRP PERIPHERA
    38. ;   MOVLW   B'11111000'
    39. ;   ANDWF   INTCON,F    ; CLEAR ALL INTERRUPT FLAGS
    40. ;   BSF INTCON,GIE  ; ENABLE GLOBAL INTERRUPT
    41. ;   BSF INTCON,PEIE ;HABILITA INTS PERIFERALS
    42. ;   BSF INTCON,INTE ;HABILITA RB0 INTERR
    43. ;BORRADO REGS PROGRAMADOR
    44. ;DEL 0X20 AL 0X6F
    45.     MOVLW 0X20
    46.     MOVWF FSR
    47.     CLRF  INDF
    48.     INCF  FSR
    49.     MOVLW 0X60 ;+1 (traslape)
    50.     XORWF FSR,W
    51.     BTFSS STATUS,Z
    52.     GOTO  $-5  
    53.     BANCO0
    54.  
    55. MAIN
    56.     NOP
    57.     GOTO    MAIN
    58.  
    59.  
    60.  
    61. ISR
    62. ;   ;-------------------------------------------------
    63.                         ; FIRST WE PRESERVE W AND STATUS REGISTER
    64.     MOVWF   W_TEMP ;SAVE EVERYTHING
    65.     SWAPF   STATUS, W
    66.     MOVWF   STATUS_TEMP
    67.     MOVFW   FSR
    68.     MOVWF   FSR_TEMP
    69.     MOVFW   PCLATH
    70.     MOVWF   PCLATH_TEMP
    71. ;0000000000000000
    72.  
    73. SERVICE
    74.     NOP     ;SETUP SOME DEBOUNCE RUTINE HERE
    75.     NOP     ;TOGGLE RC0 FOR DEMONSTRATION ONLY
    76.     MOVLW   B'00000001'
    77.     XORWF   PORTC
    78.  
    79.  
    80. ;000000000000000
    81. INT_EXIT
    82.     BCF     INTCON,INTF ; RESET THE INTF FLAG  
    83.     MOVFW   PCLATH_TEMP
    84.     MOVWF   PCLATH
    85.     MOVFW   FSR_TEMP
    86.     MOVWF   FSR
    87.     SWAPF   STATUS_TEMP, W
    88.     MOVWF   STATUS
    89.     SWAPF   W_TEMP, F
    90.     SWAPF   W_TEMP, W
    91.     RETFIE              ; RETURN FROM INTERRUPT
    92.  
    93.     RETFIE
    94.  
    95. END
    This is the include file where the macros, and other preliminaries are

    Code ( (Unknown Language)):
    1.  ;MACROS PARA CAMBIO DE BANCO RAPIDO
    2. BANCO0  MACRO
    3.         BCF STATUS,RP0
    4.         BCF STATUS,RP1
    5.         ENDM
    6.  
    7. BANCO1  MACRO
    8.         BSF STATUS,RP0
    9.         BCF STATUS,RP1
    10.         ENDM
    11. ;++++DEFINICION EQUIVALENCIAS
    12. #DEFINE MOD1 PORTA,1
    13.  
    14.  
    15. ;*******DEFINICION DE VARIABLES DEL PROGRAMADOR
    16. CBLOCK 0X0020
    17. D1
    18. D2
    19. D3
    20. D4
    21. ENDC
    22.  
    23. ;DEFINICION VARS REGS RETORNO INTERRUPCION
    24.  
    25. CBLOCK 0X0050
    26. W_TEMP
    27. STATUS_TEMP
    28. PCLATH_TEMP
    29. FSR_TEMP
    30. ENDC
    Compile and enjoy, go ahead and ask for any directions, this is a very coarse implementation but it will get you on track

    Attached there is a gif of the circuit in proteus, hope it helps
     
    • aac.jpg
      aac.jpg
      File size:
      99.9 KB
      Views:
      12
    RRITESH KAKKAR likes this.
  14. RRITESH KAKKAR

    Thread Starter Senior Member

    Jun 29, 2010
    2,831
    89
    Thanks, I have MAPLAB IDE 8.33v so, the problem is that i want to simulate the code and want to watch the register value and output port as there any simulator which can do this??
    as my oshon evaluation period is expired!!
     
  15. Felo

    Member

    Feb 20, 2012
    91
    13
    It will be awkward watching the simulation in MPLAB SIM, so I strongly recomend you get a hold of proteus, is the best tool for this and I would be able to help you a lot more that way.

    Happy hunting
     
  16. Pencil

    Active Member

    Dec 8, 2009
    271
    38
    Why wouldn't you be able to simulate the code in MPLAB?

    This is how I would do it:

    Create a project with the "Project Wizard" (see user's manual).

    Enter your code and get it to build sucessfully.

    Choose MPLAB SIM as your Debugger.

    View file registers by choosing: View->File Registers

    You can also simulate a signal on a pin by using the stimulus dialog.
    Debugger->Stimulus->New Wookbook->Pin/Register actions (tab)

    Obviously this is a very simplified explanation, but I hope it puts
    you on the right track.
     
    RRITESH KAKKAR likes this.
  17. Felo

    Member

    Feb 20, 2012
    91
    13
    Yes you could, but as I stated before is kind of awkward to visualise it on the SIM, specially if you don't have many milleage with it.
    Proteus is better for this.
     
    RRITESH KAKKAR likes this.
  18. Pencil

    Active Member

    Dec 8, 2009
    271
    38
    Felo,

    I was typing while you were posting. I wasn't meaning to
    contradict you in any way, I didn't see your post until after
    I posted. Feel free to continue, All help is welcome.
     
    Last edited: Apr 15, 2012
    RRITESH KAKKAR likes this.
  19. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    But Isis Proteus is far from free. So if the OP do not have access to proteus via his school. I think Proteus will be out of question:rolleyes:. I would suggest that the OP learn to use the MPLAB. By using MPLAB you can cover a lot ground then it comes to debugging.
     
    RRITESH KAKKAR likes this.
Loading...