{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Lab Seven - Lab Seven Temperature Sensor(Combined ADC LCD...

Info icon This preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
Lab Seven - Temperature Sensor (Combined ADC, LCD, and Sensor) Wallace Kerrigan, 1541317 [email protected] 7 November 2011
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Part One Figure 1. Flow Chart of Analog-to-Digital Reading of Temperature Sensor and LCD Display Code #define F_CPU 1000000UL #include <avr/io.h> #include <util/delay.h> #include <avr/interrupt.h> #define START 0x08 //TWSR code for start condition transmission #define MT_SLA_ACK 0x18 //TWSR code for SLA_W transmission and ACK received #define MT_DATA_ACK 0x28 //TWSR code for data byte transmitted and ACK received #define SLA_W 0x50 //Slave address (zeroth bit = 1 read command, 0 = write command) unsigned voltage = 0, voltage1 = 0, voltage2 = 0; unsigned char string2[16]; unsigned int DataSize; //Global variable used to hold string lengths //value before transmitting in SendLcdData() unsigned int ReadADC(unsigned char channel); // Assigning RaedADC as an unsigned integer void InitADC(void); //-------------------------------------------------------------------------
Image of page 2
//My delay function //------------------------------------------------------------------------- void my_delay_in_ms(unsigned int millisecs) { unsigned int i; for (i=0; i <= millisecs; i++) _delay_ms(1); } //------------------------------------------------------------------------- //Initialize the two wire interface (I2C) //------------------------------------------------------------------------- void I2C_Init(void) { TWBR = 0x44; //Sets SCL frequency to close to 0 kHz. f should be <100khz. See datasheet page 206 TWCR = 0xA5; //Enables TWI interface, see page 225 the 2nd bit TWSR = 0x01; //Sets prescaler (TWPS bits) to have prescaler value of 64, p. 227 } //------------------------------------------------------------------------- //Transmission error handler //------------------------------------------------------------------------- void ERROR(void) { // no error handling at this time } //------------------------------------------------------------------------- //The function that sends data strings to the LCD //------------------------------------------------------------------------- void SendLcdData(unsigned char data[]) { //--- Send Start condition ---// TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN); //Send start condition while (!(TWCR & (1<<TWINT))); //Wait for TWINT Flag set. This indicates that the START //condi tion has been transmitted if ((TWSR & 0xF8) != START); //Check value of TWI Status Register. Mask prescaler bits. If status { //different from START go to ERROR ERROR(); }
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
//--- Send address and "write" command ---// TWDR = SLA_W; //Load SLA_W (slave address, write) into TWDR register TWCR = (1<<TWINT) | (1<<TWEN); //Clear TWINT bit in TWCR to start transmission of address while (!(TWCR & (1<<TWINT))); //Wait for TWINT Flag set. This indicates that the SLA+W has been //trans mitted, and ACK/NACK has been received. if ((TWSR & 0xF8) != MT_SLA_ACK); //Check value of TWI Status Register. Mask prescaler bits. If status { //different from MT_SLA_ACK go to ERROR ERROR(); } //--- Send data string or LCD command ---// unsigned char counter; for(counter = 0; counter < DataSize; counter++) { TWDR = data[counter]; //Load DATA into TWDR Register. TWCR = (1<<TWINT) | (1<<TWEN); //Clear TWINT bit in TWCR to start transmission of data while (!(TWCR & (1<<TWINT))); //Wait for TWINT Flag set. This indicates that the DATA has been //trans mitted, and ACK/NACK has been received. if ((TWSR & 0xF8) != MT_DATA_ACK) //Check value of TWI Status Register. Mask prescaler bits. If { //status different from MT_DATA_ACK go to ERROR ERROR(); } my_delay_in_ms(5); } //--- Send stop command ---// TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWSTO); //Transmit STOP condition }
Image of page 4
//------------------------------------------------------------------------- //My macro for clearing the LCD //------------------------------------------------------------------------- void ClearLCD(void) { unsigned char ClearString[2] = {0xFE,0x51}; //0xFE 0x51 is the LCD specific command for clearscreen DataSize = 2; SendLcdData(ClearString); } //************************************************************************* //InitADC: initialize analog-to-digital converter //************************************************************************* void InitADC( void) { ADMUX = 0b01000000; ADCSRA = 0xC3; //Enable ADC & start 1st dummy conversion //Set ADC module prescalar to 8
Image of page 5

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern