AVR064: A Temperature Monitoring System with LCD Output. 8-bit Microcontrollers. Application Note. Features. 1 Introduction



Similar documents
AVR317: Using the Master SPI Mode of the USART module. 8-bit Microcontrollers. Application Note. Features. Introduction

General Porting Considerations. Memory EEPROM XRAM

AVR319: Using the USI module for SPI communication. 8-bit Microcontrollers. Application Note. Features. Introduction

AVR134: Real Time Clock (RTC) using the Asynchronous Timer. 8-bit Microcontrollers. Application Note. Features. 1 Introduction

AVR305: Half Duplex Compact Software UART. 8-bit Microcontrollers. Application Note. Features. 1 Introduction

AT89C5131A Starter Kit... Software User Guide

8-bit Microcontroller. Application Note. AVR400: Low Cost A/D Converter

AVR106: C functions for reading and writing to Flash memory. 8-bit Microcontrollers. Application Note. Features. Introduction

Application Note. C51 Bootloaders. C51 General Information about Bootloader and In System Programming. Overview. Abreviations

8-bit Microcontroller. Application Note. AVR105: Power Efficient High Endurance Parameter Storage in Flash Memory

AVR030: Getting Started with IAR Embedded Workbench for Atmel AVR. 8-bit Microcontrollers. Application Note. Features.

8-bit Microcontroller. Application Note. AVR415: RC5 IR Remote Control Transmitter. Features. Introduction. Figure 1.

8-bit RISC Microcontroller. Application Note. AVR182: Zero Cross Detector

8-bit Microcontroller. Application Note. AVR222: 8-point Moving Average Filter

AVR245: Code Lock with 4x4 Keypad and I2C LCD. 8-bit Microcontrollers. Application Note. Features. 1 Introduction

8-bit Microcontroller. Application Note. AVR314: DTMF Generator

8-bit RISC Microcontroller. Application Note. AVR236: CRC Check of Program Memory

AVR241: Direct driving of LCD display using general IO. 8-bit Microcontrollers. Application Note. Features. Introduction AVR

AT91 ARM Thumb Microcontrollers. AT91SAM CAN Bootloader. AT91SAM CAN Bootloader User Notes. 1. Description. 2. Key Features

AT91 ARM Thumb Microcontrollers. Application Note. Interfacing a PC Card to an AT91RM9200-DK. Introduction. Hardware Interface

8-bit Microcontroller. Application Note. AVR134: Real-Time Clock (RTC) using the Asynchronous Timer. Features. Theory of Operation.

How to Calculate the Capacitor of the Reset Input of a C51 Microcontroller 80C51. Application Note. Microcontrollers. Introduction

APPLICATION NOTE. Atmel AVR134: Real Time Clock (RTC) Using the Asynchronous Timer. Atmel AVR 8-bit Microcontroller. Introduction.

AVR033: Getting Started with the CodeVisionAVR C Compiler. 8-bit Microcontrollers. Application Note. Features. 1 Introduction

Quick Start Guide. CAN Microcontrollers. ATADAPCAN01 - STK501 CAN Extension. Requirements

Application Note. 8-bit Microcontrollers. AVR270: USB Mouse Demonstration

AVR442: PC Fan Control using ATtiny13. 8-bit Microcontrollers. Application Note. Features. 1 Introduction

AVR32701: AVR32AP7 USB Performance. 32-bit Microcontrollers. Application Note. Features. 1 Introduction

8-bit Microcontroller. Application Note. AVR461: Quick Start Guide for the Embedded Internet Toolkit. Introduction. System Requirements

8-bit. Application Note. Microcontrollers. AVR282: USB Firmware Upgrade for AT90USB

AVR1309: Using the XMEGA SPI. 8-bit Microcontrollers. Application Note. Features. 1 Introduction SCK MOSI MISO SS

APPLICATION NOTE Atmel AT02509: In House Unit with Bluetooth Low Energy Module Hardware User Guide 8-bit Atmel Microcontroller Features Description

AVR1900: Getting started with ATxmega128A1 on STK bit Microcontrollers. Application Note. 1 Introduction

8-bit RISC Microcontroller. Application Note. AVR155: Accessing an I 2 C LCD Display using the AVR 2-wire Serial Interface

AVR353: Voltage Reference Calibration and Voltage ADC Usage. 8-bit Microcontrollers. Application Note. Features. 1 Introduction

AVR32110: Using the AVR32 Timer/Counter. 32-bit Microcontrollers. Application Note. Features. 1 Introduction

AVR120: Characterization and Calibration of the ADC on an AVR. 8-bit Microcontrollers. Application Note. Features. Introduction

AVR32138: How to optimize the ADC usage on AT32UC3A0/1, AT32UC3A3 and AT32UC3B0/1 series. 32-bit Microcontrollers. Application Note.

AVR1510: Xplain training - XMEGA USART. 8-bit Microcontrollers. Application Note. Prerequisites. 1 Introduction

AVR115: Data Logging with Atmel File System on ATmega32U4. Microcontrollers. Application Note. 1 Introduction. Atmel

