Вы находитесь на странице: 1из 4

IR sensor & ADC

SUMMER TRAINING PROGRAM 2009


Lab Handout - III

IR Sensor & ADC


PART I : IR Sensor & ADC Using WinAVR 1. INTERFACING IR SENSOR (without ADC)
In this exercise well interface IR sensor directly with the MCU and print the value on LCD. MCU being a digital device will sense the o/p of sensor simply as 0 or 1. Let us connect the IR sensor at Pin 0 or port A. Code : #include <avr/io.h> #include "lcd_lib.h" #include <util/delay.h> #include <stdio.h> void main ( ) { int x; char a[10]; DDRA = 0x00; LCDinit(); LCDclr( ); while (1) { x = ( PINA & 0b00000001); sprintf(a, Value = %d ,x); LCDclr(); LCDstring(a,8); _delay_ms(300); } }

// All pins of Port A configured as input

// Bitwise ANDing with 0b00000001 gives only bit 0 of PINA

BRiCS

Process : Create a new folder. Copy lcd_lib.h & lcd_lib.c from CD-ROM>Sample Code>LCD to this new Folder. Open MFile Generator and save the MakeFilefile to your new folder. Open MakeFile with programmers notepad and make the 5 changes in Makefile as told earlier. Source list will be SRC= $(TARGET).c lcd_lib.c Open a new file in programmers notepad and save it in your folder as main.c Write the above code in main.c Compile the code Tools >Make All. If successful, transfer it to your kit Tools > Program Connect LCD and IR sensor on the kit. LCD displays the o/p of sensor as 0 or 1. Move the sensor over black and white surfaces and see the change.

IR sensor & ADC

2. IR Sensor with ADC Now we are going to connect IR sensors through ADC. Thus the analog O/p of IR Sensor is converted to a digital value in the range 0-1023. The digital value will change linearly as the sensor is moved away from the surface or put on different colors. There is no need to declare port A as input because when ADC is enabled, MCU automatically configures that pin of port A as input
Code : #include <avr/io.h> #include lcd_lib.h #include <util/delay.h> #include adc_lib.h #include<stdio.h> void main ( ) { int x; char a[15]; LCDinit(); LCDclr( ); ADCinit(10); while (1) { x = read_adc(0); sprintf(a,Value = %d LCDclr(); LCDstring(a,10); _delay_ms(300); } }

// Initializes ADC in 10 bit mode // returns the digital value of the analog i/p connected to pin 0 of port A ,x);

Process : Same as above with following two changes.


Also copy adc_lib.h and adc_lib.c to your folder just as you copied lcd_lib.h and lcd_lib.c Source list in MakeFile becomes SRC= $(TARGET).c lcd_lib.c adc_lib.c

3. Making a Digital voltmeter A voltmeter is required to print the value of the voltage and not the digital value corresponding to it. Therefore we need to get back the value of applied voltage from the digital o/p using the fundamental equation of ADC. The final equation to convert ADC o/p x to analog voltage is given below. V = (x * Vref / 1023); ask ur mentor to get Multimeter and measure the exact value of Vref on pin 32
Code : #include <avr/io.h> #include lcd_lib.h #include <util/delay.h> #include adc_lib.h #include <stdio.h> void main ( ) { int x;

BRiCS

IR sensor & ADC float v; char a[15]; LCDinit(); LCDclr( ); ADCinit(10); while (1) { x = read_adc(0); v = (x*5.0 / 512) ; sprintf(a,Voltage = %f LCDclr(); LCDstring(a,15); _delay_ms(300); } }

// Initializes ADC in 10 bit mode // returns the digital value of the voltage at pin 0 // substitute the 5.0 with exact value of Vref measured above ,v);

Process :
To you floating point numbers u need to generate the make file with some modification. Open mFile generator (Programs > WinAVR>mFile). Goto MakeFile > printf () options. Select floating point. Save this file in your New Folder. File > Save As. Rest steps same as before .Same as above program 2. After transferring the program, connect the given batterys +ve to pin 0 and ve to GND of the ADC connector on board.

PART II : IR Sensor & ADC Using CVAVR 4. IR Sensor with ADC


This code senses the IR sensor, converts its value to digital using ADC and prints on LCD. Depending upon the digital value it decides whether the sensor is over black or white surface. As told in lecture, white surface reflect more light hence the sensor o/p is a low voltage which gives a low digital value and vice-versa for black. Thus the digital value is compared with 512 (mid value) and the color is decided. Open CVAVR. Click on File>>New It will ask for option of File or Project. Select Project and click OK A window opens, prompting You are about to create a new project. Do you want to use CodeWizardAVR. Select yes. In Code Wizard. Go to Tab Chip. Select your chip as Atmega16 and frequency 4.000000MHz. Goto ADC Tab and do the following settings. Goto LCD tab. Select PORTC as LCD port and 16 as Chars/Lines. Click on File>>Generate Save & Exit. It will ask for location and file name (3 times). Save these three files in desired folder. (It is recommended to use the same name all the three times)

A C file with some pre-written code opens. To use delay in your code you need to include delay.h. Write #include <delay.h>

BRiCS

IR sensor & ADC Code : // this line is automatically generated by CVAVR #include <mega16.h> #include <lcd.h> // this line is automatically generated by CVAVR #include <delay.h> #include <stdio.h> // some code generated by CVAVR comes here void main() { int x; char a[15]; // some code generated by CVAVR comes here lcd_init(16); // this is automatically generated by CVAVR while(1) { x=read_adc(0); sprintf(a,value =%d ,x); lcd_clear(); lcd_puts(a); lcd_gotoxy(0,1); if (x<512) lcd_putsf(Color : White); else lcd_putsf(Color : Black); } } Write the code in main method. When you are finished with writing the code, compile it by clicking Projects>>Compile or press F9 Errors, if any, are displayed in the message window at bottom and navigator window at the left. 4 After correcting the errors, re-compile and then make the code- Projects>>Make or press SHIFT + F9 Now you will have your hex file in your project folder. Connect your kit and transfer the code using AVRDude GUI. Connect the IR sensor and LCD and see the o/p.

Compiled by Md Umair md.umair.dhn@gmail.com 9005850443

BRiCS

Вам также может понравиться