PIC Assembler Beginner's Problem

Thread Starter

marktyers

Joined Dec 29, 2010
7
Hi
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:

PIN1 MCLR to PIC PIN4
PIN2 VDD to PIC PIN1
PIN3 VSS to PIC PIN8
PIN4 ICSPDAT to PIC PIN7
PIN5 ICSPCLK to PIC PIN6
PIN6 AUX to PIC PIN5 (not used?)

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

Rich (BB code):
        processor     12F675	;set the correct PIC type
 
GPIO    EQU     05h
TRISO   EQU     85h
 
	ORG	0x000
        GOTO    MAIN
 
MAIN:   BSF     03h,5    ;Go to Bank 1
        MOVLW   B'000000';Put 111111 into W
        MOVWF   85h      ;Move 111111 onto TRISO (set pins as output)
        BCF     03h,5    ;return to Bank 0
 
START:  MOVLW   B'111111';put 111111 into W
        MOVWF   GPIO	 ;sets output pins high
        ;MOVLW   b'000000';put 000000 into W
        ;MOVWF   GPIO	 ;sets output pins low
	GOTO    START
END
I use the following commands to assemble to code and upload/run it on the PIC12F675

Rich (BB code):
; gpasm flash.asm                                   --assembles the program
; pk2cmd -P pic12f675 -M -F flash.hex               --burns the program to the MCU
; pk2cmd -P pic12f675 -A5 -T                        --powers the circuit at 5v
; pk2cmd -P pic12f675 -W                            --removes power from the circuit
; pk2cmd -PPIC12F675 -GF my_program.hex             --retrieves program stored on MCU
; 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:

Rich (BB code):
000000:  2801  goto	0x1
000001:  1683  bsf	0x3, 0x5
000002:  3000  movlw	0
000003:  0085  movwf	0x5
000004:  1283  bcf	0x3, 0x5
000005:  303f  movlw	0x3f
000006:  0085  movwf	0x5
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:

Rich (BB code):
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...

Thanks.
 

thatoneguy

Joined Feb 19, 2009
6,359
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.
 

Thread Starter

marktyers

Joined Dec 29, 2010
7
I tried to switch banks on the line
Rich (BB code):
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?

thanks
Mark.
 

t06afre

Joined May 11, 2009
5,934
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 :)
 

Markd77

Joined Sep 7, 2009
2,806
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.
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).

Rich (BB code):
    list    p=12F675 
    radix    hex 
    title "sound" 
        #include <p12f675.inc> 
    __config _CP_OFF & _BODEN_OFF & _MCLRE_OFF & _PWRTE_OFF & _WDT_OFF & _INTRC_OSC_NOCLKOUT 
 
 
 
    errorlevel  -302              ; suppress message 302 from list file
something like this sets up the port:
Rich (BB code):
CLRF GPIO ;Init GPIO 
    MOVLW b'00000111'                 ;all digital IO 
    MOVWF CMCON                     ;disable comparitor 
    BSF STATUS,RP0 ;Bank 1 
 
 
    clrf ANSEL                         
 
 
    MOVLW b'00111100' 
    MOVWF TRISIO  
    bcf STATUS,RP0                    ;bank0
 
Last edited:

Thread Starter

marktyers

Joined Dec 29, 2010
7
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 :)
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.

Mark
 

Thread Starter

marktyers

Joined Dec 29, 2010
7
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).

Rich (BB code):
    list    p=12F675 
    radix    hex 
    title "sound" 
        #include <p12f675.inc> 
    __config _CP_OFF & _BODEN_OFF & _MCLRE_OFF & _PWRTE_OFF & _WDT_OFF & _INTRC_OSC_NOCLKOUT 
 
 
 
    errorlevel  -302              ; suppress message 302 from list file
something like this sets up the port:
Rich (BB code):
CLRF GPIO ;Init GPIO 
    MOVLW b'00000111'                 ;all digital IO 
    MOVWF CMCON                     ;disable comparitor 
    BSF STATUS,RP0 ;Bank 1 
 
 
    clrf ANSEL                         
 
 
    MOVLW b'00111100' 
    MOVWF TRISIO  
    bcf STATUS,RP0                    ;bank0
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.
Mark
 

Thread Starter

marktyers

Joined Dec 29, 2010
7
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:

Rich (BB code):
; gpasm flash.asm                                   --assembles the program
; pk2cmd -P pic12f675 -M -F flash.hex               --burns the program to the MCU
; pk2cmd -PPIC12f675 -Y -Fflash.hex                 --verify the program
; pk2cmd -P pic12f675 -A5 -T                        --powers the circuit at 5v
; pk2cmd -P pic12f675 -W                            --removes power from the circuit
; pk2cmd -PPIC12F675 -GF my_program.hex             --retrieves program stored on MCU
; gpdasm -p12F675 my_program.hex > my_program.asm   --disassembles program

        LIST P=12F675
        #include <p12f675.inc>
        __config _CP_OFF & _BODEN_OFF & _MCLRE_OFF & _PWRTE_OFF & _WDT_OFF & _INTRC_OSC_NOCLKOUT
        ; _CP_OFF               ; code protection off
        ; _BODEL_OFF            ; brown-out reset enable bit off
        ; _MCLRE_OFF            ; MCLR bit used as a digital IO, internally tied to Vdd
        ; _PWRTE_OFF            ; power up timer enable bit off
        ; _WDT_OFF              ; watchdog timer disabled
        ; _INTRC_OSC_NOCLKOUT   ; 
        errorlevel -302

	org	0x000

MAIN:   bcf     STATUS,RP0  ;select bank 0
        clrf    GPIO        ;clear all GPIO pins
        movlw   B'111111'   ;set GP<5:0> to digital IO
        movwf   CMCON
        bsf     STATUS,RP0  ;select bank 1
        clrf    ANSEL       ;clear the analog selection register
        movlw   B'001100'   ;Put 00110 into W
        movwf   TRISIO       ;Move 00110 onto TRISIO
        bcf     STATUS,RP0  ; back to bank 0

START:  movlw   B'110011'   ;set GP<5:4><1:0> to high
        movwf   GPIO	    ;move contents of W onto GPIO
        movlw   b'000000'   ;set GP<5:0> to low
        movwf   GPIO	    ;move contents of W onto GPIO
	goto    START
END
The only question left is for someone to explain what the last config setting does...

Thanks for all of your help.
Mark
 

t06afre

Joined May 11, 2009
5,934
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.
The only question left is for someone to explain what the last config setting does...
Thanks for all of your help.
Mark
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:
 
Top