/** Configures the board hardware and chip peripherals for the demo's functionality. */ void SetupHardware(void) { #if (ARCH == ARCH_AVR8) /* Disable watchdog if enabled by bootloader/fuses */ MCUSR &= ~(1 << WDRF); wdt_disable(); /* Disable clock division */ clock_prescale_set(clock_div_1); #elif (ARCH == ARCH_XMEGA) /* Start the PLL to multiply the 2MHz RC oscillator to 32MHz and switch the CPU core to run from it */ XMEGACLK_StartPLL(CLOCK_SRC_INT_RC2MHZ, 2000000, F_CPU); XMEGACLK_SetCPUClockSource(CLOCK_SRC_PLL); /* Start the 32MHz internal RC oscillator and start the DFLL to increase it to 48MHz using the USB SOF as a reference */ XMEGACLK_StartInternalOscillator(CLOCK_SRC_INT_RC32MHZ); XMEGACLK_StartDFLL(CLOCK_SRC_INT_RC32MHZ, DFLL_REF_INT_USBSOF, F_USB); PMIC.CTRL = PMIC_LOLVLEN_bm | PMIC_MEDLVLEN_bm | PMIC_HILVLEN_bm; #endif /* Hardware Initialization */ //i2c TWI_Init(TWI_BIT_PRESCALE_1, TWI_BITLENGTH_FROM_FREQ(1, 400000)); //button ports init buttons_init(); //timer init TCCR1B |=(1<<CS10)|(1<<CS12);//prescaler 1024 TCNT1 = 0;//init counter //screen setup - software spi, sh1106 driver (sh1306 analog) /* Atmega32U4 mapping: SCK (D0) - PORTB, PIN1 == PN(1, 1) MOSI (D1) - PORTB, PIN2 RES (RST) - PORTB, PIN0 A0 (DC) - PORTB, PIN5 (OC1A/#A9) */ display_init(); display_draw_logo(); //sensors initialization adxl345_init(MEASURE_ON, RANGE_2G, ODR_800); l3g4200d_init(FIFO_HI_LO_PASS, NORM_ODR_800); hmc5883l_init(MODE_CONT, GAIN_4_7, NORM_75HZ_8AV); USB_Init(); }
void auto_init_l3g4200d(void) { assert(L3G4200D_NUM == L3G4200D_INFO_NUM); for (unsigned int i = 0; i < L3G4200D_NUM; i++) { LOG_DEBUG("[auto_init_saul] initializing l3g4200d #%u\n", i); if (l3g4200d_init(&l3g4200d_devs[i], &l3g4200d_params[i]) < 0) { LOG_ERROR("[auto_init_saul] error initializing l3g4200d #%u\n", i); continue; } saul_entries[i].dev = &(l3g4200d_devs[i]); saul_entries[i].name = l3g4200d_saul_info[i].name; saul_entries[i].driver = &l3g4200d_saul_driver; saul_reg_add(&(saul_entries[i])); } }
int main() { usartInit(57600); usartEnableReceiver(); usartEnableTransmitter(); usartStdio(); setBit(DDRB, M1_DIR); setBit(DDRD, M1_PWM); setBit(DDRD, M2_DIR); setBit(DDRD, M2_PWM); clrBit(PORTB, M1_DIR); clrBit(PORTD, M1_PWM); clrBit(PORTD, M2_PWM); clrBit(PORTD, M2_DIR); timer0FastPWMMaxMode(); timer0ClockPrescaller64(); timer0OC0ANonInvertedMode(); timer0OC0BNonInvertedMode(); timer0SetCompareAValue(0); timer0SetCompareBValue(0); timer0DeactivateCompareAInterrupt(); timer0DeactivateCompareBInterrupt(); timer2ClockPrescaller256(); timer2NormalMode(); timer2OC2AOff(); timer2OC2BOff(); timer2ActivateOverflowInterrupt(); i2c_init(); adxl345_init(); l3g4200d_init(); l3g4200d_setoffset(0.11, -1.71, -0.46); //l3g4200d_settemperatureref(); PID_init(&PID, 15,0.2,1, 0); PID_SetMode(&PID, 1); PID_SetOutputLimits(&PID, -255, 255); PID.mySetpoint = 0; motor1(0); motor2(0); // printf("\e[1;1H\e[2J"); // printf("STARTED!\r\n"); // _delay_ms(200); // motor1(120); // printf("Motor1 = 120\r\n"); // _delay_ms(500); // motor1(-120); // printf("Motor1 = -120\r\n"); // _delay_ms(500); // motor1(120); // printf("Motor1 = 120\r\n"); // _delay_ms(500); // motor1(0); // printf("Motor1 = 0\r\n"); sei(); while(1); return 0; }
PROCESS_THREAD(default_app_process, ev, data) { PROCESS_BEGIN(); printf("Hello, world\n"); _delay_ms(100); #if DEBUG printf("Begin initialization:\n"); if (microSD_init() == 0) { printf(":microSD OK\n"); microSD_switchoff(); } else { printf(":microSD FAILURE\n"); } if (adxl345_init() == 0) { printf(":ADXL345 OK\n"); } else { printf(":ADXL345 FAILURE\n"); } if (at45db_init() == 0) { printf(":AT45DBxx OK\n"); } else { printf(":AT45DBxx FAILURE\n"); } // if (bmp085_init() == 0) { // printf(":BMP085 OK\n"); // } else { // printf(":BMP085 FAILURE\n"); // } // if (l3g4200d_init() == 0) { // printf(":L3G4200D OK\n"); // } else { // printf(":L3G4200D FAILURE\n"); // } #else adxl345_init(); microSD_switchoff(); at45db_init(); microSD_init(); bmp085_init(); l3g4200d_init(); #endif adc_init(ADC_SINGLE_CONVERSION, ADC_REF_2560MV_INT); etimer_set(&timer, CLOCK_SECOND * 0.05); while (1) { PROCESS_YIELD(); etimer_set(&timer, CLOCK_SECOND); int16_t tmp; /*############################################################*/ //ADXL345 serial Test printf("X:%+6d | Y:%+6d | Z:%+6d\n", adxl345_get_x_acceleration(), adxl345_get_y_acceleration(), adxl345_get_z_acceleration()); /*############################################################*/ //L3G4200d serial Test printf("X:%+6d | Y:%+6d | Z:%+6d\n", l3g4200d_get_x_angle(), l3g4200d_get_y_angle(), l3g4200d_get_z_angle()); printf("T1:%+3d\n", l3g4200d_get_temp()); /*############################################################*/ //BMP085 serial Test printf("P:%+6ld\n", (uint32_t) bmp085_read_pressure(BMP085_HIGH_RESOLUTION)); printf("T2:%+3d\n", bmp085_read_temperature()); /*############################################################*/ //Power Monitoring printf("V:%d\n", adc_get_value_from(PWR_MONITOR_ICC_ADC)); // tmp = adc_get_value_from(PWR_MONITOR_ICC_ADC); // // tmp = adc_get_value_from(PWR_MONITOR_VCC_ADC); // /*############################################################*/ //microSD Test // uint8_t buffer[512]; // uint16_t j; // microSD_init(); // for (j = 0; j < 512; j++) { // buffer[j] = 'u'; // buffer[j + 1] = 'e'; // buffer[j + 2] = 'r'; // buffer[j + 3] = '\n'; // // j = j + 3; // } // // microSD_write_block(2, buffer); // // microSD_read_block(2, buffer); // // for (j = 0; j < 512; j++) { // printf(buffer[j]); // } // microSD_deinit(); // /*############################################################*/ //Flash Test // uint8_t buffer[512]; // uint16_t j, i; // // for (i = 0; i < 10; i++) { // //at45db_erase_page(i); // for (j = 0; j < 512; j++) { // buffer[j] = i; // } // at45db_write_buffer(0, buffer, 512); // // at45db_buffer_to_page(i); // // at45db_read_page_bypassed(i, 0, buffer, 512); // // for (j = 0; j < 512; j++) { // printf("%02x", buffer[j]); // if (((j + 1) % 2) == 0) // printf(" "); // if (((j + 1) % 32) == 0) // printf("\n"); // } // } } PROCESS_END(); }