ВУЗ: Не указан
Категория: Не указан
Дисциплина: Не указана
Добавлен: 13.06.2025
Просмотров: 2216
Скачиваний: 0
CodeVisionAVR
void ds1621_set_status(unsigned char chip, unsigned char data)
this function sets 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.
void ds1621_start(unsigned char chip)
this functions exits the DS1621, with address chip, from the power-down mode and starts the temperature measurements and the thermostat.
void ds1621_stop(unsigned char chip)
this functions enters the DS1621, with address chip, in power-down mode and stops the temperature measurements and the thermostat.
int ds1621_temperature_10(unsigned char chip)
this function returns the temperature of the DS1621 sensor with the address chip. The temperature is in °C and is multiplied by 10.
Example how to display the temperature of two DS1621 sensors with addresses 0 and 1:
/* the DS1621 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 DS1621 Functions */ #include <ds1621.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>
char display_buffer[33];
void main(void) { int t0,t1;
/* initialize the LCD, 2 rows by 16 columns */ lcd_init(16);
© 1998-2007 HP InfoTech S.R.L. |
Page 145 |
CodeVisionAVR
/* initialize the I2C bus */ i2c_init();
/* initialize the DS1621 sensor with address 0 */ /* tlow=20°C thigh=25°C */ ds1621_init(0,20,25,0);
/* initialize the DS1621 sensor with address 1 */ /* tlow=30°C thigh=35°C */ ds1621_init(1,30,35,0);
/* temperature display loop */ while (1)
{
/* read the temperature of DS1621 #0 *10°C */ t0=ds1621_temperature_10(0);
/* read the temperature of DS1621 #1 *10°C */ t1=ds1621_temperature_10(1);
/* 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 146 |
CodeVisionAVR
4.12.3 Philips PCF8563 Real Time Clock Functions
These functions are intended for easy interfacing between C programs and the PCF8563 I2C bus real time clock (RTC).
The prototypes for these functions are placed in the file pcf8563.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 pcf8563.h.
Prior to #include -ing the pcf8563.h file, you must declare which microcontroller port and port bits are used for communication with the PCF8563 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 PCF8563 Functions */ #include <pcf8563.h>
The PCF8563 Functions are:
void rtc_init(unsigned char ctrl2, unsigned char clkout, unsigned char timer_ctrl)
this function initializes the PCF8563 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 PCF8563 Functions. Only one PCF8583 chip can be connected to the I2C bus.
The ctrl2 parameter specifies the initialization value for the PCF8563 Control/Status 2 register. The pcf8563.h header file defines the following macros which allow the easy setting of the ctrl2 parameter:
•RTC_TIE_ON sets the Control/Status 2 register bit TIE to 1
•RTC_AIE_ON sets the Control/Status 2 register bit AIE to 1
•RTC_TP_ON sets the Control/Status 2 register bit TI/TP to 1
These macros can be combined using the | operator in order to set more bits to 1.
The clkout parameter specifies the initialization value for the PCF8563 CLKOUT Frequency register. The pcf8563.h header file defines the following macros which allow the easy setting of the clkout parameter:
•RTC_CLKOUT_OFF disables the generation of pulses on the PCF8563 CLKOUT output
•RTC_CLKOUT_1 generates 1Hz pulses on the PCF8563 CLKOUT output
•RTC_CLKOUT_32 generates 32Hz pulses on the PCF8563 CLKOUT output
•RTC_CLKOUT_1024 generates 1024Hz pulses on the PCF8563 CLKOUT output
•RTC_CLKOUT_32768 generates 32768Hz pulses on the PCF8563 CLKOUT output.
The timer_ctrl parameter specifies the initialization value for the PCF8563 Timer Control register. The pcf8563.h header file defines the following macros which allow the easy setting of the timer_ctrl parameter:
•RTC_TIMER_OFF disables the PCF8563 Timer countdown
•RTC_TIMER_CLK_1_60 sets the PCF8563 Timer countdown clock frequency to 1/60Hz
•RTC_TIMER_CLK_1 sets the PCF8563 Timer countdown clock frequency to 1Hz
•RTC_TIMER_CLK_64 sets the PCF8563 Timer countdown clock frequency to 64Hz
•RTC_TIMER_CLK_4096 sets the PCF8563 Timer countdown clock frequency to 4096Hz. Refer to the PCF8563 data sheet for more information.
© 1998-2007 HP InfoTech S.R.L. |
Page 147 |
CodeVisionAVR
unsigned char rtc_read(unsigned char address)
this function reads the byte stored in a PCF8563 register at address.
void rtc_write(unsigned char address, unsigned char data)
this function stores the byte data in the PCF8583 register at address.
unsigned char 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 hour, minutes and seconds.
The function return the value 1 if the read values are correct.
If the function returns 0 then the chip supply voltage has dropped below the Vlow value and the time values are incorrect.
Example:
#asm
.equ __i2c_port=0x18
.equ __sda_bit=3
.equ __scl_bit=4 #endasm
#include <pcf8563.h>
void main(void) { unsigned char ok,h,m,s;
/* initialize the I2C bus */ i2c_init();
/* initialize the RTC, Timer interrupt enabled, Alarm interrupt enabled, CLKOUT frequency=1Hz
Timer clock frequency=1Hz */
rtc_init(RTC_TIE_ON | RTC_AIE_ON,RTC_CLKOUT_1,RTC_TIMER_CLK_1);
/* read time from the RTC */ ok=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 *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 day, month and year.
void rtc_set_date(unsigned char date, unsigned char month, unsigned year)
this function sets the current date of the RTC .
© 1998-2007 HP InfoTech S.R.L. |
Page 148 |
CodeVisionAVR
void rtc_alarm_off(void)
this function disables the RTC alarm function.
void rtc_alarm_on(void)
this function enables the RTC alarm function.
void rtc_get_alarm(unsigned char *date, unsigned char *hour, unsigned char *min)
this function returns the alarm time and date of the RTC.
The *date, *hour and *min pointers must point to the variables that must receive the values of date, hour and minutes.
void rtc_set_alarm(unsigned char date, unsigned char hour, unsigned char min)
this function sets the alarm time and date of the RTC.
The date, hour and min parameters represent the values of date, hours and minutes. If date is set to 0, then this parameter will be ignored.
After calling this function the alarm will be turned off. It must be enabled using the rtc_alarm_on function.
void rtc_set_timer(unsigned char val)
this function sets the countdown value of the PCF8563 Timer.
© 1998-2007 HP InfoTech S.R.L. |
Page 149 |
CodeVisionAVR
4.12.4 Philips PCF8583 Real Time Clock Functions
These functions are intended for easy interfacing between C programs and the PCF8583 I2C bus real time clock (RTC).
The prototypes for these functions are placed in the file pcf8583.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 pcf8583.h.
Prior to #include -ing the pcf8583.h file, you must declare which microcontroller port and port bits are used for communication with the PCF8583 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 PCF8583 Functions */ #include <pcf8583.h>
The PCF8583 Functions are:
void rtc_init(unsigned char chip, unsigned char dated_alarm)
this function initializes the PCF8583 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 PCF8583 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 2 PCF8583 chips can be connected to the I2C bus, their chip address can be 0 or 1. The dated_alarm parameter specifies if the RTC alarm takes in account both the time and date (dated_alarm=1), or only the time (dated_alarm=0).
Refer to the PCF8583 data sheet for more information. After calling this function the RTC alarm is disabled.
unsigned char rtc_read(unsigned char chip, unsigned char address)
this function reads the byte stored in the PCF8583 SRAM.
void rtc_write(unsigned char chip, unsigned char address, unsigned char data)
this function stores the byte data in the PCF8583 SRAM.
When writing to the SRAM the user must take in account that locations at addresses 10h and 11h are used for storing the current year value.
unsigned char rtc_get_status(unsigned char chip)
this function returns the value of the PCF8583 control/status register.
By calling this function the global variables __rtc_status and __rtc_alarm are automatically updated. The __rtc_status variable holds the value of the PCF8583 control/status register.
The __rtc_alarm variable takes the value 1 if an RTC alarm occurred.
© 1998-2007 HP InfoTech S.R.L. |
Page 150 |
CodeVisionAVR
void rtc_get_time(unsigned char chip, unsigned char *hour, unsigned char *min, unsigned char *sec, unsigned char *hsec)
this function returns the current time measured by the RTC.
The *hour, *min, *sec and *hsec pointers must point to the variables that must receive the values of hour, minutes, seconds and hundreds of a second.
Example:
#asm
.equ __i2c_port=0x18
.equ __sda_bit=3
.equ __scl_bit=4 #endasm
#include <pcf8583.h>
void main(void) { unsigned char h,m,s,hs;
/* initialize the I2C bus */ i2c_init();
/* initialize the RTC 0, no dated alarm */
rtc_init(0,0);
/* read time from RTC 0*/ rtc_get_time(0,&h,&m,&s,&hs);
/* ........ |
*/ |
} |
void rtc_set_time(unsigned char chip, unsigned char hour, unsigned char min, unsigned char sec, unsigned char hsec)
this function sets the current time of the RTC.
The hour, min, sec and hsec parameters represent the values of hour, minutes, seconds and hundreds of a second.
void rtc_get_date(unsigned char chip, unsigned char *date, unsigned char *month, unsigned *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 day, month and year.
void rtc_set_date(unsigned char chip, unsigned char date, unsigned char month, unsigned year)
this function sets the current date of the RTC.
void rtc_alarm_off(unsigned char chip)
this function disables the RTC alarm function.
void rtc_alarm_on(unsigned char chip)
this function enables the RTC alarm function.
© 1998-2007 HP InfoTech S.R.L. |
Page 151 |
CodeVisionAVR
void rtc_get_alarm_time(unsigned char chip, unsigned char *hour, unsigned char *min, unsigned char *sec, unsigned char *hsec)
this function returns the alarm time of the RTC.
The *hour, *min, *sec and *hsec pointers must point to the variables that must receive the values of hours, minutes, seconds and hundreds of a second.
void rtc_set_alarm_time(unsigned char chip, unsigned char hour, unsigned char min, unsigned char sec, unsigned char hsec)
this function sets the alarm time of the RTC.
The hour, min, sec and hsec parameters represent the values of hours, minutes, seconds and hundreds of a second.
void rtc_get_alarm_date(unsigned char chip, unsigned char *date, unsigned char *month)
this function returns the alarm date of the RTC.
The *day and *month pointers must point to the variables that must receive the values of date and month.
void rtc_set_alarm_date(unsigned char chip, unsigned char date, unsigned char month)
this function sets the alarm date of the RTC.
© 1998-2007 HP InfoTech S.R.L. |
Page 152 |
CodeVisionAVR
4.12.5 Maxim/Dallas Semiconductor DS1307 Real Time Clock
Functions
These functions are intended for easy interfacing between C programs and the DS1307 I2C bus real time clock (RTC).
The prototypes for these functions are placed in the file ds1307.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 ds1307.h.
Prior to #include -ing the ds1307.h file, you must declare which microcontroller port and port bits are used for communication with the DS1307 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 DS1307 Functions */ #include <ds1307.h>
The DS1307 Functions are:
void rtc_init(unsigned char rs, unsigned char sqwe, unsigned char out)
this function initializes the DS1307 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 DS1307 Functions.
The rs parameter specifies the value of the square wave output frequency on the SQW/OUT pin:
•0 for 1Hz
•1 for 4096Hz
•2 for 8192Hz
•3 for 32768Hz.
If the sqwe parameter is set to 1 then the square wave output on the SQW/OUT pin is enabled. The out parameter specifies the logic level on the SQW/OUT pin when the square wave output is disabled (sqwe=0).
Refer to the DS1307 data sheet for more information.
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.
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
© 1998-2007 HP InfoTech S.R.L. |
Page 153 |