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

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

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

Добавлен: 13.06.2025

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

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

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

CodeVisionAVR

#include <ds1307.h>

void main(void) { unsigned char h,m,s;

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

/* initialize the DS1307 RTC */ rtc_init(0,0,0);

/* read time from the DS1307 RTC */ rtc_get_time(&h,&m,&s);

/* ........

*/

}

void rtc_set_time(unsigned char hour, unsigned char min, unsigned char sec)

this function sets the current time of the RTC.

The hour, min and sec parameters represent the values of hour, minutes and seconds.

void rtc_get_date(unsigned char *date, unsigned char *month, unsigned char *year)

this function returns the current date measured by the RTC.

The *date, *month and *year pointers must point to the variables that must receive the values of date, month and year.

void rtc_set_date(unsigned char date, unsigned char month, unsigned char year)

this function sets the current date of the RTC.

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

Page 154

CodeVisionAVR

4.13 Maxim/Dallas Semiconductor DS1302 Real Time Clock

Functions

These functions are intended for easy interfacing between C programs and the DS1302 real time clock (RTC).

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

Prior to #include -ing the ds1302.h file, you must declare which microcontroller port and port bits are used for communication with the DS1302.

Example:

/* the DS1302 is connected to PORTB */ /* the IO signal is bit 3 */

/* the SCLK signal is bit 4 */ /* the RST signal is bit 5 */ #asm

.equ __ds1302_port=0x18

.equ __ds1302_io=3

.equ __ds1302_sclk=4

.equ __ds1302_rst=5 #endasm

/* now you can include the DS1302 Functions */ #include <ds1302.h>

The DS1302 Functions are:

void rtc_init(unsigned char tc_on, unsigned char diodes, unsigned char res)

this function initializes the DS1302 chip.

This is the first function that must be called prior to using the other DS1302 Functions. If the tc_on parameter is set to 1 then the DS1302’s trickle charge function is enabled.

The diodes parameter specifies the number of diodes used when the trickle charge function is enabled. This parameter can take the value 1 or 2.

The res parameter specifies the value of the trickle charge resistor:

0 for no resistor

1 for a 2kΩ resistor

2 for a 4kΩ resistor

3 for a 8kΩ resistor.

Refer to the DS1302 data sheet for more information.

unsigned char ds1302_read(unsigned char addr)

this function reads a byte stored at address addr in the DS1302 registers or SRAM.

void ds1302_write(unsigned char addr, unsigned char data)

this function stores the byte data at address addr in the DS1302 registers or SRAM.

void rtc_get_time(unsigned char *hour, unsigned char *min, unsigned char *sec)

this function returns the current time measured by the RTC.

The *hour, *min and *sec pointers must point to the variables that must receive the values of hours, minutes and seconds.

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

Page 155


CodeVisionAVR

Example:

#asm

.equ __ds1302_port=0x18

.equ __ds1302_io=3

.equ __ds1302_sclk=4

.equ __ds1302_rst=5 #endasm

#include <ds1302.h>

void main(void) { unsigned char h,m,s;

/* initialize the DS1302 RTC: use trickle charge,

with 1 diode and 8K resistor */ rtc_init(1,1,3);

/* read time from the DS1302 RTC */ rtc_get_time(&h,&m,&s);

/* ........

*/

}

void rtc_set_time(unsigned char hour, unsigned char min, unsigned char sec)

this function sets the current time of the RTC.

The hour, min and sec parameters represent the values of hour, minutes and seconds.

void rtc_get_date(unsigned char *date, unsigned char *month, unsigned char *year)

this function returns the current date measured by the RTC.

The *date, *month and *year pointers must point to the variables that must receive the values of date, month and year.

void rtc_set_date(unsigned char date, unsigned char month, unsigned char year)

this function sets the current date of the RTC.

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

Page 156


CodeVisionAVR

4.14 1 Wire Protocol Functions

The 1 Wire Functions are intended for easy interfacing between C programs and various peripherals using the Maxim/Dallas Semiconductor 1 Wire protocol.

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

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

Prior to #include -ing the 1wire.h file, you must declare which microcontroller port and port bit is used for communication through the 1 Wire protocol.

Example:

/* the 1 Wire bus is connected to PORTB */ /* the data signal is bit 2 */

#asm

.equ __w1_port=0x18

.equ __w1_bit=2 #endasm

/* now you can include the 1 Wire Functions */ #include <1wire.h>

Because the 1 Wire Functions require precision time delays for correct operation, the interrupts must be disabled during their execution.

Also it is very important to specify the correct AVR chip clock frequency in the Project|Configure|C Compiler|Code Generation menu.

The 1 Wire Functions are:

unsigned char w1_init(void)

this function initializes the 1 Wire devices on the bus. It returns 1 if there were devices present or 0 if not.

