Embedded Systems Design

Low Power and Embedded Systems - Workbook 2


Introduction

In this workbook we will cover Serial communications using interrupts and the use of the Analogue to Digital Converter.

You may find that you only have time to complete Exercises 1 and 2 during the session. They are quite difficult, and you may need the help of the demonstrators. Exercise 3 takes time, but is more straightforward. Try to complete exercise 3 before the start of week 3. If you get stuck or there are parts you don't understand, then make some notes so you can ask for help from the demonstrators first thing in week 3.

Supporting material

http://www.atmel.com/dyn/resources/prod_documents/doc8271.pdfhttp://www.cl.cam.ac.uk/teaching/1112/P31/docs/atmega168pa.pdf
atmega168pa.pdf

Latest: http://www.atmel.com/dyn/resources/prod_documents/doc8271.pdf Local copy: http://www.cl.cam.ac.uk/teaching/1112/P31/docs/atmega168pa.pdf

Data sheet for the Atmel ATMEGA168PA used in these exercises. You will need to refer to this frequently. Within these workbooks this will be referred to as 'the datasheet' The section numbers referred to in these workbooks refer to revision 8271D of the datasheet dated 05/11.

Serial Lead

http://www.cl.cam.ac.uk/teaching/1112/P31/docs/usbseriallead.pdf

Specification for the Serial/USB lead used in Exercise 1.

USART_functions.c

http://www.cl.cam.ac.uk/teaching/1112/P31/code/USART_functions.c

Library USART functions for exercise 3.

Interrupt Vector Table

http://www.nongnu.org/avr-libc/user-manual/group__avr__interrupts.html

Table of interrupt vector names for ATMEL AVR series of microcontrollers.

connect_mcp9700.pdf

http://www.cl.cam.ac.uk/teaching/1112/P31/docs/connect_mcp9700.pdf

Circuit diagram for connecting the temperature sensor.

mcp9700 datasheet

Latest: http://ww1.microchip.com/downloads/en/devicedoc/21942a.pdf Local copy: http://www.cl.cam.ac.uk/teaching/1112/P31/docs/mcp9700.pdf

The manufacturers data sheet for the mcp9700 temperature sensor.

MCP1525 datasheet

Latest: http://ww1.microchip.com/downloads/en/devicedoc/21653b.pdf Local copy: http://www.cl.cam.ac.uk/teaching/1112/P31/docs/mcp1525.pdf

The manufacturers data sheet for the MCP1525 voltage reference.

An on-line version of this workbook is available at:

workbook2.html

You should check this page regularly for announcements and errata. You might find it useful to refer to the on-line version of this workbook in order to follow any provided web links or to cut and paste example code.

A note on Register and other names

In the examples which follow, you will see references to register names such as UCSR0A and bit-position names such as RXCIE0.

These names, and more importantly which ones are relevant depend upon the particular Atmel device being used.

They are defined in the library headers included in the program, for example #include <avr/io.h>

The angle brackets <avr/io.h> instruct the compiler to search for the header files in the standard library header location /usr/lib/avr/include and subdirectories, which means the file io.h will be found at /usr/lib/avr/include/avr/io.h

An instruction such as #include "config.h" (note quotes instead of angle brackets) instructs the compiler to search for the file config.h in the current directory and its sub-directories first, then in the standard library header location. A later workbook will explain the use of a configuration file such as config.h in more detail.

Variable definitions

The ATMEGA168 microcontroller is an 8 bit device. Unless you specifically need a 16 bit (or wider) variable, you should define variables as 8 bits wide to save memory space.

The compiler accepts the following definitions, and the workbook examples will usually use the form which specifies the width, eg uint8_t.

  • uint8_t is used to define an unsigned 8 bit variable, identical to char
  • int8_t is used for a signed 8 bit variable, identical to signed char
  • uint16_t is used to define an unsigned 16 bit variable, identical to unsigned int
  • int16_t is used for a signed 16 bit variable, identical to int
  • uint32_t is used to define an unsigned 32 bit variable, identical to unsigned long. Avoid using 32 bit values unless there really is no alternative.
  • int32_t is used for a signed 32 bit variable, identical to long.
  • uint64_t is used to define an unsigned 64 bit variable, identical to unsigned long long. You won't need to use it. The compiler makes a reasonable job of 16 bit values, but 32 bit will cause slow, bloated code. 64 bit is far worse than that.
  • int64_t is used for a signed 64 bit variable, identical to long long.

Exercises

Preparation

The exercises this week follow on directly from Exercise 2 of Workbook1.

Exercise 1 - Hello World via a serial data link

Notation

The ATMEGA168PA microcontroller has a single USART (Universal Synchronous and Asynchronous Receiver and Transmitter). For a full description of USART operation see section 19 of the datasheet.

Older ATMEL devices only ever had 1 USART, and the data sheets and example code referred to 'The USART' and its registers for example UCSRA.

Newer devices in the family may have more than one USART, so for reasons of compatibility these devices, which include the ATMEGA168 refer to the USART by number even if there is only one, so the USART would be referred to as USART0, and the register names also include the number for example UCSR0A.

