Académique Documents
Professionnel Documents
Culture Documents
Lab Work
Lab #13
Objective
To read an analog signal in microcontroller using an Analog-to-Digital Converter
Theory
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)
Lab Work
Lab #13
Flow Chart
Read_ADC
START
Return ADC_Result
Group Members:
Lab Work
Lab #13
Source Code
main.c
#include<reg51.h> #include<stdio.h> #include<string.h> #include<adc0831.h> #include<delay.h> #include<lcd.h> sbit AD_CS = P2^0; sbit AD_CK = P2^1; sbit AD_DO = P2^2; void main() { unsigned char temp[40],n; float value=0.0; AD_CS=0; //Output AD_CK=0; //Output AD_DO=1; //Input lcd_reset(); lcd_init (); AD_CS=1; // Before calling read_adc for 1st time AD_CS should be high that is chip select is inactive! while(1) { value=read_adc()/51.0; // 5/255 = 51 and 51.0 to process it as a float data lcd_cmd(0x80); strcpy(temp,""); strcpy(temp," Serial ADC0831"); for(n=0;n<strlen(temp);n++) // Return to beginning of 1st line of LCD // Clear buffer lcd_data(temp[n]); lcd_cmd(0xC0); // Return to beginning of 2nd line of LCD strcpy(temp,""); //Clear buffer sprintf(temp,"Reading : %0.2fV",value); for(n=0;n<strlen(temp);n++) lcd_data(temp[n]); } } Code 13.1: C-listing for ADC main program
adc0831.c
Group Members: Ali Asad (1936) Safdar Abbasi (1926)
Lab Work
Lab #13
for (i=0;i<8;i++) // for 8 bits { //Reading one bit at a time adc_clk(); ADC_result=(ADC_result << 1) | AD_DO; //Shift left operation because MSB goes first! } AD_CS=1; return ADC_result; } Code 13.2: C-listing for ADC0831 Read Routine //Deselect chip.
Group Members:
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); lcd_port = 0x30+LCD_EN; lcd_port = 0x30; DelayMs(10); lcd_port = 0x30+LCD_EN; lcd_port = 0x30; DelayMs(1); lcd_port = 0x30+LCD_EN; lcd_port = 0x30; DelayMs(1); lcd_port = 0x20+LCD_EN; lcd_port = 0x20; DelayMs(1); } void lcd_cmd (char cmd) { //Send high-nibble lcd_port = (cmd & 0xF0)|LCD_EN; lcd_port = (cmd & 0xF0); //Send low-nibble lcd_port = ((cmd << 4) & 0xF0)|LCD_EN; lcd_port = ((cmd << 4) & 0xF0);
Group Members:
Lab Work
Lab #13
// // // // //
Call LCD reset 4-bit mode - 2 line - 5x7 font. Display no cursor - no blink. Automatic Increment - No Display shift. Address DDRAM with 0 offset 80h.
void lcd_data (unsigned char dat) { //Send high-nibble lcd_port = ((dat & 0xF0)|LCD_EN|LCD_RS); lcd_port = ((dat & 0xF0)|LCD_RS); //Send low-nibble lcd_port = (((dat << 4) & 0xF0)|LCD_EN|LCD_RS); lcd_port = (((dat << 4) & 0xF0)|LCD_RS);
DelayUs(20); // DelayUs(200); } Code 13.3: C-listing for LCD 4-bit mode Routine
Group Members: