exp1

Download exp1

If you can't read please download the document

Upload: rajeev-valunjkar

Post on 09-Sep-2015

222 views

Category:

Documents


3 download

DESCRIPTION

1

TRANSCRIPT

/******************************************************************************** Written by: Vinod Desai, NEX Robotics Pvt. Ltd. Edited by: Sachitanand Malewar, NEX Robotics Pvt. Ltd. AVR Studio Version 4.17, Build 666 Date: 26th December 2010 This experiment demostrates switching on and off of MOSFET switches to turn on and off the active part of the sensors. Connections: PORTG 2 --> Power control of Sharp IR Range sensor 2, 3, 4 and red LEDs of the white line sensorsPORTH 2 --> Power control of Sharp IR Range sensor 1, 5PORTH 3 --> Power control of IR Proximity sensors 1 to 8 Note: 1. Make sure that in the configuration options following settings are done for proper operation of the code Microcontroller: atmega2560 Frequency: 14745600 Optimization: -O0 (For more information read section: Selecting proper optimization options below figure 2.22 in the Software Manual) 2. Make sure that Jumper 2, 3, 4 are open on the main board 3. Logic 1 to any of the pin turns off the power to the corresponding sensor. Logic 0 to any of the above pin turns on the power to the corresponding sensor. 4. To use IR proximity sensor as directional light intensity sensor, turn off IR proximity sensor. It will turn off IR LEDs.*********************************************************************************//******************************************************************************** Copyright (c) 2010, NEX Robotics Pvt. Ltd. -*- c -*- All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived from this software without specific prior written permission. * Source code can be used for academic purpose. For commercial use permission form the author needs to be taken. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. Sotware relased under Creative Commance cc by-nc-sa licence. For legal information refer to: http://creativecommons.org/licenses/by-nc-sa/3.0/legalcode********************************************************************************/#include #include #include unsigned char ADC_Conversion(unsigned char);unsigned char ADC_Value;unsigned char sharp, distance, adc_reading;unsigned int value;//ADC pin configurationvoid adc_pin_config (void){ DDRF = 0x00; //set PORTF direction as input PORTF = 0x00; //set PORTF pins floating DDRK = 0x00; //set PORTK direction as input PORTK = 0x00; //set PORTK pins floating}//Function to Initialize ADCvoid adc_init(){ADCSRA = 0x00;ADCSRB = 0x00; //MUX5 = 0ADMUX = 0x20; //Vref=5V external --- ADLAR=1 --- MUX4:0 = 0000ACSR = 0x80;ADCSRA = 0x86; //ADEN=1 --- ADIE=1 --- ADPS2:0 = 1 1 0}//This Function accepts the Channel Number and returns the corresponding Analog Value unsigned char ADC_Conversion(unsigned char Ch){unsigned char a;if(Ch>7){ADCSRB = 0x08;}Ch = Ch & 0x07; ADMUX= 0x20| Ch; ADCSRA = ADCSRA | 0x40; //Set start conversion bitwhile((ADCSRA&0x10)==0); //Wait for ADC conversion to completea=ADCH;ADCSRA = ADCSRA|0x10; //clear ADIF (ADC Interrupt Flag) by writing 1 to itADCSRB = 0x00;return a;}void print_sensor(char row, char coloumn,unsigned char channel){ADC_Value = ADC_Conversion(channel);}// This Function calculates the actual distance in millimeters(mm) from the input// analog value of Sharp Sensor. unsigned int Sharp_GP2D12_estimation(unsigned char adc_reading){float distance;unsigned int distanceInt;distance = (int)(10.00*(2799.6*(1.00/(pow(adc_reading,1.1546)))));distanceInt = (int)distance;if(distanceInt>800){distanceInt=800;}return distanceInt;} void buzzer_pin_config (void){ DDRC = DDRC | 0x08; //Setting PORTC 3 as output PORTC = PORTC & 0xF7; //Setting PORTC 3 logic low to turnoff buzzer}void buzzer_on (void){ unsigned char port_restore = 0; port_restore = PINC; port_restore = port_restore | 0x08; PORTC = port_restore;}void buzzer_off (void){ unsigned char port_restore = 0; port_restore = PINC; port_restore = port_restore & 0xF7; PORTC = port_restore;}//MOSFET switch port configurationvoid MOSFET_switch_config (void){ DDRH = DDRH | 0x0C; //make PORTH 3 and PORTH 1 pins as output PORTH = PORTH & 0xF3; //set PORTH 3 and PORTH 1 pins to 0 DDRG = DDRG | 0x04; //make PORTG 2 pin as output PORTG = PORTG & 0xFB; //set PORTG 2 pin to 0}//Function to Initialize PORTSvoid port_init(){ MOSFET_switch_config(); buzzer_pin_config(); adc_pin_config();}void turn_on_sharp234_wl (void) //turn on Sharp IR range sensors 2, 3, 4 and white line sensor's red LED{ PORTG = PORTG & 0xFB;}void turn_off_sharp234_wl (void) //turn off Sharp IR range sensors 2, 3, 4 and white line sensor's red LED{ PORTG = PORTG | 0x04;}void turn_on_sharp15 (void) //turn on Sharp IR range sensors 1,5{ PORTH = PORTH & 0xFB;}void turn_off_sharp15 (void) //turn off Sharp IR range sensors 1,5{ PORTH = PORTH | 0x04;}void turn_on_ir_proxi_sensors (void) //turn on IR Proximity sensors{ PORTH = PORTH & 0xF7;}void turn_off_ir_proxi_sensors (void) //turn off IR Proximity sensors{ PORTH = PORTH | 0x08;}void turn_on_all_proxy_sensors (void) // turn on Sharp 2, 3, 4, red LED of the white line sensors // Sharp 1, 5 and IR proximity sensor{ PORTH = PORTH & 0xF3; //set PORTH 3 and PORTH 1 pins to 0 PORTG = PORTG & 0xFB; //set PORTG 2 pin to 0}void turn_off_all_proxy_sensors (void) // turn off Sharp 2, 3, 4, red LED of the white line sensors // Sharp 1, 5 and IR proximity sensor{ PORTH = PORTH | 0x0C; //set PORTH 3 and PORTH 1 pins to 1 PORTG = PORTG | 0x04; //set PORTG 2 pin to 1}void init_devices (void){ cli(); //Clears the global interrupts port_init(); adc_init(); sei(); //Enables the global interrupts}//Main Functionint main(void){init_devices();unsigned int value;turn_on_sharp234_wl();_delay_ms(1500);turn_on_all_proxy_sensors();sharp = ADC_Conversion(11);//Stores the Analog value of front sharp connected to ADC channel 11 into variable "sharp"value = Sharp_GP2D12_estimation(sharp); //Stores Distance calsulated in a variable "value".while(1){if(value