// // // hello.light.45.c // // phototransistor hello-world // 9600 baud FTDI interface //CHANGE TO 115200??? (but messes up bit delays? see below) // // Neil Gershenfeld // 10/27/10 // // (c) Massachusetts Institute of Technology 2010 // Permission granted for experimental and personal use; // license for commercial sale available from MIT. // //EDITS L Perovich Nov 2012 //Switch to a pressure sensor off at ATtiny44 board //First just see if we can get a reading off this sensor, then worry about lights etc. #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_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 cal_delay() _delay_ms(500) // callibration delay #define light_delay() _delay_ms(100) // hold the light for a bit before moving on to next measure #define serial_port PORTA #define serial_direction DDRA #define serial_pin_out (1 << PA1) //My out serial pin is PA1--adjusted all of above code accordingly! //My LED output pins will be PA7 and PB0 (2nd and 6th on LHS) //My input pins (ADC) are PA2 and PA3 (4th and 5th on RHS)--these are defined later //My LED output pins will be PA7 and PB0 (2nd and 6th on LHS) #define led1_port PORTA #define led1_direction DDRA #define led1_pin_out (1 << PA7) #define led2_port PORTB #define led2_direction DDRB #define led2_pin_out (1 << PB0) 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(); } int main(void) { // // main // static char chr, high, low; // int value, high, low; uint16_t value; // // 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); // //initialize LED pins clear(led1_port, led1_pin_out); output(led1_direction, led1_pin_out); clear(led2_port, led2_pin_out); output(led2_direction, led2_pin_out); //My input pins (ADC) are PA2 and PA3 (4th and 5th on RHS)--these are defined later // init A/D // //One set for each! But we'll only do one at a time now so we don't have to deal with sending both and figuring out which is which. Comment the other. //Old version // ADMUX = (0 << REFS2) | (0 << REFS1) | (0 << REFS0) // Vcc ref // | (0 << ADLAR) // right adjust // | (0 << MUX3) | (0 << MUX2) | (1 << MUX1) | (1 << MUX0); // ADC3 (PB3) // ADCSRA = (1 << ADEN) // enable // | (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0); // prescaler /128 //Look at table 16-4 //Also table 22 (no REFS2) ADMUX = (0 << REFS1) | (0 << REFS0) // Vcc ref | (0 << ADLAR) // right adjust | (0 << MUX5) | (0 << MUX4) | (0 << MUX3) | (0 << MUX2) | (1 << MUX1) | (0 << MUX0); // ADC2 (PA2) 000010 MUX pattern for PA2 ADCSRA = (1 << ADEN) // enable | (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0); // prescaler /128 // ADMUX = (0 << REFS1) | (0 << REFS0) // Vcc ref // | (0 << ADLAR) // right adjust // | (0 << MUX5) | (0 << MUX4) | (0 << MUX3) | (0 << MUX2) | (1 << MUX1) | (1 << //MUX0); // ADC3 PA3 has pattern 000011 // ADCSRA = (1 << ADEN) // enable // | (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0); // prescaler /128 // // main loop // while (1) { // // send framing // put_char(&serial_port, serial_pin_out, 1); char_delay(); put_char(&serial_port, serial_pin_out, 2); char_delay(); put_char(&serial_port, serial_pin_out, 3); char_delay(); put_char(&serial_port, serial_pin_out, 4); char_delay(); // // initiate conversion // ADCSRA |= (1 << ADSC); // // wait for completion // while (ADCSRA & (1 << ADSC)) ; // // send result // chr = ADCL; high=chr; // high=int(chr); put_char(&serial_port, serial_pin_out, chr); char_delay(); chr = ADCH; low=chr; //low = int(chr); put_char(&serial_port, serial_pin_out, chr); char_delay(); //try to combine the values value = 256*high + low; put_char(&serial_port, serial_pin_out, value); char_delay(); //And try to do the light //Just one sensor now with a cheat--assume the weight is always on the other foot if it's not on the first one. Turn off one light turn on the other. if(value>1.5){ // if(filter>.5*filterave){ clear(led1_port, led1_pin_out); set(led2_port, led2_pin_out); // light_delay(); } //TURN ON LIGHT else{ set(led1_port, led1_pin_out); clear(led2_port, led2_pin_out); // light_delay(); } } }