#include #include #include #define output(directions,pin) (directions |= pin) // set port direction for output #define set(port,pin) (port |= pin) // set port pin #define clear(port,pin) (port &= (~pin)) // clear port pin #define pin_test(pins,pin) (pins & pin) // test for port pin #define bit_test(byte,bit) (byte & (1 << bit)) // test for bit set #define bit_delay_time 100 // bit delay for 9600 with overhead #define bit_delay() _delay_us(bit_delay_time) // RS232 bit delay #define half_bit_delay() _delay_us(bit_delay_time/2) // RS232 half bit delay #define char_delay() _delay_ms(10) // char delay #define serial_port PORTB #define serial_direction DDRB #define serial_pin_out (1 << PB2) #define TRUE 1 #define FALSE 0 static char lo_byte; static char hi_byte; void put_char(volatile unsigned char *port, unsigned char pin, char txchar) { // // send character in txchar on port pin // assumes line driver (inverts bits) // // start bit // clear(*port,pin); bit_delay(); // // unrolled loop to write data bits // if bit_test(txchar,0) set(*port,pin); else clear(*port,pin); bit_delay(); if bit_test(txchar,1) set(*port,pin); else clear(*port,pin); bit_delay(); if bit_test(txchar,2) set(*port,pin); else clear(*port,pin); bit_delay(); if bit_test(txchar,3) set(*port,pin); else clear(*port,pin); bit_delay(); if bit_test(txchar,4) set(*port,pin); else clear(*port,pin); bit_delay(); if bit_test(txchar,5) set(*port,pin); else clear(*port,pin); bit_delay(); if bit_test(txchar,6) set(*port,pin); else clear(*port,pin); bit_delay(); if bit_test(txchar,7) set(*port,pin); else clear(*port,pin); bit_delay(); // // stop bit // set(*port,pin); bit_delay(); // // char delay // bit_delay(); } uint16_t getIntValue(){ uint16_t hi = hi_byte; uint16_t lo = lo_byte; hi = hi * 256; hi += lo; return hi; } void setADC1(){ ADMUX = (0 << REFS1) | (0 << REFS0) // Vcc ref | (0 << MUX5) | (0 << MUX4) | (0 << MUX3) | (1 << MUX2) | (1 << MUX1) | (1 << MUX0); // ADC7 } void setADC2(){ ADMUX = (0 << REFS1) | (0 << REFS0) // Vcc ref | (0 << MUX5) | (0 << MUX4) | (0 << MUX3) | (0 << MUX2) | (1 << MUX1) | (1 << MUX0); // ADC3 } void setADC3(){ ADMUX = (0 << REFS1) | (0 << REFS0) // Vcc ref | (0 << MUX5) | (0 << MUX4) | (0 << MUX3) | (0 << MUX2) | (1 << MUX1) | (0 << MUX0); // ADC2 } void read1(){ setADC1(); // // initiate conversion // ADCSRA |= (1 << ADSC); // // wait for completion // while (ADCSRA & (1 << ADSC)) ; lo_byte = ADCL; hi_byte = ADCH; } void read2(){ setADC2(); // // initiate conversion // ADCSRA |= (1 << ADSC); // // wait for completion // while (ADCSRA & (1 << ADSC)) ; lo_byte = ADCL; hi_byte = ADCH; } void read3(){ setADC3(); // // initiate conversion // ADCSRA |= (1 << ADSC); // // wait for completion // while (ADCSRA & (1 << ADSC)) ; lo_byte = ADCL; hi_byte = ADCH; } void turnLED1On(){ //LED is PB0 DDRA |= _BV(PB0); //Enable output on the LED pin PORTA |= _BV(PB0); //turn LED on } void turnLED2On(){ //LED is PB1 DDRA |= _BV(PB1); //Enable output on the LED pin PORTA |= _BV(PB1); //turn LED on } void turnLED3On(){ //LED is PB2 DDRA |= _BV(PB2); //Enable output on the LED pin PORTA |= _BV(PB2); //turn LED on } void turnLED1Off(){ //LED is PB0 DDRA |= ~_BV(PB0); //Enable output on the LED pin PORTA &= ~_BV(PB0); //turn LED off } void turnLED2Off(){ //LED is PB1 DDRA |= ~_BV(PB1); //Enable output on the LED pin PORTA &= ~_BV(PB1); //turn LED off } void turnLED3Off(){ //LED is PB2 DDRA |= ~_BV(PB2); //Enable output on the LED pin PORTA &= ~_BV(PB2); //turn LED off } void sendChar(char c){ put_char(&serial_port, serial_pin_out, c); char_delay(); } void sendBytes(){ sendChar(hi_byte); sendChar(lo_byte); } int main(void) { // // main // // // set clock divider to /1 // CLKPR = (1 << CLKPCE); CLKPR = (0 << CLKPS3) | (0 << CLKPS2) | (0 << CLKPS1) | (0 << CLKPS0); // // initialize output pins // set(serial_port, serial_pin_out); output(serial_direction, serial_pin_out); // // init A/D // ADCSRA = (1 << ADEN) // enable | (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0); // prescaler /128 // // main loop // while (1) { // // send framing // sendChar(1); sendChar(2); sendChar(3); sendChar(4); read1(); sendBytes(); uint16_t val = getIntValue(); /* * int16_t int8_t int32_t */ read2(); sendBytes(); val = getIntValue(); /* * int16_t int8_t int32_t */ read3(); sendBytes(); val = getIntValue(); /* * int16_t int8_t int32_t */ } }