3-output Laser Driver for HD-DVD/ Blu-ray/DVD/ CD-ROM ATR0885. Preliminary. Summary

8-bit Microcontroller. Application Note. AVR201: Using the AVR Hardware Multiplier

AVR32100: Using the AVR32 USART. 32-bit Microcontrollers. Application Note. Features. 1 Introduction

Application Note. 8-bit Microcontrollers. AVR280: USB Host CDC Demonstration. 1. Introduction

Tag Tuning/RFID. Application Note. Tag Tuning. Introduction. Antenna Equivalent Circuit

AT86RF230 (2450 MHz band) Radio Transceiver... User Guide

AVR1301: Using the XMEGA DAC. 8-bit Microcontrollers. Application Note. Features. 1 Introduction

AVR034: Mixing C and Assembly Code with IAR Embedded Workbench for AVR. 8-bit Microcontroller. Application Note. Features.

AVR1321: Using the Atmel AVR XMEGA 32-bit Real Time Counter and Battery Backup System. 8-bit Microcontrollers. Application Note.

Using CryptoMemory in Full I 2 C Compliant Mode. Using CryptoMemory in Full I 2 C Compliant Mode AT88SC0104CA AT88SC0204CA AT88SC0404CA AT88SC0808CA

AVR1318: Using the XMEGA built-in AES accelerator. 8-bit Microcontrollers. Application Note. Features. 1 Introduction

AVR1600: Using the XMEGA Quadrature Decoder. 8-bit Microcontrollers. Application Note. Features. 1 Introduction. Sensors

AVR1922: Xplain Board Controller Firmware. 8-bit Microcontrollers. Application Note. Features. 1 Introduction

Atmel AVR4903: ASF - USB Device HID Mouse Application. Atmel Microcontrollers. Application Note. Features. 1 Introduction

AVR055: Using a 32kHz XTAL for run-time calibration of the internal RC. 8-bit Microcontrollers. Application Note. Features.

How To Use An Atmel Atmel Avr32848 Demo For Android (32Bit) With A Microcontroller (32B) And An Android Accessory (32D) On A Microcontroller (32Gb) On An Android Phone Or

8-bit Microcontroller. Application. Note. AVR204: BCD Arithmetics. Features. Introduction. 16-bit Binary to 5-digit BCD Conversion bin2bcd16

USER GUIDE EDBG. Description

Application Note. 8-bit Microcontrollers. AVR091: Replacing AT90S2313 by ATtiny2313. Features. Introduction

AT15007: Differences between ATmega328/P and ATmega328PB. Introduction. Features. Atmel AVR 8-bit Microcontrollers APPLICATION NOTE

Application Note. 8-bit Microcontrollers. AVR272: USB CDC Demonstration UART to USB Bridge

AVR151: Setup and Use of the SPI. Introduction. Features. Atmel AVR 8-bit Microcontroller APPLICATION NOTE

Table of Contents. Section 1 Introduction Section 2 Getting Started Section 3 Hardware Description

Application Note. USB Mass Storage Device Implementation. USB Microcontrollers. References. Abbreviations. Supported Controllers

32-bit AVR UC3 Microcontrollers. 32-bit AtmelAVR Application Note. AVR32769: How to Compile the standalone AVR32 Software Framework in AVR32 Studio V2

How To Prevent Power Supply Corruption On An 8Bit Microcontroller From Overheating

AVR32788: AVR 32 How to use the SSC in I2S mode. 32-bit Microcontrollers. Application Note. Features. 1 Introduction

Atmel AVR4921: ASF - USB Device Stack Differences between ASF V1 and V2. 8-bit Atmel Microcontrollers. Application Note. Features.

AVR068: STK500 Communication Protocol. 8-bit Microcontrollers. Application Note. Features. 1 Introduction

AVR125: ADC of tinyavr in Single Ended Mode. 8-bit Microcontrollers. Application Note. Features. 1 Introduction

AVR131: Using the AVR s High-speed PWM. Introduction. Features. AVR 8-bit Microcontrollers APPLICATION NOTE

Atmel AVR4920: ASF - USB Device Stack - Compliance and Performance Figures. Atmel Microcontrollers. Application Note. Features.

ET-BASE AVR ATmega64/128

3-output Laser Driver for HD-DVD/ Blu-ray/DVD/ CD-ROM ATR0885. Preliminary. Summary. Features. Applications. 1. Description

APPLICATION NOTE. Atmel AVR443: Sensor-based Control of Three Phase Brushless DC Motor. Atmel AVR 8-bit Microcontrollers. Features.

USER GUIDE. ZigBit USB Stick User Guide. Introduction

USB Test Environment ATUSBTEST- SS7400. Summary

AVR444: Sensorless control of 3-phase brushless DC motors. 8-bit Microcontrollers. Application Note. Features. 1 Introduction

AVR287: USB Host HID and Mass Storage Demonstration. 8-bit Microcontrollers. Application Note. Features. 1 Introduction

