PIC Assembler Beginner's Problem

Discussion in 'Embedded Systems and Microcontrollers' started by marktyers, Dec 29, 2010.

  1. marktyers

    Thread Starter New Member

    Dec 29, 2010
    I am new to assembler programming and am trying to get a simple program up and running on a PIC12F675 using a PICKIT2

    I have wired up the circuit using pins 1-5 on the PICKIT2 programmer according to this diagram:

    PIN2 VDD to PIC PIN1
    PIN3 VSS to PIC PIN8
    PIN6 AUX to PIC PIN5 (not used?)

    The problem is in the assembling of the code. My original code is:

    Code ( (Unknown Language)):
    1.         processor     12F675    ;set the correct PIC type
    3. GPIO    EQU     05h
    4. TRISO   EQU     85h
    6.     ORG 0x000
    7.         GOTO    MAIN
    9. MAIN:   BSF     03h,5    ;Go to Bank 1
    10.         MOVLW   B'000000';Put 111111 into W
    11.         MOVWF   85h      ;Move 111111 onto TRISO (set pins as output)
    12.         BCF     03h,5    ;return to Bank 0
    14. START:  MOVLW   B'111111';put 111111 into W
    15.         MOVWF   GPIO     ;sets output pins high
    16.         ;MOVLW   b'000000';put 000000 into W
    17.         ;MOVWF   GPIO    ;sets output pins low
    18.     GOTO    START
    19. END
    I use the following commands to assemble to code and upload/run it on the PIC12F675

    Code ( (Unknown Language)):
    1. ; gpasm flash.asm                                   --assembles the program
    2. ; pk2cmd -P pic12f675 -M -F flash.hex               --burns the program to the MCU
    3. ; pk2cmd -P pic12f675 -A5 -T                        --powers the circuit at 5v
    4. ; pk2cmd -P pic12f675 -W                            --removes power from the circuit
    5. ; pk2cmd -PPIC12F675 -GF my_program.hex             --retrieves program stored on MCU
    6. ; gpdasm -p12F675 my_program.hex > my_program.asm   --disassembles program
    The problem I have is that an LED attached to pin 2 (GP5) does not light up. I tried downloading the code and disassembling it which returns:

    Code ( (Unknown Language)):
    1. 000000:  2801  goto 0x1
    2. 000001:  1683  bsf  0x3, 0x5
    3. 000002:  3000  movlw    0
    4. 000003:  0085  movwf    0x5
    5. 000004:  1283  bcf  0x3, 0x5
    6. 000005:  303f  movlw    0x3f
    7. 000006:  0085  movwf    0x5
    8. 000007:  2805  goto 0x5
    Does anyone know what the problem is? The only issue I can see is in address 000003 where it seems to be pushing the direction flag data to address 05h rather than the address 85h specified in my original program. I also got the error:

    Code ( (Unknown Language)):
    1. flash.asm:19:Message [302] Register in operand not in bank 0. Ensure bank bits are correct.
    I have spent ages trying to figure this out and would appreciate any help you can offer...

  2. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    You need to switch banks to access different registers, such as the TRIS registers, then switch back to change the ports. Look at the "Mid-Range PIC App Notes" download from microchip, it has a few examples.

    You may find learning microcontrollers a lot more enjoyable if you start out in C or BASIC (Mikro C, Mikro Basic, Boost C, SourcBoost BASIC, Hi-Tech C, GB Basic, PIC BASIC Pro, etc). There are dozens out there, but I listed the more common ones that are relatively cheap to free.

    All of those compilers will provide a .asm file, which you can then tweak if needed, or understand the statements you used in C and how they look in Assembly for the PIC. All memory management and handling of longer integers is "magically" done with the compiler. Compilers also typically produce better/tighter code than somebody just learning assembly. Though learning assembly is also good, having a working program is more important so you do not get discouraged.
  3. marktyers

    Thread Starter New Member

    Dec 29, 2010
    I tried to switch banks on the line
    Code ( (Unknown Language)):
    2. BSF     03h,5
    this should have switched to bank 1 by setting bit 5 of the STATUS register (RP0). but the next line gets changed by the assembler from address 85h to 05h which I suspect is doe to the bank not getting switched.

    I have already learned how to use high level languages and now need to learn assembler. I get to feeling that there is a very simple explanation as to why my program does not work. It looks at if the bank is not being changed but I CANT SEE WHY!!!!

    Have spent about 2 weeks trying to get this working. Does someone have a simple ASM file that works correctly?

  4. t06afre

    AAC Fanatic!

    May 11, 2009
    Do you set your configuration bits in MPLAB, or do you not set them at all:eek: See section 9.1 Configuration Bits in the datasheet. Also what clock speed do you use, and which setting of the FOSC2:FOSC0: Oscillator Selection bits will be correct in your application?
    The human eye is very slow. In order to see the blinking you have to slow things down. I am going to bed now. But if not your problem is solved then I wake up. I will help you :)
  5. Markd77

    Senior Member

    Sep 7, 2009
    Looks fine, 85h is really 5h, it's just the 5th byte in the second bank - confusing but it's the reason that you have to switch banks anyway.
    The error shows up anyway even if you are in the right bank.

    Try this at the top:
    (It will also hide that annoying error message).

    Code ( (Unknown Language)):
    1.     list    p=12F675
    2.     radix    hex
    3.     title "sound"
    4.         #include <p12f675.inc>
    9.     errorlevel  -302              ; suppress message 302 from list file
    something like this sets up the port:
    Code ( (Unknown Language)):
    1. CLRF GPIO ;Init GPIO
    2.     MOVLW b'00000111'                 ;all digital IO
    3.     MOVWF CMCON                     ;disable comparitor
    4.     BSF STATUS,RP0 ;Bank 1
    7.     clrf ANSEL                        
    10.     MOVLW b'00111100'
    11.     MOVWF TRISIO  
    12.     bcf STATUS,RP0                    ;bank0
    Last edited: Dec 29, 2010
  6. marktyers

    Thread Starter New Member

    Dec 29, 2010
    I am not using MPLAB (using the command-line tools). There are two reasons for this. Firstly I want to understand exactly what is happening without an IDE hiding the details and secondly I am using OSX which does not support MPLAB!

    Am I correct in assuming that my main program is correct but that I need to set some configuration bits?

    Firstly what are these configuration bits? What do they do and how do I set them?

    Thanks for the help guys.

  7. marktyers

    Thread Starter New Member

    Dec 29, 2010
    Thanks for this information but I don't have the include file (can't use MPLAB as I am on OSX). I think I am getting closer to a solution but would it be possible to explain the above without using the labels (assume the include file maps memory addresses to labels?)

    Thanks for your help.
  8. marktyers

    Thread Starter New Member

    Dec 29, 2010
    I have read all your responses and have found them really helpful! The solution was to firstly grab a copy of the p12f675.inc file and drop it into my project folder. Secondly I realised I had missed some critical information on P19 of the datasheet. Basically because the 12F675 is a low pincount chip all the pins can be either analog or digital and this has to be set in the CMCON register (low = digital and high = analog). The final error was ignoring the config settings.
    Here is my working code:

    Code ( (Unknown Language)):
    1. ; gpasm flash.asm                                   --assembles the program
    2. ; pk2cmd -P pic12f675 -M -F flash.hex               --burns the program to the MCU
    3. ; pk2cmd -PPIC12f675 -Y -Fflash.hex                 --verify the program
    4. ; pk2cmd -P pic12f675 -A5 -T                        --powers the circuit at 5v
    5. ; pk2cmd -P pic12f675 -W                            --removes power from the circuit
    6. ; pk2cmd -PPIC12F675 -GF my_program.hex             --retrieves program stored on MCU
    7. ; gpdasm -p12F675 my_program.hex > my_program.asm   --disassembles program
    9.         LIST P=12F675
    10.         #include <p12f675.inc>
    11.         __config _CP_OFF & _BODEN_OFF & _MCLRE_OFF & _PWRTE_OFF & _WDT_OFF & _INTRC_OSC_NOCLKOUT
    12.         ; _CP_OFF               ; code protection off
    13.         ; _BODEL_OFF            ; brown-out reset enable bit off
    14.         ; _MCLRE_OFF            ; MCLR bit used as a digital IO, internally tied to Vdd
    15.         ; _PWRTE_OFF            ; power up timer enable bit off
    16.         ; _WDT_OFF              ; watchdog timer disabled
    17.         ; _INTRC_OSC_NOCLKOUT   ;
    18.         errorlevel -302
    20.     org 0x000
    22. MAIN:   bcf     STATUS,RP0  ;select bank 0
    23.         clrf    GPIO        ;clear all GPIO pins
    24.         movlw   B'111111'   ;set GP<5:0> to digital IO
    25.         movwf   CMCON
    26.         bsf     STATUS,RP0  ;select bank 1
    27.         clrf    ANSEL       ;clear the analog selection register
    28.         movlw   B'001100'   ;Put 00110 into W
    29.         movwf   TRISIO       ;Move 00110 onto TRISIO
    30.         bcf     STATUS,RP0  ; back to bank 0
    32. START:  movlw   B'110011'   ;set GP<5:4><1:0> to high
    33.         movwf   GPIO        ;move contents of W onto GPIO
    34.         movlw   b'000000'   ;set GP<5:0> to low
    35.         movwf   GPIO        ;move contents of W onto GPIO
    36.     goto    START
    37. END
    The only question left is for someone to explain what the last config setting does...

    Thanks for all of your help.
  9. t06afre

    AAC Fanatic!

    May 11, 2009
    Read section 9 in your datasheet. They are "switches" that configure some operations parameters of your MCU. These switches are put together in a configuration word. You will find the the symbolic name for each switch at the the end of the header file. To form a configuration word. You "and" the different switch setting together. You could also write
    __config <some number>
    but for documenting purposes it is better to use the symbolic way.
    MPLAB will not hide anything from you. But it is tool that help you in the programming, like offering both hardware and software debugging. Without MPLAB I would feel like fish on on land. Most because of the debugging tools. MPLAB do not require much CPU power so if you get your hands on some old laptop running windows. I would have used that instead of using some awkward command line tools. Also for your own sake. Do not wait two weeks before you post next time:eek: