int main(void) { initRP6M256(); initLCD(); writeString_P_WIFI("\n\nRP6 CONTROL M256 I2C Master Example Program!\n"); // IMPORTANT: I2CTWI_initMaster(100); // Initialize the TWI Module for Master operation // with 100kHz SCL Frequency // Register the event handlers: I2CTWI_setTransmissionErrorHandler(I2C_transmissionError); setLEDs(0b1111); // Turn all LEDs on! showScreenLCD("################", "################"); mSleep(500); showScreenLCD("I2C-Master", "Example Program 1"); mSleep(1000); // --------------------------------------- setLEDs(0b0000); // All LEDs off! uint8_t counter = 1; // The command and register used here - in the next example we define them all. #define CMD_SET_ACS_POWER 9 #define ACS_PWR_MED 2 // Set ACS to medium power (you can see the ACS value changes in the raw registers): I2CTWI_transmit3Bytes(I2C_RP6_BASE_ADR, 0, CMD_SET_ACS_POWER, ACS_PWR_MED); while(true) { // Increment a counter and send value to LEDs of the // Slave Controller: setLEDs(0b0001); showScreenLCD("INCREMENT", "COUNTER"); setCursorPosLCD(1, 11); writeIntegerLengthLCD(counter, DEC, 3); I2CTWI_transmit3Bytes(I2C_RP6_BASE_ADR, 0, 3, counter); counter++; // Read and display ALL registers of the slave controller: setLEDs(0b0010); readAllRegisters(); // Read the light sensors: setLEDs(0b0100); readLightSensors(); mSleep(250); } return 0; }
void testLPABallon( void ) { initLCD(); // Initialize the LC-Display (LCD) showScreenLCD("################", "################"); mSleep(500); showScreenLCD("Tst LPA BallonLIB", "versie 29jun15"); mSleep(500); // --------------------------------------- setLEDs(0b0000); // All LEDs off! // This text is fixed on the LCD. Only the // key and ADC values are changed later on! showScreenLCD("hoogte: ", "VS: BS:"); while(true) { // read some simulations parameter values and display them on teh LCD showSimParams(); // Check if a key is pressed: uint8_t key = getPressedKeyNumber(); // This function returns a 0 if no key is pressed and the key number from 1 to 5 otherwise. if(key) // If a key is pressed... (key != 0) { // ... and depending on which key was pressed, we // call some library functions related to the burner and the valve. switch(key) { case 1: Balloon_set_burner(ON); break; case 2: Balloon_set_burner(OFF); break; case 3: Balloon_set_valve(OPEN); break; case 4: Balloon_set_valve(CLOSED); break; case 5: break; } // ... wait until the key is released again... while(getPressedKeyNumber()) { showSimParams(); // use busy waiting for polling keys } } } }
void showStatus(uint8_t verdieping, uint16_t hoogte, uint8_t motor) { // "rechtsom aan", "Hoogte: 100 " showScreenLCD(" ", "H: V: "); setCursorPosLCD(0, 13); writeStringLCD_P("aan"); // motor status switch (motor) { case MOTOR_IDLE: setCursorPosLCD(0, 13); writeStringLCD_P("uit"); break; case MOTOR_LEFT: setCursorPosLCD(0, 0); writeStringLCD_P("Linksom"); break; case MOTOR_RIGHT: setCursorPosLCD(0, 0); writeStringLCD_P("Rechtsom"); break; } setCursorPosLCD(1, 3); writeIntegerLCD(hoogte, DEC); setCursorPosLCD(1, 15); writeIntegerLCD(verdieping, DEC); setLEDs(1 << verdieping); }
int main(void) { initRP6Control(); // Always call this first! The Processor will not work // correctly otherwise. initLCD(); // Initialize the LC-Display (LCD) // Always call this before using the LCD! writeString_P("\n\nRP6 CONTROL: C-code examples\n"); showScreenLCD("C Code examples", " ...."); run_testCases(); return 0; }
int main(void) { initRP6M256(); initLCD(); writeString_P_WIFI("\n\nRP6 CONTROL M32 I2C Master Example Program!\n"); writeString_P_WIFI("\nMoving...\n"); // --------------------------------------- WDT_setRequestHandler(watchDogRequest); // --------------------------------------- // Init TWI Interface: I2CTWI_initMaster(100); I2CTWI_setRequestedDataReadyHandler(I2C_requestedDataReady); I2CTWI_setTransmissionErrorHandler(I2C_transmissionError); setLEDs(0b1111); showScreenLCD("################", "################"); mSleep(1000); showScreenLCD("I2C-Master", "Movement..."); mSleep(1000); setLEDs(0b0000); // --------------------------------------- I2CTWI_transmit3Bytes(I2C_RP6_BASE_ADR, 0, CMD_SET_ACS_POWER, ACS_PWR_MED); I2CTWI_transmit3Bytes(I2C_RP6_BASE_ADR, 0, CMD_SET_WDT, true); I2CTWI_transmit3Bytes(I2C_RP6_BASE_ADR, 0, CMD_SET_WDT_RQ, true); while(true) { setLEDs(0b1001); showScreenLCD("MOVE", "FWD"); writeString_P_WIFI("\nMoving Forwards...\n"); move(70, FWD, DIST_MM(300), BLOCKING); setLEDs(0b1000); showScreenLCD("ROTATE", "LEFT"); writeString_P_WIFI("\nRotating Left...\n"); rotate(60, LEFT, 180, BLOCKING); setLEDs(0b1001); showScreenLCD("MOVE", "FWD"); writeString_P_WIFI("\nMoving Forwards...\n"); move(70, FWD, DIST_MM(300), BLOCKING); setLEDs(0b0001); showScreenLCD("ROTATE", "RIGHT"); writeString_P_WIFI("\nRotating Right...\n"); rotate(60, RIGHT, 180, BLOCKING); } return 0; }
int main(void) { initRP6Control(); // Always call this first! The Processor will not work // correctly otherwise. bars(2); writeString_P("\n\nRP6Control Selftest!\n\n"); bars(2); setLEDs(0b1111); mSleep(50); initLCD(); showScreenLCD("################", "################"); mSleep(400); showScreenLCD("################", "################"); showScreenLCD("RP6Control M32", "SELFTEST"); mSleep(1000); uint8_t keynumber = 0; while(keynumber < 6) { uint8_t key = checkReleasedKeyEvent(); if(key == keynumber) { keynumber++; showScreenLCD("PRESS BUTTON", "NUMBER "); writeIntegerLCD(keynumber,DEC); setLEDs(0b0000); writeString_P("### PRESS BUTTON NUMBER "); writeInteger(keynumber,DEC); writeString_P("!\n"); } } showScreenLCD("Testing", "BEEPER & LEDS"); mSleep(250); // Play a sound to indicate that our program starts: sound(50,50,100); setLEDs(0b0000); sound(80,50,100); setLEDs(0b0001); sound(100,50,100);setLEDs(0b0010); sound(120,50,100);setLEDs(0b0100); sound(140,50,100);setLEDs(0b1000); sound(160,50,100);setLEDs(0b1001); sound(180,50,100);setLEDs(0b1011); sound(200,50,100);setLEDs(0b1111); mSleep(400); setLEDs(0b0000); showScreenLCD("Testing", "EERPOM"); test(1); writeString_P("\nEEPROM TEST\n"); writeString_P("\nErasing 250 Bytes...\n"); uint8_t cnt; for(cnt = 0; cnt < 250; cnt++) { SPI_EEPROM_writeByte(cnt, 0xFF); while(SPI_EEPROM_getStatus() & SPI_EEPROM_STAT_WIP); } writeString_P("...Done!\nWriting 250 Bytes to EEPROM:\n"); for(cnt = 0; cnt < 250; cnt++) { writeIntegerLength(cnt, DEC, 3); SPI_EEPROM_writeByte(cnt, cnt); while(SPI_EEPROM_getStatus() & SPI_EEPROM_STAT_WIP); writeChar(','); if(cnt % 10 == 0) writeChar('\n'); } mSleep(400); setLEDs(0b1111); writeString_P("\nReading and verifying:\n"); for(cnt = 0; cnt < 250; cnt++) { uint8_t result = SPI_EEPROM_readByte(cnt); if(result != cnt) { writeString_P("\nEEPROM VERIFY ERROR!!!! EEPROM DAMAGED!!!\n"); writeString_P("Data read: "); writeInteger(result,DEC); writeString_P(", should be: "); writeInteger(cnt,DEC); writeChar('\n'); errors++; } else writeIntegerLength(result,DEC,3); writeChar(','); if(cnt % 10 == 0) writeChar('\n'); } writeString_P("\nErasing 250 Bytes...\n"); for(cnt = 0; cnt < 250; cnt++) { SPI_EEPROM_writeByte(cnt, 0xFF); while(SPI_EEPROM_getStatus() & SPI_EEPROM_STAT_WIP); } mSleep(400); setLEDs(0b0000); writeString_P("\nEEPROM TEST DONE!\n"); writeString_P("\nI2C TWI TEST:\n"); showScreenLCD("I2C TWI", "TEST"); I2CTWI_initMaster(100); I2CTWI_setTransmissionErrorHandler(I2C_transmissionError); uint8_t runningLight = 1; for(cnt = 0; cnt < 24; cnt++) { writeIntegerLength(cnt,DEC,3); writeChar(':'); writeIntegerLength(runningLight,DEC,3); writeChar(','); writeChar(' '); I2CTWI_transmit3Bytes(I2C_RP6_BASE_ADR, 0, 3, runningLight); I2CTWI_transmitByte(I2C_RP6_BASE_ADR, 29); uint8_t result = I2CTWI_readByte(I2C_RP6_BASE_ADR); if(result != runningLight) { writeString_P("\nTWI TEST ERROR!\n"); errors++; } runningLight <<= 1; if(runningLight > 32) runningLight = 1; if((cnt+1) % 6 == 0) writeChar('\n'); mSleep(100); } I2CTWI_transmit3Bytes(I2C_RP6_BASE_ADR, 0, 3, 0); writeString_P("\nTWI TEST DONE!\n"); writeString_P("\nMicrophone Test\n"); writeString_P("Hit the Microphone three times with your finger!\n"); showScreenLCD("MIC TEST:", ""); #define PREPARE 1 #define WAIT 2 uint8_t state = PREPARE; startStopwatch2(); while(true) { static uint8_t peak_count = 3; if(state == PREPARE) { if(getStopwatch2() > 250) { setCursorPosLCD(1, 6); writeIntegerLengthLCD( peak_count, DEC, 1); dischargePeakDetector(); state = WAIT; setStopwatch2(0); } } else if(state == WAIT) { uint8_t key = checkReleasedKeyEvent(); if(key) { break; } if(getStopwatch2() > 50) { uint16_t tmp = getMicrophonePeak(); if(tmp > 4) { externalPort.LEDS = 0; uint16_t i; uint8_t j; for(i = 0, j = 2; i < tmp; i+= 40) { if(i < 40) { externalPort.LEDS++; } else { externalPort.LEDS <<=1; externalPort.LEDS++; } } outputExt(); if(tmp > 120) { state = PREPARE; peak_count--; } if(peak_count == 0) break; } else setLEDs(0b0000); setStopwatch2(0); } } } writeString_P("\nMICROPHONE TEST DONE!\n"); showScreenLCD("ALL TESTS", "DONE!"); writeString_P("\n\n\n\n"); bars(2); writeString_P("\n\nALL TESTS DONE!\n\n"); if(errors) { bars(4); writeString_P("\nERROR ERROR ERROR ERROR ERROR ERROR ERROR\n"); writeString_P("\nATTENTION: TESTS FINISHED WITH ERRORS!!!\n"); writeString_P("PLEASE CHECK RP6-M32 ASSEMBLY!!!\n\n"); bars(4); writeString_P("\n\n"); } // Now we just show a running light... startStopwatch1(); uint8_t runLEDs = 1; uint8_t dir = 0; while(true) { if(getStopwatch1() > 100) { setLEDs(runLEDs); if(dir == 0) runLEDs <<= 1; else runLEDs >>= 1; if(runLEDs > 7 ) dir = 1; else if (runLEDs < 2 ) dir = 0; setStopwatch1(0); } }
int main(void) { initRP6Control(); // Always call this first! The Processor will not work // correctly otherwise. initLCD(); writeString_P("\n\nRP6Control I/O and ADC Example Program!\n"); setLEDs(0b1111); showScreenLCD("################", "################"); // Play a sound to indicate that our program starts: sound(100,40,64); sound(170,40,0); mSleep(400); setLEDs(0b0000); showScreenLCD("I/O and ADC", "Example Program"); mSleep(1000); /* Here we will show how to set and read I/O pins. You need to change this and add your own routines for the specific hardware you want to control! The 14 free I/O Pins are the following ones (definitions from RP6Control.h): ADC7 (1 << PINA7) ADC6 (1 << PINA6) ADC5 (1 << PINA5) ADC4 (1 << PINA4) ADC3 (1 << PINA3) ADC2 (1 << PINA2) IO_PC7 (1 << PINC7) IO_PC6 (1 << PINC6) IO_PC5 (1 << PINC5) IO_PC4 (1 << PINC4) IO_PC3 (1 << PINC3) IO_PC2 (1 << PINC2) IO_PD6 (1 << PIND6) IO_PD5 (1 << PIND5) ADC2 - 7 are useable as I/Os or as Analog/Digital Converter Channels. IO_PC2 - 7 and IO_PD5 and IO_PD6 are only useable as I/Os. So you have free pins on PORTA, C and D. Please note the small difference in spelling ADC Channels and I/Os (ADC_7 vs. ADC7) */ // When you want to use a port pin as output, you have to set the // DDRx register bit belonging to this port to 1. // // For example - if you want to use PORTC 7 as output, you can write: DDRC |= IO_PC7; // PC7 is output // And then you can set the Port to high or low: PORTC |= IO_PC7; // High writeString_P("\nPC7 is set to HIGH!\n\n"); mSleep(1000); // wait 1s for example... PORTC &= ~IO_PC7; // Low writeString_P("\nPC7 is set to LOW!\n\n"); // When you want to use the Port as input to read its value, // you need to clear the DDRx register bit. DDRC &= ~IO_PC6; // PC6 is input PORTC |= IO_PC6; // enable internal pullup resistor of PC6 OR ALTERNATIVELY: // PORTC &= ~IO_PC6 // disable pullup resistor of PC6 // You need this when external sensors only pull the signal low // for example or if you disconnect the sensors or ... // Now we want to output something depending on if this port pin is // high or low: writeString_P("\nCheck PC6:"); if(PINC & IO_PC6) // Check if PC6 is high writeString_P("\n\nPC6 is HIGH!\n\n"); else writeString_P("\n\nPC6 is LOW!\n\n"); // Hints for DDRx and PORTx Registers: // DDRx = 0 and PORTx = 0 ==> Input without internal Pullup // DDRx = 0 and PORTx = 1 ==> Input with internal Pullup // DDRx = 1 and PORTx = 0 ==> Output low // DDRx = 1 and PORTx = 1 ==> Output high // "=1" indicates that the appropriate bit is set. // To read the ADC channels, you can use the readADC() function. // First you have to make the pins INPUTs, of course: DDRA &= ~ADC7; DDRA &= ~ADC2; // When you run this program with nothing connected // to the ADCs, you will most likely measure only junk // data - for example the ADC could show 210 or 623 or // anything else randomly. // ------------------------------------------- while(true) { writeString_P("\nADC7: "); uint16_t adc7 = readADC(ADC_7); // Read ADC Channel 7 writeInteger(adc7, DEC); writeString(" | ADC2: "); uint16_t adc2 = readADC(ADC_2); // Read ADC Channel 2 writeInteger(adc2, DEC); writeChar('\n'); mSleep(500); } return 0; }