int main(void){ char t_chr[7]; uint32_t v=0, v_old = 0; memset(t_chr, '\0', sizeof(t_chr)); USART_init(MYUBRR); /* Remove garbage from serial terminal */ USART_transmit('\r'); ADC_init(); PWM16_init(); while(1){ v = ADC_read(0); if (v_old != v) { v_old = v; snprintf(t_chr,sizeof(t_chr),"%u",v); USART_write(t_chr); USART_transmit(' '); OCR1A = CYCLE_LENGTH_MIN - ONE_UNIT * v; OCR1B = OCR1A/2; snprintf(t_chr,sizeof(t_chr),"%u",OCR1A); USART_write(t_chr); USART_transmit('\n'); USART_transmit('\r'); _delay_ms(500); } } return 1; }
int main(){ init(); MCUSR &= ~_BV(WDRF); wdt_disable(); get_motor_cnt(); struct motor_state* motors = allocate_motors(); /* while(1){ //PORTD = 0xFF; //PORTD = 0b01010101; //PORTD = 0b00001000;//Pins 3 and 2 show the same value! =( DDRD = 0xFF; PORTD = 0b10100000; } */ unsigned char i; while(1){ current_count = current_count % 100; for (i = 0; i < num_motors; ++i){ if (motor_is_enabled(motors + i) && get_motor_speed(motors + i) > current_count){ USART_transmit(get_motor_speed(motors + i) + 65); activate_motor(i, motors); } else { deactivate_motor(i, motors); } } if (new_motor_state){ memcpy(motors, motor_buf, sizeof(struct motor_state) * num_motors); new_motor_state = 0; USART_transmit('c'); //print_motor_state(motors); } } }
void main(void) { initialize_ports(); //initialize ports InitADC(); while(1){//main loop new_digital = ReadADC(0x20);//read analog input on PORTC pin0, left justify if(new_digital != old_digital){ //process change //PORTB=new_digital; //in PORTB input USART_transmit(0x44); PORTB=new_digital; //in PORTB input } //end if new_digital old_digital=new_digital;//update PORTC } //end while(1) } //end main
// *********************************************************** // Main program // int main(void) { DDRD = 0x30; //4-5-pins output DDRB = 0xFF; // DDRC = 0xFF; // // External interrupt adjustments GICR |= 1 << INT0; //Enable INT0 (PD2) MCUCR &= ~(1 << ISC01); //The low level of INT0 generates an interrupt request. MCUCR &= ~(1 << ISC00); //UART Port speed 115200 for the crystal freq. 7.3MHz USART_init (); ADC_init(); PWM_init(); USART_transmit('c');// Change the directory on USART_transmit('d');//ARM to home. Cannot run USART_transmit(' ');///home/status STATUS__ USART_transmit('/');//need to cd to /home USART_transmit('h'); USART_transmit('o'); USART_transmit('m'); USART_transmit('e'); USART_transmit(0x0A); while(1) { //Check if the status of controller changed then send via UART otherwise don't send if (memcmp ((char *)&PREV_AVR_STATUS, (char *)&AVR_STATUS, sizeof (avr_status)) != 0 ) { send_status(); //after sending the status make the previous status - actual memcpy ((char *)&PREV_AVR_STATUS, (char *)&AVR_STATUS, sizeof (avr_status)); } _delay_us(10); // time which regulate the frequency of checking the status } // Infinite loop }//int main(void)
int main(void){ uint16_t v0,v1,v2; double t0,t1,t2; char t_chr[7]; memset(t_chr, '\0', sizeof(t_chr)); USART_init(MYUBRR); /* Remove garbage from serial terminal */ USART_transmit('\r'); ADC_init(); PWM_init(); while(1){ v0 = ADC_read(0); /* read from ADC0 */ v1 = ADC_read(1); /* read from ADC0 */ v2 = ADC_read(2); /* read from ADC0 */ t0 = v0 / 4; t1 = v1 / 4; t2 = v2 / 4; dtostrf(t0, 5, 1, t_chr); USART_write("0: "); USART_write(t_chr); USART_write("\r\n"); dtostrf(t1, 5, 1, t_chr); USART_write("1: "); USART_write(t_chr); USART_write("\r\n"); dtostrf(t2, 5, 1, t_chr); USART_write("2: "); USART_write(t_chr); USART_write("\r\n"); OCR0A = t0; OCR0B = t1; OCR2A = t2; _delay_ms(500); } return 1; }
void I2C_read(unsigned char znak) { //uint16_t read; // START TWCR =(1 << TWINT) | (1 << TWSTA) | ( 1 << TWEN ); while(!(TWCR & (1 << TWINT))); // SLA+W TWDR = (adres_slave << 1) | WRITE; TWCR =(1 << TWINT) | (1 << TWEN); while(!(TWCR & (1 << TWINT))); // Przeslanie komendy TWDR = znak; TWCR =(1 << TWINT) | (1 << TWEN); while(!(TWCR & (1 << TWINT))); // START TWCR =(1 << TWINT) | (1 << TWSTA) | ( 1 << TWEN ); while(!(TWCR & (1 << TWINT))); // SLA+R TWDR = (adres_slave << 1) | READ ; TWCR =(1 << TWINT) | (1 << TWEN); while(!(TWCR & (1 << TWINT))); // READ the 6-byte message dane_indeks = 0; while (dane_indeks <6) { // Read byte TWCR =(1 << TWINT) | (1 << TWEN); // Generate ACK if not the last byte if(dane_indeks!= 5) TWCR |= (1<<TWEA); while(!(TWCR & (1 << TWINT))); dane[dane_indeks] = TWDR; USART_transmit(dane[dane_indeks]); dane_indeks++; } // STOP TWCR =(1 << TWINT) | (1<<TWEN) | (1<<TWSTO); }
void initialize( void ) { CPU_PRESCALE(0); USART_init(BAUD_RATE); USART_transmit('\f'); // Send form feed to clear the terminal. USART_send_string("WunderBoard initializing...\r\n"); USART_send_string("\tSetting ADC prescaler and disabling free running mode...\r\n"); setup_ADC(ADC_PRESCALER_32, FALSE); USART_send_string("\tEnabling ADC...\r\n"); ADC_enable(); USART_send_string("\tSetting ADC reference to Vcc...\r\n"); ADC_set_reference(ADC_REF_VCC); // Configure IO // USART_send_string("\tConfiguring IO...\r\n"); //DDRx corresponds to PORTx/PINx, dependng on direction of data flow -- PORT for output, PIN for input DDRA = 0x00; // Buttons and switches DDRB = 0xE7; // Red enable, green enable and audio out DDRC = 0xff; // Discrete LEDs DDRE = 0x47; // LED Column DDRF = 0x00; // Accelerometer // Disable pullups and set outputs low // PORTA = 0x00; PORTB = 0x01; PORTC = 0x81; PORTE = 0x00; PORTF = 0x00; //Set OC1A to toggle TCCR1A = 0b01000000; // Clk/64 and CTC mode TCCR1B = 0b00001011; OCR1A = 24; USART_send_string("\tSetting SPI\r\n"); //Set the SPI bus appropriately to use the LED array SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR0); }
int main(void){ char t_chr[7]; uint32_t v=0; memset(t_chr, '\0', sizeof(t_chr)); USART_init(MYUBRR); /* Remove garbage from serial terminal */ USART_transmit('\r'); ADC_init(); PWM16_init(); while(1){ v = ADC_read(0); snprintf(t_chr,sizeof(t_chr),"%u",v); USART_write(t_chr); USART_transmit(' '); v = (v * 180) / 1024; snprintf(t_chr,sizeof(t_chr),"%u",v); USART_write(t_chr); USART_transmit('\n'); USART_transmit('\r'); OCR1A = OCR_MIN + v * ONE_DEGREE; snprintf(t_chr,sizeof(t_chr),"%u",OCR1A); USART_write(t_chr); USART_transmit('\n'); USART_transmit('\r'); _delay_ms(500); } return 1; }
void USART_write(char *str){ uint8_t i = 0; for(i = 0; str[i] != '\0'; i++) USART_transmit(str[i]); }
void USART_writeln( char *s ) { USART_puts( s ); USART_transmit( '\n' ); USART_transmit( '\r' ); }
void mainfunction() { uint8_t header, data; bool received_data = false; enable_irqs(); while (true) { if (received_data) { int twi_send_err = 0; // Send to right instance depending on header if (header == 0x00) { // FireFly is sending control commands. Relay to control module. twi_send_err = TWI_master_send_message(TWI_CONTROL_MODULE_ADDRESS, header, data); } else if (header == 0x01) { // Control module is sending. Relay to firefly cli(); USART_transmit(header, data); sei(); } else if (header == 0x02) { // FireFly is sending a calibration command. Relay to sensor module. twi_send_err = TWI_master_send_message(TWI_SENSOR_MODULE_ADDRESS, header, data); } else if (header >= 0x03 && header <= 0x0B) { // Sensor module is sending. Relay to control module and FireFly. twi_send_err = TWI_master_send_message(TWI_CONTROL_MODULE_ADDRESS, header, data); cli(); USART_transmit(header, data); sei(); } else if (header == 0x0C) { // Error. Relay to FireFly. cli(); USART_transmit(header, data); sei(); // } else if(header==0x0D) { #ifdef USE_PING ping_received=true; #endif } else { // Invalid header. Send error message send_error(0x00); } // If there was any error with TWI if (twi_send_err) { // TWI error. Send error message. send_error(0x01); } received_data = false; } #ifdef USE_PING else if (ping_counter<0) { ping_counter=1<<16; cli(); USART_transmit(0x0D , 0x00); sei(); if(!ping_received) TWI_master_send_message(TWI_CONTROL_MODULE_ADDRESS , 0x00 , 0x06); ping_received=0; } #endif int twi_rec_err = 0; if (sensor_module_interrupt) { sensor_module_interrupt = false; // Get data from sensor module twi_rec_err = TWI_master_receive_message(TWI_SENSOR_MODULE_ADDRESS, &header, &data); received_data = true; } else if (control_module_interrupt) { control_module_interrupt = false; // Get data from control module twi_rec_err = TWI_master_receive_message(TWI_CONTROL_MODULE_ADDRESS, &header, &data); received_data = true; } else if (firefly_received_data) { cli(); header = firefly_header; data = firefly_data; firefly_received_data = false; received_data = true; sei(); } if (twi_rec_err) { // TWI error. Send error message. send_error(0x02); } #ifdef USE_PING ping_counter--; #endif } }
inline void send_error(uint8_t error_code) { cli(); USART_transmit(0x0C, error_code); sei(); }
void send_no_data_command(uint8_t address, uint8_t command) { // The address and command in the address byte USART_transmit(USART_ADDRESS_BYTE, (address | command)); }
void send_status(void) { USART_transmit('.'); USART_transmit('/'); USART_transmit('s'); USART_transmit('t'); USART_transmit('a'); USART_transmit('t'); USART_transmit('u'); USART_transmit('s'); USART_transmit(' '); USART_transmit(AVR_STATUS.left_engine_direction);// Transmit left_engine_direction USART_transmit(AVR_STATUS.left_engine_speed);// Transmit left_engine_speed USART_transmit(AVR_STATUS.right_engine_direction);// Transmit right_engine_direction USART_transmit(AVR_STATUS.right_engine_speed);// Transmit right_engine_speed USART_transmit(AVR_STATUS.battery_voltage);// Transmit battery_voltage USART_transmit(AVR_STATUS.range_tens);// Transmit range tens to the aim USART_transmit(AVR_STATUS.range_numbers);// Transmit range tens to the aim USART_transmit(AVR_STATUS.last_command); //Transmit last sent command USART_transmit(0x0a); }