Robotics Research Laboratory Louisiana State University
UART – (Universal Asynchronous Receiver/Transmitter) ◦ Minimum required connection (RX,TX, and Ground) RX – Receiver (yellow) TX – Transmitter(green) 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: for ttyACM for Bluetooth connection
Config.h ◦ Set : #define CFG_USE_UART0 1 Hardware.h ◦ Set : #define UART0_BAUD_RATE57600 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
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 }
Some tasks need to be executed independently from main process Some tasks need to be executed with specific timing ◦ ADC conversion, UART, PWM, etc Some tasks need to be executed from arbitrary input signals ◦ external interrupts Internal / External Interrupts
Internal Interrupt ◦ Timer interrupt ◦ Counter interrupt ◦ ADC, SPM READY(Store Program Memory Ready),EE Ready External Interrupt INT0 = SCL [PD0] INT1 = SDA [PD1] INT2 = RXD1 [PD2] INT3 = TXD1 [PD3] INT4 = OC3B [PE4] INT5 = OC3C [PE5] INT6= T3[PE6] INT7= ICP3 [PE7] RESET= RESET [RESET] SPI, UART, etc
Main program Interrupt service routine Interrupt occur Interrupt Register Bit check Interrupt Trigger check Save Main program PC at stack Jump to Triggered Interrupt Vector Execute Interrupt Service Routine Load PC from Stack then, return to Main program
1) Falling edge2) Rising edge 3) Low level High: + 4 V Low: 0.9 V
ISCn1ISCn0Trigger Signal 00The low level of INTn 01Reserved 10The falling edge of INTn 11The rising edge of INTn // Grab the rising edge. EICRB |= (( 1 << ISC71 )|( 1 << ISC70 )|( 1 << ISC61 )|( 1 << ISC60 )); // External interrupt control register B EIFR = (( 1 << INTF7 ) | ( 1 << INTF6 )); // External interrup flag register EIMSK |= (( 1 << INT7 ) | ( 1 << INT6 )); // External interrup mask register DDRE &= ~(( 1 << 6 ) | ( 1 << 7 )); // PE6 & PE7 set input PORTE |= (( 1 << 6 ) | ( 1 << 7 )); // pullup for input sei(); // set interrupt cli();// unset interrupt
int leftCounter = 0; int prevLeft = 0; int runFlag = 0; long cnt = ; main(1){ // Grab the rising edge. EICRB |= (( 1 << ISC71 )|( 1 << ISC70 )|( 1 << ISC61 )|( 1 << ISC60 )) // External interrupt control register B EIFR = (( 1 << INTF7 ) | ( 1 << INTF6 )); // External interrup flag register EIMSK |= (( 1 << INT7 ) | ( 1 << INT6 )); // External interrup mask register sei(); // set enable interrupt while(1){ if (leftCounter != prevLeft){ prevLeft = leftCounter; PIN(LED,1,SET_TOGGLE); } while (cnt-- > 0){ } cnt = ; } // interrupt service routine for int6 SIGNAL(SIG_INTERRUPT6){ leftCounter++; PIN(LED,0,SET_TOGGLE); } // SIG_INTERRUPT6
Make a counter for the optical switches on wheels Make a clear the counter when ‘c’ message is received from UART Make a report the counter when ‘r’ message is received from UART Make a robot move forward when ‘s’ message is received from UART until the counter value is 90
Interrupts may need to set disable interrupt setting during their job processing ◦ for preventing endless chaining interrupts ◦ for some tasks need no interruption ADC conversion Transmitting and receiving procedure SPI, UART, JTAG, etc cli(): disable interrupt setting sei(): enable interrupt setting
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 int interrupt_counter=0; int a2d_value =0; main(1){ while(1){ cli();// clear interrupt enable set a2d_value = a2d_10(0);// read a2d value sei();// set enable interrupt fprintf(u0,” a2d= %d, counter = “, a2d_value,interrrupt_counter); delay(500); }
It allows interrupt processes to read/write global variables even if main process is holding the variables ◦ Special Function Registers(SFR) are defined by volatile type ◦ If interrupt functions and the main function share a global variable, you should define the variable as volatile type - read/write failure
Consideration for using optical switch on interrupt pins Black Bar IR detector Case 1 Output : 0 Case 2 Output : 1 Case 3 Output : 0 or 1