Code ASM - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 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
the problem is the project doesn't work properly.
Code ASM - [expand] 1 goto main
Code ASM - [expand] 1 movlw b'110100'; config GP0 GP1 GP4 as outputs, GP3 as in
goto db_dn;
sorry... :| i'm going to describe the loop cycle:
1st state: all leds off
2nd state: while GP3=0 (button pressed) -> GP0=1 and GP4=1
3th state: GP3=1 (button not pressed) -> GP4=1
4th state: while GP3=0 -> GP1=1 and GP4=0
and now all leds are off and the cycle repeats
View attachment 97608
delay
movlw .26; max count = 20ms/768us = 26
movwf db_cnt;
clrf dc1;
dn_dly
incfsz dc1, f; delay 256x3 = 768us
goto dn_dly;
decfsz db_cnt, f; else repeat to archieve max count
goto dn_dly;
btfsc GPIO, 3; if button not press (GP3=1)
goto delay;
btfsc on,0;
goto off_loop;
goto on_loop;
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 #define _XTAL_FREQ 4000000 #include <xc.h> #include <stdio.h> #include <stdlib.h> #include <delays.h> #pragma config OSC = XT #pragma config WDT = OFF #pragma config CP = OFF #pragma config MCLRE = OFF int main(int argc, char** argv) { unsigned char state = 0; TRIS = 0b001000; GPIO = 0x00; while(1){ if((GPIObits.GP3 == 0) && (state == 0)){ __delay_ms(50); if((GPIObits.GP3 == 0) && (state == 0)){ GPIO = GPIObits.GP0 = 1; GPIO = GPIObits.GP1 = 0; GPIO = GPIObits.GP4 = 1; state = 1; } } if((GPIObits.GP3 == 1) && (state == 1)){ __delay_ms(50); if((GPIObits.GP3 == 1) && (state == 1)){ GPIO = GPIObits.GP0 = 0; GPIO = GPIObits.GP1 = 0; GPIO = GPIObits.GP4 = 1; state = 0; } } if((GPIObits.GP3 == 0) && (state == 1)){ __delay_ms(50); if((GPIObits.GP3 == 0) && (state == 1)){ GPIO = GPIObits.GP0 = 0; GPIO = GPIObits.GP1 = 1; GPIO = GPIObits.GP4 = 0; state = 0; } } } return (EXIT_SUCCESS); }
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?