BASIC4MCU | AVR | 타이머 | 코드비젼용 DS1302
페이지 정보
작성자 키트 작성일2017-08-29 09:28 조회5,686회 댓글0건본문
//// 코드비젼// (DS1302)//#include <mega128.h>#include <stdio.h>#include <delay.h>//#define SEC 0x80#define MIN 0x82#define HOUR 0x84#define DATE 0x86#define MONTH 0x88#define YEAR 0x8c// Port D#define RTC_SCL PORTD.0 //rtc D.0#define RTC_SDA_WR PORTD.1 //rtc D.1#define RTC_SDA_RD PIND.1 //rtc D.1#define RTC_SDA_DDR DDRD.1 //rtc D.1#define RTC_RST PORTD.2 //rtc D.2char rtc_Init[8]={0x10,1,1,0,0,0,0,0}; // 시간초기값 년월일시분초char time[8]; //년월일시분초//void write_ds1302(char addr, char data){char i;RTC_SCL=0; RTC_RST=1; delay_us(2);for(i=0; i<8; i++){if((addr>>i)&0x01) RTC_SDA_WR=1;else RTC_SDA_WR=0;delay_us(2); RTC_SCL=1; delay_us(1); RTC_SCL=0; delay_us(1);}for(i=0; i<8; i++){if((data>>i)&0x01) RTC_SDA_WR=1;else RTC_SDA_WR=0;delay_us(2); RTC_SCL=1; delay_us(1); RTC_SCL=0; delay_us(1);}RTC_RST=0;}//char read_ds1302(char addr){char i, data=0;RTC_RST=1; delay_us(2);//writefor(i=0; i<8; i++){if((addr>>i)&0x01) RTC_SDA_WR=1;else RTC_SDA_WR=0;delay_us(2); RTC_SCL=1; delay_us(1); RTC_SCL=0; delay_us(1);}// readRTC_SDA_DDR=0; RTC_SDA_WR=1;for(i=0; i<8; i++){data >>= 1;if(RTC_SDA_RD)data|=0x80;delay_us(1); RTC_SCL=1; delay_us(2); RTC_SCL=0; delay_us(1);}RTC_SDA_DDR=1; RTC_SCL=0; delay_us(1); RTC_RST=0;return data;}//void save_time(void){write_ds1302(YEAR, time[0]); write_ds1302(MONTH, time[1]);write_ds1302(DATE, time[2]); write_ds1302(HOUR, time[3]);write_ds1302(MIN, time[4]); write_ds1302(SEC, time[5]);}//void init_time(void){char i;for(i=0; i<8; i++)time[i]=rtc_Init[i];save_time();}//void get_time(void){time[0]=read_ds1302(YEAR+1); if(time[0]>0x99)init_time();time[1]=read_ds1302(MONTH+1); if(time[1]>0x12)init_time();time[2]=read_ds1302(DATE+1); if(time[2]>0x31)init_time();time[3]=read_ds1302(HOUR+1); if(time[3]>0x23)init_time();time[4]=read_ds1302(MIN+1); if(time[4]>0x59)init_time();time[5]=read_ds1302(SEC+1); if(time[5]>0x59)init_time();}//void TX1_CH(char c){ while(!(UCSR1A&0x20)); UDR1=c; } // 송신 1바이트void TX1_STR(char *s){ while(*s)TX1_CH(*s++); } // 송신 문자열//void main(void){char buf[20];RTC_RST=0;RTC_SDA_WR=0; RTC_SCL=0;DDRD=0x07; // rtc D.0~D.2 outwrite_ds1302(0x8E,0); write_ds1302(0x90,0xA4);UCSR0B=8; UBRR0L=103; // 9600while(1){get_time();sprintf(buf,"%02x/%02x/%02x",time[0],time[1],time[2]); TX1_STR(buf);sprintf(buf,"%02x:%02x:%02x",time[3],time[4],time[5]); TX1_STR(buf);}}//-----------------------------------------------------------------------------//-----------------------------------------------------------------------------#define F_CPU 16000000UL // 16 MHz#include <avr/io.h>#include <stdio.h>#include <util/delay.h>//#define SEC 0x80#define MIN 0x82#define HOUR 0x84#define DATE 0x86#define MONTH 0x88#define YEAR 0x8c// Port D#define RTC_SCL_1 PORTD|=1 //rtc D.0#define RTC_SCL_0 PORTD&=~1 //rtc D.0#define RTC_SDA_WR_1 PORTD|=2 //rtc D.1#define RTC_SDA_WR_0 PORTD&=~2 //rtc D.1#define RTC_SDA_RD (PIND&2) //rtc D.1#define RTC_SDA_DDR_1 DDRD|=2 //rtc D.1#define RTC_SDA_DDR_0 DDRD&=~2 //rtc D.1#define RTC_RST_1 PORTD|=4 //rtc D.2#define RTC_RST_0 PORTD&=~4 //rtc D.2//char rtc_Init[8]={0x10,1,1,0,0,0,0,0}; // 시간초기값 년월일시분초char time[8]; //년월일시분초//void wr_ds1302(char addr,char data){char i;RTC_SCL_0; RTC_RST_1; _delay_us(2);for(i=0;i<8;i++){if((addr>>i)&1)RTC_SDA_WR_1; else RTC_SDA_WR_0;_delay_us(2); RTC_SCL_1; _delay_us(1); RTC_SCL_0; _delay_us(1);}for(i=0;i<8;i++){if((data>>i)&1)RTC_SDA_WR_1; else RTC_SDA_WR_0;_delay_us(2); RTC_SCL_1; _delay_us(1); RTC_SCL_0; _delay_us(1);}RTC_RST_0;}//char read_ds1302(char addr){char i,data=0;RTC_RST_1; _delay_us(2);//writefor(i=0;i<8;i++){if((addr>>i)&1)RTC_SDA_WR_1; else RTC_SDA_WR_0;_delay_us(2); RTC_SCL_1; _delay_us(1); RTC_SCL_0; _delay_us(1);}// readRTC_SDA_DDR_0; RTC_SDA_WR_1;for(i=0;i<8;i++){data>>=1;if(RTC_SDA_RD)data|=0x80;_delay_us(1); RTC_SCL_1; _delay_us(2); RTC_SCL_0; _delay_us(1);}RTC_SDA_DDR_1; RTC_SCL_0; _delay_us(1); RTC_RST_0;return data;}//void save_time(void){wr_ds1302(YEAR,time[0]); wr_ds1302(MONTH,time[1]);wr_ds1302(DATE,time[2]); wr_ds1302(HOUR,time[3]);wr_ds1302(MIN,time[4]); wr_ds1302(SEC,time[5]);}//void init_time(void){unsigned char i;for(i=0;i<8;i++)time[i]=rtc_Init[i];save_time();}//void get_time(void){time[0]=read_ds1302(YEAR+1); if(time[0]>0x99)init_time();time[1]=read_ds1302(MONTH+1); if(time[1]>0x12)init_time();time[2]=read_ds1302(DATE+1); if(time[2]>0x31)init_time();time[3]=read_ds1302(HOUR+1); if(time[3]>0x23)init_time();time[4]=read_ds1302(MIN+1); if(time[4]>0x59)init_time();time[5]=read_ds1302(SEC+1); if(time[5]>0x59)init_time();}//void TX1_CH(char c){ while(!(UCSR1A&0x20)); UDR1=c; } // 송신 1바이트void TX1_STR(char *s){ while(*s)TX1_CH(*s++); } // 송신 문자열//int main(void){char buf[20];RTC_RST_0; RTC_SDA_WR_0; RTC_SCL_0;DDRD=0x07; // rtc D.0~D.2 outwr_ds1302(0x8E,0); wr_ds1302(0x90,0xA4);UCSR0B=8; UBRR0L=103; // 9600while(1){get_time();sprintf(buf,"%02x/%02x/%02x ",time[0],time[1],time[2]); TX1_STR(buf);sprintf(buf,"%02x:%02x:%02x\r\n",time[3],time[4],time[5]); TX1_STR(buf);}}//-----------------------------------------------------------------------------//-----------------------------------------------------------------------------
댓글 0
조회수 5,686등록된 댓글이 없습니다.