#include <msp430f249.h> #include "KU.h"
#define ADCST0 P5OUT &= ~BIT4
#define ADCST1 P5OUT |= BIT4
#define ADCALE0 P5OUT &= ~BIT5
#define ADCALE1 P5OUT |= BIT5
#define ADCEOC0 P5OUT &= ~BIT6
#define ADCEOC1 P5OUT |= BIT6
#define ADCOE0 P5OUT &= ~BIT7
#define ADCOE1 P5OUT |= BIT7
uchar ADC1;
ulong ADC2;
uint MC_wan;
uint MC_qian;
uint MC_bai;
uint MC_shi;
uint MC_ge;
void ADC_START(void)
{
ADCALE1;
ADCST1;
delay_ms(5);
ADCALE0;
ADCST0;
delay_ms(5);
}
char ADC_READ()
{
uchar data;
data=0x00;
delay_ms(5);
ADCOE1;
ADC_START();
delay_ms(5);
data=P4IN;
ADCOE0;
return data;
}
int main( void )
{
WDTCTL = WDTPW + WDTHOLD;
P1SEL = 0x00;
P1DIR = 0xFF; //P1液晶数据
P3SEL = 0x00;
P3DIR|= BIT0+BIT1+BIT2; //P3液晶控制
LCD_init();
LCD_clear();
delay_ms(50);
P4SEL = 0x00;
P4DIR = 0x00;
P5SEL = 0x00;
P5DIR |= BIT4+BIT5+BIT6;
while(1)
{
ADC1=ADC_READ();
ADC2=(ADC1*100)/51;
MC_wan=(ADC2/10000);
MC_qian=(ADC2-(MC_wan*10000))/1000;
MC_bai=((ADC2-(MC_wan*10000)-(MC_qian*1000))/100);
MC_shi=((ADC2-(MC_wan*10000)-(MC_qian*1000)-(MC_bai*100))/10);
MC_ge=(ADC2-(MC_wan*10000)-(MC_qian*1000)-(MC_bai*100)-(MC_shi*10));
LCD_clear();
LCD_write_str(0,0,"Voltage:");
LCD_write_char(0x07,1,MC_bai+48);
LCD_write_char(0x08,1,46);
LCD_write_char(0x09,1,MC_shi+48);
LCD_write_char(0x0A,1,MC_ge+48);
LCD_write_char(0x0D,1,86);
delay_ms(500);
}
}
下面为库函数
//**************************************************//****
//延时设置~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
//***********************************************************
#define CPU_F ((double)4000000) ////主时钟的频率4MHz
#define delay_us(x) __delay_cycles((long)(CPU_F*(double)x/1000000.0))
#define delay_ms(x) __delay_cycles((long)(CPU_F*(double)x/1000.0))
#define RS0 P3OUT &= ~BIT0
#define RS1 P3OUT |= BIT0
#define RW0 P3OUT &= ~BIT1
#define RW1 P3OUT |= BIT1
#define EN0 P3OUT &= ~BIT2
#define EN1 P3OUT |= BIT2
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
//驱动1602**************************
void LCD_write_com(unsigned char com)
{
RS0;
RW0;
EN0;
P1OUT = com;
delay_ms(1);
EN1; delay_ms(1);
EN0;
}
void LCD_write_data(unsigned char data)
{
RS1;
RW0;
EN0;
P1OUT = data;
delay_ms(1);
EN1;
delay_ms(1);
EN0;
}
void LCD_clear(void)
{
LCD_write_com(0x01);
delay_ms(1);
}
void LCD_write_str(unsigned char x,unsigned char y,unsigned char *s)
{
if (y == 0)
{
LCD_write_com(0x80 + x);
}
else
{
LCD_write_com(0xC0 + x);
}
while (*s)
{
LCD_write_data( *s);
s ++;
}
}
void LCD_write_char(unsigned char x,unsigned char y,unsigned char data)
{
if (y == 0)
{
LCD_write_com(0x80 + x);
}
else
{
LCD_write_com(0xC0 + x);
}
LCD_write_data( data);
}
void LCD_init(void)
{
EN0;
delay_ms(1);
LCD_write_com(0x38);
delay_ms(1);
LCD_write_com(0x0C);
delay_ms(1);
LCD_write_com(0x06);
delay_ms(1);
LCD_write_com(0x01);
delay_ms(1);
}
|