Sunteți pe pagina 1din 11

Project 7 All Together Now In this project you will combine the three sensors and the switch

h youve previously experimented with and write a program to control them. General Specification This device will consist of a Temperature Sensor, a Wind Speed Sensor, a Wind Direction Sensor, a push button switch and a bank of 8 LEDs. The controlling program will periodically sample the sensors and display the currently selected sensors data onto the bank of 8 LEDs. The push button switch will be used to select which sensor to display. 1. How will the switch that selects which sensor data to display work (this is a question of defining how 1 switch will select from 3 different possibilities)?

Hardware Design Using the schematics from Projects 3 through 6 as examples combine schematics into one integrated schematic using the Eagle schematic capture program (ask your instructor for help on using the Eagle program). Make sure you use the following Port Assignments for the ATtiny261. PortA 0:7 LEDs PortB 1 PortB 2:4 PortB 5 PortB 6 PortB 7 Push Button Switch Wind Direction Wind Speed Temperature Sensor Unassigned

Now connect the circuit on your protoboard. Use the following drawing to help layout your protoboard connections.

2. When you have your program running come back to this step and record the current consumption for the whole device. Program Specification This program will integrate all of the individual programs from the previous projects into a cohesive whole. The program needs to initialize the ADC for the temperature sensor, initialize the input port for the wind direction sensor, initialize the input port for the switch, initialize the input port for the wind speed interrupt, initialize the interrupt registers and initialize the Output Port. The program will also set the displayed sensor to temperature. The program will then enter the main loop. The main loop will poll the temperature sensor and the wind direction sensor and store the results. The main loop will also display the chosen sensor data on the bank of 8 LEDs. Finally the main loop will poll the switch. If the switch is depressed the next sensor will become the displayed sensor. Sensor display order will be current temperature, maximum temperature, minimum temperature, wind direction, wind speed, and maximum wind speed. An interrupt routine will store the time accumulated in the timer and reset the timer. The interrupt routine will also translate the time accumulated between interrupts into a wind speed and check the wind speed against the stored maximum wind speed. Polling the temperature sensor consists of starting an A to D conversion, waiting for the conversion to complete, then storing the converted number. The program needs to check the current temperature against the stored maximum and minimum temperatures. Polling the wind direction sensor consists of reading from the photo interrupters, translating the 3 bit binary number read from the photo interrupters into a bit position and storing the bit position.

Target microcontroller is AVRs ATtiny261. Program Flowchart 6. Create a flow chart for the Program Specification

/****************************************************************** * AllTogether.c * * Programmer: Thomas Trickel * * Creation Date: 11/22/8 * Last Modification Date: 11/22/8 * * Description:This program will integrate all of the individual programs from the previous projects * into a cohesive whole. The program needs to: * initialize the ADC for the temperature sensor, * initialize the input port for the wind direction sensor, * initialize the input port for the switch, * initialize the input port for the wind speed interrupt, * initialize Timer1 for timing Wind Speed pulses, * initialize the interrupt registers and * initialize the Output Port for displaying information on the LEDs. * The program will also set the displayed sensor to temperature. * The program will then enter the main loop. The main loop will * poll the temperature sensor and * poll the wind direction sensor and * store the results. * The main loop will also display the chosen sensor data on the bank of 8 LEDs. * Finally the main loop will poll the switch. * If the switch is depressed the next sensor will become the displayed sensor. * Sensor display order will be * current temperature, * maximum temperature, * minimum temperature, * wind direction, * wind speed, and * maximum wind speed. * * An interrupt routine will * store the time accumulated in the timer and * reset the timer. * The interrupt routine will also * translate the time accumulated between interrupts into a wind speed and * check the wind speed against the stored maximum wind speed. * * Polling the temperature sensor consists of * starting an A to D conversion,

* waiting for the conversion to complete, then * storing the converted number. The program needs to * check the current temperature against the stored max and min temperatures. * * Polling the wind direction sensor consists of * reading from the photo interrupters, * translating the 3 bit binary number read from the photo interrupters * into a bit position and storing the bit position. * * Target microcontroller is AVR's ATtiny261 * * * Input: User input via a single pushbutton switch to determine which sensor's data to display. * input will also be from an LM35DZ temperature sensor, a Hall Effect switch and * a set of three Photo Interrupters. * * Output: The chosen sensor's data will be displayed on a bank of eight LEDs * ******************************************************************/ #include <avr/io.h> #include <avr/interrupt.h> unsigned int unsigned int char sensor or not int LM35 is stored here int here int here char char WindTime; MinWindTime; //the time between wind speed interrupts //until the wind speed sensor has been calibrated //the maximum speed is a minimum time

WindSpeedInterruptFlag = 0; //used to tell whether the magnet is over the CurrentTemperature; MaxTemp; MinTemp; WindDirection; DisplayedSensor = 0; //The most recently read value of the //The highest temperature read is stored //The lowest temperature read is stored // //0 = Current Temperature //1 = Maximum Temperature, //2 = Minimum Temperature //3 = Wind Direction

//4 = Wind Speed //5 = Maximum Wind Speed //6 = //7 = char ISR(PCINT_vect) MaxDisplayedSensor = 5; // signal handler for Pin Change interrupt

