📄 main.c
字号:
/*----------NUAA PEPA-----------*/
#include <iom128v.h>
#define uchar unsigned char
#define uint unsigned int
//---------------DS1302日历---------------------
#define ds1302_rst0 PORTE&=~(1<<4)
#define ds1302_rst1 PORTE|=(1<<4)
#define ds1302_clk0 PORTE&=~(1<<2)
#define ds1302_clk1 PORTE|=(1<<2)
#define ds1302_io0 PORTE&=~(1<<3)
#define ds1302_io1 PORTE|=(1<<3)
#define ds1302_io_out DDRE|=(1<<3)
#define ds1302_io_in DDRE&=~(1<<3)
//-----------------------------------------------
//----------------------wp2002液晶显示---------
#define wp2002_rs0 PORTE&=~(1<<7)
#define wp2002_rs1 PORTE|=(1<<7)
#define wp2002_cs0 PORTE&=~(1<<5)
#define wp2002_cs1 PORTE|=(1<<5)
#define wp2002_write_lcd PORTE&=~(1<<6)
#define wp2002_read_lcd PORTE|=(1<<6)
#define wp2002_dir1 DDRC=0xff
#define wp2002_dir0 DDRC=0
#define wp2002_port_value0 PORTC=0x00
#define wp2002_port_value1 PORTC=0xff
#define wp2002_data PORTC
#define wp2002_data_in PINC
//---------------------------------------------
uchar wp2002_lcd_databuffer[40]=" Nj HangLe Elec Ltd. 1993-2007 ";
uchar ds1302_databuffer[9]={0x00,0x34,0x15,0x08,0x08,0x03,0x07,0x80,0x00};
void wp2002_delay2()
{ uint i=20;
while(i--);
}
void wp2002_delay()
{ uint i=100;
while(i--);
}
void wp2002_lcd_write(uchar datause,uchar num)
{
if(num) wp2002_rs1;//数据寄存器
else wp2002_rs0;//指令寄存器
wp2002_write_lcd;
wp2002_dir1;
wp2002_data=datause;
wp2002_cs1;
wp2002_delay2();
wp2002_cs0;
}
uchar wp2002_lcd_read(uchar num)
{ uchar datause;
if(num) wp2002_rs1;
else wp2002_rs0;
wp2002_dir0;
wp2002_data=0xff;
wp2002_read_lcd;
wp2002_cs1;
datause=wp2002_data_in;
wp2002_cs0;
return datause;
}
void wp2002_lcd_initializtion()
{
wp2002_dir1;
DDRE|=(1<<5);
DDRE|=(1<<6);
DDRE|=(1<<7);
wp2002_cs0;
wp2002_read_lcd;
wp2002_rs1;
wp2002_port_value1;
wp2002_lcd_write(0x01,0);wp2002_delay();
wp2002_lcd_write(0x02,0);wp2002_delay();
wp2002_lcd_write(0x38,0);wp2002_delay();
wp2002_lcd_write(0x38,0);wp2002_delay();
wp2002_lcd_write(0x38,0);wp2002_delay();
wp2002_lcd_write(0x38,0);wp2002_delay();
wp2002_lcd_write(0x38,0);wp2002_delay();
wp2002_lcd_write(0x38,0);wp2002_delay();
wp2002_lcd_write(0x38,0);wp2002_delay();
wp2002_lcd_write(0x38,0);wp2002_delay();
wp2002_lcd_write(0x0c,0);wp2002_delay();
wp2002_lcd_write(0x06,0);wp2002_delay();
wp2002_lcd_write(0x80,0);wp2002_delay();
//wp2002_lcd_write(0x45,1);
//wp2002_lcd_write(0x41,1);
//wp2002_lcd_write(0x59,1);
// wp2002_lcd_write(0xc0,0);
//wp2002_lcd_write(0x4c,1);
//wp2002_lcd_write(0x65,1);
wp2002_lcd_write(0x40,0);wp2002_delay();
}
uchar wp2002_readbusy()//0表示空闲
{ if(wp2002_lcd_read(0)&0x80) return 0;
return 1;
}
void wp2002_lcd_display()
{ uchar i;
while(wp2002_readbusy());
wp2002_lcd_write(0x02,0);wp2002_delay();
wp2002_lcd_write(0x80,0);wp2002_delay();
for(i=0;i<20;i++)
wp2002_lcd_write(wp2002_lcd_databuffer[i],1);
wp2002_lcd_write(0xc0,0);
for(i=0;i<20;i++)
wp2002_lcd_write(wp2002_lcd_databuffer[i+20],1);
}
void delay()
{ uint i=65;
while(i--);
}
void delay2()
{ uint i=65535,j=5;
while(i--)
{ while(j--);
j=5;
}
}
//-------------------------------------------------------
void ds1302_delay()
{ uchar i=10;
while(i--);
}
void ds1302_writecommand(uchar commandhere)
{ uchar i;
ds1302_clk0;
ds1302_rst1;
ds1302_io_out;
for(i=0;i<8;i++)
{
if(commandhere&0x01) ds1302_io1;
else ds1302_io0;
ds1302_delay();
ds1302_clk1;
ds1302_delay();
ds1302_clk0;
commandhere>>=1;
}
}
void ds1302_write(uchar commandhere,uchar num)
{
uchar i,j,k;
ds1302_writecommand(commandhere);
ds1302_io_out;
for(j=0;j<num;j++)
{
for(i=0;i<8;i++)
{
if(ds1302_databuffer[j]&0x01) ds1302_io1;
else ds1302_io0;
delay();
ds1302_clk1;
delay();
ds1302_clk0;
ds1302_databuffer[j]>>=1;
ds1302_delay();
}
}
ds1302_rst0;
}
void ds1302_read()
{
uchar i,j,datause=0;
ds1302_writecommand(0xbf);
ds1302_io_in;
ds1302_io1;
for(j=0;j<8;j++)
{
for(i=0;i<8;i++)
{ datause>>=1;
if(PIND&0x40) datause|=(1<<7);
else datause&=~(1<<7);
ds1302_clk1;ds1302_delay();
ds1302_clk0;ds1302_delay();
}
ds1302_databuffer[j]=datause;
}
ds1302_rst0;
ds1302_io_out;
}
void ds1302_initializtion()
{
ds1302_write(0x8e,1);
ds1302_write(0xbe,9);
}
//-----------------------------------------------------------
void port_initializtion()
{
uchar temp;
DDRD=0xff;
PORTD=0;
ds1302_rst0;
ds1302_clk0;
ds1302_io_out;
ds1302_io1;
DDRC=0xff;
PORTF|=(1<<1);
PORTF|=(1<<0);
DDRF|=(1<<1);
DDRF|=(1<<0);
}
void main()
{ uchar i=20,num=0,temp,temp2; uchar hour[2],min[2],sec[2];
unsigned long cs5532_temp=0;
port_initializtion();
wp2002_lcd_initializtion();
ds1302_initializtion();
delay2();
delay2();
delay2();
wp2002_lcd_display();
delay2();
delay2();
for(i=0;i<6;i++) wp2002_lcd_databuffer[i]=0x20;
for(i=14;i<20;i++) wp2002_lcd_databuffer[i]=0x20;
while(1)
{
ds1302_read();
while(num==ds1302_databuffer[0]) ds1302_read();
num=ds1302_databuffer[0];
sec[0]=ds1302_databuffer[0]&0x0f;sec[0]+=0x30;
sec[1]=ds1302_databuffer[0]&0x70;sec[1]>>=4;sec[1]+=0x30;
min[0]=ds1302_databuffer[1]&0x0f;min[0]+=0x30;
min[1]=ds1302_databuffer[1]&0x70;min[1]>>=4;min[1]+=0x30;
hour[0]=ds1302_databuffer[2]&0x0f;hour[0]+=0x30;
hour[1]=ds1302_databuffer[2]&0xf0;
hour[1]>>=4;
hour[1]+=0x30;
wp2002_lcd_databuffer[6]=hour[1];
wp2002_lcd_databuffer[7]=hour[0];
wp2002_lcd_databuffer[8]=':';
wp2002_lcd_databuffer[9]=min[1];
wp2002_lcd_databuffer[10]=min[0];
wp2002_lcd_databuffer[11]=':';
wp2002_lcd_databuffer[12]=sec[1];
wp2002_lcd_databuffer[13]=sec[0];
temp=ds1302_databuffer[3]&0x0f;temp+=0x30;
wp2002_lcd_databuffer[34]=temp;
temp=ds1302_databuffer[3]&0xf0;temp>>=4;temp+=0x30;
wp2002_lcd_databuffer[33]=temp;
wp2002_lcd_databuffer[32]='/';
temp=ds1302_databuffer[4]&0x0f;temp+=0x30;
wp2002_lcd_databuffer[31]=temp;
temp=ds1302_databuffer[4]&0xf0;temp>>=4;temp+=0x30;
wp2002_lcd_databuffer[30]=temp;
wp2002_lcd_databuffer[29]='/';
temp=ds1302_databuffer[6]&0x0f;temp+=0x30;
wp2002_lcd_databuffer[28]=temp;
temp=ds1302_databuffer[6]&0xf0;temp>>=4;temp+=0x30;
wp2002_lcd_databuffer[27]=temp;
wp2002_lcd_databuffer[26]=0x30;
wp2002_lcd_databuffer[25]=0x32;
wp2002_lcd_databuffer[36]=0x48;
wp2002_lcd_databuffer[37]=0x4c;
wp2002_lcd_display();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -