short READ_LED_STATE(short lednumber) { short ret_val; ret_val = -1; switch(lednumber) { case 0: {} case 1: {} case 2: { ret_val = mPORTDReadBits(1 << lednumber); break; } case 3: {} case 4: { ret_val = mPORTBReadBits(1 << (lednumber-3)); } default: { break; } } return ret_val; }
static PT_THREAD (protothread_key(struct pt *pt)) { PT_BEGIN(pt); static int keypad, i, pattern; // order is 0 thru 9 then * ==10 and # ==11 // no press = -1 // table is decoded to natural digit order (except for * and #) // 0x80 for col 1 ; 0x100 for col 2 ; 0x200 for col 3 // 0x01 for row 1 ; 0x02 for row 2; etc static int keytable[12]={0x108, 0x81, 0x101, 0x201, 0x82, 0x102, 0x202, 0x84, 0x104, 0x204, 0x88, 0x208}; // init the keypad pins A0-A3 and B7-B9 // PortA ports as digital outputs mPORTASetPinsDigitalOut(BIT_0 | BIT_1 | BIT_2 | BIT_3); //Set port as output // PortB as inputs mPORTBSetPinsDigitalIn(BIT_7 | BIT_8 | BIT_9); //Set port as input while(1) { // read each row sequentially mPORTAClearBits(BIT_0 | BIT_1 | BIT_2 | BIT_3); pattern = 1; mPORTASetBits(pattern); // yield time PT_YIELD_TIME_msec(30); //mPORTAClearBits(BIT_0 | BIT_1 | BIT_2 | BIT_3); //pattern = 1; mPORTASetBits(pattern); for (i=0; i<4; i++) { keypad = mPORTBReadBits(BIT_7 | BIT_8 | BIT_9); if(keypad!=0) {keypad |= pattern ; break;} mPORTAClearBits(pattern); pattern <<= 1; mPORTASetBits(pattern); } // search for keycode if (keypad > 0){ // then button is pushed for (i=0; i<12; i++){ if (keytable[i]==keypad) break; } } else i = -1; // no button pushed // draw key number tft_fillRoundRect(30,200, 100, 28, 1, ILI9340_BLACK);// x,y,w,h,radius,color tft_setCursor(30, 200); tft_setTextColor(ILI9340_YELLOW); tft_setTextSize(4); sprintf(buffer,"%d", i); if (i==10)sprintf(buffer,"*"); if (i==11)sprintf(buffer,"#"); tft_writeString(buffer); // NEVER exit while } // END WHILE(1) PT_END(pt); } // keypad thread
void setupCNModuleAndPIRInput() { //PIR Input and notification A1 mPORTBSetPinsDigitalIn(BIT_2); //A1 PIR Input (CN4 module) mCNOpen(CN_OFF | CN_IDLE_CON | CN_IDLE_CON, CN4_ENABLE, CN4_PULLUP_ENABLE); // read port(s) to clear mismatch mPORTBReadBits(BIT_2); // configure interrupts and clear change notice interrupt flag ConfigIntCN(CHANGE_INT_ON | CHANGE_INT_PRI_3); mCNClearIntFlag(); // Clear interrupt flag }
void setupCNModuleAnd_IR_PIR_Input() { //T2 used to sample IR Input signals setupTimer2(); //IR INPUT mPORTDSetPinsDigitalIn(BIT_7); //D5 IR Input (CN16 module, RD7) //PIR input mPORTBSetPinsDigitalIn(BIT_2); //A1 PIR Input (CN4 module, RB2) // setup the change notice options //(ensure that CN continues working in sleep mode) mCNOpen(CN_OFF | CN_IDLE_CON, CN16_ENABLE | CN4_ENABLE, CN16_PULLUP_ENABLE | CN4_PULLUP_ENABLE); // read port(s) to clear mismatch mPORTDReadBits(BIT_7); mPORTBReadBits(BIT_2); mPORTBReadBits(BIT_4); // configure interrupts and clear change notice interrupt flag ConfigIntCN(CHANGE_INT_ON | CHANGE_INT_PRI_3); mCNClearIntFlag(); // Clear interrupt flag }
static PT_THREAD(protothread_CSense(struct pt *pt)) { PT_BEGIN(pt); while (1) { if (config1 & CUR_SENSE_EN) { if(Status2 & M7_CUR){ if(M7_LastDir != M7_Dir)//if direction has changed, lower CS flag, which enables PWM writing Status2 &= !M7_CUR; } if(Status2 & M6_CUR){ if(M6_LastDir != M6_Dir) Status2 &= !M6_CUR; } if(Status2 & M6_CUR){ if(M6_LastDir != M6_Dir) Status2 &= !M6_CUR; } if(Status2 & M5_CUR){ if(M5_LastDir != M5_Dir) Status2 &= !M5_CUR; } if(Status2 & M4_CUR){ if(M4_LastDir != M4_Dir) Status2 &= !M4_CUR; } if(Status2 & M3_CUR){ if(M3_LastDir != M3_Dir) Status2 &= !M3_CUR; } if (mPORTBReadBits(BIT_3)) {//CS triggered! M7_PWM = 0; SetDCOC1PWM(M7_PWM);//Halt motor Status2 |= M7_CUR;//set CS flag M7_LastDir = M7_Dir;//save direction } if (mPORTBReadBits(BIT_1)) { M6_PWM = 0; SetDCOC2PWM(M6_PWM); Status2 |= M6_CUR; M6_LastDir = M6_Dir; } if (mPORTAReadBits(BIT_3)) { M5_PWM = 0; SetDCOC3PWM(M5_PWM); Status2 |= M5_CUR; M5_LastDir = M5_Dir; } if (mPORTBReadBits(BIT_4)) { M4_PWM = 0; SetDCOC5PWM(M4_PWM); Status2 |= M4_CUR; M4_LastDir = M4_Dir; } if (mPORTAReadBits(BIT_4)) { M3_PWM = 0; SetDCOC4PWM(M3_PWM); Status2 |= M3_CUR; M3_LastDir = M3_Dir; } } PT_YIELD_TIME_msec(10); } PT_END(pt); }