AVR+DS1302時(shí)鐘芯片測(cè)試程序
#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 ); } //時(shí)鐘口,以及啟動(dòng)口定義為輸出, 注意這里的寫法, 不影響其它位
#define DS1302_DATA_DDR_OUT DDRA |= ( 1 << 2 ) //數(shù)據(jù)口為輸出
#define DS1302_DATA_DDR_IN { DDRA &=~ ( 1 << 2 ); PORTD |= ( 1 << 2 ); } //輸入為高阻態(tài)
//-------------------------DS1302部分------------------------------------
//設(shè)置時(shí)間 年 月 日 周 時(shí) 分 秒
uchar DS1302TIME[8] = { 0x20, 0x08, 0x10, 0x17, 0x04, 0x21, 0x39, 0x56 };
uchar DS1302TIMEDISP[18];
//定義標(biāo)志位, 初始化為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; //數(shù)據(jù)口為輸出, 高阻態(tài)
DS1302_RST_CLR; //啟動(dòng)為低電平
DS1302_SCLK_CLR; //時(shí)鐘為低電平
}
//向地址寫數(shù)據(jù)
//RST被設(shè)置為高就啟動(dòng)了一個(gè)數(shù)據(jù)傳送的過程
void DS1302_write_bytetoaddr( uchar addr, uchar byte )
{
uchar addr_t = addr; //轉(zhuǎn)移數(shù)據(jù)
uchar byte_t = byte;
uchar i = 0;
DS1302_DATA_DDR_OUT; //輸出
DS1302_RST_CLR;
DS1302_SCLK_CLR; //時(shí)鐘為低電平
DS1302_RST_SET; //啟動(dòng)數(shù)據(jù)傳輸
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;
}
//從地址讀數(shù)據(jù)
//RST被設(shè)置為高就啟動(dòng)了一個(gè)數(shù)據(jù)傳送的過程
uchar DS1302_read_bytefromaddr( uchar addr )
{
uchar addr_t = addr; //轉(zhuǎn)移數(shù)據(jù)
uchar byte_t;
uchar i = 0;
DS1302_DATA_DDR_OUT; //輸出
DS1302_RST_CLR;
DS1302_SCLK_CLR; //時(shí)鐘為低電平
DS1302_RST_SET; //啟動(dòng)數(shù)據(jù)傳輸
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; //數(shù)據(jù)為輸入高阻態(tài)
byte_t = 0;
for( i = 0; i < 8; i++ )
{
byte_t = byte_t >> 1; //先移位
if( DS1302_DTTA_CHECK ) //讀數(shù)據(jù)輸出口
byte_t |= 0x80;
else
byte_t &= 0x7f;
DS1302_SCLK_SET;
DS1302_SCLK_CLR;
}
DS1302_RST_CLR;
return byte_t;
}
//將十六進(jìn)制碼變?yōu)槭M(jìn)制碼
//如: 0x45 -- > 45d
uchar hex2dec( uchar hex_data )
{
//十位 //個(gè)位
return ( ( ( hex_data >> 4 ) * 10 ) + ( hex_data & 0b00001111 ) );
}
//寫時(shí)間年月日
//08-10-15 14:36:56
void DS1302_write_time( void )
{
DS1302_write_bytetoaddr( 0X8E, 0X00 ); //寫控制寄存器 WP000 0000 <===> WP = 0, 數(shù)據(jù)可以寫入
DS1302_write_bytetoaddr( 0X80, 0X80 ); //寫秒寄存器, 停止時(shí)鐘
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] ); //小時(shí)
DS1302_write_bytetoaddr( 0X82, DS1302TIME[6] ); //分
DS1302_write_bytetoaddr( 0X80, DS1302TIME[7] ); //秒, 開戶時(shí)鐘脈沖
DS1302_write_bytetoaddr( 0X8E, 0X80 ); //寫控制寄存器, 保護(hù)數(shù)據(jù)
}
//讀時(shí)間年月日
void DS1302_read_time( void )
{
DS1302_write_bytetoaddr( 0X8E, 0X00 ); //寫控制寄存器 WP000 0000 <===> WP = 0, 數(shù)據(jù)可以寫入
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 ); //小時(shí)
DS1302TIME[6] = DS1302_read_bytefromaddr( 0X83 ); //分
DS1302TIME[7] = DS1302_read_bytefromaddr( 0X81 ); //秒, 開戶時(shí)鐘脈沖
DS1302_write_bytetoaddr( 0X8E, 0X80 ); //寫控制寄存器, 保護(hù)數(shù)據(jù)
}
//將時(shí)間分解
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;
}
}
//將時(shí)間合成
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] );
}
}
//顯示時(shí)間
void disptime( void )
{
TimeSpare(); //將每一個(gè)時(shí)間分隔為兩部分,十位部分,個(gè)位部分。
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計(jì)數(shù)部分主要對(duì)模式選擇時(shí)閃動(dòng)要更改的數(shù)據(jù)-----------------
//---------------------------------------按鍵部分------------------------------------
#define on 1
#define off 0
uchar model_year = 0; //年
uchar model_month = 0; //月
uchar model_date = 0; //日
uchar model_hour = 0; //時(shí)
uchar model_min = 0; //分
uchar model_sec = 0; //秒
//測(cè)度燈
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值, 閃動(dòng)該位
switch( model )
{
//年十位
case 1:
{
if( t == 1 ) t = 0;
else if( t == 0 ) { LCD_write_char( 1, 8, ' ' ); delay_nms( 100 ); t = 1; }
}; break;
//年個(gè)位
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;
//月個(gè)位
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;
//日個(gè)位
case 6:
{
if( t == 1 ) t = 0;
else if( t == 0 ) { LCD_write_char( 1, 15, ' ' ); delay_nms( 100 ); t = 1; }
}; break;
//時(shí)十位
case 7:
{
if( t == 1 ) t = 0;
else if( t == 0 ) { LCD_write_char( 2, 8, ' ' ); delay_nms( 100 ); t = 1; }
}; break;
//時(shí)個(gè)位
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;
//分個(gè)位
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;
//秒個(gè)位
case 12:
{
if( t == 1 ) t = 0;
else if( t == 0 ) { LCD_write_char( 2, 15, ' ' ); delay_nms( 100 ); t = 1; }
}; break;
default: break;
}
}
//模式閃動(dòng)
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;
}
//中斷服務(wù)程序
#pragma interrupt_handler int0_isr:iv_INT0
void int0_isr( void )
{
//external interupt on INT0
CLI(); //關(guān)閉全局中斷
if( ( ( PIND & 0b11110100 ) != 0b11110100 ) )
{
delay_nms( 40 ); //去除抖動(dòng)
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();
}
}