{ //**************************** //NOTE; This is a pin CHANGE interrupt // this means that when the pin transitions from a high to a low // AND when the pin transitions from a low back to a high // // this means that when the Hall Effect switch turns on // AND when it turns back off // // subsequently each magnet passing results in two interrupts if (WindSpeedInterruptFlag == 1) interrupt { WindTime = TCNT1; WindTime |= ((char)TC1H << 8); TCCR1B |= 0x40; TC1H = 0; Counter TCNT1 = 0; WindSpeedInterruptFlag = 0; change is ignored //if the current wind time is less than //the saved min wind time change //the saved min wind time to the current wind time if (WindTime < MinWindTime) MinWindTime = WindTime; } else WindSpeedInterruptFlag = 1; //set WISF so the next pin change checks the timer } void InitInterrupts (void) //clear WISF so the next pin //read Timer1's low byte //add in Timer1's high byte //Set bit 6 to clear the prescaler //clear all 10 bits of Timer1 //Only stop the timer every other

{ GIMSK |= 0x10; PCMSK1 &= 0x20; sei(); } void InitTimer1 (void) { TCCR1B |= 0x0F; } void InitAD (void) { //start of the InitAD function /* A/D Register definitions/ ADMUX = 0b10101001; /* Bit 7 6 5 4 3 2 1 0 REFS1 REFS0 ADLAR MUX4 MUX3 MUX2 MUX1 MUX0 REFS2, REFS1, REFS0 = 110 Internal 2.5v as reference without external bypass capacitor, disconnected from AREF. Bit 5 left adjusted results Bits 4-0 MUX4:0: Analog Channel and Gain Selection Bits 001001 select ADC9 or PB6 */ ADCSRA = 0b10000110; /* Bit 76543210 ADEN ADSC ADATE ADIF ADIE ADPS2 ADPS1 ADPS0 Bit 7 Enables ADC Bit 6 ADSC 1 starts each conversion in Single Conversion mode In Single Conversion mode, write this bit to one to start each conversion. ADSC will read as one as long as a conversion is in progress. When the conversion is complete, it returns to zero. Bit 5 ADC Auto Trigger Enable Bit 4 ADIF: ADC Interrupt Flag Bit 3 ADIE: ADC Interrupt Enable Bits 2:0 ADPS2:0: ADC Prescaler Select Bits */ ADCSRB = 0b00010000; ADTS1 ADTS0 Bit 7 BIN: Bipolar Input Mode Bit 6 GSEL: Gain Select Bit 5 Res: Reserved Bit Bit 4 REFS2: Reference Selection Bit /*Bit 7 6 5 4 3 2 1 0 - BIN GSEL - REFS2 MUX5 ADTS2 //enable Pin Change Interrupts from pins 8 to 11 //enable pin change interrupt on pin 8 (PortB 5) // enable interrupts

//Bits 3:0 are Clock Select Bits 1111 = PCK/16384

Bit 3 MUX5: Analog Channel and Gain Selection Bit 5 Bits 2:0 ADTS2:0: ADC Auto Trigger Source */ return; } /*end of InitAD function*/

void PollTemperature (void) { ADCSRA = ADCSRA | (1 << ADSC); register*/ /*this starts and AtoD conversion*/ while ((ADCSRA | (1 << ADSC)) == 1) /*wait for the ADSC bit to go low*/ { /*signaling the completion of an AtoD conversion*/ } CurrentTemperature = ADCH; if (CurrentTemperature > MaxTemp) { MaxTemp = CurrentTemperature; } if (CurrentTemperature < MinTemp) { MinTemp = CurrentTemperature; } } char GetEncoderPosition (void) { char RawOpticalEncoderReading; char EncoderPosition; /*end of the PollTemperature function*/ /*Set the ADSC bit in the ADCSRA

//the number read from PortB pins 2,3 &4 //the Optical Encoder number on bits 0,1 & 2

RawOpticalEncoderReading = PINB; //read PortB pins EncoderPosition = RawOpticalEncoderReading & 0x1C; EncoderPosition >>= 2; places // thereby moving bits 2,3 & 4 to bits 0,1 & 2 return (EncoderPosition); //mask off all but bits 2,3 & 4

//Shift the Raw Encoder Position right by two

} char TranslateEncoderPosition(char EncoderPosition) { char TranslatedEncoderPosition; position

//Encoder Position number translated into a bit

TranslatedEncoderPosition = 0b00000001 << EncoderPosition; return(TranslatedEncoderPosition); } void PollWindDirection (void) { char Encoder_Position;

//number read from the Optical Encoder

Encoder_Position = GetEncoderPosition(); WindDirection = TranslateEncoderPosition(Encoder_Position); } int main (void) { temperature sensor*/ InitTimer1(); InitInterrupts(); DDRA = 0xFF; DDRB = 0x00; PORTB = 0xFF; resistors MaxTemp = 0; MinTemp = 100; expect our first reading to be less than it*/ while (1) { PollTemperature(); /*loop forever*/ /*initialize MaxTemp to be a very low value so we can expect our first reading to be greater than it*/ /*initialize MinTemp to be a very high value so we can //setup PortA for all output //setup PortB to be all input //Writing all ones to an input port enables the pull up //start of the Main function InitAD(); /*initialize the AD converter to read from the

PollWindDirection(); switch (DisplayedSensor) sensor's data { case 0: PORTA = CurrentTemperature; break; case 1: PORTA = MaxTemp; break; case 2: PORTA = MinTemp; break; case 3: PORTA = WindDirection; break; case 4: PORTA = WindTime; break; case 5: PORTA = MinWindTime; break; } if (bit_is_clear(PINB, 1)) { DisplayedSensor = DisplayedSensor + 1; next sensor if (DisplayedSensor > MaxDisplayedSensor) //check to see if the displayed sensor is greater { sensors DisplayedSensor = 0; } } } return (0); } //return from the Main function //end of the forever while //if it is set it back to 0 //than the number of defined //if the switch is closed //set the displayed sensor to the //display the currently selected

S-ar putea să vă placă și