/* * HtM_Sensors.c * * Created: 11/21/2017 9:11:11 * Author : Casey Evans */ /* Original template from the Tinysaur project */ /* Adapted basic functional code from Neil's code */ #include #include // ==================================== // ATtiny // 44A // +--------+ // --+Vcc GND+--------- // LED - PB0 --+ +-- PA0 // LED - PB1 --+ +-- PA1 // LED - PB3 --+ +-- PA2 // LED - PB2 --+ +-- PA3 // LED - PA7 --+ +-- PA4 // LED - PA6 --+ +-- PA5 // +--------+ // ==================================== // Define the I/O port to be used for the sensors. // The sensors each use one or two ports and I will define them accordingly. // That way they will line up properly on the headers. #define LED_PORT PA7 #define BUTTON_PORT PB2 #define SERIAL_PORT PA1 #define SENSE1 PA2 #define SENSE2 PA3 #define BUTTON_IN (1 << PB2) #define nsamples 100 // number of samples to accumulate int main(void) { // Set ports as outputs // The DDRA/B is the data direction register // The code below // - shifts the "1" on left to the desired position // - does bitwise "OR" with the value in the port register. DDRA |= (1 << LED_PORT); PORTA |= (1 << SERIAL_PORT); DDRA |= (1 << SERIAL_PORT); // Good practice to leave these as outputs until we need them DDRA |= (1 << SENSE1); DDRA |= (1 << SENSE2); // Sets port as input DDRB &= ~(1 << BUTTON_PORT); // Engages pull-up resistor PORTB |= (1 << BUTTON_PORT); // variables (from Neil's code) static uint16_t count; static uint32_t accum; // set clock divider to /1 (from Neil's code) CLKPR = (1 << CLKPCE); CLKPR = (0 << CLKPS3) | (0 << CLKPS2) | (0 << CLKPS1) | (0 << CLKPS0); // Magnetic sensor init A/D (from Neil's code) ADMUX = (0 << REFS1) | (0 << REFS0) // Vcc ref | (1 << ADLAR) // left adjust | (0 << MUX5) | (0 << MUX4) | (0 << MUX3) | (0 << MUX2) | (1 << MUX1) | (0 << MUX0); // ADC2 ADCSRA = (1 << ADEN) // enable | (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0); // prescaler /128 // Put char from Neil's code 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 *port &= (~pin); _delay_us(102); // unrolled loop to write data bits if (txchar & (1 << 0)) *port |= pin; else *port &= (~pin); _delay_us(102); if (txchar & (1 << 1)) *port |= pin; else *port &= (~pin); _delay_us(102); if (txchar & (1 << 2)) *port |= pin; else *port &= (~pin); _delay_us(102); if (txchar & (1 << 3)) *port |= pin; else *port &= (~pin); _delay_us(102); if (txchar & (1 << 4)) *port |= pin; else *port &= (~pin); _delay_us(102); if (txchar & (1 << 5)) *port |= pin; else *port &= (~pin); _delay_us(102); if (txchar & (1 << 6)) *port |= pin; else *port &= (~pin); _delay_us(102); if (txchar & (1 << 7)) *port |= pin; else *port &= (~pin); _delay_us(102); // stop bit *port |= pin; _delay_us(102); // char delay _delay_us(102); } while (1) { // accumulate samples accum = 0; for (count = 0; count < nsamples; ++count) { // initiate conversion ADCSRA |= (1 << ADSC); // wait for completion while (ADCSRA & (1 << ADSC)) ; // add result accum += ADC; } // send framing put_char(&PORTA, (1 << SERIAL_PORT), 1); _delay_ms(10); put_char(&PORTA, (1 << SERIAL_PORT), 2); _delay_ms(10); put_char(&PORTA, (1 << SERIAL_PORT), 3); _delay_ms(10); put_char(&PORTA, (1 << SERIAL_PORT), 4); _delay_ms(10); // send result put_char(&PORTA, (1 << SERIAL_PORT), (accum & 255)); _delay_ms(10); put_char(&PORTA, (1 << SERIAL_PORT), ((accum >> 8) & 255)); _delay_ms(10); put_char(&PORTA, (1 << SERIAL_PORT), ((accum >> 16) & 255)); _delay_ms(10); } return (0); }