AVR2006: Design and characterization of the Radio Controller Board's 2.4GHz PCB Antenna. Application Note. Features.

USB 2.0 Full-Speed Host/Function Processor AT43USB370. Summary. Features. Overview

DIP Top View VCC A16 A15 A12 A7 A6 A5 A4 A3 A2 A1 A0 I/O0 I/O1 I/O2 GND A17 A14 A13 A8 A9 A11 A10 I/O7 I/O6 I/O5 I/O4 I/O3. PLCC Top View VCC A17

AN3265 Application note

Application Note. Migrating from RS-232 to USB Bridge Specification USB Microcontrollers. Doc Control. References. Abbreviations

8-bit RISC Microcontroller. Application Note. AVR910: In-System Programming

AVR318: Dallas 1-Wire master. 8-bit Microcontrollers. Application Note. Features. Introduction

AN3252 Application note

8-bit RISC Microcontroller. Application Note. AVR335: Digital Sound Recorder with AVR and DataFlash

tinyavr Microcontrollers White Paper is the registered trademark of Atmel Corporation, 2325 Orchard Parkway, San Jose, CA Rev.

AT91SAM ARM-based Flash MCU. Application Note

Application Note. 8-bit Microcontrollers. AVR293: USB Composite Device

AVR Butterfly Training. Atmel Norway, AVR Applications Group

SMARTCARD XPRO. Preface. SMART ARM-based Microcontrollers USER GUIDE

Atmel AVR4027: Tips and Tricks to Optimize Your C Code for 8-bit AVR Microcontrollers. 8-bit Atmel Microcontrollers. Application Note.

STK User Guide

APPLICATION NOTE. Atmel AT01095: Joystick Game Controller Reference Design. 8-/16-bit Atmel Microcontrollers. Features.

AVR106: C Functions for Reading and Writing to Flash Memory. Introduction. Features. AVR 8-bit Microcontrollers APPLICATION NOTE

Step Motor Controller. Application Note. AVR360: Step Motor Controller. Theory of Operation. Features. Introduction

8051 Flash Microcontroller. Application Note. A Digital Thermometer Using the Atmel AT89LP2052 Microcontroller

8-bit Microcontroller. Application Note. AVR410: RC5 IR Remote Control Receiver

Transcription:

AVR064: A Temperature Monitoring System with LCD Output Features Presenting data on an LCD-display Temperature measurement Real Time Clock (RTC) UART communication with a PC PWM implementation 8-bit Microcontrollers Application Note 1 Introduction The STK502 board is a top module designed to add ATmega169(P) support to the STK500 development board from Atmel. STK500 and STK502 provide all hardware needed to get started developing with the ATmega169(P). This application note is meant to be an example of how to use the ATmega169(P) and the STK502. It includes: A complete ATmega169(P) code example written in C-code. Flowcharts explaining the code. Instruction on how to configure the STK502. A pre-programmed ATmega169(P) including the example in this application note is shipped with each STK502 kit. The source code is found on the AVR Technical Library CD shipped with the STK502 or on the Atmel AVR web site, http://www.atmel.com/products/avr/. Figure 1-1. The STK502 board Rev.

2 Application overview This application note describes how to get started with the ATmega169(P) microcontroller (MCU), the first AVR that has a built in LCD-controller/driver. This application is a temperature control application, including a Real Time Clock (RTC). It will monitor the temperature through a sensor, and regulate the temperature if a heating/cooling element is attached. Figure 2-1. Application overview ATmega16 STK500 SWITCHES L C D I/O U A R T Timer2 ADC 32 khz STK500 LEDS Heating/ coolingelement NTC Thermistor The LCD starts with scrolling the text: STK502 example application for ATmega169(P). It is required that the example code is programmed into the ATmega169(P) and the hardware is set up according to the section Hardware Configuration on page 6. Select a desired temperature set point. When the temperature goes below this set point value, the Heater I/O pin will go high, and a LED on STK500 will flash. When the temperature goes above the set point value, the Cooler I/O pin will go high, and another LED on the STK500 will flash. The duty cycle of the LED flashing will vary with the actual temperature deviation from the set point (the greater the deviation is, the brighter the LEDs will shine) The LCD will display time and temperature information. All data that is presented on the LCD will also be sent through the UARTinterface and can be received by etc a standard terminal. Pressing a button on the STK500 will shift between the different information on the LCD. This information is: CLOCK: RTC clock running on the ATmega169(P) DATE: Calculated from the RTC-clock SET POINT: Selected temperature TEMPERATURE: Measured temperature OFFSET: Difference between the measured temperature and the set point CONTRAST: Shows all the segments available with the default hardware strapping. Adjusting the CLOCK, DATE, SET POINT or the CONTRAST can be done by using three of the SWITCHES on the STK500. Since these switches are used for different 2 AVR064

