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.

Ouput issues on a 12F683

Status
Not open for further replies.

eskimohunter

New Member
Hi I'm new to PICs so this is probably a stupid mistake but I can't seem to change the GPIO register.

The program compiles fine (no warnings or errors) and all the other registers update fine (status TRISO and W) in the watch window. However whenever I try bsf or bcf instructions they do nothing and movwf instructions are just messed up (trying to write 8 results in a 0 being written, writing a 24 results in a 16 being written etc etc) :s. I know theres registers like ANSEL but the data sheet says they only affect input. I'm also aware of the 'Read-Modify-Write problem' but that shouldn't affect the MPLAB SIM. I'm not sure if the problems even in code as previously working code now doesn't want to function.

I can post code if people want to but I suspect it's either MPLABs got messed up somehow or I'm not setting up the GPIO properly. As far as I was aware though you only need to set the TRISIO bits to 0.

Any help much appreciated :)

Rob
 
Hi I'm new to PICs so this is probably a stupid mistake but I can't seem to change the GPIO register.

The program compiles fine (no warnings or errors) and all the other registers update fine (status TRISO and W) in the watch window. However whenever I try bsf or bcf instructions they do nothing and movwf instructions are just messed up (trying to write 8 results in a 0 being written, writing a 24 results in a 16 being written etc etc) :s. I know theres registers like ANSEL but the data sheet says they only affect input. I'm also aware of the 'Read-Modify-Write problem' but that shouldn't affect the MPLAB SIM. I'm not sure if the problems even in code as previously working code now doesn't want to function.

I can post code if people want to but I suspect it's either MPLABs got messed up somehow or I'm not setting up the GPIO properly. As far as I was aware though you only need to set the TRISIO bits to 0.

Any help much appreciated :)

Rob

setting TRISIO bits to 0, makes the corespondent GP as output ( except for GP3), It would be better if you post your full code, as the proper setting is very important. I've faced the same problem.
 
Last edited:
Full code

Code:
;------------------------------------------------------------------------------
; PROCESSOR DECLARATION
;------------------------------------------------------------------------------

     LIST      P=12F683              ; list directive to define processor
     #INCLUDE <P12F683.INC>          ; processor specific variable definitions

;------------------------------------------------------------------------------
; CONFIGURATION WORD SETUP
;------------------------------------------------------------------------------

    __CONFIG   _FCMEN_ON & _IESO_OFF & _CP_OFF & _CPD_OFF & _BOD_OFF & _MCLRE_ON & _WDT_OFF & _PWRTE_ON & _INTRC_OSC_NOCLKOUT 

;------------------------------------------------------------------------------
; RESET VECTOR
;------------------------------------------------------------------------------

RESET_VECTOR  CODE    0x0000  ; processor reset vector
        GOTO    START         ; go to beginning of program


;------------------------------------------------------------------------------
; MAIN PROGRAM
;------------------------------------------------------------------------------

MAIN_PROG     CODE

START

;---------------
;**** Setup ****
;---------------

;***Setup the port***
	bsf STATUS,RP0
	clrf TRISIO
	bcf STATUS,RP0

;***Variable declaration***
RED		equ 20h;	Red value
GREEN	equ 21h;	Green value
BLUE	equ 22h;	Blue Value
RED2	equ	23h;	----------------
GREEN2	equ	24h;	Used in workings
BLUE2	equ	25h;	----------------
COUNT	equ 26h; 	Counter for the main loop
;***Variable Initilization***
	clrf	RED2	;---------------------------
	clrf	GREEN2	;Clear the working registers
	clrf	BLUE2	;---------------------------
	clrf	COUNT	;Clear the main count register

;-------------------
;**** Main Loop ****
;-------------------
MAIN
	movlw	64h
	movwf	COUNT	;Set Count to 100
;***Set RGB values***
	movlw	14h	;RED RGB value is 20
	movwf	RED
	movlw	51h	;GREEN RGB value is 81
	movwf	GREEN
	movlw	12h	;BLUE RGB value is 18
	movwf	BLUE
