AVR064: STK502 – 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 Introduction 8-bit Microcontroller Application Note The STK502 board is a top module designed to add ATmega169 support to the STK500 development board from Atmel. STK500 and STK502 provide all hardware needed to get started developing with the ATmega169.
Application Overview This application note describes how to get started with the ATmega169 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. It will monitor the temperature through a sensor, and regulate the temperature if a heating/cooling unit is attached. Figure 1.
AVR064 Figure 2. Menu System Menu 1 Menu 2 Menu 3 + HOUR + CLOCK MINUTE + SECOND - + DAY + DATE MONTH + YEAR - + SET POINT 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 21.
Hardware Description ATmega169 ATmega169 is an ultra low power AVR 8-bit RISC microcontroller. It includes 16K byte Self-programming Flash Program memory, 1K byte SRAM, 512 byte EEPROM, and 8channel 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 ATmega169V, or 4 MIPS throughput at 4 MHz for the ATmega169L.
AVR064 LCD Display 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.
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 5 and Figure 6 shows how to set up the cables and switches. Figure 5. Cable Settings Figure 6. Switch Configuration 6 • 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.
AVR064 • Connect the “Segment pins from ATmega169” to the “STK502 LCD pins” with the 34-pin cable. • Place a jumper on the 2-pin 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 twoscrew terminal block, 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 SENSOR position. • Connect PG5 and RST with a jumper, on PORTG/RST.
ATmega169 Firmware The firmware that realizes the temperature control application is written in IAR EWAVR 2.27b. The timing related functions are written for an ATmega169 running at 1 Mhz except the RTC clock and the LCD frame rate which is clocked from an external 32 kHz crystal. The crystal is mounted on the STK502 board. Interrupts Used LCD Start of Frame In this interrupt the data from the LCD_displayData buffer is latched to the LCD Data Registers.
AVR064 Main Loop Figure 8 shows the main loop. Figure 8.
Initialize After a Reset the firmware will initialize the ATmega169 and its integrated peripherals. The initialization runs only one time after a Reset. Figure 9. Initialize Initialize 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 LCD Display.
AVR064 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. Enable all segment pins on the ATmega169. Select the 32 kHz as clock source for the LCD, and set the prescaler bits. Select 1/4 duty cycle and 1/3 bias.
Temperature Calculation In this function the voltage over the NTC thermistor will be measured and the temperature calculated. Figure 11. Temperature Calculation ADC_conversion Run an Analog to Digital Conversion. Increment the Number of A/D Converions Measured Temperature 32 Times? NO YES Calculate the Voltage from the ADC Value, and Use it in a Formula to Calulate the Corresponding Temperature.
AVR064 Receive Data from PC These routines take care of data coming from the PC through the UART interface. Figure 12. Receive Packet from PC Store_RX_data Interrupt [USART_RXC_vect] Void USART0_RXC_interrupt (Void) RX_Packet Complete? RXC Interrupt NO YES Read the UDR0 Register which Contains the Received Byte.
Store_Rx_data 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 ASCII-space (0x20). 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 1.
AVR064 Transmit Packet to PC These routines transmit the data from ATmega169 to the PC. Figure 13. Transmit Packet to PC Send_TX_data Ongoing Transmission? interrupt [USART0_UDRE_vect] void USART0_UDRE_interrupt(void) YES UDRE Interrupt NO Load Preamble Bytes in Transmit Buffer Bytes Left to Send? NO YES Load 0x20, ASCII: "Space" in Transmit Buffer. Transmit One Byte Disable UDRE Interrupt Convert One HEX Byte to 2-3 ASCII Bytes.
Table 2.
AVR064 STK500 Switches Figure 14.
To adjust the variable MINUTE: Press SW7 until “CLOCK” appears in the LCD display, and select this by pressing SW6 to activate Menu 2 under “CLOCK”. Pressing SW7 will now toggle between the alternatives in Menu 2, Hour, Minute, and Second. Press SW7 until the variable MINUTE is blinking in the LCD display, and select this by pressing SW6. Now Menu 3 is activated and the colons should disappear. Pressing SW7 will increase the variable MINUTE and SW6 will decrease.
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. Scroll Function Figure 16.
LCD Set-up Data Figure 17. LCDsetupData LCDsetupData Menu 1 Active? No Yes Load Welcome String and Activate Infinite Scrolling Menu 2 Active? No Yes Menu 3 Active? Yes No Load a String (Depending on Menu 1) to be Scrolled Once. Enable Colons Enable Colons Disable Colons Return If Menu 1 isn’t active the welcome string will scroll over the LCD. If Menu 1 is active but not Menu 2, the corresponding string will be scrolled once over the LCD and then the data belonging.
AVR064 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 “RS-232 SPARE” on the STK500 and a comport on the PC. A standard terminal can be used, e.g, HyperTerminal. Set up the terminal as shown in Figure 18. Figure 18. Port Settings Press the connect button and the data from the ATmega169 should appear as in Figure 19. The data is presented according to Table 1. Table 3.
Figure 19. HyperTerminal One can also adjust the variables within the ATmega169 from the terminal. This has to be done according to Table 1. For example, 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 will be November 25, 2002. And the temperature set point will be 24°C.
Atmel Headquarters Atmel Operations Corporate Headquarters Memory 2325 Orchard Parkway San Jose, CA 95131 TEL 1(408) 441-0311 FAX 1(408) 487-2600 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 Tsimhatsui East Kowloon Hong Kong TEL (852) 2721-9778 FAX (852) 2722-1369 Japan 9F, Tonetsu Shinkawa Bldg.