#include "msp430g2553.h"
#define uint unsigned int
#define uchar unsigned char
#define ulong unsigned long
uint aver[64]={0};
uchar flag=0;
uchar zifu[14]={"0123456789ux=z"};
uint maxaver=0;
long dis=0;
double max=0,maxr=0;
#define SID BIT3
#define SCLK BIT2
#define LCDPORT P1OUT
#define SID_1 LCDPORT |= SID
#define SID_0 LCDPORT &= ~SID
#define SCLK_1 LCDPORT |= SCLK
#define SCLK_0 LCDPORT &= ~SCLK
void delay(unsigned char ms)
{
unsigned char i,j;
for(i=ms;i>0;i--)
for(j=120;j>0;j--);
}
/***********************************************************
*名 称:LCD_Write_cmd()
*功 能:写一个命令到LCD12864
*入口参数:cmd:待写入的命令,无符号字节形式
*出口参数:无
*说 明:写入命令时,RW=0,RS=0 扩展成24位串行发送
*格 式:11111 RW0 RS 0 xxxx0000 xxxx0000
* |最高的字节 |命令的bit7~4|命令的bit3~0|
***********************************************************/
void write_cmd(uchar cmd)
{
uchar i;
uchar i_data;
i_data=0xf8;
SCLK_0;
for(i=0;i<8;i++)
{
SID_0;
if(i_data&0x80)
SID_1 ;
_delay_cycles(100);
SCLK_0;
SCLK_1;
_delay_cycles(10);
i_data<<=1;
}
i_data=cmd;
i_data&=0xf0;
for(i=0;i<8;i++)
{
SID_0;
if(i_data&0x80)
SID_1 ;
_delay_cycles(100);
SCLK_0;
SCLK_1;
_delay_cycles(10);
i_data<<=1;
}
i_data=cmd;
i_data<<=4;
for(i=0;i<8;i++)
{
SID_0;
if(i_data&0x80)
SID_1 ;
_delay_cycles(100);
SCLK_0;
SCLK_1;
_delay_cycles(10);
i_data<<=1;
}
_delay_cycles(100);
}
/***********************************************************
*名 称:LCD_Write_Byte()
*功 能:向LCD12864写入一个字节数据
*入口参数:byte:待写入的字符,无符号形式
*出口参数:无
*范 例:LCD_Write_Byte('F') //写入字符'F'
***********************************************************/
void write_dat(uchar dat)
{
uchar i;
uchar i_data;
i_data=0xfa;
SCLK_0;
for(i=0;i<8;i++)
{
SID_0;
if(i_data&0x80)
SID_1 ;
_delay_cycles(100);
SCLK_0;
SCLK_1;
_delay_cycles(10);
i_data<<=1;
}
i_data=dat;
i_data&=0xf0;
for(i=0;i<8;i++)
{
SID_0;
if(i_data&0x80)
SID_1 ;
i_data<<=1;
_delay_cycles(100);
SCLK_0;
SCLK_1;
_delay_cycles(10);
}
i_data=dat;
i_data<<=4;
for(i=0;i<8;i++)
{
SID_0;
if(i_data&0x80)
SID_1 ;
i_data<<=1;
_delay_cycles(100);
SCLK_0;
SCLK_1;
_delay_cycles(10);
}
_delay_cycles(100);
}
/***********************************************************
*名 称:LCD_pos()
*功 能:设置液晶的显示位置
*入口参数:x:第几行,1~4对应第1行~第4行
* y:第几列,0~15对应第1列~第16列
*出口参数:无
*范 例:LCD_pos(2,3) //第二行,第四列
***********************************************************/
void lcd_pos(uchar x,uchar y)
{
uchar pos;
switch(x)
{
case 1:pos=0x80;break;
case 2:pos=0x90;break;
case 3:pos=0x88;break;
case 4:pos=0x98;break;
default:pos=0x80;
}
pos += y;
write_cmd(pos);
}
void LCD_init(void)
{
write_cmd(0x30);
delay(5);
write_cmd(0x0C);
delay(5);
write_cmd(0x01);
delay(5);
write_cmd(0x02);
delay(5);
}
void Sys_clock_init(void)
{
uint i=0;
BCSCTL1 |= CALBC1_16MHZ;
DCOCTL |= CALDCO_16MHZ;
for(i=0;i<65535;i++);
BCSCTL2 |= SELM_0;
BCSCTL2 |= DIVS_0;
}
void AD_init(void)
{
ADC10CTL0 = ADC10ON + ADC10IE +MSC + ADC10SHT_3;
ADC10CTL1 |= CONSEQ_3 + INCH_1;;
ADC10AE0 |= BIT0+BIT1;
ADC10DTC1 = 0x40;
}
void IO_init(void)
{
//P1REN |= BIT0+BIT1;
//P1OUT =0x00;
//P1DIR |= BIT0+BIT1;
//P1DIR |=BIT0+BIT1+BIT2+BIT3;
P1DIR = 0x0C;
}
void jisuan()
{
uchar j;
maxr=0;
maxaver=0;
for(j=0;j<64;j+=2)
{
if(aver[j]>maxaver && aver[j]<1024)
maxaver=aver[j];
}
maxr=(maxaver*3.56)/1023;
max=maxr*10000;
dis=max;
lcd_pos(2,2);
write_dat(zifu[dis/10000]);
write_dat('.');
write_dat(zifu[dis/1000%10]);
write_dat(zifu[dis/100%10]);
write_dat(zifu[dis/10%10]);
write_dat(zifu[dis%10]);
write_dat('V');
maxr=0;
maxaver=0;
for(j=1;j<64;j+=2)
{
if(aver[j]>maxaver && aver[j]<1024)
maxaver=aver[j];
}
maxr=(maxaver*3.56)/1023;
max=maxr*10000;
dis=max;
lcd_pos(4,2);
write_dat(zifu[dis/10000]);
write_dat('.');
write_dat(zifu[dis/1000%10]);
write_dat(zifu[dis/100%10]);
write_dat(zifu[dis/10%10]);
write_dat(zifu[dis%10]);
write_dat('V');
}
void main(void)
{
WDTCTL = WDTPW + WDTHOLD;
IO_init();
LCD_init();
delay(100);
Sys_clock_init();
lcd_pos(1,0);
write_dat(zifu[10]);
write_dat(zifu[11]);
write_dat(zifu[12]);
lcd_pos(3,0);
write_dat(zifu[10]);
write_dat(zifu[13]);
write_dat(zifu[12]);
AD_init();
while(1)
{
ADC10CTL0&=~ENC;
while(ADC10CTL1&ADC10BUSY);
ADC10CTL0|=ENC+ADC10SC;
ADC10SA=(unsigned int)aver;
_bis_SR_register(LPM0_bits+GIE);//关闭cpu,开启中断
}
}
#pragma vector=ADC10_VECTOR
__interrupt void ADC10_ISR(void)
{
jisuan();
_bic_SR_register_on_exit(CPUOFF+GIE);
} |