mega8+1602+1302+18b20做的电子万年历
编程环境是cvavr
还望各位高手多多指点
先上图
(原文件名:20100404001.jpg)
(原文件名:20100404002.jpg)
(原文件名:20100404003.jpg)
(原文件名:20100404009.jpg)
(原文件名:20100404010.jpg)
(原文件名:20100404011.jpg)
(原文件名:20100404012.jpg)
1602
#define RS PORTD.7
#define RW PORTD.6
#define EN PORTD.5
#define LCD_DAT PORTC
unsigned char lcd_buffer[];
void wr_lcd_inf(unsigned char com,unsigned char inf)
{
unsigned char com_temp,data_temp;
DDRD=0b11100001;//lcd控制引脚
if(com==0)
{
delay_ms(3);
RS=0;
RW=0;
EN=0;
DDRC=0x0f;//lcd数据引脚
com_temp=inf;
LCD_DAT=0x0f&com_temp>>4;//消除对PORTC.4的影响
EN=1;
EN=0;
com_temp=inf;
LCD_DAT=0x0f&com_temp;
EN=1;
EN=0;
}
else
{
delay_ms(3);
RS=1;
RW=0;
EN=0;
DDRC=0x0f;//lcd数据引脚
data_temp=inf;
LCD_DAT=0x0f&data_temp>>4;
EN=1;
EN=0;
data_temp=inf;
LCD_DAT=0x0f&data_temp;
EN=1;
EN=0;
}
}
void wr_address(unsigned char x,unsigned char y)
{
unsigned char address;
if(y>0)
address=0xc0+x;
else
address=0x80+x;
wr_lcd_inf(0,address);
}
void wr_lcd_word(unsigned char x,unsigned char y,unsigned char s)
{
wr_address(x,y);
wr_lcd_inf(1,s);
}
void wr_lcd_string(unsigned char x,unsigned char y,unsigned char *s)
{
wr_address(x,y);
while(*s)
{
wr_lcd_inf(1,*s);
s++;
}
}
void lcd_init()
{
wr_lcd_inf(0,0x28);//设置为4线,2行,5*7字型
wr_lcd_inf(0,0x06);//AC+1
wr_lcd_inf(0,0x0c);//开启显示屏,光标关闭,不闪烁
wr_lcd_inf(0,0x01);//清楚显示屏
}
18b20
#define TEMP_DAT PORTD.4
unsigned char buffer[];//显示字符串缓存
unsigned char temper[5];
void ds18b20_init(void)
{
TEMP_DAT=1;
DDRD.4=0;
delay_us(50);
TEMP_DAT=0;
DDRD.4=1;//设置输出口
delay_us(900);
TEMP_DAT=1;
DDRD.4=0;//设置输出口
delay_us(50);
while(PIND.4)
{
wr_lcd_inf(0,0x01);
sprintf(buffer,"no ds18b20");
wr_lcd_string(2,0,buffer);
}
delay_us(500);
TEMP_DAT=1;
DDRD.4=0;
}
void wr_ds18b20_dat(unsigned char ds18b20_dat)//写数据
{
unsigned char i;
for(i=0;i<8;i++)
{
TEMP_DAT=1;
DDRD.4=1;
delay_us(10);
TEMP_DAT=0;
delay_us(10);
TEMP_DAT=0x1&ds18b20_dat;//与
delay_us(60);
ds18b20_dat=ds18b20_dat>>1;
}
TEMP_DAT=1;
DDRD.4=1;
delay_us(30);
}
unsigned char re_ds18b20_dat()
{
unsigned char i,ds18b20_dat=0;
for(i=0;i<8;i++)
{
TEMP_DAT=1;
DDRD.4=1;
delay_us(10);
ds18b20_dat=ds18b20_dat>>1;
TEMP_DAT=0;
DDRD.4=1;
delay_us(10);
TEMP_DAT=1;
DDRD.4=0;
delay_us(10);
if(PIND.4)
{
ds18b20_dat=0x80|ds18b20_dat;
}
delay_us(60);
}
TEMP_DAT=1;
DDRD.4=1;
delay_us(30);
return ds18b20_dat;
}
void set_ds18b20()//设定精度
{
ds18b20_init();
wr_ds18b20_dat(0xcc);
wr_ds18b20_dat(0x4e);
wr_ds18b20_dat(0x00);
wr_ds18b20_dat(0x00);
wr_ds18b20_dat(0x7f);//精度为12位
}
unsigned int get_ds18b20()
{
unsigned char teml,temh,wm;
unsigned int warm, cold;
set_ds18b20();
ds18b20_init();
wr_ds18b20_dat(0xcc);
wr_ds18b20_dat(0x44);//开始转换
delay_ms(1000);
ds18b20_init();
wr_ds18b20_dat(0xcc);//跳过ROM
wr_ds18b20_dat(0xbe);//读可擦写芯片(9 byte)
teml=re_ds18b20_dat();//低8位
temh=re_ds18b20_dat();//高8位
wm=temh;
wm=wm>>7;
if(!wm)
{
temh&=0b00000111;
warm=(unsigned int)((temh*256+teml)*0.0625*100);
return warm;
}
else
{
temh&=0b00000111;
cold=(unsigned int)((~(temh*256+teml)+1)*0.0625*100);
cold+=20000; //0下-号显示标记
return cold;
}
}
void trans_ds18b20()
{
unsigned int T;
T=get_ds18b20();
temper[0]=T/10000+48;
temper[1]=T/1000%10+48;
temper[2]=T/100%10+48;
temper[3]=T/10%10+48;
temper[4]=T%10+48;
}
1302
#define SCLK PORTB.0
#define RST PORTB.1
#define IO PORTB.2
unsigned char time[8];
void ds1302_wdat(unsigned char ds1302_wdat)
{
unsigned char i;
for(i=0;i<8;i++)
{
IO=0;
DDRB.2=1;
SCLK=0;
DDRB.0=1;
if(ds1302_wdat&0x01)//与
IO=1;
SCLK=1;
ds1302_wdat=ds1302_wdat>>1;
}
}
unsigned char ds1302_rdat()
{
unsigned char i,ds1302_dat=0;
for(i=0;i<8;i++)
{
DDRB.2=0;
IO=1;
DDRB.0=1;
SCLK=1;
SCLK=0;
ds1302_dat=ds1302_dat>>1;
if(PINB.2)
ds1302_dat=ds1302_dat|0b10000000;//或
}
return(ds1302_dat);
}
unsigned char re_ds1302(unsigned char ds1302_address)
{
unsigned char ds1302_dat;
DDRB.1=1;
RST=0;
DDRB.0=1;
SCLK=0;
RST=1;
ds1302_wdat(ds1302_address);
ds1302_dat=ds1302_rdat();
RST=0;
return(ds1302_dat);
}
void wr_ds1302(unsigned char ds1302_address,unsigned char ds1302_dat)
{
DDRB.1=1;
RST=0;
DDRB.0=1;
SCLK=0;
RST=1;
ds1302_wdat(ds1302_address);
ds1302_wdat(ds1302_dat);
RST=0;
}
/*
void set_ds1302()
{
wr_ds1302(0x8e,0x00);
ds1302_wdat(0xbe);
ds1302_wdat(0x50);
ds1302_wdat(0x00);
ds1302_wdat(0x00);
ds1302_wdat(0x02);
ds1302_wdat(0x04);
ds1302_wdat(0x05);
ds1302_wdat(0x10);
ds1302_wdat(0x8e);
}
*/
void set_ds1302()
{
wr_ds1302(0x8e,0x80);
wr_ds1302(0x80,0x50);
wr_ds1302(0x82,0x59);
wr_ds1302(0x84,0x22);
wr_ds1302(0x86,0x02);
wr_ds1302(0x88,0x04);
wr_ds1302(0x8a,0x05);
wr_ds1302(0x8c,0x10);
wr_ds1302(0x8e,0x80);
wr_ds1302(0x90,0xab);
}
void read_ds1302()
{
time[0]=re_ds1302(0x81);//秒
time[1]=re_ds1302(0x83);//分
time[2]=re_ds1302(0x85);//小时
time[3]=re_ds1302(0x87);//日期
time[4]=re_ds1302(0x89);//月份
time[5]=re_ds1302(0x8b);//星期
time[6]=re_ds1302(0x8d);//年份
}
还没有写按键程序
下面是pcb和程序
点击此处下载 ourdev_543690.rar(文件大小:17K) (原文件名:avr mega8数字表.rar)
点击此处下载 ourdev_543691.rar(文件大小:94K) (原文件名:mega8.rar) |
|
|
|