//DS1302的示例程序,需关闭JTAG功能,在程序中关闭的
//在LCD1602上显示写入和读出的时间和日历数据,不提供断电保持功能
//编译环境 AVR Studio 4.13/AVR GCC
//系统时钟7.3728MHz,设置熔丝位为外部高频石英晶体振荡,启动时间4.1ms
//***********************************************************************
// 包含文件
//***********************************************************************
#include <string.h>
#include <stdio.h>
#define F_CPU 7372800 // 单片机 主频为7.3728MHz,用于延时子程序
#include <util/delay.h>
#include <avr/io.h>
#include <avr/iom16.h>
#include <avr/signal.h> //中断信号头文件
//***********************************************************************
// 定义变量区
//***********************************************************************
/*复位脚*/
#define RST_CLR PORTC &= ~(1 << PC2) //电平置低
#define RST_SET PORTC |= (1 << PC2) //电平置高
#define RST_IN DDRC &= ~(1 << PC2) //方向输入
#define RST_OUT DDRC |= (1 << PC2) //方向输出
/*双向数据*/
#define IO_CLR PORTC &= ~(1 << PC3) //电平置低
#define IO_SET PORTC |= (1 << PC3) //电平置高
#define IO_R PINC & (1 << PC3) //电平读取
#define IO_IN DDRC &= ~(1 << PC3) //方向输入
#define IO_OUT DDRC |= (1 << PC3) //方向输出
/*时钟信号*/
#define SCK_CLR PORTC &= ~(1 << PC4) //时钟信号
#define SCK_SET PORTC |= (1 << PC4) //电平置高
#define SCK_IN DDRC &= ~(1 << PC4) //方向输入
#define SCK_OUT DDRC |= (1 << PC4) //方向输出
#define RS_CLR PORTD &= ~(1 << PD5) //RS置低
#define RS_SET PORTD |= (1 << PD5) //RS置高
#define RW_CLR PORTD &= ~(1 << PD6) //RW置低
#define RW_SET PORTD |= (1 << PD6) //RW置高
#define EN_CLR PORTD &= ~(1 << PD7) //E置低
#define EN_SET PORTD |= (1 << PD7) //E置高
#define ds1302_sec_add 0x80 //秒数据地址
#define ds1302_min_add 0x82 //分数据地址
#define ds1302_hr_add 0x84 //时数据地址
#define ds1302_date_add 0x86 //日数据地址
#define ds1302_month_add 0x88 //月数据地址
#define ds1302_day_add 0x8a //星期数据地址
#define ds1302_year_add 0x8c //年数据地址
#define ds1302_control_add 0x8e //控制数据地址
#define ds1302_charger_add 0x90
#define ds1302_clkburst_add 0xbe
#define delay_us(x) _delay_us(x) //AVR GCC延时函数 x(us)
#define delay_ms(x) _delay_ms(x) //AVR GCC延时函数 x(ms)
#define uchar unsigned char
#define uint unsigned int
unsigned char time_buf[8] = {0x20,0x08,0x11,0x08,0x09,0x55,0x00,0x02};
//***********************************************************************
//向DS1302写入一字节数据
//***********************************************************************
void ds1302_write_byte(unsigned char addr, unsigned char d) {
unsigned char i;
RST_SET; //启动DS1302总线
//写入目标地址:addr
IO_OUT;
addr = addr & 0xFE; //最低位置零
for (i = 0; i < 8; i ++) {
if (addr & 0x01) {
IO_SET;
}
else {
IO_CLR;
}
SCK_SET;
SCK_CLR;
addr = addr >> 1;
}
//写入数据:d
IO_OUT;
for (i = 0; i < 8; i ++) {
if (d & 0x01) {
IO_SET;
}
else {
IO_CLR;
}
SCK_SET;
SCK_CLR;
d = d >> 1;
}
RST_CLR; //停止DS1302总线
}
//***********************************************************************
// 从DS1302读出一字节数据
//***********************************************************************
unsigned char ds1302_read_byte(unsigned char addr) {
unsigned char i;
unsigned char temp;
RST_SET; //启动DS1302总线
//写入目标地址:addr
IO_OUT;
addr = addr | 0x01; //最低位置高
for (i = 0; i < 8; i ++) {
if (addr & 0x01) {
IO_SET;
}
else {
IO_CLR;
}
SCK_SET;
SCK_CLR;
addr = addr >> 1;
[1] [2] [3] 下一页
本文关键字:程序 AVR单片机,单片机-工控设备 - AVR单片机