int main() { chipInit(); timer0Init(); //init_adc(ADC_CHANS, INT_VREF_TYPE); adcInit(0, AVCC_VREF_TYPE); uart_init(); w1Init(); sei(); ee_magic = MAGIC16; initCommands(); initVM(); ee_magic = 0; while (1) { char tticks = getClearTimerTicks(); while (tticks) { tticks--; ds18b20_step(0, TIME_UNIT); ds18b20_step(1, TIME_UNIT); vmStep(TIME_UNIT); handlePWM(TIME_UNIT); } handleIO(); } return 0; }
int main() { gpioPinInit(&pin[LED1], 1, 18, OUTPUT_PIN); gpioPinInit(&pin[LED2], 0, 13, OUTPUT_PIN); gpioPinInit(&pin[LED3], 1, 13, OUTPUT_PIN); gpioPinInit(&pin[LED4], 2, 19, OUTPUT_PIN); gpioPinInit(&pin[JLEFT], 5, 0, INPUT_PIN); gpioPinInit(&pin[JDOWN], 5, 1, INPUT_PIN); gpioPinInit(&pin[JUP], 5, 2, INPUT_PIN); gpioPinInit(&pin[JCENTER], 5, 3, INPUT_PIN); gpioPinInit(&pin[JRIGHT], 5, 4, INPUT_PIN); timer0Init(1, timer0Handler); while (true) { if (buttonPressedAndReleased(&pin[JLEFT])) { flashing[LED1] = !flashing[LED1]; } if (buttonPressedAndReleased(&pin[JDOWN])) { flashing[LED2] = !flashing[LED2]; } if (buttonPressedAndReleased(&pin[JUP])) { flashing[LED3] = !flashing[LED3]; } if (buttonPressedAndReleased(&pin[JRIGHT])) { flashing[LED4] = !flashing[LED4]; } if (buttonPressedAndReleased(&pin[JCENTER])) { flashing[LED1] = !flashing[LED1]; flashing[LED2] = !flashing[LED2]; flashing[LED3] = !flashing[LED3]; flashing[LED4] = !flashing[LED4]; } } }
void sysInit(void){ PMOD_DDR |= _BV(PMOD0) | _BV(PMOD1) | _BV(PMOD2) | _BV(PORTD4); //Set PMOD as outputs WIZ_DDR &= ~(_BV(INT_W5500)); //Int is an input WIZ_PORT |= _BV(INT_W5500); //Enable pull up WIZ_DDR |= _BV(SS_W5500); //Set SS as output AUX_DDR &= ~(_BV(BTN0) | _BV(ISENSE) | _BV(VSENSE)); //Button set as input Isense and Vsense inputs(will be used for ADC) AUX_DDR |= _BV(WP_EEP); //WP pin for eeprom as output LED_DDR |= _BV(LED_R) | _BV(LED_G) | _BV(LED_B); AUX_PORT |= _BV(BTN0); //Enable pull up on btn PMOD_PORT |= _BV(PMOD0) | _BV(PMOD1) | _BV(PMOD2); //Full auto uartInit(); i2c_init(); sei(); DDRB |= _BV(PORTB2) | _BV(PORTB1) | _BV(PORTB0); SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR0); // SPI enable, Master, f/16 uartPutsP("\n\n[Base Station]\n"); uartPutsP(">Pins init\n"); timer0Init(); uartPutsP(">Timer 0 init\n"); timer1Init(); uartPutsP(">Timer 1 init\n"); }
void UserInit(void) { mInitAllLEDs(); mInitAllSwitches(); InitAdc(); old_sw2 = sw2; Pump1tris = 0; Pump2tris = 0; TRISVPP = 0; //output TRISVPP_RST=0; //output TRISPGD=0; TRISPGC=0; TRISVDD=0; TRISVPP_RUN=0; VPP_RUN=0; //run = off PGD_LOW=1; TRISPGD_LOW=1; //LV devices disabled, high impedance / input PGC_LOW=1; TRISPGC_LOW=1; //LV devices disabled, high impedance / input VPP = 1; //VPP is low (inverted) VPP_RST=0; //No hard reset (inverted PGD=0; PGC=0; INTCON2bits.RBPU=1; //disable Portb pullup resistors timer1Init(); timer0Init(); }//end UserInit
int main(void) { timeCount = 0; hardwareInit(); uart0_init(BAUD_SETTING); /* Initialise timer to divide by 1025, giving 32ms time tick */ timer0Init(0,5); rfm12_init(); wdtInit(); sei(); indx = 0; for(;;) { wdt_reset(); uint8_t sendMessage = false; uint16_t character = uart0_getc(); /* Wait for a serial incoming message to be built. */ if (character != UART_NO_DATA) { inBuf[indx++] = (uint8_t)(character & 0xFF); /* Signal to transmit if a CR was received, or string too long. */ sendMessage = ((indx > MAX_MESSAGE) || (character == 0x0D)); } /* Send a transmission if message is ready to send, or something was received and time waited is too long. */ if (sendMessage || (timeCount++ > TIMEOUT)) { /* Wait for the transmit buffer to be freed and message loaded. */ if ((indx > 0) && (rfm12_tx(indx, 0, inBuf) != RFM12_TX_OCCUPIED)) indx = 0; timeCount = 0; } rfm12_tick(); /* If an RF incoming message has been received, take it from the buffer one character at a time and transmit via the serial port. */ if (rfm12_rx_status() == STATUS_COMPLETE) { uint8_t *bufferContents = rfm12_rx_buffer(); uint8_t messageLength = rfm12_rx_len(); uint8_t i; for (i=0;i<messageLength;i++) { uart0_putc(bufferContents[i]); } /* Clear the "in use" status of the receive buffer to be available for rfm12lib. */ rfm12_rx_clear(); } } }
void rtcInit(void) { // set up timer for RTC operation // initialize real-time registers RtcTime.totaltics = 0; RtcTime.tics = 0; RtcTime.seconds = 0; RtcTime.minutes = 0; RtcTime.hours = 0; RtcTime.day = 1; RtcTime.month = 1; RtcTime.year = 2000; // select the correct RTC timer based on bit defines #ifdef AS2 // use timer2 for most AVRs // initialize timer 2 timer2Init(); // count with 32.768KHz/8 timer2SetPrescaler(TIMER_CLK_DIV8); // switch to asynchronous input (32KHz crystal) sbi(ASSR, AS2); // attach service to real-time clock interrupt // rtcService() will be called at ((32768/8)/256) = 16Hz timerAttach(TIMER2OVERFLOW_INT, rtcService); #else #ifdef AS0 // use timer0 for ATmega103, ATmega128 // initialize timer 0 timer0Init(); // count with 32.768KHz/8 timer0SetPrescaler(TIMER_CLK_DIV8); // switch to asynchronous input (32KHz crystal) sbi(ASSR, AS0); // attach service to real-time clock interrupt // rtcService() will be called at ((32768/8)/256) = 16Hz timerAttach(TIMER0OVERFLOW_INT, rtcService); #endif #endif }
//-------------------------------------------------------------------------------------------------------------------------- void uComOnChipInitial(void) { // BYTE testi = 89; // BYTE test2[2] ; // BYTE i; SystemInit(); GPIO_init(); SetUARTFunction(); // set uart operation right AnalogI2CInit(); I2C1Init(100000); //adv7181 DSA_Init(); timer0Init(); // Delayms(2000); ADV_RST_LOW(); REST_N_380_LOW(); Delayms(500); ADV_RST_HIGH(); REST_N_380_HIGH(); }
int main(void) { uartInit(); timer0Init(); timer2Init(); externalInterrupts(); DDRB |= (1<<PB4) | (1<<PB5) | (1<<PB7); //set PC0-3 to output for stepper control //PC0-PC3 are used for stepper control //PC0=37, PC1=36, PC2=35, PC3=34 DDRC |= (1<<PC0) | (1<<PC1) | (1<<PC2) | (1<<PC3); sei(); while(1) { switch(state){ case INIT: uartInit(); // uartSends("Is it my turn?\nEnter '2' to start targeting mode\n"); state = MYTURN; break; case MYTURN: if(i >= 2){ uartSendc(uartData[0]); uartSendc(uartData[1]); if(uartData[0] == 1){ // uartSendc(0b00000001); // uartSendc(uartData[1]); PORTB |= (1<<PB5); OCR2A = uartData[1]; } if(uartData[0] == 2){ // uartSends("Test\n"); uartSendc(0b00000010); uartSendc(uartData[1]); // rampMotorSpeed(uartData[1]); OCR2A = uartData[1]; PORTB &= ~(1<<PB5); } if(uartData[0] == 3){ uartSends("To IDCUP\n"); PORTB &= ~(1<<PB7); state = IDCUP; break; } else {state = MYTURN;} i = 0; }else {state = MYTURN;} _delay_ms(250); state = MYTURN; break; case IDCUP: uartSends("In IDCUP\n"); state = TAKEAIM; break; case TAKEAIM: //Turn on PID and start setting it //Allow PID to automatically control settings PIDsetMode(AUTOMATIC); //Positive proportional control PIDsetControllerDirection(DIRECT); //set sample time in ms - should be handled in interrupt? PIDsetSampleTime(50); //Minimum and Maximum output values PIDsetOutputLimits(0,255); //Set PID params (Kp, Ki, Kd) PIDsetTunings(6,0.6,0.6); //Start controller PIDinitialize(); //Set Setpoint to 0 RPM Setpoint = 0; state = LAUNCHBALL; break; case LAUNCHBALL: //Write print tachometer output //Number of cycles divided by (16000000/1024) // RPM = 60/(timeStamp/ 15625); // uartSendc(rotation); if(i >= 2){ if(uartData[0] == 1){ //go back to beginning PIDsetMode(MANUAL); state = MYTURN; break; } if(uartData[0] == 3){ uartSendc(uartData[1]); //byte 1 is setpoint Setpoint = (uartData[1]); uartSendc((uint8_t)Setpoint); } i = 0; }else { state = LAUNCHBALL;} _delay_ms(100); state = LAUNCHBALL; break; case HITMISS: state = ERRORCORRECTION; break; case ERRORCORRECTION: uartSends("Correcting errors! Try again!"); state = MYTURN; break; case TRACKBALL: state = BLOCKSHOT; break; case BLOCKSHOT: state = MYTURN; break; } } }