Figure 2-2. Menu System Menu 1 Menu 2 Menu 3 AVR064 functions, there is a need for a menu system. See Figure 2-2 for an overview of how the menus are arranged in this application. CLOCK DATE SET POINT HOUR MINUTE SECOND DAY MONTH YEAR SET POINT + - + - + - + - + - + - + - TEMPERATURE OFFSET CONTRAST CONTRAST + - Please see section STK500 switches on page 17, for more detailed information on how to use the menu system. The CLOCK, DATE and SET POINT can also be adjusted from the UART interface. See section Terminal on page 20. The implementation is designed to be used with the STK502 and the LCD-display that is included in this starter-kit. For technical specifications and the LCD-bit mapping please refer to the STK502 User Guide found in the AVR Studio help system, and for more information on the LCD-driver see Application note AVR065 - LCD driver for the STK502 LCD. 3

3 Hardware description 3.1 ATmega169(P) The ATmega169(P) is an ultra low power AVR 8-bit RISC microcontroller. It includes 16K of bytes self-programming Flash Program Memory, 1K bytes SRAM, 512 Byte EEPROM and 8 Channel 10-bit A/D-converter, JTAG interface for on-chip-debugging and 4 X 25 Segment LCD Driver. It can do up to 1 MIPS throughput at 1 MHz for ATmega169(P)V, or 4 MIPS throughput at 4 MHz for the ATmega169(P)L. The ATmega169(P) is an excellent choice for low power applications that requires user interaction (LCD + keyboard) and the possibility to interface analog sensors etc. Figure 3-1. ATmega169 The ATmega169P has lower power consumption in power-down and power-save than ATmega169 revision E, but there are also other differences, e.g. oscillator. Please see application note AVR098 and the ATmega169(P) datasheets for more information. 4 AVR064

AVR064 3.2 STK502 The STK502 board is a top module designed to add ATmega169(P) support to the STK500 development board from Atmel. The STK502 includes connectors and hardware allowing full utilization of the new features of the ATmega169(P) (including an LCD-display), while the Zero Insertion Force (ZIF) socket allows easy use of TQFP packages for prototyping. Figure 3-2. STK502 top module for STK500 (pin1 location marked) See the STK502 User Guide for more information about the STK502 3.2.1 LCD-display 3.2.2 NTC-thermistor Liquid Crystal Displays (LCDs) are categorized as non-emissive display devices. In that respect, they do not produce any form of light like a Cathode Ray Tube (CRT). LCDs are composed of a polarized liquid crystalline material in between two plates of glass. Typically, one plate is called the common or backplane, and the other is called a segment or frontplane. In a reflective LCD panel (one that has no back light) a voltage difference applied across the two electrodes will result in a polarization that will prevent the light from reflecting back to the observer. This will appear as a dark segment and is, therefore, considered ON. A lack of voltage difference will allow the light to reflect back and is considered OFF. For more information on the LCD-driver, see application note AVR065: LCD driver for the STK502 LCD Various types of sensors can be used to measure temperature. One of these is the thermistor, or temperature-sensitive resistor. Most thermistors have a negative temperature coefficient (NTC), meaning the resistance goes up as temperature goes down. Of all passive temperature measurement sensors, thermistors have the highest sensitivity (resistance change per degree of temperature change). Thermistors do not have a linear temperature/resistance curve. The NTC-thermistor used with this application has a resistance of 10kΩ at 25 C ( ), beta-value of 3450 and a tolerance of ±1%. The voltage over the NTC can be found using the A/D converter in the ATmega169(P). See the ATmega169(P) datasheets for 5

how to use the ADC. And by the use of Equation 3-1, the temperature can be calculated. Equation 3-1. Calculation of temperature from measured ADC values Temperature = ln V β = 3450 V V T T ADC ref zero amb = Voltage calculated from the A/D conversion =1.263V = 273 K ref V ADC β V ADC = 298 K (273 C + 25 C) β + Tamb T zero 3.3 Hardware configuration In order to make the example code work, it is required to set up the cables and switches in the correct order. Figure 3-3 and Figure 3-4 shows how to set up the cables and switches. Figure 3-3. Cable settings 6 AVR064

