ВУЗ: Не указан

Категория: Не указан

Дисциплина: Не указана

Добавлен: 13.06.2025

Просмотров: 2217

Скачиваний: 0

ВНИМАНИЕ! Если данный файл нарушает Ваши авторские права, то обязательно сообщите нам.

CodeVisionAVR

4.11.3 LCD Functions for displays connected in 8 bit memory mapped mode

These LCD Functions are intended for easy interfacing between C programs and alphanumeric LCD modules built with the Hitachi HD44780 chip or equivalent.

The LCD is connected to the AVR external data and address buses as an 8 bit peripheral.

This type of connection is used in the Kanda Systems STK200+ and STK300 development boards. For the LCD connection, please consult the documentation that came with your development board.

These functions can be used only with AVR chips that allow using external memory devices.

The prototypes for these functions are placed in the file lcdstk.h, located in the ..\INC subdirectory. This file must be #include -ed before using the functions.

The following LCD formats are supported in lcdstk.h: 1x8, 2x12, 3x12, 1x16, 2x16, 2x20, 4x20, 2x24 and 2x40 characters.

The LCD Functions are:

void _lcd_ready(void)

waits until the LCD module is ready to receive data.

This function must be called prior to writing data to the LCD with the _LCD_RS0 and _LCD_RS1 macros.

Example:

/* enables the displaying of the cursor */ _lcd_ready();

_LCD_RS0=0xe;

The _LCD_RS0, respectively _LCD_RS1, macros are used for accessing the LCD Instruction Register with RS=0, respectively RS=1.

void lcd_write_byte(unsigned char addr, unsigned char data);

writes a byte to the LCD character generator or display RAM.

Example:

/* LCD user defined characters

Chip: AT90S8515

Memory Model: SMALL

Data Stack Size: 128 bytes

Use an 2x16 alphanumeric LCD connected to the STK200+ LCD connector */

/* include the LCD driver routines */ #include <lcdstk.h>

typedef unsigned char byte;

© 1998-2007 HP InfoTech S.R.L.

Page 137

CodeVisionAVR

/* table for the user defined character

arrow that points to the top right corner */ flash byte char0[8]={

0b0000000,

0b0001111,

0b0000011,

0b0000101,

0b0001001,

0b0010000,

0b0100000,

0b1000000};

/* function used to define user characters */ void define_char(byte flash *pc,byte char_code)

{

byte i,a; a=(char_code<<3) | 0x40;

for (i=0; i<8; i++) lcd_write_byte(a++,*pc++);

}

void main(void)

{

/* initialize the LCD for 2 lines & 16 columns */ lcd_init(16);

/* define user character 0 */ define_char(char0,0);

/* switch to writing in Display RAM */ lcd_gotoxy(0,0);

lcd_putsf("User char 0:");

/* display used defined char 0 */ lcd_putchar(0);

while (1); /* loop forever */

}

unsigned char lcd_read_byte(unsigned char addr);

reads a byte from the LCD character generator or display RAM.

unsigned char lcd_init(unsigned char lcd_columns)

initializes the LCD module, clears the display and sets the printing character position at row 0 and column 0. The numbers of columns of the LCD must be specified (e.g. 16). No cursor is displayed. The function returns 1 if the LCD module is detected and 0 if it is not.

This is the first function that must be called before using the other high level LCD Functions.

void lcd_clear(void)

clears the LCD and sets the printing character position at row 0 and column 0.

void lcd_gotoxy(unsigned char x, unsigned char y)

sets the current display position at column x and row y. The row and column numbering starts from 0.

© 1998-2007 HP InfoTech S.R.L.

Page 138


CodeVisionAVR

void lcd_putchar(char c)

displays the character c at the current display position.

void lcd_puts(char *str)

displays at the current display position the string str, located in SRAM.

void lcd_putsf(char flash *str)

displays at the current display position the string str, located in FLASH.

4.12 I2C Bus Functions

The I2C Functions are intended for easy interfacing between C programs and various peripherals using the Philips I2C bus.

These functions treat the microcontroller as a bus master and the peripherals as slaves.

The prototypes for these functions are placed in the file i2c.h, located in the ..\INC subdirectory. This file must be #include -ed before using the functions.

Prior to #include -ing the i2c.h file, you must declare which microcontroller port and port bits are used for communication through the I2C bus.

Example:

