Professional Documents
Culture Documents
14.1.2010
101
Embedded C-Programming
14.1.2010
Data transfer with embedded devices Data transfer between microcontroller and peripherals
Most of the time embedded system controls actions in its environment with the help of different sensors (temperature sensor, pressure sensor, surface height sensor etc.) and reacts immediately to the changes in the environment with actuators (electric motors, relays, warning lights and buzzer etc.) All the changes in the environment causes a data transfer event
http://en.wikipedia.org/wiki/Data_transfer
102
Embedded C-Programming
14.1.2010
Data transfer with embedded devices When microcontroller exchanges information with peripherals we should take count e.g.
To what device the information is transferred or from where we read it Is the peripheral ready to data transfer, and if not, how do we get it to that mode? The features of the peripheral determines what protocol, transfer speed, media and bus we use (serial/parallel, USB, RS232, SPI, Bluetooth, CAN, etc.) What is the service order, priority, if several 103 pheripherals need service simultaneously
Embedded C-Programming
14.1.2010
Data transfer with embedded devices Data can be transferred between microcontroller and peripheral only when both of them are ready to do so
If one of them is not ready to receive data for some reason, it is useless to send it Sometimes the data must be transferred just in the right moment Sometimes data must be sent immediately (alarm, dangerous errors)
104
Embedded C-Programming
14.1.2010
Polling
Simple interface Slow, and uses a lot of computer time
Interrupts
More complex structure Fast, does not waste computer time
http://en.wikipedia.org/wiki/Interrupt http://members.shaw.ca/climber/avrinterrupts.html
105
Embedded C-Programming
14.1.2010
Embedded C-Programming
14.1.2010
Embedded C-Programming
14.1.2010
108
Embedded C-Programming
14.1.2010
Microwire
http://en.wikipedia.org/wiki/Microwire
109
Embedded C-Programming
14.1.2010
Data transfer with embedded devices Protocols in AVR (between computer systems)
RS232, asynchronous serial interface
http://www.open-research.org.uk/ARMuC/RS232.html
RS485, transceiver
http://en.wikipedia.org/wiki/EIA-485
110
Embedded C-Programming
14.1.2010
Serial communication
In serial form we need only two wires Serial form comes with the cost of speed 1/8 + parallel/serial and serial/parallel transforms We also need to tell the receiver when the information starts and how many bits we will transfer and speed
Some or these can be preset
We also need error detection and parity bits and some 111 intelligence to handle these
Embedded C-Programming
14.1.2010
RS232 is the most typical form of serial data transfer in PCs (to the peripherals)
Min. three wires (outgoing, incoming and ground) altho the connector has 9 pins (peripheral can use these) Data transfer speeds are standardized to be 2n1200 bits per second
112
Embedded C-Programming
14.1.2010
Each ASCII character transferrer trough RS232 has start and stop bits, so the every character is synchronized individually Usually the legth of data is 8N1 meaning that one character needs 8 bits + parity bit
113
Embedded C-Programming
14.1.2010
114
Embedded C-Programming
14.1.2010
Embedded C-Programming
14.1.2010
Data transfer with embedded devices Using protocol stack, protocol that transfers information is relatively easy with C-language and AVR
Basically, you just include the libraries, that contains the functions needed to operate certain protocol, to your project and you will get the access to those command needed Programming is then relatively easy, see AVR USART Explained for more info
http://winavr.scienceprog.com/avr-gcc-tutorial/avr-usart-explained.html
More info
http://pierrick.calvet.free.fr/Usart.php
116
Embedded C-Programming
14.1.2010
Data transfer with embedded devices Eg. Sending byte of information through USART
We write USART_Sendbyte function that we call when we want to send a byte, 8-bit of information (recalled for every byte)
//called with unsigned byte (8-bit character) void USART_SendByte(uint8_t Data) { // can also be called after UDRE interrupt // Every time we check if the previous USART byte have been transmitted // new byte will be transmitted after previous is sent // UCSRA is status register attached to USART, UDRE is (USART Data Register Empty) bit // UDRE is 6th bit of that register, it raises up when UDR is ready to receive next byte // UDR will contain a byte until we have read it, after that it can receive new one while((UCSRA&(1<<UDRE)) == 0); // Transmit data, simply write the data into UDR register, system will transmit it from there UDR = Data; 117 }
Embedded C-Programming
14.1.2010
Data transfer with embedded devices USART receiving is practically as easy, except that we usually wait interrupt from USART before calling the USART_receive function
uint8_t USART_vReceiveByte() // function is called after interrupt from USART received { // interrupt vector is USART_RXC_vect // We will wait until a whole byte has been received (all 8-bits) // RXC (USART Receive Complete) is the 8th bit of the UCSRA register, it will raise up when // new character comes to UDR, will generate RXC interrupt // charcter will remain in UDR register until we read it while((UCSRA&(1<<RXC)) == 0) ; // Return received data, simply by returning the data that is now in the UDR register return UDR; } See, http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&t=48188 for more info
118