Help with assembly code for 16F627/8

Discussion in 'Embedded Systems and Microcontrollers' started by mr boss, Jun 4, 2009.

  1. mr boss

    Thread Starter Member

    Jun 3, 2009

    This is my first post here. I need some advice on modifying 16F84 code for 16F627/8. I am trying to learn myself assembly language from
    The tutorial uses a 16F84 for all there explanations.

    Now to the problem. I am stuck on "delay loops" I have burned the example code from the tutorial to both my pics. (16F627/8).

    The example code is supposed to make a LED on pin 18 of the pic oscillate. When I applied power to the little circuit the Led didnt oscilate at all. I did check it with a scope. I aslo don't know how to use a external Rc oscillator for the pic, so I used a 32.768khz crystal.

    Last but not least. Ive readed the datasheet on my pics, and according to it, I should first disable the comparators. Ive used the datasheets small example code to disable it, but then when the computer put the code together it shows errors.

    Any help would be great

    Here is the code:

    Code ( (Unknown Language)):
    2. ;*****Set up the Constants****
    4. STATUS        equ       03h         ;Address of the STATUS register
    5. TRISA         equ       85h         ;Address of the tristate register for port A
    6. PORTA         equ       05h         ;Address of Port A
    7. COUNT1        equ       08h         ;First counter for our delay loops
    8. COUNT2        equ       09h         ;Second counter for our delay loops
    9. CMCON         equ       1Fh
    11. ; Turn off coparators ;
    13. movlw   07h
    14. movwf   CMCON
    16. ;****Set up the port****
    18. bsf                STATUS,5         ;Switch to Bank 1
    19. movlw              00h              ;Set the Port A pins
    20. movwf              TRISA            ;to output.
    21. bcf                STATUS,5         ;Switch back to Bank 0
    23. ;****Turn the LED on****
    25. Start       movlw          02h        ;Turn the LED on by first putting
    26.             movwf          PORTA      ;it into the w register and then
    27.                                       ;on the port
    29. ;****Start of the delay loop 1****
    31. Loop1          decfsz       COUNT1,1       ;Subtract 1 from 255
    32.                goto         Loop1          ;If COUNT is zero, carry on.
    33.                decfsz       COUNT2,1       ;Subtract 1 from 255
    34.                goto         Loop1          ;Go back to the start of our loop.                                                                      
    35.                                            ;This delay counts down from                                            
    36.                                            ;255 to zero, 255 times
    38. ;****Delay finished, now turn the LED off****
    40. movlw              00h                ;Turn the LED off by first putting
    41. movwf              PORTA              ;it into the w register and then on                                                                      
    42.                                       ;the port
    44. ;****Add another delay****
    46. Loop2        decfsz             COUNT1,1           ;This second loop keeps the
    47.              goto               Loop2              ;LED turned off long enough for
    48.              decfsz             COUNT2,1           ;us to see it turned off
    49.              goto               Loop2              ;
    51. ;****Now go back to the start of the program
    53. goto                 Start              ;go back to Start and turn LED                                                                  
    54.                                         ;on again
    56. ;****End of the program****
    58. end  
  2. eblc1388

    Senior Member

    Nov 28, 2008
    Your problem is an easy one to solve.

    16F627/8 does not has memory at location 08H and 09H in Bank0 so your delay loop counter will not work.

    In addition, choosing 08h or 09h even for 16F84 is a poor choice as that two locations is reserved for EEPROM access. There are a lot of other free memory available in the 16F84 Bank0 so using those two locations just cause additional confusion and uncertainty to beginners.

    To recap, the proper user memory location address for 16F84 is starting from 0Ch onward and for 16F627/8 is 20h. The data memory area is clearly mentioned under the section "Data Memory Organization" in both 16F84 and 16F627/8 datasheet.

    Code ( (Unknown Language)):
    2. ;*****Set up the Constants for 16F627/8 ****
    4. COUNT1        equ       20h         ;First counter for our delay loops
    5. COUNT2        equ       21h         ;Second counter for our delay loops
    An additional point is that one does not have to define all the register file names and their addresses. There is a file "P16F627.INC" which user can "include" into the ASM file which then list all these names and their respective addresses.

    Find and check out the content of that file under the directory "..\Microchip\MPASM Suite"
  3. mr boss

    Thread Starter Member

    Jun 3, 2009
    Thanks alot for your help. If the tutorial makes use of a memory location like that again, can I just ignore it, and change it to a differnt one? And also, is it really nessecary to turn off the comparators? Will it affect the ports if I dont turn it off?
  4. eblc1388

    Senior Member

    Nov 28, 2008
    Yes you certainly can and should do so.

    Of most codes I have seen on the internet no one ever use that two locations as user variable UNLESS all available memory space in BANK0 has been used up for other purposes. I don't know why the author did that in the first place.

    The CMCON register value determine the nature of portA RA0 to RA3 pins so one has to configure it correctly into digital mode if one wants to use these pins as digital I/O on port A.

    There is a good reason behind why Microchip has chosen to default the pins to analogue mode.