void restoreOutputStates(void) { int idx = 0; byte o1 = eeRead(EE_PORTSTAT + 0); byte o2 = eeRead(EE_PORTSTAT + 1); for (idx = 0; idx < 8; idx++) { if ((Ports[idx].cfg & 0x01) == 0) { byte o = (o1 >> idx) & 0x01; writeOutput(idx, o); } }
void setupIO(byte clr) { int idx = 0; // all digital I/O ADCON0 = 0x00; ADCON1 = 0x0F; TRISBbits.TRISB5 = 0; // LED1 cbus activity TRISBbits.TRISB7 = 0; // LED2 learning mode TRISBbits.TRISB6 = 0; // LED3 running TRISAbits.TRISA2 = 1; /* SW */ TRISCbits.TRISC0 = 1; /* RFID1 */ TRISCbits.TRISC1 = 1; /* RFID2 */ TRISCbits.TRISC2 = 1; /* RFID3 */ TRISCbits.TRISC3 = 1; /* RFID4 */ TRISCbits.TRISC4 = 1; /* RFID8 */ TRISCbits.TRISC5 = 1; /* RFID7 */ TRISCbits.TRISC6 = 1; /* RFID6 */ TRISCbits.TRISC7 = 1; /* RFID5 */ TRISAbits.TRISA0 = 1; /* SENS1 */ TRISAbits.TRISA1 = 1; /* SENS2 */ TRISAbits.TRISA3 = 1; /* SENS3 */ TRISAbits.TRISA4 = 1; /* SENS4 */ TRISAbits.TRISA5 = 1; /* SENS5 */ TRISBbits.TRISB0 = 1; /* SENS6 */ TRISBbits.TRISB4 = 1; /* SENS7 */ TRISBbits.TRISB1 = 1; /* SENS8 */ LED1 = PORT_OFF; LED2 = PORT_OFF; LED3 = PORT_OFF; if (checkFlimSwitch() || eeRead(EE_CLEAN) == 0xFF) { eeWrite(EE_CLEAN, 0); for (idx = 0; idx < 16; idx++) { eeWriteShort(EE_PORT_ADDR + 2 * idx, idx + 1); } for (idx = 0; idx < 5 * 5; idx++) { eeWrite(EE_RFID + idx, 0); } } for (idx = 0; idx < 8; idx++) { RFID[idx].addr = eeReadShort(EE_PORT_ADDR + 2 * idx); Sensor[idx].addr = eeReadShort(EE_PORT_ADDR + 16 + 2 * idx); // read rfid for SoD RFID[idx].data[0] = eeRead(EE_SCANRFID + idx * 5 + 0); RFID[idx].data[1] = eeRead(EE_SCANRFID + idx * 5 + 1); RFID[idx].data[2] = eeRead(EE_SCANRFID + idx * 5 + 2); RFID[idx].data[3] = eeRead(EE_SCANRFID + idx * 5 + 3); RFID[idx].data[4] = eeRead(EE_SCANRFID + idx * 5 + 4); } for (idx = 0; idx < 5; idx++) { AllowedRFID[idx].data[0] = eeRead(EE_RFID + idx * 5 + 0); AllowedRFID[idx].data[1] = eeRead(EE_RFID + idx * 5 + 1); AllowedRFID[idx].data[2] = eeRead(EE_RFID + idx * 5 + 2); AllowedRFID[idx].data[3] = eeRead(EE_RFID + idx * 5 + 3); AllowedRFID[idx].data[4] = eeRead(EE_RFID + idx * 5 + 4); } }
void main(void) { unsigned char swTrig = 0; byte l3 = 1; lDelay(); Wait4NN = FALSE; isLearning = FALSE; led1timer = 0; doSOD = 0; ioIdx = 0; doEV = 0; evIdx = 0; NV1 = eeRead(EE_NV); initIO(); resetOutputs(); NN_temp = eeRead(EE_NN) * 256; NN_temp += eeRead(EE_NN + 1); if (NN_temp == 0 || NN_temp == 0xFFFF) NN_temp = DEFAULT_NN; CANID = eeRead(EE_CANID); if (CANID == 0 || CANID == 0xFF) CANID = NN_temp & 0xFF; initCAN(); delay(); restoreOutputStates(); delay(); SOD = eeRead(EE_SOD) * 256; SOD += eeRead(EE_SOD + 1); if (SOD == 0 || SOD == 0xFFFF) SOD = DEFAULT_SOD; // Loop forever (nothing lasts forever...) while (1) { CANMsg cmsg; unsigned char txed = 0; LED3 = PORT_ON; l3 ^= 1; // Check for Rx packet and setup pointer to it while (canbusRecv(&cmsg)) { // Decode the new command LED1 = 1; led1timer = 20; txed = parseCmd(&cmsg); } LED3 = PORT_OFF; doTimedOff(ioIdx); if (checkInput(ioIdx, doSOD)) { ioIdx++; if (ioIdx >= 16) { ioIdx = 0; doSOD = 0; } } if (l3) { if (doPortEvent(evIdx)) { evIdx++; if (evIdx >= 16) { evIdx = 0; doEV = 0; } } } if (checkFlimSwitch() && !swTrig) { swTrig = 1; } else if (!checkFlimSwitch() && swTrig) { swTrig = 0; if (Wait4NN) { Wait4NN = 0; LED2 = 0; } else { CANMsg canmsg; LED2 = 1; canmsg.b[d0] = OPC_RQNN; canmsg.b[d1] = NN_temp / 256; canmsg.b[d2] = NN_temp % 256; canmsg.b[dlc] = 3; canbusSend(&canmsg); Wait4NN = 1; } } } }
void setupIO(byte clr) { int idx = 0; byte firstTime = eeRead(EE_FIRSTTIME); // all digital I/O ADCON0 = 0x00; ADCON1 = 0x0F; TRISBbits.TRISB6 = 0; /* LED1 */ TRISBbits.TRISB7 = 0; /* LED2 */ TRISBbits.TRISB5 = 0; /* LED3 */ TRISAbits.TRISA2 = 1; /* Push button */ LED1 = 0; LED2 = 0; LED3 = 0; if (firstTime == 0xFF) { eeWrite(EE_FIRSTTIME, 0); clr = TRUE; } // following presets are written to eeprom if the flim switch is preshed at boot // preset port 1-8 as output for (idx = 0; idx < 8; idx++) { Ports[idx].cfg = PORTCFG_OUT; Ports[idx].status = 0; Ports[idx].timedoff = 0; Ports[idx].timer = 0; Ports[idx].evtnn = 0; Ports[idx].addr = idx + 1; if (clr) { eeWrite(EE_PORTCFG + idx, Ports[idx].cfg); eeWriteShort(EE_PORTNN + (2 * idx), Ports[idx].evtnn); eeWriteShort(EE_PORTADDR + (2 * idx), Ports[idx].addr); } } // preset port 9-16 as input with off delay for (idx = 8; idx < 16; idx++) { Ports[idx].cfg = PORTCFG_IN | PORTCFG_OFFDELAY; Ports[idx].status = 0; Ports[idx].timedoff = 0; Ports[idx].timer = 0; Ports[idx].evtnn = 0; Ports[idx].addr = idx + 1; if (clr) { eeWrite(EE_PORTCFG + idx, Ports[idx].cfg); eeWriteShort(EE_PORTNN + (2 * idx), Ports[idx].evtnn); eeWriteShort(EE_PORTADDR + (2 * idx), Ports[idx].addr); } } if (clr) { eeWrite(EE_PORTSTAT + 0, 0); eeWrite(EE_PORTSTAT + 1, 0); } for (idx = 0; idx < 16; idx++) { Ports[idx].cfg = eeRead(EE_PORTCFG + idx); if ((Ports[idx].cfg & PORTCFG_IO) == PORTCFG_OUT) Ports[idx].status = eeRead(EE_PORTSTAT + idx); Ports[idx].timedoff = 0; Ports[idx].timer = 0; Ports[idx].evtnn = eeReadShort(EE_PORTNN + (2 * idx)); Ports[idx].addr = eeReadShort(EE_PORTADDR + (2 * idx)); } for (idx = 0; idx < 16; idx++) configPort(idx); }
/* * ee_read_short() - read a short (16 bit) word from EEPROM * * Data is stored in little endian format */ unsigned short eeReadShort(unsigned char addr) { unsigned char byte_addr = addr; unsigned short ret = eeRead(byte_addr++); ret = ret | ((unsigned short) eeRead(byte_addr) << 8); return ret; }