Binary Count - PIC16F684 Programming in Assembly

Discussion in 'Programmer's Corner' started by Glitchezz, Apr 26, 2012.

  1. Glitchezz

    Thread Starter New Member

    Apr 26, 2012
    3
    0
    Hello,

    I have a university project due soon that requires me to count from 0 - 7 in binary.

    I'm using the Microchip PICKit1 Flash Starter Circuit board (please see image).

    http://i48.tinypic.com/k3qbr6.jpg

    I understand this is a fairly simple thing to code but I can't grasp my head around it.

    I know how to count in binary, I just can't code it.

    I've tried several codes that just don't seem to work.
    I've searched on www.mstracey.btinternet.co.uk and used the tutorials but the codes produce errors. I'm just getting frustrated now :(

    I would really appreciate if someone could help me complete this and it will not go unnoticed!
     
  2. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    The tutorials on that page are for the 16F84 so won't work on the 16F684 without a few changes.
    By count in binary, do you mean output binary on 3 pins with a delay between the counts?
    If you post the code you have so far and the errors that you are getting then we should be able to help fix them.
     
  3. panic mode

    Senior Member

    Oct 10, 2011
    1,318
    304
    first work on the problem, then detail and eventually everything will fit together.
    if you are supposed to count in binary, what exactly is counted? is it some internaly generated clock or external event?
    and how to count in binary? or any numbering system? how do YOU count? then translate that into something that machine (mcu) does.

    when i count, i just keep adding 1 to whatever current value is. if i have to restart, i assume that i have to start from zero. does this help?
     
  4. atferrari

    AAC Fanatic!

    Jan 6, 2004
    2,645
    759
    Is your problem how to express / show the results of your counting?

    Just to be sure, are you required to count one unit at a time?
     
    Last edited: Apr 27, 2012
  5. Glitchezz

    Thread Starter New Member

    Apr 26, 2012
    3
    0
    Sorry for my late reply.

    When I say count in binary I mean the lights need to represent 0 - 7 in binary with a 1 second delay between each.

    E.g. 7 in binary would be lights D0, D1 and D2 on.

    Here is some sample code which is meant to be close to what I need to do but it doesn't seem to light the lights in the right order.

    Again sorry. I'm spending today trying my best to sort this out!

    Code ( (Unknown Language)):
    1.  
    2. ; WRITTEN BY            TA
    3. ; DATE                  28/03/11
    4. ; FILE SAVED AS         SAMPLE6.ASM
    5. ; DEVICE                PIC16F684
    6. ; OSCILLATOR            XT (4MHZ)
    7. ; WATCHDOG              DISABLED
    8. ; FUNCTION              LEDs are switched ON in a binary sequence
    9. ;                       (THIS TEST PROGRAM IS USED IN PIC LABORATORY 1)
    10.     list      p=16f684            ; list directive to define processor
    11.     #include <p16f684.inc>        ; processor specific variable definitions
    12. __CONFIG  _CP_OFF & _WDT_OFF &_PWRTE_ON & _INTRC_OSC_NOCLKOUT & _MCLRE_OFF & _CPD_OFF
    13.  
    14. ;*******************************Equates****************************************
    15. PORTA EQU 05h
    16. COUNT1 EQU 20h; Set up two counters to count down a delay
    17. COUNT2 EQU 21h
    18.  
    19. ;*******************************Defines****************************************
    20. ; define input/output designation for LEDs (what TRISA will equal)
    21.  
    22. #define TRIS_D0_D1  B'00001111' ; TRISIO setting for D0 and D1
    23. #define TRIS_D2_D3  B'00101011' ; TRISIO setting for D2 and D3
    24. #define TRIS_D4_D5  B'00011011' ; TRISIO setting for D4 and D5
    25.  
    26. ; define LED state (what PORTA will equal)
    27.  
    28. #define D0_ON   B'00010000'     ; D0 LED
    29. #define D1_ON   B'00100000'     ; D1 LED
    30. #define D2_ON   B'00010000'     ; D2 LED
    31.  
    32. ;****************************** Start of Program ******************************
    33.  
    34.            org     0x000               ; processor reset vector
    35.  
    36. ;************* Initialize PortA **********
    37.            movlw    B'00111111'     ; Set all I/O pins of PORTA as inputs
    38.            TRIS    PORTA               
    39.            clrf    PORTA               ; clear all outputs
    40.  
    41. ;************** LED0 ON *********************
    42. START        movlw  TRIS_D0_D1      ; move predefined value to TRISA to switch ON LED0
    43.       TRIS   PORTA
    44.       movlw D0_ON              ; move predefined value to PORTA TO switch ON LED0
    45.       movwf PORTA
    46.       decfsz   COUNT1,1       ;Subtract 1 from 00h (this gives 255 or FFh)
    47.           goto     START          ;If COUNT is zero, carry on.
    48.           decfsz   COUNT2,1       ;Subtract 1 from 00h
    49.           goto     START          ;Go back to the start of our loop
    50. ;************** LED1 ON *********************
    51. LOOP2       movlw   D1_ON              ; move predefined value to PORTA TO switch ON LED1
    52.             movwf   PORTA
    53.             decfsz   COUNT1,1       ;Subtract 1 from 00h (this gives 255 or FFh)
    54.                 goto     LOOP2          ;If COUNT is zero, carry on.
    55.                 decfsz   COUNT2,1       ;Subtract 1 from 00h
    56.                 goto     LOOP2          ;Go back to the start of our loop
    57. ;************** LED0 and LED1 ON **************
    58. LOOP3  movlw    D0_ON              ; move predefined value to PORTA TO switch ON LED0
    59.       movwf PORTA
    60.       movlw D1_ON              ; move predefined value to PORTA TO switch ON LED1
    61.       movwf PORTA
    62.           decfsz  COUNT1,1        ;Subtract 1 from 00h (this gives 255 or FFh)
    63.           goto    LOOP3           ;If COUNT is zero, carry on.
    64.           decfsz  COUNT2,1        ;Subtract 1 from 00h
    65.           goto    LOOP3           ;Go back to the start of our loop
    66. ;************** LED2 ON *********************  
    67. LOOP4       movlw      TRIS_D2_D3      ; move predefined value to TRISA to switch ON LED2
    68.            TRIS    PORTA
    69.            movlw       D2_ON               ; move predefined value to PORTA TO switch ON LED2
    70.            movwf   PORTA
    71.                decfsz   COUNT1,1       ;Subtract 1 from 00h (this gives 255 or FFh)
    72.                goto     LOOP4          ;If COUNT is zero, carry on.
    73.                decfsz   COUNT2,1       ;Subtract 1 from 00h
    74.                goto     LOOP4          ;Go back to the start of our loop
    75. ;************** LED0 and LED2 ON **************
    76. LOOP5       movlw      TRIS_D0_D1       ; move predefined value to TRISA to switch ON LED0
    77.            TRIS    PORTA
    78.            movlw       D0_ON                ; move predefined value to PORTA TO switch ON LED0
    79.            movwf       PORTA
    80.            movlw       TRIS_D2_D3       ; move predefined value to TRISA to switch ON LED2
    81.            TRIS    PORTA
    82.            movlw       D2_ON                ; move predefined value to PORTA TO switch ON LED2
    83.            movwf   PORTA
    84.            decfsz  COUNT1,1        ;Subtract 1 from 00h (this gives 255 or FFh)
    85.                goto    LOOP5           ;If COUNT is zero, carry on.
    86.                decfsz  COUNT2,1        ;Subtract 1 from 00h
    87.                goto    LOOP5           ;Go back to the start of our loop
    88. ;************** LED1 and LED2 ON **************
    89. LOOP6       clrf       PORTA               ; clear all outputs
    90.            movlw       TRIS_D0_D1       ; move predefined value to TRISA to switch ON LED1
    91.            TRIS    PORTA
    92.            movlw       D1_ON                ; move predefined value to PORTA TO switch ON LED1
    93.            movwf   PORTA
    94.            movlw      TRIS_D2_D3       ; move predefined value to TRISA to switch ON LED2
    95.            TRIS   PORTA
    96.            movlw      D2_ON             ; move predefined value to PORTA TO switch ON LED2
    97.            movwf      PORTA
    98.            decfsz  COUNT1,1        ;Subtract 1 from 00h (this gives 255 or FFh)
    99.                goto    LOOP6           ;If COUNT is zero, carry on.
    100.                decfsz  COUNT2,1        ;Subtract 1 from 00h
    101.                goto    LOOP6           ;Go back to the start of our loop
    102. ;************** LED0,LED1 and LED2 ON **************
    103. LOOP7       movlw      TRIS_D0_D1       ; move predefined value to TRISA to switch ON LED0 and LED1
    104.            TRIS    PORTA
    105.            movlw       D0_ON                ; move predefined value to PORTA TO switch ON LED0
    106.            movwf       PORTA
    107.            movlw       D1_ON                ; move predefined value to PORTA TO switch ON LED1
    108.            movwf       PORTA
    109.            movlw       TRIS_D2_D3       ; move predefined value to TRISA to switch ON LED2
    110.            TRIS    PORTA
    111.            movlw       D2_ON                ; move predefined value to PORTA TO switch ON LED2
    112.            movwf       PORTA
    113.            decfsz  COUNT1,1        ;Subtract 1 from 00h (this gives 255 or FFh)
    114.                goto    LOOP7           ;If COUNT is zero, carry on.
    115.                decfsz  COUNT2,1        ;Subtract 1 from 00h
    116.                goto    LOOP7           ;Go back to the start of our loop
    117.  
    118. ;**********End of program *****************
    119.  
    120.           goto    START           ; indefinite loop
    121.                 END                       ; End instruction is needed by some compilers and also useful in case we miss goto instruction
    122.  
    123. ;*********************************************************************************
     
    Last edited: Apr 28, 2012
  6. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    I'm confused, are the LEDs wired in an unusual way that requires TRISA to be changed?
    If so could you describe it?
    If the LEDs were on 3 pins they could be set to output and left that way.
    I thought "TRIS PORTA" wouldn't work on the PIC16F684, but I just checked in the simulator and apparently it does. It's not the recommended method but only because it isn't compatible with other PICs.
     
  7. Glitchezz

    Thread Starter New Member

    Apr 26, 2012
    3
    0
    Yes. According to the poorly explained tutorials that our university supply. The 8 LEDs are not connected in a straight forward way.

    "Connecting 8 LEDs to four pins means that connections are not very straight forward."

    [​IMG]


    This results in following settings for TRISA and PORTA
    B'00001111' ; TRISA setting for D0 and D1
    B'00101011' ; TRISA setting for D2 and D3
    B'00011011' ; TRISA setting for D4 and D5
    B'00111001' ; TRISA setting for D6 and D7

    Data sent to PORTA to turn on corresponding LED
    B'00010000' ; D0 LED ON
    B'00100000' ; D1 LED ON
    B'00010000' ; D2 LED ON
    B'00000100' ; D3 LED ON
    B'00100000' ; D4 LED ON
    B'00000100' ; D5 LED ON
    B'00000100' ; D6 LED ON
    B'00000010' ; D7 LED ON
     
  8. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    Ah, Charlieplexing.
    A feature of that is that only one LED is on at a given time. To give the impression of more than one lit, you have to alternately turn them on at a fairly fast rate (1ms per LED would look good). In your code where you aim to turn two on, what actually happens is the first one is turned on for only a few microseconds, then the other is turned on for the rest of the period.
    It's more complicated than you need and it's for a different PIC, but if you click on my name, then blog, you should find something that might give you a few ideas.
     
Loading...