unsigned char w1_read(void)

this function reads a byte from the 1 Wire bus.

unsigned char w1_write(unsigned char data)

this function writes the byte data to the 1 Wire bus.

It returns 1 if the write process completed normally or 0 if not.

unsigned char w1_search(unsigned char cmd,void *p)

this function returns the number of devices connected to the 1 Wire bus. If no devices were detected then it returns 0.

The byte cmd represents the Search ROM (F0h), Alarm Search (ECh) for the DS1820/DS18S20, or other similar commands, sent to the 1 Wire device.

The pointer p points to an area of SRAM where are stored the 8 bytes ROM codes returned by the device. After the eighth byte, the function places a ninth status byte which contains a status bit returned by some 1 Wire devices (e.g. DS2405).

Thus the user must allocate 9 bytes of SRAM for each device present on the 1 Wire bus.

If there is more then one device connected to the 1 Wire bus, than the user must first call the w1_search function to identify the ROM codes of the devices and to be able to address them at a later stage in the program.

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

Page 157

CodeVisionAVR

Example:

#include <90s8515.h>

/* specify the port and bit used for the 1 Wire bus */ #asm

.equ __w1_port=0x18 ;PORTB

.equ __w1_bit=2 #endasm

/* include the 1 Wire bus functions prototypes */ #include <1wire.h>

/* include the printf function prototype */ #include <stdio.h>

/* specify the maximum number of devices connected to the 1 Wire bus */

#define MAX_DEVICES 8

/* allocate SRAM space for the ROM codes & status bit */ unsigned char rom_codes[MAX_DEVICES][9];

/* quartz crystal frequency [Hz] */ #define xtal 4000000L

/* Baud rate */ #define baud 9600

void main(void) { unsigned char i,j,devices;

/* initialize the UART's baud rate */ UBRR=xtal/16/baud-1;

/* initialize the UART control register

TX enabled, no interrupts, 8 data bits */ UCR=8;

/* detect how many DS1820/DS18S20 devices are connected to the bus and

store their ROM codes in the rom_codes array */ devices=w1_search(0xf0,rom_codes);

/* display the ROM codes for each detected device */ printf("%-u DEVICE(S) DETECTED\n\r",devices);

if (devices) {

for (i=0;i<devices;i++) {

printf("DEVICE #%-u ROM CODE IS:", i+1);

for (j=0;j<8;j++) printf("%-X ",rom_codes[i][j]); printf("\n\r");

};

};

while (1); /* loop forever */

}

unsigned char w1_crc8(void *p, unsigned char n)

this function returns the 8 bit DOW CRC for a block of bytes with the length n, starting from address p.

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

Page 158


CodeVisionAVR

4.14.1 Maxim/Dallas Semiconductor DS1820/DS18S20 Temperature

Sensors Functions

These functions are intended for easy interfacing between C programs and the DS1820/DS18S20 1 Wire bus temperature sensors.

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

The 1 Wire bus functions prototypes are automatically #include -ed with the ds1820.h.

Prior to #include -ing the ds1820.h file, you must declare which microcontroller port and port bit are used for communication with the DS1820/DS18S20 through the 1 Wire bus.

Example:

/* specify the port and bit used for the 1 Wire bus */ #asm

.equ __w1_port=0x18 ;PORTB

.equ __w1_bit=2 #endasm

/* include the DS1820/DS18S20 functions prototypes */ #include <ds1820.h>

The DS1820/DS18S20 functions are:

unsigned char ds1820_read_spd(unsigned char *addr)

this function reads the contents of the SPD for the DS1820/DS18S20 sensor with the ROM code stored in an array of 8 bytes located at address addr.

The functions returns the value 1 on succes and 0 in case of error.

If only one DS1820/DS18S20 sensor is used, no ROM code array is necessary and the pointer addr must be NULL (0).

The contents of the SPD will be stored in the structure:

struct __ds1820_scratch_pad_struct

{

unsigned char temp_lsb,temp_msb, temp_high,temp_low, res1,res2, cnt_rem,cnt_c,

crc;

} __ds1820_scratch_pad;

defined in the ds1820.h header file.

int ds1820_temperature_10(unsigned char *addr)

this function returns the temperature of the DS1820/DS18S20 sensor with the ROM code stored in an array of 8 bytes located at address addr.

The temperature is measured in °C and is multiplied by 10. In case of error the function returns the value -9999.

If only one DS1820/DS18S20 sensor is used, no ROM code array is necessary and the pointer addr must be NULL (0).

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

Page 159

CodeVisionAVR

If several sensors are used, then the program must first identify the ROM codes for all the sensors. Only after that the ds1820_temperature_10 function may be used, with the addr pointer pointing to the array which holds the ROM code for the needed device.