AVR064 Figure 3-4. Switch configuration Connect PORTE on the STK502 to the SWITCHES-header on the STK500 with a 10-pin cable. Connect PB5/PB6 to LED5/LED6, PB4/PB7 to respectively Heating/Cooling element. If no heating/cooling element is available, just connect PORTB to the LEDS using a 10pins cable. Connect PE0/PE1 on the STK500 to the RXD/TXD. Connect the Segment pins from ATmega169(P) to the STK502 LCD pins with the 34pins cable. Place a jumper on the 2pins header 19 24 Insert the NTC-thermistor in the screw-terminal. All of the three switches on the STK502 should be in the position towards the screw-terminal, i.e. the TOSC switch should be in the TOSC-position, the AREF switch should be in the VREF-position and the PF[1:0] should be in the SENSORposition. Connect PG5 and RST with a jumper, on PORTG/RST. And most importantly insert the ATmega169(P) in the ZIF-socket. The ATmega169(P) that comes with the STK502 kit, is pre-programmed with the example code. If it is required to reprogram the ATmega169(P), see the STK502 User Guide for help on this topic. The AVR064.hex file that should be programmed into the ATmega169(P) can be found on the AVR Software and Technical Library -CD that comes with the STK502, and on the ATMEL web site (http://www.atmel.com/products/avr/). If the ATmega169(P) is reprogrammed, make sure the fuses are set up according to Figure 3-5. 7

Figure 3-5. Fuse settings As Figure 3-5 describes, the only fuses that should be programmed is: Brown-out detection disabled JTAG Interface Enabled Boot Flash section size = 1024 words Int. RC Osc.; Start-up time 6CK + 65 ms 4 ATmega169(P) firmware 4.1 Interrupts used 4.1.1 LCD Start of Frame This section contains information about the source code and functions. The firmware can be downloaded from the Atmel website: http://www.atmel.com/products/avr/. For compiler info and settings, device settings, target setup info and comprehensive source documentation please see the readme.html file included with the source. The timing related functions are written for an ATmega169(P) running at 1Mhz (except the RTC-clock and the LCD-frame rate that is clocked from an external 32kHz crystal), and the prescaler for the system clock is therefore set to 1/8. Please note that the ATmega169 revision B runs at 4 MHz, so the communication speed of the UART will be reduced from 9,600 to 4,800 baud, but otherwise will be ok. In this interrupt the data from the LCD_displayData buffer is latched to the LCD Data Registers. The variable LCD_Blink toggles every time this interrupt occurs. The interrupt is dependent of the external 32kHz crystal. 8 AVR064

4.1.2 Timer/Counter2 Overflow AVR064 This interrupt is used to increment the variable SECOND, which the whole RTC-clock builds on. Timer/Counter2 is clocked asynchronous from the 32kHz and is therefore independent of the clock frequency. 4.1.3 USART0, RX complete This interrupt takes care of incoming data from the UART interface. 4.1.4 USART0, Data Register Empty This interrupt transmits data out through the UART interface. 4.2 Main loop Figure 4-1 shows the main loop. Figure 4-1. Main loop Initialize Time and date update Temperature calculation Store data from receive-buffer Send data from transmit-buffer Check status on STK500 buttons Update LCD 4.3 Initialize After a reset the firmware will initialize the ATmega169(P) and its integrated peripherals. The initialization runs only one time after a reset. 9

Figure 4-2. Initialize Initialization Set PORTB as output. Set PORTE as input. Set up Timer1 with PWM. Phase Correct, 10-bit. Set up the Real Time Clock, using Timer2 in asynchronous mode. Set up the UART. Baudrate = 9600 @ 1Mhz Set up the ADC Set up the LCD with 1/4 duty cycle and 1/3 bias. Enable all segments. Set up data for the LCDdisplay. Scrolling text. Return PORTB is set as output and should be connected to the LEDS on STK500. PB5 (OC1A) and PB6 (OC1B) show the offset between measured temperature and selected temperature set point. PB4 and PB7 are heating and cooling pins respectively. Connect a heating and cooling element to these pins. DDRE is set as input and should be connected to the SWITCHES on the STK500. PE7, PE6 and PE5 are used to select what information should be displayed on the LCD and adjusting time/date, temperature set point and the LCD contrast. Timer/Counter1 is set up with PWM to use on the OC1A/OC1B (PB5/PB6) pins. Enable Timer/Counter2 with asynchronous operation, for the RTC. By using an external 32kHz crystal the RTC can run independently of the ATmega169(P) system clock, and will also run during sleep. Set up the UART with both RX and TX enable, baud rate 9600 @ 1 MHz, asynchronous operation, 8 bit character size, 1 stop bit and disable parity mode. Set up the ADC in single-ended mode. (Differential mode can be selected by setting ADC_init(Differential) instead of ADC_init(SingleEnded) in the source code). Disable digital input on PORTF and run a dummy ADC conversion. 10 AVR064

AVR064 Enable all segment pins on the ATmega169(P). Select the 32kHz as clock source for the LCD, and set the prescaler bits thereafter. Select 1/4 duty cycle and 1/3 bias. Set up Timer/Counter0 Compare Match interrupt to give the required delays for the scrolling and blinking speed of the information on the LCD-display. Start scrolling the initial string over the LCD-display. 4.4 Time and date update This routine updates the clock and date according to the variable SECOND that gets incremented every second in the Timer/Counter2 Overflow interrupt routine. The whole update routine is self-explaining from the flow-chart. Figure 4-3. Time and date update Time_update SECOND larger than 59? Increment MINUTE Clear SECOND The variable SECOND is incremented in the Timer2 Overflow Interrupt routine. MINUTE larger than 59? Increment HOUR Clear MINUTE HOUR larger than 23? Increment DAY Clear HOUR DAY larger than number of days in month? (check if leap year) Increment MONTH Set DAY = 1 MONTH larger than 12? Increment YEAR_LO Set MONTH = 1 YEAR_LO larger than 99? Increment YEAR_HI Clear YEAR_LO Return 11

4.5 Temperature calculation In this function the voltage over the NTC-thermistor will be measured and the temperature calculated. Figure 4-4. Temperature calculation ADC_conversion Run an Analog to Digital Conversion. Increment the number of A-D conversions Measured temperature 32 times? Calculate the voltage from the ADC-value, and use it in a formula to calulate the corresponding temperature. Find the difference between the measured temperature and the setpoint, and if necessary, set heating or cooling pin. Return Start by doing an A/D conversion. The average of 32 ADC results is used in a formula to calculate the corresponding temperature. The heating or cooling pin is set depending on the difference between the calculated temperature and the temperature set point. The temperature set point is selected by the user. The bigger the difference is, the brighter the heating or cooling LED will shine. 4.6 Receive data from PC These routines take care of data coming from the PC through the UART interface. 12 AVR064

AVR064 Figure 4-5. Receive packet from PC interrupt [USART_RXC_vect] void USART0_RXC_interrupt(void) Store_RX_data RXC interrupt RX_Packet complete? Read the UDR0-reg which contains the received byte. There may be up to three ASCII bytes to get one HEX-byte Preamble received? Byte in receivebuffer = 0x0D or 0x20? (end of packet or new byte) Store received byte in receive buffer Convert ASCII-byte to HEX Received byte = 0x0D? (ascii value for line feed, end of packet) Any byte converted? Preamble received = FALSE RX_Packet complete = TRUE Store the HEX-byte to SRAM Return from interrupt Byte in receivebuffer = 0x0D? Set RX_Packet complete = FALSE Return 4.6.1 USART_RXC_interrupt 4.6.2 Store_Rx_data: Receiving data from the PC is done in the USART_RXC_interrupt routine. It will discard all data until the correct preamble bytes are received. Then it will store the succeeding bytes in a receive buffer until the byte for Line Feed appears (ASCII value: 0x0D) This indicates the end of the packet and RX_Packet_complete flag will be set to TRUE. The packet is then converted from ASCII to hexadecimal. One HEX-byte can contain 1-3 ASCII bytes. ASCII-bytes that belong to different HEX-bytes are separated by an 13

. The converted HEX-bytes get continuously stored in the correct place in SRAM until the Line-Feed byte appears, which is the end of the packet. Table 4-1. Receive Packet from PC Data Size Preamble STK502 6 byte HOUR MINUTE SECOND DATE MONTH YEAR_HI YEAR_LO SET_POINT ASCII-carriage return (0x0D) ASCII-line feed (0x0A) 4.7 Transmit packet to PC Transferring the data in ASCII allows a standard terminal to be used on the PC. These routines transmit the data from ATmega169(P) to the PC 14 AVR064

AVR064 Figure 4-6. Transmit packet to PC Send_TX_data interrupt [USART0_UDRE_vect] void USART0_UDRE_interrupt(void) On-going transmission? UDRE interrupt Load Preamble bytes in transmit-buffer Bytes left to send? Load 0x20, ASCII: "space" in transmit buffer. Disable UDREinterrupt Convert one HEX-byte to 2-3 ASCII-bytes. Transmit one byte HEX-bytes left to convert? Return from interrupt Load 0x0D, ASCII:"line feed" in the end of packet Enable UDREinterrupt, that will start the transfer. Return A transmit packet starts with the preamble bytes, and then the HEX-bytes that are to be transmitted get converted to ASCII-bytes and loaded in the packet. Between each HEX-byte that gets converted, an ASCII-byte for space (0x20) is inserted. At the end of the packet, an ASCII-byte for Line Feed is added to indicate the end of frame. The transmission starts by enabling the UDRE interrupt. When all bytes are transmitted the UDRE interrupt gets disabled. 15

Table 4-2. Transmit packet to PC Data Size Preamble STK502 6 byte HOUR MINUTE SECOND DATE MONTH YEAR_HI YEAR_LO SET_POINT TEMP_HIGHBYTE TEMP_LOWBYTE OFFSET Firmware revision ASCII-carriage return (0x0D) ASCII-line feed (0x0A) 16 AVR064

AVR064 4.8 STK500 switches Figure 4-7. CheckButtons CheckButtons Read buttons Buttons released from last time? Yes No Return Menu2 active? Yes Menu3 active? Yes No No A Button A, B or C? C A Button A, B or C? C A Button A, B or C? C B B B Shift Menu1 Activate Menu2 Deactivate Menu1 Shift Menu2 Activate Menu3 Deactivate Menu2 Increase value Decrease value Deactivate Menu3 Run LCDsetupData Run LCDsetupData Run LCDsetupData Return Return Return There are three switches that are used as inputs to the application. To do several tasks with only three switches, a menu system is needed. Figure 4-7 shows three menus in a hierarchy, which are used in this code. See Figure 2-1 for an overview of the menus. Figure 4-7 refers to Button A/B/C, in the application these buttons can be found at: ButtonA is SW7 that is connected to PE7. ButtonB is SW6 that is connected to PE6. ButtonC is SW5 that is connected to PE5. Example: After a RESET the LCD is set up to scroll a text. None of the three menus are active. Pressing the SW7 will toggle between the alternatives in Menu1 (Clock, Date, Set point, Temperature, Offset and Contrast) To adjust the variable MINUTE: Press SW7 until CLOCK appears in the LCDdisplay, and select this by pressing SW6 to activate Menu2 under CLOCK. Pressing SW7 will now toggle between the alternatives in Menu2 (Hour, Minute and Second). Press SW7 until the variable MINUTE is blinking in the LCD-display, and select this by pressing SW6. Now Menu3 is activated (the colons should disappear). Pressing SW7 will increase the variable MINUTE and SW6 will decrease. When desired value has been selected, press SW5 to deactivate Menu3, and go back to Menu2. Press SW5 once more to deactivate Menu2 and go back to Menu1. The same procedure can be used to adjust the other variables as well. 17

4.9 LCD 4.9.1 LCD update Writing to the LCD requires an LCD driver. The driver used in this application is described in the application note AVR065: LCD Driver for the STK502LCD. Figure 4-8. LCD_update LCD_update No LCD_ updatecomplete = TRUE? Yes Set LCD_updateRequired = FALSE Scrolling text? No No Write data fromtransmit- Buffer? Yes Clear all specialsegments Go to LCDscrollMSG function. Enable all segments Yes Set specialsegments if required Load one byte from transmitbuffer Set LCD_updateComplete = FALSE Set LCD_updateRequired = TRUE Activate blinking if required Write the digit to LCD_displayBuffer Return Yes 6 digits written to buffer? No This function will load data into the LCD_displayBuffer. First check if the LCD has been updated with the data already in the LCD_displayBuffer. If so, set the LCD_update required to FALSE. This will prevent the LCD to be updated with incomplete data, if an LCD Start of Frame interrupt should occur during this function. If a text-string is to be scrolled, clear display and call the LCDscrollMSG function. If no text to scroll, check if there is data to write from the TransmitBuffer, and load the data into the LCD_displayBuffer. Digits can be set to blink on the display. To do this the digit will be loaded with either its data value or a, depending on the variable LCD_Blink. 18 AVR064

AVR064 After the LCD_displayBuffer has been updated, the LCD_updatedComplete will be set to FALSE and LCD_updateRequired to TRUE. This will cause the LCD_displayBuffer to be written to the LCD in the LCD Start of Frame interrupt. 4.9.2 Scroll function Figure 4-9. LCDscrollMsg LCDscrollMsg String-pointer at the end of string? Yes LCD-display empty? Yes No Write six characters from the String to the LCD_displayBuffer Increment string pointer No Add one 'space' and write the remaining characters from string to the LCD_displayBuffer Clear string-counter If not set to infinite scrolling, decrement the NumberOfScroll variable Return This function shifts the six digits on the LCD one step to the left. The scroll function uses a pointer to keep track of what characters to shift in and out of the LCD. When all the six digits have been updated, the pointer gets incremented by one in order to shift the text-string one step the next time this function is called. If the pointer has reached the end of the string, the LCD has to be filled up with one ASCII-space at the time until all of the six digits are blank. This will fade out the text string. 19

4.9.3 LCD set-up data Figure 4-10. LCDsetupData LCDsetupData Menu1 active? No Load Welcome string and activate infinite scrolling Yes Menu2 active? Yes Menu3 active? Yes No Load a string (depending on Menu1) to be scrolled once. No Enable colons Enable colons Disable colons Return If Menu1 isn t active the welcome will scroll over the LCD. If Menu1 is active but not Menu2, the corresponding string will be scrolled once over the LCD and then the belonging data. If Menu2 is active but not Menu3, just enable the colons. And if Menu3 is active, disable the colons to indicate that the current variable can now be adjusted. 5 Terminal All temperature and time information is transmitted through the UART-interface. A program on a PC can receive this data by connecting a serial-cable between the RS232 SPARE on the STK500 and a com-port on the PC. A standard terminal can be used, e.g. HyperTerminal. Set up the terminal with the settings shown in Figure 5-1. 20 AVR064

AVR064 Figure 5-1. Port Settings Press the connect-button and the data from the ATmega169(P) should appear as in Figure 5-2. The data is presented according to Table 5-1. Table 5-1. Transmit Packet from ATmega169 according to Figure 5-2 Data Preamble Hour 15 Minute 14 Second 22 Day 04 Month 11 Year high 20 Year low 02 Set point 25 ºC high byte 25 ºC low byte 14 Offset 00 Versions number 01 Value STK502 21

Figure 5-2. HyperTerminal One can also adjust the variables within the ATmega169(P) from the terminal. This has to be done according to Table 4-1. E.g. write: STK502 14 37 02 25 11 20 02 24 in the terminal, and press enter to indicate end of frame. This will adjust the clock to 14h37m02s, the date to 25. November 2002, and the temperature set point will be 24 C. 22 AVR064

AVR064 6 Table of Contents Features... 1 1 Introduction... 1 2 Application overview... 2 3 Hardware description... 4 3.1 ATmega169(P)... 4 3.2 STK502... 5 3.2.1 LCD-display... 5 3.2.2 NTC-thermistor... 5 3.3 Hardware configuration... 6 4 ATmega169(P) firmware... 8 4.1 Interrupts used... 8 4.1.1 LCD Start of Frame... 8 4.1.2 Timer/Counter2 Overflow... 9 4.1.3 USART0, RX complete... 9 4.1.4 USART0, Data Register Empty... 9 4.2 Main loop... 9 4.3 Initialize... 9 4.4 Time and date update... 11 4.5 Temperature calculation... 12 4.6 Receive data from PC... 12 4.6.1 USART_RXC_interrupt... 13 4.6.2 Store_Rx_data:... 13 4.7 Transmit packet to PC... 14 4.8 STK500 switches... 17 4.9 LCD... 18 4.9.1 LCD update... 18 4.9.2 Scroll function... 19 4.9.3 LCD set-up data... 20 5 Terminal... 20 6 Table of Contents... 23 Disclaimer... 24 23

Disclaimer Atmel Corporation 2325 Orchard Parkway San Jose, CA 95131, USA Tel: 1(408) 441-0311 Fax: 1(408) 487-2600 Regional Headquarters Europe Atmel Sarl Route des Arsenaux 41 Case Postale 80 CH-1705 Fribourg Switzerland Tel: (41) 26-426-5555 Fax: (41) 26-426-5500 Asia Room 1219 Chinachem Golden Plaza 77 Mody Road Tsimshatsui East Kowloon Hong Kong Tel: (852) 2721-9778 Fax: (852) 2722-1369 Japan 9F, Tonetsu Shinkawa Bldg. 1-24-8 Shinkawa Chuo-ku, Tokyo 104-0033 Japan Tel: (81) 3-3523-3551 Fax: (81) 3-3523-7581 Atmel Operations Memory 2325 Orchard Parkway San Jose, CA 95131, USA Tel: 1(408) 441-0311 Fax: 1(408) 436-4314 Microcontrollers 2325 Orchard Parkway San Jose, CA 95131, USA Tel: 1(408) 441-0311 Fax: 1(408) 436-4314 La Chantrerie BP 70602 44306 Nantes Cedex 3, France Tel: (33) 2-40-18-18-18 Fax: (33) 2-40-18-19-60 ASIC/ASSP/Smart Cards Zone Industrielle 13106 Rousset Cedex, France Tel: (33) 4-42-53-60-00 Fax: (33) 4-42-53-60-01 1150 East Cheyenne Mtn. Blvd. Colorado Springs, CO 80906, USA Tel: 1(719) 576-3300 Fax: 1(719) 540-1759 Scottish Enterprise Technology Park Maxwell Building East Kilbride G75 0QR, Scotland Tel: (44) 1355-803-000 Fax: (44) 1355-242-743 RF/Automotive Theresienstrasse 2 Postfach 3535 74025 Heilbronn, Germany Tel: (49) 71-31-67-0 Fax: (49) 71-31-67-2340 1150 East Cheyenne Mtn. Blvd. Colorado Springs, CO 80906, USA Tel: 1(719) 576-3300 Fax: 1(719) 540-1759 Biometrics/Imaging/Hi-Rel MPU/ High Speed Converters/RF Datacom Avenue de Rochepleine BP 123 38521 Saint-Egreve Cedex, France Tel: (33) 4-76-58-30-00 Fax: (33) 4-76-58-34-80 Literature Requests www.atmel.com/literature Disclaimer: The information in this document is provided in connection with Atmel products. No license, express or implied, by estoppel or otherwise, to any intellectual property right is granted by this document or in connection with the sale of Atmel products. EXCEPT AS SET FORTH IN ATMEL S TERMS AND CONDITIONS OF SALE LOCATED ON ATMEL S WEB SITE, ATMEL ASSUMES LIABILITY WHATSOEVER AND DISCLAIMS ANY EXPRESS, IMPLIED OR STATUTORY WARRANTY RELATING TO ITS PRODUCTS INCLUDING, BUT T LIMITED TO, THE IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR N-INFRINGEMENT. IN EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT, CONSEQUENTIAL, PUNITIVE, SPECIAL OR INCIDENTAL DAMAGES (INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF PROFITS, BUSINESS INTERRUPTION, OR LOSS OF INFORMATION) ARISING OUT OF THE USE OR INABILITY TO USE THIS DOCUMENT, EVEN IF ATMEL HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. Atmel makes no representations or warranties with respect to the accuracy or completeness of the contents of this document and reserves the right to make changes to specifications and product descriptions at any time without notice. Atmel does not make any commitment to update the information contained herein. Unless specifically provided otherwise, Atmel products are not suitable for, and shall not be used in, automotive applications. Atmel s products are not intended, authorized, or warranted for use as components in applications intended to support or sustain life. Atmel Corporation 2006. All rights reserved. Atmel, logo and combinations thereof, Everywhere You Are, AVR, AVR Studio and others, are the registered trademarks or trademarks of Atmel Corporation or its subsidiaries. Other terms and product names may be trademarks of others.