#include
#define CPU_F ((double)8000000)
#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 uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
/*12864应用指令*/
#define CLEAR_SCREEN 0x01 //清屏指令:清屏且AC值为00H
#define AC_INIT 0x02 //将AC设置为00H。且游标移到原点位置
#define CURSE_ADD 0x06 //设定游标移到方向及图像整体移动方向(默认游标右移,图像整体不动)
#define FUN_MODE 0x30 //工作模式:8位基本指令集
#define DISPLAY_ON 0x0c //显示开,显示游标,且游标位置反白
#define DISPLAY_OFF 0x08 //显示关
#define CURSE_DIR 0x14 //游标向右移动:AC=AC+1
#define SET_CG_AC 0x40 //设置AC,范围为:00H~3FH
#define SET_DD_AC 0x80
#define RS_CLR P2OUT &= ~BIT7 //RS置低
#define RS_SET P2OUT |= BIT7 //RS置高
#define RW_CLR P2OUT &= ~BIT6 //RW置低
#define RW_SET P2OUT |= BIT6 //RW置高
#define EN_CLR P2OUT &= ~BIT5 //E置低
#define EN_SET P2OUT |= BIT5 //E置高
#define DataPort P3OUT //P3口为数据口
#define Num_of_Results 8
unsigned char tab[]={'0','1','2','3','4','5','6','7','8','9','.','v'};
//float inx;
volatile unsigned int results[Num_of_Results];
//*************************************************************************
// 系统时钟初始化
//*************************************************************************
void Clock_Init()
{
UCSCTL1 = DCORSEL_2; // 设置DCO范围
UCSCTL3 |= SELREF_2; // 选择FLL参考源自REFO
UCSCTL4 = SELM_3 + SELA_2 + SELS_4; // 配置 MCLK = DCOC,SMCLK =DCODIV,ACLK=REFO
while (SFRIFG1 & OFIFG) // 清除 OFIFG,and XT1OFFG ,DCOFFG
{
UCSCTL7 &= ~( XT1LFOFFG + DCOFFG);
SFRIFG1 &= ~OFIFG;
}
}
//*************************************************************************
// MSP430内部看门狗初始化
//*************************************************************************
void WDT_Init()
{
WDTCTL = WDTPW + WDTHOLD; //关闭看门狗
}
//*************************************************************************
// 初始化IO口子程序
//*************************************************************************
void Port_init()
{
P3SEL = 0x00;
P3DIR = 0xFF;
P2SEL = 0x00;
P2DIR|= BIT5 + BIT6 + BIT7;
}
//***********************************************************************
// 显示屏命令写入函数
//***********************************************************************
void LCD_write_com(unsigned char com)
{
RS_CLR;
RW_CLR;
EN_SET;
DataPort = com;
delay_ms(5);
EN_CLR;
}
//***********************************************************************
// 显示屏数据写入函数
//***********************************************************************
void LCD_write_data(unsigned char data)
{
RS_SET;
RW_CLR;
EN_CLR;
DataPort = data;
EN_SET;
// delay_ms(5);
EN_CLR;
}
//***********************************************************************
// 显示屏清空显示
//***********************************************************************
void LCD_clear(void)
{
LCD_write_com(0x01);
//delay_ms(5);
}
//***********************************************************************
//函数名称:DisplayCgrom(uchar hz)显示CGROM里的汉字
//***********************************************************************
void DisplayCgrom(uchar addr,int b)
{
int i;
int a[7];
a[0]=b/10000;
a[1]=10;
a[2]=(b/1000)%10;
a[3]=(b/100)%10;
a[4]=(b/10)%10;
a[5]=(b/1)%10;
a[6]=11;
LCD_write_com(addr);
delay_ms(5);
for(i=0;i<=6;i++)
{
LCD_write_data(tab[a]);
// delay_ms(5);
}
}
//****************************************************************
//函数名称:Display()显示测试结果
//****************************************************************
//***********************************************************************
// 显示屏初始化函数
//***********************************************************************
void LCD_init(void)
{
LCD_write_com(FUN_MODE); //显示模式设置
delay_ms(5);
LCD_write_com(FUN_MODE); //显示模式设置
delay_ms(5);
LCD_write_com(DISPLAY_ON); //显示开
delay_ms(5);
LCD_write_com(CLEAR_SCREEN); //清屏
delay_ms(5);
}
int main(void)
{
WDT_Init();//看门狗设置
P6SEL |= 0x01; // Enable A/D channel A0
ADC12CTL0 = ADC12ON+ADC12SHT0_8+ADC12MSC; // Turn on ADC12, set sampling time
// set multiple sample conversion
ADC12CTL1 = ADC12SHP+ADC12CONSEQ_2; // Use sampling timer, set mode
ADC12IE = 0x01; // Enable ADC12IFG.0
ADC12CTL0 |= ADC12ENC; // Enable conversions
ADC12CTL0 |= ADC12SC; // Start conversion
__bis_SR_register(LPM4_bits + GIE); // Enter LPM4, Enable interrupts
__no_operation(); // For debugger
}
#pragma vector=ADC12_VECTOR
__interrupt void ADC12ISR (void)
{
static unsigned char index = 0;
unsigned char i,j;
int a[8];
int t,ave=0;
int voltage;
switch(__even_in_range(ADC12IV,34))
{
case 0: break; // Vector 0: No interrupt
case 2: break; // Vector 2: ADC overflow
case 4: break; // Vector 4: ADC timing overflow
case 6: // Vector 6: ADC12IFG0
results[index] = ADC12MEM0; // Move results
index++; // Increment results index, modulo; Set Breakpoint1 here
if (index == 8)
{
for(i=0;i
{for(j=i+1;j
{
if(a>a[j])
{
t=a;
a=a[j];
a[j]=t;
}
}
}
for(i=0;i
{
ave=ave+results;
}
ave=ave/4;
voltage=(ave)*33000/4095;
Clock_Init(); //系统时钟设置
Port_init(); //系统初始化,设置IO口属性
delay_ms(100); //延时100ms
LCD_init(); //液晶参数初始化设置
LCD_clear();
DisplayCgrom(0x80,voltage); //显示汉字
index = 0;
}
case 8: break; // Vector 8: ADC12IFG1
case 10: break; // Vector 10: ADC12IFG2
case 12: break; // Vector 12: ADC12IFG3
case 14: break; // Vector 14: ADC12IFG4
case 16: break; // Vector 16: ADC12IFG5
case 18: break; // Vector 18: ADC12IFG6
case 20: break; // Vector 20: ADC12IFG7
case 22: break; // Vector 22: ADC12IFG8
case 24: break; // Vector 24: ADC12IFG9
case 26: break; // Vector 26: ADC12IFG10
case 28: break; // Vector 28: ADC12IFG11
case 30: break; // Vector 30: ADC12IFG12
case 32: break; // Vector 32: ADC12IFG13
case 34: break; // Vector 34: ADC12IFG14
default: break;
}
}
|
0
|
|
|
|