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.

What am I doing wrong? (PIC ASM)

Status
Not open for further replies.
Okay, just to get myself back into having fun with PIC's, I wrote a little program to just make PORTA's bits turn on and off with a delay.

Code:
	#include P16F876A.INC

main	BSF	STATUS,5
	MOVLW	0x00
	MOVWF	TRISA
	BCF	STATUS,5

loop	CALL	delay
	COMF	PORTA
	GOTO	loop

delay	MOVLW	0x10
	MOVWF	0x20
cyc	DECFSZ	0x20
	GOTO	cyc
	RETURN
	
	END

The delay/cyc loop just sticks 16 in general register 0x20 and loops back to provide a delay for the rest of the loop, which just uses a COMF to (One would hope) invert all the bits in PORTA. But, it doesn't, which leads me to (correctly) assume that I've done something wrong.

In Proteus, it won't even do anything, and in Oshonsoft's PIC Simulator, it first turns all of PORTA on at the first iteration of the loop, but then only inverts one of the bits thereafter on each iteration. Could someone tell me what I did wrong? The code is simple, and seems solid...Maybe I'm just not using COMF correctly?
 
Maybe you need an ORG and a CONFIG line. Also, isn't port A configured as analogue by default?

Mike.
 
Pommie said:
Maybe you need an ORG and a CONFIG line. Also, isn't port A configured as analogue by default?

Mike.

The CONFIG line isn't needed for simulation (as far as I know), and the step-by-step debugger looks solid...But the thing just doesn't work as I'd expect it to.
 
You need to switch port A to digital.

Try,
Code:
main	BSF	STATUS,5
[COLOR="Red"]	MOVLW	0x06	; Configure all pins
	MOVWF	ADCON1	; as digital inputs[/COLOR]
	MOVLW	0x00
	MOVWF	TRISA
	BCF	STATUS,5

You can output bits with the port set as analogue but any reads will return zero. Your complement will therefore always read zero and always write 0xff. RA4 is the exception as it is not an analogue input.

Mike.
 
Last edited:
hi,
This will run on your Oshonsoft Simulator & MPLAB, as Pommie states, you missed ADCON1

Code:
;USED WITH OSHONSOFT SIM

		list      p=16f876A
	#include <p16f876A.inc>

 	errorlevel -302, -207

 __CONFIG _CP_OFF & _XT_OSC & _PWRTE_ON  & _WDT_OFF & _LVP_OFF  & _DEBUG_OFF



main:	BSF	STATUS,RP0
	MOVLW 0X07
	MOVWF ADCON1
	MOVLW 0X00
	MOVWF TRISA
	BCF STATUS,RP0



loop:	CALL	delay
	COMF	PORTA,F
	GOTO	loop

delay:	MOVLW	0x10
	MOVWF	0x20
cyc:	DECFSZ	0x20,F
	GOTO	cyc
	RETURN
	
	END

Eric
 
Last edited:
May I sugget to make a template asm file for each microcontrolle partnumber. Put every bit, flag, value, and meaning to all SFRs at the top. Pull from this template at the start of a new project and remove unessential code as desired.

Similar to this but not like this:
Code:
        LIST    P=16F648
         INCLUDE "P16F648A.INC"
	INCLUDE "SHORTS.INC"
        __CONFIG _LVP_OFF & _MCLRE_OFF & _INTRC_OSC_NOCLKOUT & _BODEN_OFF & _CP_ON & _WDT_OFF & _PWRTE_OFF
        ERRORLEVEL -302        

        
	CBLOCK 0X20 ; TEMPORARY STORAGE-------------
	CNTJ, CNTK, CNTP1, CNTP2, CNTP3, CNT1, CNT2, W_REG, STATUS_REG
         PORTB_TEMP, MS_TIME, MS_TIME_TEMP, DEB_CNT, DECOUNT
	;------------THIS PROGRAM NEEDS...---------
	SDELAYCOUNT
	ENDC ;--------TEMPORARY STORAGE-------------

	ORG	0X04
START:	;-----PORT I/O CONFIGURATION-------------------------------
         BANK0
	BSF	CMCON, CM2		;DISABLE COMPARATOR.
	BSF	CMCON, CM1		
	BSF	CMCON, CM0
	BANK1
         LOADF    TRISA, B'11110111'
         LOADF    TRISB, B'00000000'
         BSF	OPTION_REG, NOT_RBPU	;BCF/BSF = ENABLE/DISABLE PULL-UP.
	BCF	OPTION_REG, INTEDG 	;BSF/BCF = HIGH/LOW - RB0/INT EDGE TRIGGER
		;------POWER ON RESET CONFIGURATION--------------------------
	BSF	PCON, OSCF	;C/S = 37KHZ/4MHZ     OSCILLATOR SPEED.
	BANK1	;------INTERRUPT CONFIGURATION-------------------------------
	BCF	INTCON, PEIE	;C/S = DIS/ENABLE     PERIPHERAL INTERRUPTS.
	BCF	PIE1, TMR1IE  	;C/S = DIS/ENABLE     TIMER1     INTERRUPT.
	BCF	PIE1, CCP1IE	;C/S = DIS/ENABLE	    CAPTURE    INTERRUPT.
	BCF	PIE1, CMIE	;C/S = DIS/ENABLE	    COMPARATOR INTERRUPT.
	BCF	PIE1, EEIE	;C/S = DIS/ENABLE	    EEPROM     INTERRUPT.
	BCF	INTCON, GIE  	;C/S = DIS/ENABLE     GLOBAL     INTERRUPT.
	BCF	INTCON, T0IE	;C/S = DIS/ENABLE	    TIMER0     INTERRUPT.
         BANK0	;----TIMER1 CONFIGURATION-----------------------------------
	BCF	T1CON, TMR1ON 	;C/S = DIS/ENABLE     	     TIMER1 ON.
	BCF	T1CON, TMR1CS 	;C/S = IN/EXTERNAL(RA6)   		CLOCK.
         BANK1	;----TIMER0 CONFIGURATION-----------------------------------
	BCF	OPTION_REG, T0CS	;C/S = IN/EXTERNAL(RA4)   		CLOCK.
	BCF	OPTION_REG, PSA	;C/S = TIMER0/WDT    PRESCALER ASSIGNMENT.
	BCF	OPTION_REG, .2	;PRESCALER
	BCF	OPTION_REG, .1	;000=1:2,  001=1:4,  010=1:8,   011=1:16
	BCF	OPTION_REG, .0	;100=1:32, 101=1:64, 110=1:128, 111=1:256
	BANK0	;-------------------------------------------------------------
;*********MAIN**************************
	CLRF	PORTB

MAIN:
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top