00001
00002
00003
00004
00005
00006 #include "piccolo_all.h"
00007 #define USE_OR_MASKS //timers.h-ben "VAGY" művelettel egyesíthető makrók legyenek
00008 #include "timers.h"
00009
00010
00044 #define SW1_PRESSED() SW1_minta==0 //lenyomott állapot feltétele
00045 #define SW1_RELEASED() SW1_minta==1 //felengedett állapot feltétele
00046
00047
00048 #define SW3 PORTBbits.RB5
00049
00050 volatile unsigned char delay,
00051 SW1_minta,
00052 pnrSW1;
00053
00054 typedef enum {
00055 WAIT_FOR_PRESS = 0,
00056 WAIT_FOR_RELEASE
00057 } ISRSTATE;
00058
00059 ISRSTATE isrState=WAIT_FOR_PRESS;
00060
00061
00062 typedef enum {
00063 STATE_RESET = 0,
00064 STATE_WAIT_FOR_PNR1,
00065 STATE_WAIT_FOR_PNR2,
00066 STATE_BLINK,
00067 STATE_WAIT_FOR_RELEASE3
00068 } MAINSTATE;
00069
00070
00071 MAINSTATE LastState = STATE_RESET;
00072
00073 #pragma interrupt hi_isr
00074 void hi_isr(void) {
00075
00076 if(INTCONbits.TMR0IF && INTCONbits.TMR0IE) {
00077 SW1_minta = SW1;
00078 switch (isrState) {
00079 case WAIT_FOR_PRESS:
00080 if (SW1_PRESSED() && pnrSW1==0)
00081 isrState = WAIT_FOR_RELEASE;
00082 break;
00083 case WAIT_FOR_RELEASE:
00084 if (SW1_RELEASED()) {
00085 isrState = WAIT_FOR_PRESS;
00086 pnrSW1 = 1;
00087 break;
00088 }
00089 default:
00090 isrState = WAIT_FOR_RELEASE;
00091 }
00092 INTCONbits.TMR0IF = 0;
00093 if(delay) delay--;
00094 }
00095
00096 #if defined(USB_INTERRUPT)
00097 USBDeviceTasks();
00098 #endif //USB_INTERRUPT
00099 }
00100
00101 #pragma interruptlow lo_isr
00102 void lo_isr() {
00103 }
00104
00109 void printNewState (MAINSTATE currentState) {
00110 if (LastState != currentState) {
00111 switch (currentState) {
00112 case STATE_WAIT_FOR_PNR1:
00113 outString("STATE_WAIT_FOR_PNR1 - LED1 is off\n");
00114 break;
00115 case STATE_WAIT_FOR_PNR2:
00116 outString("STATE_WAIT_FOR_PNR2 - LED1 is on\n");
00117 break;
00118 case STATE_BLINK:
00119 outString("STATE_BLINK - LED1 blinks, waiting for SW1 press\n");
00120 break;
00121 case STATE_WAIT_FOR_RELEASE3:
00122 outString("STATE_WAIT_FOR_RELEASE3 - LED is on\n");
00123 break;
00124 }
00125 }
00126 LastState = currentState;
00127 }
00128
00129
00130 void main(void) {
00131 MAINSTATE mystate;
00132 mystate = STATE_RESET;
00133 isrState=WAIT_FOR_PRESS;
00134 pnrSW1 = 0;
00135
00136 RCONbits.IPEN=1;
00137 INTCONbits.GIEH=1;
00138 INTCONbits.GIEL=0;
00139 InitializeSystem();
00140
00141 INTCON2bits.TMR0IP=1;
00142 OpenTimer0(TIMER_INT_ON
00143 | T0_16BIT
00144 | T0_SOURCE_INT
00145 | T0_PS_1_4 );
00146
00147 TRISBbits.TRISB5=1;
00148 #if defined(__18F14K50)
00149
00150
00151 ANSELHbits.ANS11=0;
00152 WPUBbits.WPUB5=1;
00153 INTCON2bits.RABPU=0;
00154 #else
00155
00156
00157 INTCON2bits.RBPU=0;
00158 #endif
00159
00160 while (!usb_cdc_kbhit()) {
00161 ProcessIO();
00162 }
00163 #if defined(__18F4550)
00164 outString("PICCOLO-4550");
00165 #elif defined(__18F14K50)
00166 outString("PICCOLO-14K50");
00167 #endif
00168 outString(" - ledswitch_tmr0.c program\n");
00169 BlinkUSBStatus_enabled=0;
00170 LEDport = 0;
00171
00172
00173
00174
00175 while (1) {
00176 printNewState(mystate);
00177 switch (mystate) {
00178
00179 case STATE_WAIT_FOR_PNR1:
00180 mLED_1_Off();
00181 if (pnrSW1) {
00182 pnrSW1 = 0;
00183 mystate = STATE_WAIT_FOR_PNR2;
00184 }
00185 break;
00186
00187 case STATE_WAIT_FOR_PNR2:
00188 mLED_1_On();
00189 if (pnrSW1) {
00190 pnrSW1 = 0;
00191
00192 if (SW3) mystate = STATE_BLINK;
00193 else mystate = STATE_WAIT_FOR_PNR1;
00194 }
00195 break;
00196
00197 case STATE_BLINK:
00198 if(delay==0) {
00199 mLED_1_Toggle();
00200 delay=12;
00201 }
00202
00203 if (SW1_PRESSED()) mystate = STATE_WAIT_FOR_RELEASE3;
00204 break;
00205
00206 case STATE_WAIT_FOR_RELEASE3:
00207 mLED_1_On();
00208
00209
00210 if (pnrSW1) {
00211 pnrSW1 = 0;
00212 mystate = STATE_WAIT_FOR_PNR1;
00213 }
00214 break;
00215
00216 default:
00217 mystate=STATE_WAIT_FOR_PNR1;
00218 }
00219
00220 ProcessIO();
00221 }
00222 }
00223