Embedded Systems – IE403

Lab Work

Lab #13

To read an analog signal in microcontroller using an Analog-to-Digital Converter

An analog-to-digital converter (abbreviated ADC, A/D or A to D) is a device which converts a continuous quantity to a discrete time digital representation. Typically, an ADC is an electronic device that converts an input analog voltage or current to a digital number proportional to the magnitude of the voltage or current.

While there are many ways of implementing an ADC, there are three conceptual steps that occur: 1. The signal is sampled. 2. The sampled signal is quantized. 3. The quantized signal is digitally coded. Basic ADC Concepts  Resolution

The resolution of the converter indicates the number of discrete values it can produce over the range of analog values. The values are usually stored electronically in binary form, so the resolution is usually expressed in bits. In consequence, the number of discrete values available, or "levels", is a power of two. For example, an ADC with a resolution of 8 bits can encode an analog input to one in 256 different levels, since 28 = 256.  Conversion Time

The analog signal is continuous in time and it is necessary to convert this to a flow of digital values. It is therefore required to define the rate at which new digital values are sampled from the analog signal. The rate of new values is called the sampling rate or sampling frequency of the converter. Since a practical ADC cannot make an instantaneous conversion, the input value must necessarily be held constant during the time that the converter performs a conversion (called the conversion time). Applications AD converters are used virtually everywhere where an analog signal has to be processed, stored, or transported in digital form. Fast video ADCs are used, for example, in TV tuner cards. Slow on-chip 8, 10, 12, or 16 bit ADCs are common in microcontrollers. Very fast ADCs are needed in digital oscilloscopes, and are crucial for new applications like software defined radio.
Group Members: Ali Asad (1936) Safdar Abbasi (1926)

Embedded Systems – IE403 Lab Work Lab #13 Flow Chart Read_ADC START Init. ADC _Result = 0 Init. LCD display AD_CS = 0 ADC active AD_CS = 1 ADC inactive Pulse AD_CK High and Low Read_ADC Pulse AD_CK High and Low Send reading to LCD Loop 8 times to read 8 bits Read ADC bit AD_DO in ADC_Result END Shift-bit ADC_Result Return ADC_Result Group Members: Ali Asad (1936) Safdar Abbasi (1926) . ADC and uC Pins Init.

//Input lcd_reset().h> #include<lcd.n. sbit AD_CK = P2^1. // Return to beginning of 2nd line of LCD strcpy(temp.h> #include<adc0831. // 5/255 = 51 and 51.0."Reading : %0.0 to process it as a float data lcd_cmd(0x80). AD_CS=0.2fV".h> #include<string. strcpy(temp. //Output AD_CK=0.h> #include<delay.n++) lcd_data(temp[n]). strcpy(temp.c #include<reg51. //Output AD_DO=1. float value=0. for(n=0. void main() { unsigned char temp[40].n<strlen(temp).n++) // Return to beginning of 1st line of LCD // Clear buffer lcd_data(temp[n]). AD_CS=1.n<strlen(temp).0. } } Code 13.""). for(n=0.1: C-listing for ADC main program adc0831. // Before calling read_adc for 1st time AD_CS should be high that is chip select is inactive! while(1) { value=read_adc()/51.h> #include<stdio.Embedded Systems – IE403 Lab Work Lab #13 Source Code main.c Group Members: Ali Asad (1936) Safdar Abbasi (1926) ." Serial ADC0831").h> sbit AD_CS = P2^0.""). //Clear buffer sprintf(temp. lcd_init ().value). lcd_cmd(0xC0). sbit AD_DO = P2^2.

DelayUs(10).i<8.i++) // for 8 bits { //Reading one bit at a time adc_clk().Embedded Systems – IE403 #include<reg51.h> #include<delay. } int read_adc() { int ADC_result=0. unsigned char i.h> sbit AD_CS = P2^0.2: C-listing for ADC0831 Read Routine //Deselect chip. Group Members: Ali Asad (1936) Safdar Abbasi (1926) . adc_clk(). return ADC_result. AD_CK=0. sbit AD_DO = P2^2. } Code 13. DelayUs(10). AD_CS=0.) //Cycle Clock for (i=0. sbit AD_CK = P2^1. ADC_result=(ADC_result << 1) | AD_DO. void adc_clk() { AD_CK=1. //Shift left operation because MSB goes first! } AD_CS=1. Lab Work Lab #13 //Chip Select //Clock //Data Out (serially) //Select chip (initiate conversion.

h> /* LCD Connections to 8051 D4 .P1.7 EN . lcd_port = (cmd & 0xF0).3 RS .5 D6 .c #include<reg8252. lcd_port = 0x30+LCD_EN. lcd_port = 0x20.6 D7 . lcd_port = 0x20+LCD_EN. } void lcd_cmd (char cmd) { //Send high-nibble lcd_port = (cmd & 0xF0)|LCD_EN. lcd_port = 0x30.P1. DelayMs(1). lcd_port = 0x30+LCD_EN.P1.P1.h> #include<delay. Group Members: Ali Asad (1936) Safdar Abbasi (1926) . DelayMs(10). lcd_port = 0x30.Embedded Systems – IE403 lcd. //Send low-nibble lcd_port = ((cmd << 4) & 0xF0)|LCD_EN. DelayMs(1).P1.1 Lab Work Lab #13 */ //The pins used are same as explained earlier #define lcd_port P1 //LCD Registers addresses #define LCD_EN 0x08 #define LCD_RS 0x04 void lcd_reset() { DelayMs(20).P1. lcd_port = 0x30+LCD_EN.P1. DelayMs(1).4 D5 .h> #include<string.2 RW . lcd_port = 0x30. lcd_port = ((cmd << 4) & 0xF0).

lcd_cmd(0x28). lcd_cmd(0x80). Display no cursor .Embedded Systems – IE403 Lab Work Lab #13 DelayUs(20). lcd_port = ((dat & 0xF0)|LCD_RS). Automatic Increment . //Send low-nibble lcd_port = (((dat << 4) & 0xF0)|LCD_EN|LCD_RS).no blink. lcd_port = (((dat << 4) & 0xF0)|LCD_RS). } Code 13. lcd_cmd(0x06). DelayUs(20). //DelayUs(200).3: C-listing for LCD 4-bit mode Routine Group Members: Ali Asad (1936) Safdar Abbasi (1926) . lcd_cmd(0x0C). } void lcd_init () { lcd_reset(). // DelayUs(200). void lcd_data (unsigned char dat) { //Send high-nibble lcd_port = ((dat & 0xF0)|LCD_EN|LCD_RS). Address DDRAM with 0 offset 80h.2 line . } // // // // // Call LCD reset 4-bit mode .No Display shift.5x7 font.

Sign up to vote on this title
UsefulNot useful

Master Your Semester with Scribd & The New York Times

Special offer for students: Only $4.99/month.

Master Your Semester with a Special Offer from Scribd & The New York Times

Cancel anytime.