Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

programming/compiling in MPLAB problem

Status
Not open for further replies.

Hank Fletcher

New Member
I'm having trouble getting some code going to do what I'd expect it to do. Can I have some pointers? Here's my code:

Code:
;********************************************************************************

;Set up the assembler options (Chip type, clock source, other bits and pieces)
 LIST p=16F88, r=DEC
#include <P16F88.inc>
 __CONFIG _CONFIG1, _INTRC_IO & _MCLR_ON & _WDT_OFF & _LVP_OFF

;********************************************************************************


;********************************************************************************

;Start of the main program
STATUS          equ  03h           ;this assigns the word STATUS to the value of 03h,
                                   ;which is the address of the STATUS register.

TRISB           equ 86h            ;This assigns the word TRISB to the value of 86h,
                                   ;which is the address of the Tri-State register for PortB

PORTB           equ 06h            ;This assigns the word PORTB to 06h which is the
                                   ;address of Port B.

;********************************************************************************

	bsf		STATUS,5		; select Bank 1
	movlw	00h				; set the W register to 00h which
    movwf	TRISB			; sets all pins on PortB as outputs
    bcf		STATUS,5		; select Bank 0

;Start of the main program
Start	movlw	b'10'	; put binary value in W register to
        movwf   PORTB   ; set PortB.1 high an PortB.0 low
       ; movlw   b'00'   ; put binary value in W register to
       ; movwf   PORTA   ; set both PortB pins low again
        goto    Start   ; rinse'n'repeat!

		

 END
I should remark this is my first attempt at writing directly in assembly, and compiling the code in MPLAB. Maybe I'm making a mistake there? Do I just have to export a hex file, and save my assembly, then import the hex file and then program the chip? That's what I've been doing, anyway, and every time I've programmed I haven't been warned of any errors.

I thought this code would set PortB.1 high, but testing with a voltmeter I don't see that happening (it just kind of sits like most of the other PortB pins around 0.05V when the power's on).

Everything after "Start of main program," is mine, but the code before that is part of the header I've copied from what GCBASIC compiles to initialize the chip. There's a variety of other stuff I cut that GCBASIC does by default to initialize the chip, too, so maybe there's something I'm missing about that, too?
 
Last edited:
MPLABs built in simulator should be your first line of troubleshooting. First thing I noticed was you're missing an ORG statement.

ORG 0x000

Will tell MPASM where to put the code.

Second copy & paste from a compiled language is not a good idea IMO.

Your program should redefine definitions like the example you posted.
 
Last edited:
If you have an ICD or the Simulator you can single step through the program, else the LED turns on & off way to fast to see.
Code:
        LIST p=16F88, r=DEC
        #include <P16F88.inc>
        __CONFIG _CONFIG1, _INTRC_IO & _MCLR_ON & _WDT_OFF & _LVP_OFF
        org     0x000
    bsf        STATUS,RP0        ; select Bank 1
    movlw    0x00                ; set the W register to 00h which
        movwf    TRISB            ; sets all pins on PortB as outputs
        bcf        STATUS,RP0        ; select Bank 0
;Start of the main program
Start    movlw    b'00000010'    ; put binary value in W register to
        movwf   PORTB           ; set PortB.1 high an PortB.0 low
        movlw   b'00000000'   ; put binary value in W register to
        movwf   PORTB           ; set both PortB pins low again
        goto    Start           ; rinse'n'repeat!
 END
 
I'm having trouble getting some code going to do what I'd expect it to do. Can I have some pointers? Here's my code:

Code:
 LIST p=16F88, r=DEC
Better to leave out the "r=DEC" radix change and leave it at the default hex radix. No biggy though.


These are unnecessary and redundant:
Code:
STATUS          equ  03h           ;this assigns the word STATUS to the value of 03h,
                                   ;which is the address of the STATUS register.

TRISB           equ 86h            ;This assigns the word TRISB to the value of 86h,
                                   ;which is the address of the Tri-State register for PortB

PORTB           equ 06h            ;This assigns the word PORTB to 06h which is the
                                   ;address of Port B.
They're already defined in
Code:
#include <P16F88.inc>


Instead of this:
Code:
	bsf	STATUS,5		; select Bank 1
	movlw	00h			; set the W register to 00h which
	movwf	TRISB			; sets all pins on PortB as outputs
	bcf	STATUS,5		; select Bank 0
do this:
Code:
	banksel	TRISB		;bank 1
	movlw	0x00
	movwf	TRISB
	banksel	PORTA		;bank 0


You need an org statement before your code starts
Code:
	org	0x00
Start	movlw	b'00000010'	; put binary value in W register to
        movwf   PORTB   ; set PortB.1 high an PortB.0 low
       ; movlw   0   ; put binary value in W register to
       ; movwf   PORTA   ; set both PortB pins low again
        goto    Start   ; rinse'n'repeat!
 
Last edited:
Is this better? It's all me, although I'm trying to adapt the code from a tutorial to use PortA instead of PortB.
Code:
ORG 0x000

;********************************************************************************

;Start of the main program
STATUS          equ  03h           ;this assigns the word STATUS to the value of 03h,
                                   ;which is the address of the STATUS register.

TRISB           equ 86h            ;This assigns the word TRISB to the value of 86h,
                                   ;which is the address of the Tri-State register for PortB

PORTB           equ 06h            ;This assigns the word PORTB to 06h which is the
                                   ;address of Port B.

;********************************************************************************

	bsf		STATUS,5		; select Bank 1
	movlw	00h				; set the W register to 00h which
    movwf	TRISB			; sets all pins on PortB as outputs
    bcf		STATUS,5		; select Bank 0

;Start of the main program
Start	movlw	b'10'	; put binary value in W register to
        movwf   PORTB   ; set PortB.1 high an PortB.0 low
       ; movlw   b'00'   ; put binary value in W register to
       ; movwf   PORTA   ; set both PortB pins low again
        goto    Start   ; rinse'n'repeat!

	
 END
1) I don't know how to use the simulator
2) I don't know what you mean by this:
Your program should redefine definitions like the example you posted.
3) Presuming I'm setting the configuration bits in MPLAB, what else am I missing?

Thanks.
 
LOL you'll have to scroll up to see my version of your program :) Meh here it is again
Code:
        LIST p=16F88, r=DEC
        #include <P16F88.inc>
        __CONFIG _CONFIG1, _INTRC_IO & _MCLR_ON & _WDT_OFF & _LVP_OFF
        org     0x000
    bsf        STATUS,RP0        ; select Bank 1
    movlw    0x00                ; set the W register to 00h which
        movwf    TRISB            ; sets all pins on PortB as outputs
        bcf        STATUS,RP0        ; select Bank 0
;Start of the main program
Start    movlw    b'00000010'    ; put binary value in W register to
        movwf   PORTB           ; set PortB.1 high an PortB.0 low
        movlw   b'00000000'   ; put binary value in W register to
        movwf   PORTB           ; set both PortB pins low again
        goto    Start           ; rinse'n'repeat!
 END
 
I was wondering about banksel as I was working through the tutorial. Is that from the include file, too? How can I know what protocol to follow in accordance with the other details of the include file, such as simply saying "movwf TRISB."

Yeah, I saw your program and I'll give it a try - thanks, Bill! Thanks for breaking it down, too, futz.

FYI: I know I'm coding to make the LED blink really fast, but I should still be able to read one or two volts off the pin with a multimeter. That's good enough for me right now - this is all part of me learning to toggle two pins fast enough to get some kind of NTSC signal going with just an internal oscillator.
 
Last edited:
FYI: I know I'm coding to make the LED blink really fast, but I should still be able to read one or two volts off the pin with a multimeter.
If the pin is toggling at anything faster than once every second or two, forget about getting a reading with a multimeter (unless maybe your meter is real high end). They just don't work fast enough. Set the pin high and leave it high and measure and it will read 5V.
 
I tried Bill's correction to my program, but it doesn't seem to do anything. Am I missing something really goofy here, like it terms of saving the .asm and .hex and programming the mcu?
 
