#include<iom16v.h>
#include<macros.h>
#define uchar unsigned char
#define uint unsigned int
//12864管脚定义
#define PSB_0 PORTB &= ~(1 << PB1)
#define PSB_1 PORTB |= (1 << PB1)
#define EN_0 PORTB &= ~(1 << PB0)
#define EN_1 PORTB |= (1 << PB0)
#define RW_0 PORTB &= ~(1 << PB2)
#define RW_1 PORTB |= (1 << PB2)
#define RS_0 PORTB &= ~(1 << PB3)
#define RS_1 PORTB |= (1 << PB3)
#define DATAPORT PORTA
//1302管脚定义
#define RST_CLR PORTC &= ~(1 << PC6)
#define RST_SET PORTC |= (1 << PC6)
#define RST_IN DDRC &= ~(1 << PC6)
#define RST_OUT DDRC |= (1 << PC6)
#define IO_CLR PORTC &= ~(1 << PC1)
#define IO_SET PORTC |= (1 << PC1)
#define IO_R PINC & (1 << PC1)
#define IO_IN DDRC &= ~(1 << PC1)
#define IO_OUT DDRC |= (1 << PC1)
#define SCK_CLR PORTC &= ~(1 << PC0)
#define SCK_SET PORTC |= (1 << PC0)
#define SCK_IN DDRC &= ~(1 << PC0)
#define SCK_OUT DDRC |= (1 << PC0)
uchar t;
uchar t,htem,ltem;//定义温度的十位和个位
uchar sec,min,hour,day,month,week,year,year2=20,ah=0,am=0;//定义时间各变量
uchar year_moon,month_moon,day_moon,week;
uchar c_sun,year_sun,month_sun,day_sun;
uchar Flag_1820Error=0;
uint operData;
void delay_5us(void) //5us左右
{
uchar x="7";
while(x)
{
x--;
}
}
void delay_15us(void) //15us左右
{
uchar x="27";
while(x)
{
x--;
}
}
void delay_60us(void) //60us左右
{
uchar x="117";
while(x)
{
x--;
}
}
void delay_nms(uint n)
{
uint i="0",j;
while(i<n)
{
for(j=1;j<1140;j++);
i++;
}
}
void m16_init()
{
PORTA="0x00";
DDRA="0xff";
PORTB="0x00";
DDRB="0xff";
PORTC="0xff";
DDRC="0xff";
}
void wcode(uchar command)
{
RS_0;
delay_nms(2);
RW_0;
delay_nms(2);
DATAPORT="command";
delay_nms(2);
EN_1;
delay_nms(2);
EN_0;
}
void wdata(uchar data)
{
RS_1;
delay_nms(2);
RW_0;
delay_nms(2);
DATAPORT="data";
delay_nms(2);
EN_1;
delay_nms(2);
EN_0;
}
void lcd_pos(uchar X,uchar Y)
{
uchar pos;
if(X==0)
{X=0x80;}
else if(X==1)
{X=0x90;}
else if(X==2)
{X=0x88;}
else if(X==3)
{X=0x98;}
pos="X"+Y;
wcode(pos);
}
void lcd_init()
{
PSB_1;
NOP();
NOP();
wcode(0x30);
delay_nms(1);
wcode(0x0c);
delay_nms(1);
wcode(0x01);
delay_nms(1);
}
void writeString(uchar * str)
{
uchar i = 0;
while(str != '\0')
{
wdata(str[i++]);
}
}
void init_1820(void)
{
uchar i;
uint j="0";
PORTC|=(1<<7); //"1"
PORTC&=~(1<<7); //"0"
for(i=0;i<8;i++)delay_60us();//480us以上
PORTC|=(1<<7); //"1"
DDRC&=~(1<<7); //"PINC7 is INPUT"
delay_15us(); //15~60us
delay_15us();
Flag_1820Error=0;
while(PINC&(1<<7))
{ delay_60us();
j++;
if(j>=18000){Flag_1820Error=1;break;}
}
DDRC|=(1<<7); //PORTC7 is OUTPUT
PORTC|=(1<<7); //"1"
for(i=0;i<4;i++)delay_60us(); //240us
}
void write_1820(uchar x)
{
uchar m;
for(m=0;m<8;m++)
{
if(x&(1<<m)) //写数据,从低位开始
{PORTC&=~(1<<7);delay_5us(); //"0",5us
PORTC|=(1<<7); //write"1"
delay_15us(); //15~45us
delay_15us();
delay_15us();
}
else
{PORTC&=~(1<<7);delay_15us();//"0",15us
delay_15us(); //write"0"
delay_15us(); //15~45us
delay_15us();
PORTC|=(1<<7); //"1"
}
}
PORTC|=(1<<7); //"1"
}
uchar read_1820(void)
{
uchar temp,k,n;
temp="0";
for(n=0;n<8;n++)
{
PORTC&=~(1<<7); //"0"
delay_5us();
PORTC|=(1<<7); //"1"
delay_5us();
DDRC&=~(1<<7);
tengjingshu_112148725 2010-4-19 09:47