/* the I2C bus is connected to PORTB */ /* the SDA signal is bit 3 */

/* the SCL signal is bit 4 */ #asm

.equ __i2c_port=0x18

.equ __sda_bit=3

.equ __scl_bit=4 #endasm

/* now you can include the I2C Functions */ #include <i2c.h>

The I2C Functions are:

void i2c_init(void)

this function initializes the I2C bus.

This is the first function that must be called prior to using the other I2C Functions.

unsigned char i2c_start(void)

issues a START condition.

Returns 1 if bus is free or 0 if the I2C bus is busy.

void i2c_stop(void)

issues a STOP condition.

unsigned char i2c_read(unsigned char ack)

reads a byte from the bus.

The ack parameter specifies if an acknowledgement is to be issued after the byte was read. Set ack to 0 for no acknowledgement or 1 for acknowledgement.

© 1998-2007 HP InfoTech S.R.L.

Page 139

CodeVisionAVR

unsigned char i2c_write(unsigned char data)

writes the byte data to the bus.

Returns 1 if the slave acknowledges or 0 if not.

Example how to access an Atmel 24C02 256 byte I2C EEPROM:

/* the I2C bus is connected to PORTB */ /* the SDA signal is bit 3 */

/* the SCL signal is bit 4 */ #asm

.equ __i2c_port=0x18

.equ __sda_bit=3

.equ __scl_bit=4 #endasm

/* now you can include the I2C Functions */ #include <i2c.h>

/* function declaration for delay_ms */ #include <delay.h>

#define EEPROM_BUS_ADDRESS 0xa0

/* read a byte from the EEPROM */

unsigned char eeprom_read(unsigned char address) { unsigned char data;

i2c_start(); i2c_write(EEPROM_BUS_ADDRESS); i2c_write(address); i2c_start();

i2c_write(EEPROM_BUS_ADDRESS | 1); data=i2c_read(0);

i2c_stop(); return data;

}

/* write a byte to the EEPROM */

void eeprom_write(unsigned char address, unsigned char data) { i2c_start();

i2c_write(EEPROM_BUS_ADDRESS); i2c_write(address); i2c_write(data);

i2c_stop();

/* 10ms delay to complete the write operation */ delay_ms(10);

}

void main(void) { unsigned char i;

/* initialize the I2C bus */ i2c_init();

/* write the byte 55h at address AAh */ eeprom_write(0xaa,0x55);

/* read the byte from address AAh */ i=eeprom_read(0xaa);

while (1); /* loop forever */

}

© 1998-2007 HP InfoTech S.R.L.

Page 140


CodeVisionAVR

4.12.1 National Semiconductor LM75 Temperature Sensor

Functions

These functions are intended for easy interfacing between C programs and the LM75 I2C bus temperature sensor.

The prototypes for these functions are placed in the file lm75.h, located in the ..\INC subdirectory. This file must be #include -ed before using the functions.

The I2C bus functions prototypes are automatically #include -ed with the lm75.h.

Prior to #include -ing the lm75.h file, you must declare which microcontroller port and port bits are used for communication with the LM75 through the I2C bus.

Example:

/* the I2C bus is connected to PORTB */ /* the SDA signal is bit 3 */

/* the SCL signal is bit 4 */ #asm

.equ __i2c_port=0x18

.equ __sda_bit=3

.equ __scl_bit=4 #endasm

/* now you can include the LM75 Functions */ #include <lm75.h>

The LM75 Functions are:

void lm75_init(unsigned char chip,signed char thyst,signed char tos, unsigned char pol)

this function initializes the LM75 sensor chip.

Before calling this function the I2C bus must be initialized by calling the i2c_init function. This is the first function that must be called prior to using the other LM75 Functions.

If more then one chip is connected to the I2C bus, then the function must be called for each one, specifying accordingly the function parameter chip.

Maximum 8 LM75 chips can be connected to the I2C bus, their chip address can be from 0 to 7. The LM75 is configured in comparator mode, where it functions like a thermostat.

The O.S. output becomes active when the temperature exceeds the tos limit, and leaves the active state when the temperature drops below the thyst limit.

Both thyst and tos are expressed in °C.

pol represents the polarity of the LM75 O.S. output in active state.

If pol is 0, the output is active low and if pol is 1, the output is active high. Refer to the LM75 data sheet for more information.

int lm75_temperature_10(unsigned char chip)

