原创 12864并行方式

2010-4-17 22:14 2982 8 8 分类: MCU/ 嵌入式

#include<iom16v.h>
#include<macros.h>
#define uchar unsigned char
#define uint unsigned int


#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 RST_0 PORTB &= ~(1 << PB4)
#define RST_1 PORTB |= (1 << PB4)


#define DATAPORT PORTA


#define OUT_SCK DDRC |= (1 << 0)
#define OUT_SIO DDRC |= (1 << 1)
#define OUT_RST DDRC |= (1 << 6)


#define IN_SIO DDRC &= ~(1 << 1)


#define SET_SCK PORTC |= (1 << 0)
#define SET_SIO PORTC |= (1 << 1)
#define SET_RST PORTC |= (1 << 6)


#define CLR_SCK PORTC &= ~(1 << 0)
#define CLR_SIO PORTC &= ~(1 << 1)
#define CLR_RST PORTC &= ~(1 << 6)


#define GET_SIO PINC & (1 << 1)
#define DELAY() {NOP();NOP();NOP();}
#define data0 operData&0x01
#define data7 operData&0x80
uint operData;
uchar sec,min,hour;
/*--------------------------------------------------------------------
函数名称:DS1302写一个字节
--------------------------------------------------------------------*/
void writeByte(void)
{
 uint i;
 OUT_SIO;
 DELAY();
 for(i=0;i<8;i++)
 {
  CLR_SCK;
  if(data0)
  SET_SIO;
  else
  CLR_SIO;
  SET_SCK;
  operData = operData>>1;
}
}
/*--------------------------------------------------------------------
函数名称:DS1302读一个字节
--------------------------------------------------------------------*/
void readByte(void)
{
 uint i;
 IN_SIO;
 SET_SCK;
 for(i=0;i<8;i++)
 {
  operData = operData>>1;
  CLR_SCK;
  DELAY();
  if(GET_SIO)
  operData |= 0x80;
  SET_SCK;     //可能是上升沿
 }
}
/*--------------------------------------------------------------------
函数名称:DS1302的一个完整写操作
--------------------------------------------------------------------*/
void write(uint addr,uint dat)
{
  CLR_RST; //不一样
  CLR_SCK;
  SET_RST;
  operData="addr";
  writeByte();
  operData="dat";
  writeByte();
  CLR_RST;
}
/*--------------------------------------------------------------------
函数名称:DS1302的一个完整读操作
--------------------------------------------------------------------*/
uint read(uint cmd)
{
  CLR_SCK;
  SET_RST;
  operData="cmd";
  writeByte();
  readByte();
  CLR_RST;
  return(operData);
}
/*--------------------------------------------------------------------
函数名称:DS1302读时间
--------------------------------------------------------------------*/
void DS1302_getTime()
{
  sec="read"(0x81);
  sec=(sec>>4)*10+(sec&0x0f);
  min="read"(0x83);
  min=(min>>4)*10+(min&0x0f);
  hour="read"(0x85);
  hour=(hour>>4)*10+(hour&0x0f);
}



/*--------------------------------------------------------------------
函数名称:DS1302设置时间
--------------------------------------------------------------------*/
void DS1302_setTime()
{
 /* openWP();*/
  write(0x8e,0x00);
  write(0x84,0X09);
  write(0x82,0X58);
  write(0x80,0X58);
  write(0x8e,0x80);
 /* closeWP();*/
}
/*--------------------------------------------------------------------
函数名称:DS1302初始化
--------------------------------------------------------------------*/
void DS1302_init(void)
{
OUT_SCK;
OUT_SIO;
OUT_RST;
}



void delay_1ms()
{
  uint i;
  for(i=1;i<1140;i++);
}


void delay_nms(uint n)
{
  uint i="0";
  while(i<n)
  {
    delay_1ms();
 i++;
  }
}


void m16_init()
{
  PORTA="0x00";
  DDRA="0xff";
  PORTB="0x00";
  DDRB="0xff";
}


void wcode(uchar command)
{
  delay_nms(5);
  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)
{
  delay_nms(5);
  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;
  RST_1;
  NOP();
  NOP();
  wcode(0x30);
  delay_nms(5);
  wcode(0x0c);
  delay_nms(5);
  wcode(0x01);
  delay_nms(5);
}


void writeString(uchar * str)
{
    uchar i = 0;
    while(str != '\0')
    {
        wdata(str[i++]);
    }
}



void main()
{
 m16_init();
 lcd_init();
 DS1302_init();
 DS1302_setTime();
 while(1)
 {
  DS1302_getTime();
  lcd_pos(0,0);
  writeString("2009年10月21日");
  lcd_pos(1,0);
  writeString("星期六");
  lcd_pos(1,3);
  wdata(hour/10+0x30);
  wdata(hour%10+0x30);
  lcd_pos(1,5);
  wdata(min/10+0x30);
  wdata(min%10+0x30);
  lcd_pos(1,7);
  wdata(sec/10+0x30);
  wdata(sec%10+0x30);
 }
}

文章评论0条评论)

登录后参与讨论
我要评论
0
8
关闭 站长推荐上一条 /2 下一条