The Exercise

Transmitting data via a serial data link, either using the RS232 standard or by USB is a common requirement. There are two complicating factors that we will need to consider in this part of the exercise:

  1. Serial communication relies on a reasonably accurate clock for the Baud rate. The internal oscillator would be accurate enough, but we will switch to using an external 8.000 MHz crystal as the clock source, which entails using the Full Swing Crystal Oscillator option for our microcontroller. We derive the Baud rate clock for serial communication from this clock source. We will use a baud rate of 19200 bits per second, which can be derived from an 8MHz clock accurately. At high baud rates, we would choose a crystal such as 7.3728 MHz to give a clock which can be divided more precisely to minimise the clock error. See Section 19.10 of the datasheet for more details.

  2. RS232 uses positive and negative voltage levels, typically +9V and -9 V which will require the use of a voltage level converter. When viewing RS232 traffic with an oscilloscope, note that a 1 is represented by a negative voltage, a 0 by a positive one. Not all PCs have serial ports, and so an alternative is to use the USB interface on the PC, although this requires some additional hardware. In this exercise we will use a converter lead which takes care of the voltage level conversion, and additionally carries out the USB negotiation when the lead is first connected to the host PC.

Microcontroller Clock

Programming the fuses needs to be followed by a make program, a power cycle is not sufficient. You previously copied and renamed workbook1/exercise2/exercise2.c to workbook2/exercise1/exercise1.c You will need to change exercise2 to exercise1 in the Makefile in 4 places. If no change has been made to the program, then make program should cause the LED to flash 8 times as fast as it did in workbook 1.

Once this has been achieved, the first of the two problems has been solved - the device is running from an accurate clock source.

USB connection

As part of the equipment available to you, there is an RS232 to USB converter lead. This takes the form of a cable with USB type A plug on one end and a 6 way socket on the other. It contains a voltage level converter and carries out the USB negotiation with the host.

A datasheet for this device is available from http://www.cl.cam.ac.uk/teaching/1112/P31/docs/usbseriallead.pdf. Section 4.1 gives the connection details.

Also available is a lab made, right angled 6 way adaptor. On this adaptor the thin pins fit into the prototyping board, and the thicker, square pins connect to the USB lead.

Important

Make sure you only fit the small pins in the prototyping board - the thick pins will damage the board, possibly giving you unreliable connections in future.
PC serial software

The important part to note here is the ttyUSB1. The serial device has been recognised and can now be referred to as /dev/ttyUSB1

The programmer and the USB serial device both use the FT232 serial chip, so there is no way to distinguish the two when they are plugged in. If you always plug the programmer in first, and the serial lead second, then the programmer should be /dev/ttyUSB0 and the serial lead /dev/ttyUSB1 Leave them plugged in to the computer, and if you need to unplug them, do so at the development board end, so they retain their ttyUSB port numbers. It is possible sometimes for the serial ports to lock up, and in this case the only solution is to unplug at the computer and replug. If you suspect the serial port is misbehaving, typing dmesg may tell you more.

Microcontroller software

Refer to section 20 of the datasheet, and write a function to initialise the USART. You will need to refer to section 20.11 to set the correct values for baud rate and framing format, i.e. 19200 Baud, with 8 data bits, no parity and 1 stop bit. This format is written in the style 19200,N,8,1. Turn off double speed. You can assign a value to UBRR0 which is a 16 bit register in one go - the compiler will do the correct thing. When calculating the throughput, note that there is also 1 start bit and 1 stop bit, so transmitting a byte takes 10 bit times.

You will need to set the following registers

  	
void USART0_init(void) {
  	
	UBRR0	=							// Set baud rate to 19200
	UCSR0A	=							// Turn off double speed		
	UCSR0B	=							// Enable Receiver and Tranmsitter.
	UCSR0C	=							// Asynchronous mode, 8 data bits, 1 stop bit, clock polarity = 0 

	finally:
	UCSR0A |= (1<<TXC0);		// clear any existing transmits

}
  	

Section 20.6.1 of the datasheet also has a function to transmit a byte, which you can incorporate in your program.

Debugging:

  • The circuit being used does not employ flow control. The microcontroller will transmit data even if there is no device connected, or there is no program on the PC that is listening to the data stream.
  • You can monitor the TxD output of the microcontroller with an oscilloscope. To select a suitable timebase, consider that the Baud rate refers to the number of bits transmitted per second. The microcontroller should be transmitting at 19200 baud, which is roughly 50us per bit.
  • Test for the presence of the 8 MHz clock on PB0
  • Is the LED on your prototyping board flashing at the correct rate of 1Hz ?
  • Is the transmit from the microcontroller correctly connected to receive on the USB serial device ?
  • If you see graphical characters, then it is likely the baud rate is wrong by a factor of 2, or the clock isn't accurately set.

Exercise 2 - Caeser Cipher Loopback

In this exercise you will create a loopback - that is to receive data from the PC, and immediately transmit it back. You will change the data to remove any confusion with the local echo function of the serial terminal program during testing.

