help on simple pic 12c508a project

Microcontroller Topics
Post Reply
User avatar
zipius
Posts: 1
Joined: Mon Oct 21, 2013 11:40 pm

help on simple pic 12c508a project

Post by zipius » Mon Oct 21, 2013 11:47 pm

hi,

i'm making my first pic project, with a 12c508a, very simple project wich is:

press a button (in GP3) toogle a led (GP0) while button is pressed and switch on another led on GP4, and when i press GP3 again the toogle the GP1 led also while the button is pressed and turn off GP4 led. the objective is turn on and off a device with 2 buttons with a single button.

the problem is the project doesn't work properly. maybe i can get some help here.

here's the code:

Code: Select all

list p=12c508a
#include <p12c508a.inc>


;*******CONFIG********
__CONFIG	_MCLRE_OFF & _CP_OFF & _WDT_OFF & _IntRC_OSC 

;*******variables********
TEMP_VAR	UDATA 0x07
sGPIO	 RES 1;	 shadow copy of GPIO
db_cnt	 RES 1;	 debounce counter
dc1	 RES 1;	 delay counter
on	 RES 1;	 system on flag

;*******RC cal*******
RCCAL	CODE	 0x1FF;	 vector reset processor
RES 1;

;*******VECTOR RESET*******
RESET	CODE	 0x000;	 effective reset vector
movwf	 OSCCAL;	 factory osc cal


;*******MAIN PROGRAM*******

;*******INIT*******
ini
clrf	 GPIO;	 clean out
clrf	 sGPIO;	 actualizar shadow reg
movlw	 b'110100';	 config GP0 GP1 GP4 as outputs, GP3 as in
tris	 GPIO;	
movlw	 b'1011111';	 config internal pull-ups ON
option;

;*******PROGRAMA*******

main
clrf	 on;
clrf	 dc1;
clrf	 db_cnt;

;wait button press
db_dn
movlw	 .26;	 max count = 20ms/768us = 26
movwf	 db_cnt;	
clrf	 dc1;
dn_dly	
incfsz	 dc1, f;	 delay 256x3 = 768us
goto	 dn_dly;
btfsc	 GPIO, 3;	 if button not press (GP3=1)
goto	 db_dn;	 reset cont
decfsz	 db_cnt, f;	 else repeat to archieve max count
goto	 dn_dly;

;OUTS
btfss	 on, 0;	 test if flag ON/OFF is active
goto	 liga;
goto	 desliga;

;OUT "ON" (GP0)
liga
movf	 sGPIO, w;
xorlw	 b'001001';	 activate bit GP0 (saida on) and GP4 (led)
movwf	 sGPIO;	 on shadow reg
movwf	 GPIO;	
bsf	 on, 0;	 activate flag ON/OFF	
goto	 db_up;	

desliga
movf	 sGPIO, w;
xorlw	 b'001010';	 activate bit GP1 (out off) and turn GP4 led off
movwf	 sGPIO;	
movwf	 GPIO;
bcf	 on, 0;	 deactivate flag ON/OFF

;wait for button release
db_up
movlw	 .26;	 max count = 20ms/768us = 26
movwf	 db_cnt;
clrf	 dc1;
up_dly
incfsz	 dc1, f;	 delay 256x3 = 768us
goto	 up_dly;
btfss	 GPIO, 3;	 if boton is pressed (GP3=0)
goto	 db_up;	 restart count
decfsz	 db_cnt, f;	 else repeat until max count is reached
goto	 up_dly;

;update outs
btfsc	 on, 0;	 test flag ON/OFF to know wich bit is on
goto	 desliga_out;
goto	 liga_out;

;clean out GP0 or GP1
desliga_out
movf	 sGPIO, w;
xorlw	 b'000010';	
movwf	 sGPIO;
movwf	 GPIO;
goto	 db_dn;

liga_out
movf	 sGPIO, w;
xorlw	 b'000001';
movwf	 sGPIO;
movwf	 GPIO;
goto	 db_dn;

END


sorry for bad english in program wich is written in portuguese.

hope someone can help me.

thanks
User avatar
SemiconductorCat
Major
Major
Posts: 455
Joined: Mon Aug 22, 2011 8:42 pm
Location: currently in hyperspace

Re: help on simple pic 12c508a project

Post by SemiconductorCat » Tue Oct 22, 2013 4:26 am

I reviewed your code. I think lines bellow are syntactically flawed.
Check and review those lines back.

you are talking about the toggle led GP0 or GP1.
see these lines. I think you are messing up with GP1 and GP0 , what actually the
toggle LED is connected?

Code: Select all

xorlw    b'001010';    activate bit GP1 (out off) and turn GP4 led off
Is that GP1 or GP0 ?


EDIT:
and assuming that you need to toggle one LED connected in GP0 to GP1 and GP1 to GP0 on each
key release , then still I could see some flaw, because initial condition is both GP0 and GP1 are
both off. Either GP0 or GP1 need to be on.
Post Reply

Return to “Microcontrollers”