標(biāo)題:
MSP430、DS1302、LCD1602程序
[打印本頁(yè)]
作者:
hushe
時(shí)間:
2015-12-22 21:39
標(biāo)題:
MSP430、DS1302、LCD1602程序
#include "io430.h"
unsigned char tt0[]="Time:";
unsigned char tt1[]="Data:";
unsigned char tt2[]="week:";
unsigned char tt3[]="temp:";
//static char ch[7][3]={"Sun","Mon","Tue","Wed","Thu","Tri","Sat"};
unsigned char TT,temp1;
unsigned char temp2;
void delay(unsigned char us)
{
while(us--);
}
unsigned char Reset(void)
{
unsigned char k;
P2DIR |=BIT6;
P2OUT &=~BIT6;
delay(145);
P2OUT |=BIT6;
delay(10);
P2DIR &=~BIT6;
k=P2IN;
delay(90);
return k;
}
//---------------------------
unsigned char ReadByte(void)
{
unsigned char j,buf=0;
for(j=0;j<8;j++)
{
buf=buf>>1;
P2DIR |=BIT6;
P2OUT &=~BIT6;
__no_operation();
__no_operation();
P2OUT |=BIT6;
__no_operation();
__no_operation();
__no_operation();
__no_operation();
__no_operation();
__no_operation();
__no_operation();
P2DIR &=~BIT6;
if(0x40 & P2IN==1)
buf|=0x80;
delay(8);
}
return buf;
}
//-------------------------------
void WriteByte(unsigned char dat)
{
unsigned char j;
P2DIR |=BIT6;
for(j=0;j<8;j++)
{
if(dat&0x01)
{
P2OUT &=~BIT6;
__no_operation();
__no_operation();
__no_operation();
P2OUT |=BIT6;
delay(10);
}
else
{
P2OUT &=~BIT6;
delay(10);
P2OUT |=BIT6;
__no_operation();
__no_operation();
__no_operation();
}
dat=dat>>1;
}
}
//------------------
unsigned char Convert(void)
{
if(Reset() & 0x40==0x00)
{
WriteByte(0xcc);
WriteByte(0x44);
return 0xff;
}
else
{
return 0x00;
}
}
//---------------------------------------
void ReadFlash(void)
{
unsigned char Lsb,Msb;
if(Reset() & 0x40==0x00)
{
WriteByte(0xcc);
WriteByte(0xbe);
Lsb=ReadByte();
Msb=ReadByte();
temp1=Lsb;
temp2=Msb;
}
else
{
temp1=0;
temp2=0;
}
}
void w1602com(unsigned char dat)
{
P2DIR |=0x07;
P1DIR =0xFF;
P2OUT &=~(BIT0 + BIT1);
P1OUT=dat;
P2OUT &=~BIT2;
delay(2);
P2OUT |=BIT2;
delay(4);
P2OUT &=~BIT2;
}
void w1602dat(unsigned char dat)
{
P2DIR |=0x07;
P1DIR =0xFF;
P2OUT |=BIT0;
P2OUT &=~BIT1;
P1OUT=dat;
P2OUT &=~BIT2;
delay(2);
P2OUT |=BIT2;
delay(4);
P2OUT &=~BIT2;
}
void init1602()
{
w1602com(0x38);
w1602com(0x06);
w1602com(0x0c);
w1602com(0x01);
}
unsigned char r1302(unsigned char addr)
{
unsigned char n,BB,AA=0x00;
P2DIR |=BIT3+BIT4+BIT5;
P2OUT &=~(BIT3+BIT4+BIT5);
__no_operation();
P2OUT |=BIT3;
BB=addr;
for(n=0;n<8;n++)
{
P2OUT &=~BIT4;
if(BB & 0x01)
P2OUT |=BIT5;
else
P2OUT &=~BIT5;
P2OUT |=BIT4;
BB=BB>>1;
}
P2DIR &=~BIT5;
for(n=0;n<8;n++)
{
if(P2IN & 0x20)
AA |=0x80;
P2OUT |=BIT4;
AA=AA>>1;
P2OUT &=~BIT4;
}
P2OUT &=~BIT3;
return(AA);
}
int main( void )
{
// Stop watchdog timer to prevent time out reset
WDTCTL = WDTPW + WDTHOLD;
unsigned char i;
init1602();
while(1)
{
w1602com(0x80);
for(i=0;i<5;i++)
w1602dat(tt0[i]);
w1602dat(0x30+(r1302(0x85)>>4 & 0x0f));
w1602dat(0x30+(r1302(0x85) & 0x0f));
w1602dat(0x2d);
w1602dat(0x30+(r1302(0x83)>>4 & 0x0f));
w1602dat(0x30+(r1302(0x83) & 0x0f));
w1602dat(0x2d);
w1602dat(0x30+(r1302(0x81)>>4 & 0x0f));
w1602dat(0x30+(r1302(0x81) & 0x0f));
w1602com(0xc0);
for(i=0;i<5;i++)
w1602dat(tt1[i]);
w1602dat(0x30+(r1302(0x87)>>4 & 0x0f));
w1602dat(0x30+(r1302(0x87) & 0x0f));
w1602dat(0x2d);
w1602dat(0x30+(r1302(0x89)>>4 & 0x0f));
w1602dat(0x30+(r1302(0x89) & 0x0f));
w1602dat(0x2d);
w1602dat(0x30+(r1302(0x8d)>>4 & 0x0f));
w1602dat(0x30+(r1302(0x8d) & 0x0f));
}
}
復(fù)制代碼
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1