You are on page 1of 9

Embedded C-Programming

14.1.2010

Lecture 5 12.1. 2010


Programming of embedded devices
Bus-based embedded automation systems How to use and modify the protocol stack in embedded devices?

Data transfer Protocol stack

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

Data transfer with embedded devices


Data in this framework means real data like information and messages from the serial port or single bit information about if some switch have been set or reset The basic methods for inform the need of data transform are (explained earlier)

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

Data transfer with embedded devices Stack


Stack is last in first out type pile of information It is runtime memory where we can store address locations and variable values when we jump in the program, when return we read the variable values back from the stack Stack can also be used for storing e.g. immediate results of some mathematical calculation Stack use requires stack pointer that points to the top of the stack Stack also has some limiting addresses over which the stack cannot grow, and also starting address when empty http://en.wikipedia.org/wiki/Stack_(data_structure)
106

Embedded C-Programming
14.1.2010

Data transfer with embedded devices Protocol stack


Basically in this context means the program (clauses) that processes the protocols
E.g. software programmers talk about loading a stack, which means to load the software required to use a specific set of protocols Other common phrase is binding a stack, which means linking a set of network protocols to a network interface card (NIC), every NIC should have at least one stack attached A set of network protocol layers that work together are sometimes called protocol stack, e.g. The OSI reference model or TCP/IP protocol, that define communication over the internet http://en.wikipedia.org/wiki/Protocol_stack
107

Embedded C-Programming
14.1.2010

Timer/counter T/C Timer is used in microcontrollers e.g. for


Creating a clock Counter Pulse/event length measurement In order to generate sound etc. signals Controlling or measuring the pulse width Sometimes controlling the bit length in self-made communication protocols (between microcontroller and some device)
http://en.wikibooks.org/wiki/Embedded_Systems/Atmel_AVR#Timer.2FCounters http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&t=50106

108

Embedded C-Programming
14.1.2010

Data transfer with embedded devices Protocols in AVR (inside system)


IC2, little bus that uses two lines - clock and data
http://www.open-research.org.uk/ARMuC/I2C.html

SPI, single master bus


http://www.open-research.org.uk/ARMuC/SPI.html

PDC, peripheral data controller


http://www.open-research.org.uk/ARMuC/PDC.html

1-wire, 1, master, multiple slaves


http://www.open-research.org.uk/ARMuC/1-Wire.html

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

USB, Universal Serial Bus


http://www.open-research.org.uk/ARMuC/USB.html

Ethernet, frame based local area network


http://en.wikipedia.org/wiki/Ethernet

CAN, Controller Area Network


http://www.open-research.org.uk/ARMuC/CAN.html

RS485, transceiver
http://en.wikipedia.org/wiki/EIA-485
110

Embedded C-Programming
14.1.2010

Data transfer with embedded devices Parallel communication


If we send data in parallel form (8 bit) we need 8 wires plus ground

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

Data transfer with embedded devices Serial communication


Intelligence comes in the form of
UART, universal asynchronous receiver transmitter or USART, universal asynchronous and synchronous receiver transmitter The latter can also handle the synchronous data transfer

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

Data transfer with embedded devices RS232


Uses +12v and -12v voltage levels AVR uses +5v, so some transducer is needed
AVR may include MAX232 circuit that does this

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

Data transfer with embedded devices UART


Universal Asynchronous Receiver Transmitter Integrated inside AVR microcontroller UART circuit is versatile asynchronous receiver/transmitter that handles serial communication Operates one character per time Check if receiver character is whole Transmitter writes new character to the UART output register after previous character is sent

114

Embedded C-Programming
14.1.2010

Data transfer with embedded devices UART


Connected to several registers
UCR, control register, directing the incoming and outgoing data and interrupts UBRR, baud rate register, defines the data transfer speed USR, register that tells the operation state of the circuit UDR, data register, outgoing and incoming data goes through this register See, Using USART of AVR Microcontrollers for more info how to set parameters (bits) for different registers
http://extremeelectronics.co.in/avr-tutorials/using-the-usart-of-avr-microcontrollers/
115

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

You might also like