AVR+DS1302时钟芯片测试程序
扫描二维码
随时随地手机看文章
#include
#include
#include "delay.h"
#include "lcd.h"
#define uchar unsigned char
#define uint unsigned int
#define DS1302_RST_SET PORTA |= ( 1 << 0 )
#define DS1302_RST_CLR PORTA &=~ ( 1 << 0 )
#define DS1302_SCLK_SET PORTA |= ( 1 << 1 )
#define DS1302_SCLK_CLR PORTA &=~ ( 1 << 1 )
#define DS1302_DATA_SET PORTA |= ( 1 << 2 )
#define DS1302_DATA_CLR PORTA &=~ ( 1 << 2 )
#define DS1302_DTTA_CHECK PINA & ( 1 << 2 )
#define DS1302_CONTROL_DDR_OUT { DDRA |= ( 1 << 0 ) | ( 1 << 1 ); } //时钟口,以及启动口定义为输出, 注意这里的写法, 不影响其它位
#define DS1302_DATA_DDR_OUT DDRA |= ( 1 << 2 ) //数据口为输出
#define DS1302_DATA_DDR_IN { DDRA &=~ ( 1 << 2 ); PORTD |= ( 1 << 2 ); } //输入为高阻态
//-------------------------DS1302部分------------------------------------
//设置时间 年 月 日 周 时 分 秒
uchar DS1302TIME[8] = { 0x20, 0x08, 0x10, 0x17, 0x04, 0x21, 0x39, 0x56 };
uchar DS1302TIMEDISP[18];
//定义标志位, 初始化为0
uchar model = 0; //模式变化值
uchar t = 0;
uchar count = 0;
uchar count1sok = 0;
//DS1302初始化
//初始化要求/RST为低电平,SCLK为低电平
void DS1302_init( void )
{
DS1302_CONTROL_DDR_OUT; //控制口为输出
DS1302_DATA_DDR_IN; //数据口为输出, 高阻态
DS1302_RST_CLR; //启动为低电平
DS1302_SCLK_CLR; //时钟为低电平
}
//向地址写数据
//RST被设置为高就启动了一个数据传送的过程
void DS1302_write_bytetoaddr( uchar addr, uchar byte )
{
uchar addr_t = addr; //转移数据
uchar byte_t = byte;
uchar i = 0;
DS1302_DATA_DDR_OUT; //输出
DS1302_RST_CLR;
DS1302_SCLK_CLR; //时钟为低电平
DS1302_RST_SET; //启动数据传输
for( i = 0; i < 8; i++ )
{
if( addr_t & 0x01 )
DS1302_DATA_SET; //输出1
else
DS1302_DATA_CLR; //输出0
DS1302_SCLK_SET; //上升沿
DS1302_SCLK_CLR;
addr_t = addr_t >> 1;
}
for( i = 0; i < 8; i++ )
{
if( byte_t & 0x01 )
DS1302_DATA_SET;
else
DS1302_DATA_CLR;
DS1302_SCLK_SET;
DS1302_SCLK_CLR;
byte_t = byte_t >> 1;
}
DS1302_RST_CLR;
}
//从地址读数据
//RST被设置为高就启动了一个数据传送的过程
uchar DS1302_read_bytefromaddr( uchar addr )
{
uchar addr_t = addr; //转移数据
uchar byte_t;
uchar i = 0;
DS1302_DATA_DDR_OUT; //输出
DS1302_RST_CLR;
DS1302_SCLK_CLR; //时钟为低电平
DS1302_RST_SET; //启动数据传输
for( i = 0; i < 8; i++ )
{
if( addr_t & 0x01 )
DS1302_DATA_SET; //输出1
else
DS1302_DATA_CLR; //输出0
DS1302_SCLK_SET; //上升沿
DS1302_SCLK_CLR;
addr_t = addr_t >> 1;
}
DS1302_DATA_DDR_IN; //数据为输入高阻态
byte_t = 0;
for( i = 0; i < 8; i++ )
{
byte_t = byte_t >> 1; //先移位
if( DS1302_DTTA_CHECK ) //读数据输出口
byte_t |= 0x80;
else
byte_t &= 0x7f;
DS1302_SCLK_SET;
DS1302_SCLK_CLR;
}
DS1302_RST_CLR;
return byte_t;
}
//将十六进制码变为十进制码
//如: 0x45 -- > 45d
uchar hex2dec( uchar hex_data )
{
//十位 //个位
return ( ( ( hex_data >> 4 ) * 10 ) + ( hex_data & 0b00001111 ) );
}
//写时间年月日
//08-10-15 14:36:56
void DS1302_write_time( void )
{
DS1302_write_bytetoaddr( 0X8E, 0X00 ); //写控制寄存器 WP000 0000 <===> WP = 0, 数据可以写入
DS1302_write_bytetoaddr( 0X80, 0X80 ); //写秒寄存器, 停止时钟
DS1302_write_bytetoaddr( 0X8C, DS1302TIME[1] ); //年
DS1302_write_bytetoaddr( 0X88, DS1302TIME[2] ); //月
DS1302_write_bytetoaddr( 0X86, DS1302TIME[3] ); //日
DS1302_write_bytetoaddr( 0X8A, DS1302TIME[4] ); //周日
DS1302_write_bytetoaddr( 0X84, DS1302TIME[5] ); //小时
DS1302_write_bytetoaddr( 0X82, DS1302TIME[6] ); //分
DS1302_write_bytetoaddr( 0X80, DS1302TIME[7] ); //秒, 开户时钟脉冲
DS1302_write_bytetoaddr( 0X8E, 0X80 ); //写控制寄存器, 保护数据
}
//读时间年月日
void DS1302_read_time( void )
{
DS1302_write_bytetoaddr( 0X8E, 0X00 ); //写控制寄存器 WP000 0000 <===> WP = 0, 数据可以写入
DS1302TIME[1] = DS1302_read_bytefromaddr( 0X8D ); //年
DS1302TIME[2] = DS1302_read_bytefromaddr( 0X89 ); //月
DS1302TIME[3] = DS1302_read_bytefromaddr( 0X87 ); //日
DS1302TIME[4] = DS1302_read_bytefromaddr( 0X8B ); //周日
DS1302TIME[5] = DS1302_read_bytefromaddr( 0X85 ); //小时
DS1302TIME[6] = DS1302_read_bytefromaddr( 0X83 ); //分
DS1302TIME[7] = DS1302_read_bytefromaddr( 0X81 ); //秒, 开户时钟脉冲
DS1302_write_bytetoaddr( 0X8E, 0X80 ); //写控制寄存器, 保护数据
}
//将时间分解
void TimeSpare( void )
{
uchar t = 0;
uchar i = 0;
uchar j = 0;
for( i = 0; i < 8; i++ )
{
t = i * 2;
DS1302TIMEDISP[t] = DS1302TIME[i] >> 4;
DS1302TIMEDISP[t + 1] = DS1302TIME[i] & 0x0f;
}
}
//将时间合成
void TimeMix( void )
{
uchar t = 0;
uchar i = 0;
uchar j = 0;
for( i = 0; i < 8; i++ )
{
t = i * 2;
DS1302TIME[i] = ( ( DS1302TIMEDISP[t] << 4 ) | DS1302TIMEDISP[t + 1] );
}
}
//显示时间
void disptime( void )
{
TimeSpare(); //将每一个时间分隔为两部分,十位部分,个位部分。
LCD_write_char( 1, 8, DS1302TIMEDISP[2] + 0X30 );
LCD_write_char( 1, 9, DS1302TIMEDISP[3] + 0X30 );
LCD_write_char( 1, 10, '/' );
LCD_write_char( 1, 11, DS1302TIMEDISP[4] + 0X30 );
LCD_write_char( 1, 12, DS1302TIMEDISP[5] + 0X30 );
LCD_write_char( 1, 13, '/' );
LCD_write_char( 1, 14, DS1302TIMEDISP[6] + 0X30 );
LCD_write_char( 1, 15, DS1302TIMEDISP[7] + 0X30 );
LCD_write_char( 2, 8, DS1302TIMEDISP[10] + 0X30 );
LCD_write_char( 2, 9, DS1302TIMEDISP[11] + 0X30 );
LCD_write_char( 2, 10, ':' );
LCD_write_char( 2, 11, DS1302TIMEDISP[12] + 0X30 );
LCD_write_char( 2, 12, DS1302TIMEDISP[13] + 0X30 );
LCD_write_char( 2, 13, ':' );
LCD_write_char( 2, 14, DS1302TIMEDISP[14] + 0X30 );
LCD_write_char( 2, 15, DS1302TIMEDISP[15] + 0X30 );
}
//**********************************************************************************
//-----------------------T0计数部分主要对模式选择时闪动要更改的数据-----------------
//---------------------------------------按键部分------------------------------------
#define on 1
#define off 0
uchar model_year = 0; //年
uchar model_month = 0; //月
uchar model_date = 0; //日
uchar model_hour = 0; //时
uchar model_min = 0; //分
uchar model_sec = 0; //秒
//测度灯
void led_on( uchar onoff )
{
DDRB |= ( 1 << PB0 );
if( onoff == 1 )
PORTB &=~ ( 1 << PB0 );
if( onoff == 0 )
PORTB |= ( 1 << PB0 );
}
//按键引脚端口初始化
void key_port_init( void )
{
DDRD &=~ ( 1 << PD2 ) | ( 1 << PD4 ) | ( 1 << PD5 ) | ( 1 << PD6 ) | ( 1 << PD7 ); //定义上拉电阻
PORTD |= ( 1 << PD2 ) | ( 1 << PD4 ) | ( 1 << PD5 ) | ( 1 << PD6 ) | ( 1 << PD7 ); //不影响其它位
}
//T0寄存器初始化
//TIMER0 initialize - prescale:1024
// WGM: CTC
// desired value: 20mSec
// actual value: 20.096mSec (-0.5%)
void timer0_init(void)
{
TCCR0 = 0x00; //stop
TCNT0 = 0x64; //set count
OCR0 = 0x9C; //set compare
TCCR0 = 0x0D; //start timer
}
void flash_bit( void )
{
//判断model值, 闪动该位
switch( model )
{
//年十位
case 1:
{
if( t == 1 ) t = 0;
else if( t == 0 ) { LCD_write_char( 1, 8, ' ' ); delay_nms( 100 ); t = 1; }
}; break;
//年个位
case 2:
{
if( t == 1 ) t = 0;
else if( t == 0 ) { LCD_write_char( 1, 9, ' ' ); delay_nms( 100 ); t = 1; }
}; break;
//月十位
case 3:
{
if( t == 1 ) t = 0;
else if( t == 0 ) { LCD_write_char( 1, 11, ' ' ); delay_nms( 100 ); t = 1; }
}; break;
//月个位
case 4:
{
if( t == 1 ) t = 0;
else if( t == 0 ) { LCD_write_char( 1, 12, ' ' ); delay_nms( 100 ); t = 1; }
}; break;
//日十位
case 5:
{
if( t == 1 ) t = 0;
else if( t == 0 ) { LCD_write_char( 1, 14, ' ' ); delay_nms( 100 ); t = 1; }
}; break;
//日个位
case 6:
{
if( t == 1 ) t = 0;
else if( t == 0 ) { LCD_write_char( 1, 15, ' ' ); delay_nms( 100 ); t = 1; }
}; break;
//时十位
case 7:
{
if( t == 1 ) t = 0;
else if( t == 0 ) { LCD_write_char( 2, 8, ' ' ); delay_nms( 100 ); t = 1; }
}; break;
//时个位
case 8:
{
if( t == 1 ) t = 0;
else if( t == 0 ) { LCD_write_char( 2, 9, ' ' ); delay_nms( 100 ); t = 1; }
}; break;
//分十位
case 9:
{
if( t == 1 ) t = 0;
else if( t == 0 ) { LCD_write_char( 2, 11, ' ' ); delay_nms( 100 ); t = 1; }
}; break;
//分个位
case 10:
{
if( t == 1 ) t = 0;
else if( t == 0 ) { LCD_write_char( 2, 12, ' ' ); delay_nms( 100 ); t = 1; }
}; break;
//秒十位
case 11:
{
if( t == 1 ) t = 0;
else if( t == 0 ) { LCD_write_char( 2, 14, ' ' ); delay_nms( 100 ); t = 1; }
}; break;
//秒个位
case 12:
{
if( t == 1 ) t = 0;
else if( t == 0 ) { LCD_write_char( 2, 15, ' ' ); delay_nms( 100 ); t = 1; }
}; break;
default: break;
}
}
//模式闪动
void modelflash( void )
{
model++;
model %= 13;
}
//加
void add_timebybit( void )
{
switch( model )
{
case 1: DS1302TIMEDISP[2]++; if( DS1302TIMEDISP[2] == 10 ) DS1302TIMEDISP[2] = 0; break;
case 2: DS1302TIMEDISP[3]++; if( DS1302TIMEDISP[3] == 10 ) DS1302TIMEDISP[3] = 0; break;
case 3: DS1302TIMEDISP[4]++; if( DS1302TIMEDISP[4] == 2 ) DS1302TIMEDISP[4] = 0; break;
case 4: DS1302TIMEDISP[5]++; if( DS1302TIMEDISP[5] == 10 ) DS1302TIMEDISP[5] = 0; break;
case 5: DS1302TIMEDISP[6]++; if( DS1302TIMEDISP[6] == 4 ) DS1302TIMEDISP[6] = 0; break;
case 6: DS1302TIMEDISP[7]++; if( DS1302TIMEDISP[7] == 10 ) DS1302TIMEDISP[7] = 0; break;
case 7: DS1302TIMEDISP[10]++; if( DS1302TIMEDISP[10] == 3 ) DS1302TIMEDISP[10] = 0; break;
case 8: DS1302TIMEDISP[11]++; if( DS1302TIMEDISP[11] == 10 ) DS1302TIMEDISP[11] = 0; break;
case 9: DS1302TIMEDISP[12]++; if( DS1302TIMEDISP[12] == 7 ) DS1302TIMEDISP[12] = 0; break;
case 10: DS1302TIMEDISP[13]++; if( DS1302TIMEDISP[13] == 10 ) DS1302TIMEDISP[13] = 0; break;
case 11: DS1302TIMEDISP[14]++; if( DS1302TIMEDISP[14] == 7 ) DS1302TIMEDISP[14] = 0; break;
case 12: DS1302TIMEDISP[15]++; if( DS1302TIMEDISP[15] == 10 ) DS1302TIMEDISP[15] = 0; break;
default : break;
}
TimeMix();
DS1302_write_time();
}
//确定部分
void sure( void )
{
model = 0;
}
//中断服务程序
#pragma interrupt_handler int0_isr:iv_INT0
void int0_isr( void )
{
//external interupt on INT0
CLI(); //关闭全局中断
if( ( ( PIND & 0b11110100 ) != 0b11110100 ) )
{
delay_nms( 40 ); //去除抖动
if( ( ( PIND & 0B11110100 ) != 0B11110100 ) )
{
switch( PIND & 0B11110100 )
{
//模式
case 0b11100000 : modelflash(); break;
//加
case 0b11010000 : add_timebybit(); break;
//减
case 0b10110000 : break;
//确定
case 0b01110000 : sure(); break;
default : break;
}
}
}
SEI();
}
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
key_port_init();
LCD_init();
DS1302_init();
//timer0_init();
MCUCR = 0x02;
GICR = 0x40;
TIMSK = 0x02; //timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
//**********************************************************************************
void main( void )
{
init_devices();
LCD_write_string( 1, 0, "DATA 20 / / " );
LCD_write_string( 2, 0, "TIME : : " );
DS1302_write_time();
while( 1 )
{
DS1302_read_time();
disptime();
delay_nms( 100 );
flash_bit();
}
}