Example:

#include <90s8515.h>

/* specify the port and bit used for the 1 Wire bus */ #asm

.equ __w1_port=0x18 ;PORTB

.equ __w1_bit=2 #endasm

/* include the DS1820/DS18S20 functions prototypes */ #include <ds1820.h>

/* include the printf function prototype */ #include <stdio.h>

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

/* quartz crystal frequency [Hz] */ #define xtal 4000000L

/* Baud rate */ #define baud 9600

/* maximum number of DS1820/DS18S20 connected to the bus */ #define MAX_DEVICES 8

/* DS1820/DS18S20 devices ROM code storage area, 9 bytes are used for each device

(see the w1_search function description),

but only the first 8 bytes contain the ROM code and CRC */

unsigned char rom_codes[MAX_DEVICES][9];

main()

{

unsigned char i,devices; int temp;

/* initialize the UART's baud rate */ UBRR=xtal/16/baud-1;

/* initialize the UART control register

TX enabled, no interrupts, 8 data bits */ UCR=8;

/* detect how many DS1820/DS18S20 devices are connected to the bus and

store their ROM codes in the rom_codes array */ devices=w1_search(0xf0,rom_codes);

/* display the number */

printf("%-u DEVICE(S) DETECTED\n\r",devices);

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

Page 160


CodeVisionAVR

/* if no devices were detected then halt */ if (devices==0) while (1); /* loop forever */

/* measure and display the temperature(s) */ while (1)

{

for (i=0;i<devices;)

{

temp=ds1820_temperature_10(&rom_codes[i][0]); printf("t%-u=%-i.%-u\xf8C\n\r",++i,temp/10, abs(temp%10));

};

};

}

unsigned char ds1820_set_alarm(unsigned char *addr,signed char temp_low, signed char temp_high)

this function sets the low (temp_low) and high (temp_high) temperature alarms of the DS1820/DS18S20.

In case of success the function returns the value 1, else it returns 0.

The alarm temperatures are stored in both the DS1820/DS18S20's scratchpad SRAM and its EEPROM.

The ROM code needed to address the device is stored in an array of 8 bytes located at address addr. If only one DS1820/DS18S20 sensor is used, no ROM code array is necessary and the pointer addr must be NULL (0).

The alarm status for all the DS1820/DS18S20 devices on the 1 Wire bus can be determined by calling the w1_search function with the Alarm Search (ECh) command.

Example:

#include <90s8515.h>

/* specify the port and bit used for the 1 Wire bus */ #asm

.equ __w1_port=0x18 ;PORTB

.equ __w1_bit=2 #endasm

/* include the DS1820/DS18S20 functions prototypes */ #include <ds1820.h>

/* include the printf function prototype */ #include <stdio.h>

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

/* quartz crystal frequency [Hz] */ #define xtal 4000000L

/* Baud rate */ #define baud 9600

/* maximum number of DS1820/DS18S20 connected to the bus */ #define MAX_DEVICES 8

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

Page 161

CodeVisionAVR

/* DS1820/DS18S20 devices ROM code storage area, 9 bytes are used for each device

(see the w1_search function description),

but only the first 8 bytes contain the ROM code and CRC */ unsigned char rom_codes[MAX_DEVICES][9];

/* allocate space for ROM codes of the devices which generate an alarm */

unsigned char alarm_rom_codes[MAX_DEVICES][9];

main()

{

unsigned char i,devices; int temp;

/* initialize the UART's baud rate */ UBRR=xtal/16/baud-1;

/* initialize the UART control register

TX enabled, no interrupts, 8 data bits */ UCR=8;

/* detect how many DS1820/DS18S20 devices are connected to the bus and

store their ROM codes in the rom_codes array */ devices=w1_search(0xf0,rom_codes);

/* display the number */

printf("%-u DEVICE(S) DETECTED\n\r",devices);

/* if no devices were detected then halt */ if (devices==0) while (1); /* loop forever */

/* set the temperature alarms for all the devices temp_low=25°C temp_high=35°C */

for (i=0;i<devices;i++)

{

printf("INITIALIZING DEVICE #%-u ", i+1);

if (ds1820_set_alarm(&rom_codes[i][0],25,35)) putsf("OK"); else putsf("ERROR");

};

while (1)

{

/* measure and display the temperature(s) */ for (i=0;i<devices;)

{

temp=ds1820_temperature_10(&rom_codes[i][0]); printf("t%-u=%-i.%-u\xf8C\n\r",++i,temp/10, abs(temp%10));

};

/* display the number of devices which generated an alarm */

printf("ALARM GENERATED BY %-u DEVICE(S)\n\r", w1_search(0xec,alarm_rom_codes));

};

}

Refer to the DS1820/DS18S20 data sheet for more information.

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

Page 162