#include "io430.h"
unsigned char tb1[]=" Power:";
unsigned char tb2[]="Volt";
unsigned char tb3[]=" low Power";
unsigned char tb4[]=" Power Full";
unsigned char tb5[]=" percent:";
///////////////////////////////////////////////////////////////////////
// P1=dat; rs=P2.4,e=P2.5 /////
// ADC0832: CLK=P2.0;DI=P2.1; ////
// DO=P2.2; CS=P2.3 ////
/////////////////////////////////////////////////////////////////////
void w1602(unsigned char t,unsigned char dat)
{
char n;
if(t==0)
P2OUT &=~BIT4;
else
P2OUT |=BIT4;
P1OUT =dat;
P2OUT &=~BIT5;
n=5;
while(n--);
P2OUT |=BIT5;
n=10;
while(n--);
P2OUT &=~BIT5;
}
unsigned char rADC0832()
{
unsigned char temp=0,k;
P2OUT |=BIT3;
__no_operation();
P2OUT &=~BIT3; //start
P2OUT &=~BIT0;
__no_operation();
P2OUT |=BIT1;
__no_operation();
P2OUT |=BIT0;
__no_operation();//DI=1
P2OUT &=~BIT0;
__no_operation();
P2OUT |=BIT0;
__no_operation(); //DI=1
P2OUT &=~BIT0;
P2OUT &=~BIT1;
__no_operation();
P2OUT |=BIT0;
__no_operation(); //DI=1
P2OUT &=~BIT0;
__no_operation();
P2OUT |=BIT0;
__no_operation(); //DI=1
P2OUT &=~BIT0;
for(k=0;k<8;k++)
{
temp=temp<<1;
P2OUT |=BIT0;
if(P2IN & BIT2)temp |=0x01;
P2OUT &=~BIT0;
}
return temp;
}
void init1602()
{
w1602(0,0x38);
w1602(0,0x06);
w1602(0,0x0c);
w1602(0,0x01);
}
int main( void )
{
// Stop watchdog timer to prevent time out reset
WDTCTL = WDTPW + WDTHOLD;
unsigned int d,d1,dd;
P1DIR =0xff;
P2DIR =0x3B;
//P2REN |=BIT2;
P2OUT |=BIT2;
init1602();
while(1)
{
d=196 * rADC0832();//25000(3V=153(29988) 3.7=189(37)
dd=100*(rADC0832()-153)/36;
////////////////////////////
w1602(0,0x80);
for(d1=0;d1<7;d1++)
w1602(1,tb1[d1]);
w1602(1,(d/10000)+0x30);
w1602(1,0x2E);
w1602(1,(d%10000)/1000+0x30);
w1602(1,((d%10000)%1000)/100+0x30);
for(d1=0;d1<4;d1++)
w1602(1,tb2[d1]);
w1602(1,0x20);
w1602(1,0x20);
///////////////////////////
w1602(0,0xc0);
if(d<29988)
{
for(d1=0;d1<12;d1++)
w1602(1,tb3[d1]);
w1602(1,0x20);
w1602(1,0x20);
w1602(1,0x20);
w1602(1,0x20);
}
else
{
if(d>37044)
{
for(d1=0;d1<13;d1++)
w1602(1,tb4[d1]);
w1602(1,0x20);
w1602(1,0x20);
w1602(1,0x20);
}
else
{
for(d1=0;d1<9;d1++)
w1602(1,tb5[d1]);
if((dd/10)==0)
w1602(1,0x20);
else
w1602(1,dd/10+0x30);
w1602(1,dd%10+0x30);
w1602(1,0x25);
w1602(1,0x20);
w1602(1,0x20);
w1602(1,0x20);
w1602(1,0x20);
}
}
}
}
ADC0832的時序圖詳解:http://www.zg4o1577.cn/bbs/dpj-51688-1.html
|