Embedded Systems Design

Low Power and Embedded Systems - Workbook 3


Introduction

In this workbook we will make a precise timer, and we will attach a Liquid Crystal Display (LCD) to the microcontroller via an interface board.

Supporting material

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_lcd_schematic.pdf

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

Schematic for LCD interface PCB.

serial_lcd_pcb.pdf

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

Printed Circuit Board layout for LCD interface PCB, for reference only.

lcd_interface_micro.c

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

Microcontroller implementation for the LCD interface PCB, for reference only.

Interrupt Vector Table

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

Table of interrupt vector names for ATMEL AVR series microcontrollers.

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

workbook3.html

Exercise 1 - Precise timing

The _delay_ms() function used in workbook 1 is useful, especially during testing, but it does not take account of time spent servicing interrupts. To get precise timing, it is necessary to use a timer.

The ATMEGA168 has three timers. Timer0 and Timer2 are 8 bit, and Timer1 is 16 bit. Have a look at sections 15 and 16 in the datasheet to get an overview of what the timers can do. The requirement of this exercise is to take our ADC samples at exactly 1 second intervals.

One mode which can be selected for a timer is to count from zero up to a value held in an 8 bit register: OCR0A for Timer0, OCR2A for Timer2, or a 16-bit register, OCR1A for Timer1. When the timer reaches the value held in the compare register it immediately resets the counter to zero, issues an interrupt, and continues counting from zero. This mode is referred to as CTC mode, and is the one we will use in this exercise.

One second is rather a long period with a master clock of 8 MHz, so the 16 bit timer will be the appropriate one to use.

Even so, at full clock rate this would only give a maximum period of 2^16/(8 x 10^6), or approx 8.1 milliseconds. A divider prescaler can be introduced between the 8 MHz master clock and the timer input, which makes it possible to greatly increase the maximum time period achievable by the 16 bit counter. The prescaler divider value P, is selectable in a number of powers of 2, up to a maximum value of 2^16.

As we have seen, with a 16 bit timer, the maximum period we can create is about 8.1 ms. We need to choose a prescaler so the maximum count (2^16) gives a period greater than 1 second. The best match from the prescaler values available (see section 16.11.3) is 256. Dividing by a higher number would reduce the resolution of the timer.

- The period of the 16 bit timer input clock is now 0.125uS * 256 = 32us

- To get a 16 bit timer period of 1s, the 16 bit timer register has to be set to 1/32us = 31250, or in hexadecimal 0x7A12

These can be written as a 16 bit value OCR1A = 0x7A12 or if written as two 8 bit values they must be written high byte OCR1AH first then Low byte OCR1AL.

The main loop will reduce to something like:

    while (1) {
        if (gotadcvalue == 1) {
            USART_transmit_uint8(adch) ;		
            USART_transmit(0x0A) ;		
            USART_transmit(0x0D) ;		
            gotadcvalue = 0 ;
        }	
    }

Exercise 2 connecting an LCD

So far the only output from the microcontroller has been via a serial data connection, or by flashing an LED. In this exercise you will add an interface to an LCD controller and display. There are a wide range of these alphanumeric displays on the market, with different size and backlight options, but nearly all use either a Hitachi or Seiko controller. These controllers are almost identical in operation, but the initialisation is fiddly, requiring a set of commands with minimum periods between them. The LCDs themselves run from a 5V supply,

To speed up development for you and to reduce the number of IO pins required, a small PCB is provided which takes a clock and data signal and does the interfacing to the LCD for you.

There is also a supporting library to make the LCD easier to use.

For your interest, links to the schematic diagram, Printed Circuit Board, and associated C code for the library are available in the supporting material section at the start of the worksheet.

Hardware connections.
Software.

To use the LCD, it is necessary to incorporate a library of low level drivers which take care of communicating via the LCD interface board. For flexibility, this library allows you to connect the LCD via any two lines on the same port of your microcontroller. For this exercise we will use PD6 and PD7 on PORTD.

For code portability the library uses generic names for controlling these pins, and these need to be defined and the definitions made visible to both the library function and the main C program. This is done as follows:

The following section will prove very useful in the future, but can be skipped if you are behind schedule.

Before the class next week, create a display of your crsid which scrolls smoothly and readably in one of the four directions.

Show it to one of the demonstrators at the beginning of next weeks session.

You will need to think about your choice of mini-project, in particular if you want to try something which isn't on the suggestions list then talk to Ian Wassell or Brian Jones as soon as possible, to check that what you propose is suitable, and also that it is possible to get the parts required in time for you to start in week 5.

Copyright Ian Wassell and Brian Jones 2009-2011