SUBCALL	;--------------------------------------------------------------Calls SUB 100 times
	Call	SUB		;Call the RGB subroutine
	decfsz	COUNT	;See if count is zero, if it is go back to main, if not run SUB again
	goto	SUBCALL	;---------------------------------------------------------------------
	goto	MAIN

;------------------
;**** Sub Loop ****
;------------------
SUB
	call	REDFUNC
	call	GREENFUNC
	call	BLUEFUNC
	RETURN


;---------------
;*** REDFUNC ***
;---------------
REDFUNC
	movfw	RED		;move RED into the working register
	movwf	RED2	;Copy RED to RED2
	incf	RED2	;Increment RED2
	decf	RED2	;Decrement RED2
	btfss	STATUS,Z;If RED=0 then the result of this will be to skip the goto REDN0
	goto	REDN0	;If RED > 0
	bcf		GPIO,GP0;If RED = 0 then turn off the LED
	nop				;-------------------------------------------------------------
	nop				; Make the RED = 0 brach the same length as the RED > 0 branch
	nop				;-------------------------------------------------------------
	RETURN			;return to SUB
REDN0
	bsf		GPIO,GP0;If RED > 0 then turn on the LED
	decf	RED		; Decrement the RED RGB value
	RETURN 			;return to SUB

;---------------
;*** GREENFUNC ***
;---------------
GREENFUNC
	movfw	GREEN	;move GREEN into the working register
	movwf	GREEN2	;Copy GREEN to GREEN2
	incf	GREEN2	;Increment GREEN2
	decf	GREEN2	;Decrement GREEN2
	btfss	STATUS,Z;If GREEN=0 then the result of this will be to skip the goto GREENN0
	goto	GREENN0	;If GREEN > 0
	bcf		GPIO,GP1;If GREEN = 0 then turn off the LED
	nop				;-------------------------------------------------------------
	nop				; Make the GREEN = 0 brach the same length as the GREEN > 0 branch
	nop				;-------------------------------------------------------------
	RETURN			;return to SUB
GREENN0
	bsf		GPIO,GP1;If GREEN > 0 then turn on the LED
	decf	GREEN		; Decrement the GREEN RGB value
	RETURN 			;return to SUB

;---------------
;*** BLUEFUNC ***
;---------------
BLUEFUNC
	movfw	BLUE	;move BLUE into the working register
	movwf	BLUE2	;Copy BLUE to BLUE2
	incf	BLUE2	;Increment BLUE2
	decf	BLUE2	;Decrement BLUE2
	btfss	STATUS,Z;If BLUE=0 then the result of this will be to skip the goto BLUEN0
	goto	BLUEN0	;If BLUE > 0
	bcf		GPIO,GP2;If BLUE = 0 then turn off the LED
	nop				;-------------------------------------------------------------
	nop				; Make the BLUE = 0 brach the same length as the BLUE > 0 branch
	nop				;-------------------------------------------------------------
	RETURN			;return to SUB
BLUEN0
	bsf		GPIO,GP2;If BLUE > 0 then turn on the LED
	decf	BLUE	; Decrement the BLUE RGB value
	RETURN 			;return to SUB

;****Program END****-----------------------------------------
        END                       ; directive 'end of program'

There's the code, probably pretty inefficient but as it was the work of 1-2 hours not too bad. It's code to control a RGB LED. Anyway the code itself works, except for changing the GPIO register e.g. 'bsf GPIO,GP2' will do nothing for some reason. I'm only using outputs 0-2 so the 3rd pin being input only is not an issue.

Rob
 
I know theres registers like ANSEL but the data sheet says they only affect input.

That is not correct. When you write a bit of a port with the BCF and BSF instructions, the whole port is read and the specified bit is set/cleared. The result is then copied to the port. Bits that are configured as analog (default) are read as '0' and will be written as '0'.
Try configuring the ANSEL register and post you results. Note that the ANSEL settings affect MPLAB simulations.
 
Last edited:
=d=d=d

Thanks everyone, it was not setting the CMCON0 register that was giving me the headache, I had the comparator enabled :( stupid mistake. Lesson learnt, follow the setup procedure in the Data sheet :) Everything working fine now, thanks

Rob
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top