The change to be made is to make a Caeser cipher: Add 3 to the characters a-z (mapping x,y,z back to a,b,c) and similarly for A-Z. Leave all others characters unchanged.

However, there is an immediate problem. If our device is in a loop waiting for characters from the serial input, it cannot be doing anything else. This is unsatisfactory, since all other actions are blocked, including flashing the LED.

The solution is to set up an interrupt, so that when a byte is received an appropriate action is taken. In this exercise the byte will be modified and sent back to the PC. There is a problem with this approach, which will be addressed in a later exercise, but for the simple case in this exercise the problem can be ignored.

There are three steps to setting up the interrupt:

  1. The creation of a chunk of code called an Interrupt Service Routine (ISR) which will be called when the interrupt occurs. The C compiler has a set of names defined in interrupt.h for these ISRs. There is a list of these ISR names avr_interrupts

    The compiler takes care of the housekeeping part of the interrupt call, e.g. saving hardware register contents and return addresses.

    As an example, the Interrupt Service Routine run when the USART receives a complete byte is coded as:

    ISR(USART_RX_vect) {
    
    // TODO add code to process the byte received in UDR0
    
    }
    

    Note: The naming isn't entirely consistent - you might expect the ISR name to be USART0_RX_Vect, and it is for later devices in the family.

  2. Changing the UCSR0B register so that an interrupt is generated when the correct condition occurs - in this case a byte is received on the serial input.

  3. Clearing any conditions which would cause a spurious interrupt (in this case clear any received data) then Enable interrupts (they are disabled at power up)

Note when testing:

In minicom, use <ctrl>A E to turn local echo on or off.

If you type hello, you should see khoor in minicom (echo off) or hkehloloor (echo on).

Also note that the LED flashing is carried out by the main() portion of the code, and the serial receive and transmit is carried out entirely in the interrupt code. The 1Hz flashing will now be less accurate because the _delay_ms() functions take no account of the time spent in the interrupt routine. This will be addressed in a later exercise.

Exercise 3 - Reading a value from the Analogue to Digital converter

The objective of this exercise is to connect an MCP9700 temperature sensor to one of the analogue inputs on the ATMEGA168, read the resulting analogue voltage using the built in Analogue to Digital Converter, and transmit the result as a decimal number using the serial interface once every second.

This builds upon the code already written for Exercise 2.

You will need to refer to section 24 of the datasheet for details of the Analogue to Digital converter (ADC). The microcontroller has one 10 bit ADC, which can be programmed to select its input from one of eight IO pins. The 10 bit result can be presented left adjusted or right adjusted in two bytes.

The ADC inputs share pins with IO ports. It is necessary to define those pins as inputs. It is also possible to reduce power consumption of the device by powering off the circuitry within the device associated with the digital inputs.

The ADC requires a clock in the frequency range 0-200kHz to function. This is derived from the master CPU clock, currently 8MHz (called clkio).

The ADC also requires a reference voltage, which must be higher than the highest input voltage to be applied. The ADC reference voltage can be derived internally or externally supplied. For this exercise we will use an external voltage reference IC, the MCP1525 to generate our reference voltage.

To make the code implementation easier, we will use an interrupt generated when the ADC conversion is complete.

Listing for USART_transmit and USART_transmit_uint8


// Transmit an 8 bit value as a single character via serial

void USART_transmit( unsigned char data ) {

	while ( !( UCSR0A & (1<<UDRE0)) )				// Wait for empty transmit buffer
    ;
	UCSR0A |= (1<<TXC0); 							// clear txc flag
	UDR0 = data;									// Put data into buffer, sends the data
}



// Transmit an unsigned 8 bit value via serial as a series of 
//   up to three decimal characters, eg 0xFF sent as 255

void USART_transmit_uint8(uint8_t val) {
	unsigned char buf[3];
	int8_t ptr;
	for(ptr=0;ptr<3;++ptr) {
		buf[ptr] = (val % 10) + '0';
		val /= 10;
	}
	for(ptr=2;ptr>0;--ptr) {
		if (buf[ptr] != '0') break;
	}
	for(;ptr>=0;--ptr) {
		USART_transmit(buf[ptr]);
	}
}

Exercise 3 - Debugging

Things to consider when testing

  1. Is the LED flashing? If not consider whether the program ever gets that far, or whether the program is executing an endless loop in an interrupt service routine.

  2. The loopback code is still in place, so pressing a key on the terminal should immediately echo it back (check that it isn't local echo in Minicom).

  3. If the result is always zero, does the mcp9700 have a positive supply ? You can check the output voltage with a scope or multimeter.

    If the result is always high, does the voltage reference have a suitable supply, and is the ARef input at the correct voltage. ?

    Are you reading the correct input ?

    Holding the temperature sensor should make the reading rise.

Reading for next week

The next workbook will use the timers in the microcontroller. Read through sections 15 and 16 of the datasheet to get an overview of the timer capabilities. Don't worry about the detail at this stage - workbook 3 will cover it.

Copyright Ian Wassell and Brian Jones 2009-2011