this function returns the temperature of the LM75 sensor with the address chip. The temperature is in °C and is multiplied by 10.

A 300ms delay must be present between two successive calls to the lm75_temperature_10 function.

© 1998-2007 HP InfoTech S.R.L.

Page 141

CodeVisionAVR

Example how to display the temperature of two LM75 sensors with addresses 0 and 1:

/* the LM75 I2C bus is connected to PORTB */ /* the SDA signal is bit 3 */

/* the SCL signal is bit 4 */ #asm

.equ __i2c_port=0x18

.equ __sda_bit=3

.equ __scl_bit=4 #endasm

/* include the LM75 Functions */ #include <lm75.h>

/* the LCD module is connected to PORTC */ #asm

.equ __lcd_port=0x15 #endasm

/* include the LCD Functions */ #include <lcd.h>

/* include the prototype for sprintf */ #include <stdio.h>

/* include the prototype for abs */ #include <math.h>

/* include the prototypes for the delay functions */ #include <delay.h>

char display_buffer[33];

void main(void) { int t0,t1;

/* initialize the LCD, 2 rows by 16 columns */ lcd_init(16);

/* initialize the I2C bus */ i2c_init();

/* initialize the LM75 sensor with address 0 */ /* thyst=20°C tos=25°C */ lm75_init(0,20,25,0);

/* initialize the LM75 sensor with address 1 */ /* thyst=30°C tos=35°C */ lm75_init(1,30,35,0);

© 1998-2007 HP InfoTech S.R.L.

Page 142


CodeVisionAVR

/* temperature display loop */ while (1)

{

/* read the temperature of sensor #0 *10°C */ t0=lm75_temperature_10(0);

/* 300ms delay */ delay_ms(300);

/* read the temperature of sensor #1 *10°C */ t1=lm75_temperature_10(1);

/* 300ms delay */ delay_ms(300);

/* prepare the displayed temperatures */ /* in the display_buffer */ sprintf(display_buffer, "t0=%-i.%-u%cC\nt1=%-i.%-u%cC",

t0/10,abs(t0%10),0xdf,t1/10,abs(t1%10),0xdf);

/* display the temperatures */ lcd_clear(); lcd_puts(display_buffer);

};

}

© 1998-2007 HP InfoTech S.R.L.

Page 143

CodeVisionAVR

4.12.2 Maxim/Dallas Semiconductor DS1621 Thermometer/

Thermostat Functions

These functions are intended for easy interfacing between C programs and the DS1621 I2C bus thermometer/thermostat.

The prototypes for these functions are placed in the file ds1621.h, located in the ..\INC subdirectory. This file must be #include -ed before using the functions.

The I2C bus functions prototypes are automatically #include -ed with the ds1621.h.

Prior to #include -ing the ds1621.h file, you must declare which microcontroller port and port bits are used for communication with the DS1621 through the I2C bus.

Example:

/* the I2C bus is connected to PORTB */ /* the SDA signal is bit 3 */

/* the SCL signal is bit 4 */ #asm

.equ __i2c_port=0x18

.equ __sda_bit=3

.equ __scl_bit=4 #endasm

/* now you can include the DS1621 Functions */ #include <ds1621.h>

The DS1621 Functions are:

void ds1621_init(unsigned char chip,signed char tlow,signed char thigh, unsigned char pol)

this function initializes the DS1621 chip.

Before calling this function the I2C bus must be initialized by calling the i2c_init function. This is the first function that must be called prior to using the other DS1621 Functions.

If more then one chip is connected to the I2C bus, then the function must be called for each one, specifying accordingly the function parameter chip.

Maximum 8 DS1621 chips can be connected to the I2C bus, their chip address can be from 0 to 7. Besides measuring temperature, the DS1621 functions also like a thermostat.

The Tout output becomes active when the temperature exceeds the thigh limit, and leaves the active state when the temperature drops below the tlow limit.

Both tlow and thigh are expressed in °C.

pol represents the polarity of the DS1621 Tout output in active state.

If pol is 0, the output is active low and if pol is 1, the output is active high. Refer to the DS1621 data sheet for more information.

unsigned char ds1621_get_status(unsigned char chip)

this function reads the contents of the configuration/status register of the DS1621 with address chip.

Refer to the DS1621 data sheet for more information about this register.

© 1998-2007 HP InfoTech S.R.L.

Page 144