If the pin is toggling at anything faster than once every second or two, forget about getting a reading with a multimeter (unless maybe your meter is real high end). They just don't work fast enough. Set the pin high and leave it high and measure and it will read 5V.
Yeah, I thought that might be the deal, which is why setting both pins to zero is remmed out in my OP. I'll give Bill's code a try again with those two lines remmed (is that even a verb?) again, and see what the multimeter says.
 
Last edited:
You need a delay, or a scope. The 16F88 runs way to fast to see it flash.

I have a demo program for the 16F88 in the Firefly manual, might be helpful.

PS it is RB1 (PIN7) your checking right? And you do have a 0.1uf cap near the power pins plus a 10K to 22K pullup on pin4 (MCLR)... Right...
 
Last edited:
PS it is RB1 (PIN7) your checking right? And you do have a 0.1uf cap near the power pins plus a 10K to 22K pullup on pin4 (MCLR)... Right..
Yeah, I'm working through the goof list. I'm checking the right pin, and I have MCLR pull up. I've never put a cap across the power pins, and I've never had any trouble by not doing so, but I'll give that a try.
 
I tried this:
Code:
list p=16f88
	include (p16f88.inc)
	__CONFIG _CONFIG1, 0x2F34
	org 0
	bsf STATUS, RP0
	movlw b'00001110'
	movwf OPTION_REG
	movlw b'00111111'
	movwf TRISA
	bcf STATUS, RP0
	movlw b'10000000'
	xorwf PORTA, f
	sleep
	end
Still no blinking. Does this code presume an external oscillator? I'm not using one.
 
Still no blinking. Does this code presume an external oscillator? I'm not using one.
Here's a working blinky for RA1 of the 16F88:
Code:
	LIST	p=16F88
	include "P16F88.inc"

	cblock	0x20		;start of general purpose registers
	d1,d2,d3
	endc

	org	0x0000
init
	banksel CMCON		;bank 1
	movlw	0x07		;turn comparators off
	movwf	CMCON
	clrf	ANSEL		;all pins digital
	clrf	TRISA		;all pins outputs
	movwf	TRISB
	banksel	PORTA		;select bank 0

main	bsf	PORTA,1
	call	delay
	bcf	PORTA,1
	call	delay
	goto	main

delay	movlw	0x15			;2499992 cycles
	movwf	d1
	movlw	0x74
	movwf	d2
	movlw	0x05
	movwf	d3
delay_0	decfsz	d1, f
	goto	dd2
	decfsz	d2, f
dd2	goto	dd3
	decfsz	d3, f
dd3	goto	delay_0
	return

	end
 
Last edited:
Here's a working blinky for RA1 of the 16F88
I'll give that a try. I'm sure with Bill's code it's something basic I'm missing in terms of writing, compiling, programming the PIC.

Sorry to really break it down here, but do you mind walking me through it? I've successfully made a fair share of PIC programs and circuits using GCBASIC, but I'm looking to take the training wheels off! I know a lot of this might seem really obvious, but it seems the only way to troubleshoot it, so here it goes:

1) I open up MPLAB.
2) I select File> New.
3) This opens an "Untitled" window, which I notice is very bare, as in no code or even PC numbers in the margin when I type stuff in. That's new to me, because they're always there when you compile from GCBASIC.
4) I copy and paste in futz's code, but the text is all black, as in no colour-coding that I usually see in a compiled GCBASIC program. Also, the "program," "release from reset," etc icons are not present.

That's where I'm at. Can you get me to point B?
 
1) I open up MPLAB.
2) I select File> New.
3) This opens an "Untitled" window, which I notice is very bare, as in no code or even PC numbers in the margin when I type stuff in.
That's because you haven't saved the file yet. Save it as blinky.asm and then you'll get your code colours.
I use the code templates located at:
C:\Program Files\Microchip\MPASM Suite\Template\Code
They provide a nice starting point for building a project. Look for the file f88temp.asm
Also, the "program," "release from reset," etc icons are not present.
That's where I'm at. Can you get me to point B?
To get these, you need to select your programmer. Click "Programmer/Select Programmer" and select the ICD2, etc. Same goes for the debugger; you won't have any icons for it until you select which one to use.
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top