100 likes | 201 Views
CSC-2700 – (3) Introduction to Robotics. Robotics Research Laboratory Louisiana State University. What we learned in last class. Analog to Digital (ADC, A/D or A to D) Converting an analog voltage to a digital value that can be used by a microcontroller.
E N D
CSC-2700 – (3) Introduction to Robotics Robotics Research Laboratory Louisiana State University
What we learned in last class • Analog to Digital (ADC, A/D or A to D) • Converting an analog voltage to a digital value that can be used by a microcontroller. • There are many sources of analog signals to be measured such as light intensity, temperature, distance, position, etc. • ATMega128 ADC has 10 bits resolution (0~1024) • Has 8 channels through a multiplexer • 8 pins on PORTF • Need to set PORTF as input without pull-up • Has own power supply (labeled AVCC) • Allows measuring voltages from 0 to 5 volts with a resolution of 5/1024 volts, or 4.88 mV
IR sensor (line detector) & ADC 0V drop = Xi mA x 0 Ω 5 V S1 = 0 ~ 20000 Ω + Connect to ADC X V drop = Xi mA x S1 Ω 5 V - R2 = 1000 Ω X v+ restV drop = Xi mA x 1000Ω 0 V Resistance value of S1(IR sensor) can be changed by sensing
A2D function uint16_t a2d_10( uint8_t Channel ){ // Select the channel in a manner which leaves REFS0 and REFS1 un touched. ADMUX = ( ADMUX & (( 1 << REFS1 ) | ( 1 << REFS0 ))) | Channel; // Start the conversion ADCSR = ADCSR | ( 1 << ADSC ); // Wait for it to complete while ( ADCSR & ( 1 << ADSC )); return ADC; // ADC defined at avr/iom128.h ( special function register: SFR_IO16) } // a2d_10 /home/csc2700/csc2700/40-ADC-01
Universal Asynchronous Receiver / Transmitter - UART • Translates data between parallel and serial forms • UARTs are commonly used in conjunction with communication standards • ex) EIA RS-232, RS-422 or RS-485 • Character framing • Start bit: logic low • Stop bit : logic high ( 1 or 2 bits) • Parity bit : optional (even or odd) • Important Setting for Serial UART : • Baud Speed , Flow control, Port
Connection configuration for UART • Minimum required connection • RX(yellow),TX(green), and Ground(black) • Our programmer has 2 serial port • ttyACM0 : ISP programming port • ttyACM1 : UART serial port • Wire connection • PE0 Yellow wire • PE1 Green wire • GND Black wire • Open Gtk-term • Set port : /dev/ttyACM1 • Speed : 57600 for ttyACM1 9600 for Bluetooth connection
Send A2D values through UART • Config.h • Set : #define CFG_USE_UART0 1 • Hardware.h • Set : #define UART0_BAUD_RATE 57600 • ADC_test.c • Add : #include "UART.h” • Create file pointer : FILE *u0; // for UART0 • Open u0 • if defined( __AVR_LIBC_VERSION__ ) • u0 = fdevopen( UART0_PutCharStdio, UART0_GetCharStdio ); • #else • u0 = fdevopen( UART0_PutCharStdio, UART0_GetCharStdio, 0 ); • #endif • Send values using fprintf(u0,”your message %d”, variable); /home/csc2700/csc2700/40-ADC-02
Receiving values from UART • Check the UART buffer first • int UART0_IsCharAvailable() • Read a character from UART buffer • int UART0_GetChar() int counter; char tmpChar; While(1){ if ( UART0_IsCharAvailable() ) { tmpChar = UART0_GetChar(); if ( tmpChar == ‘s'){ // start moving }else if ( tmpChar == ‘c'){ // clear counter }else if ( tmpChar == ‘r’){ // report counter number } } }
Let’s make simple UART program • Make a led0 on when ‘0’ message is received from UART • Make a led0 off when button0 is pressed • Make a led1 on when ‘1’ message is received from UART • Make a led1 off when button1 is pressed • Send “!!!good bye!!!” message to UART tx when “bye” message is received from UART rx
Let’s make a motor control program • Make a led0 ON and a motor clockwise spin when ‘4’ message is received from UART • Make a led1 ON and a motor anti-clockwise spin when ‘6’ message is received from UART • Make a led2 ON and speed of the motor increase when ‘8’ message is received from UART • Make a led3 ON and speed of the motor decrease when ‘2’ message is received from UART