/*************************************************************************
功能:将键盘按下的值显示在NOKIA显示屏上
硬件:拨码开关不需要拨动
*************************************************************************/
#include <msp430x42x.h>
#include "5510.h"
#define Num_of_Results 8
static unsigned int results[Num_of_Results];
void trans_volt(unsigned int adcout);
unsigned char entervolt[5];
unsigned int j;//键盘输入数据个数
unsigned int limit=60;//输入电压阈值
unsigned int option=0;//菜单选项
unsigned int optionnumber=0;
unsigned int select=1;//1-处于菜单模式,0-处于执行模式
void key(void);
void voltoverwarn(void);
unsigned long adcout;
void tishi(void);
volatile unsigned int i; // Use volatile to prevent removal
// by compiler optimization
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
FLL_CTL0 |= XCAP14PF; // Configure load caps
for (i = 0; i < 10000; i++); // Delay for 32 kHz crystal to
// stabilize
SD16CTL = SD16REFON+SD16SSEL0; // 1.2V ref, SMCLK,0XOO14,00010100interrupt disabled
SD16INCTL0 |= SD16INTDLY_0; // *****Interrupt on 4th sample,0x0000
SD16CCTL0 |= SD16IE ; // Enable interrupt,0x0008
SD16INCTL0|=SD16INCH_3; //选择A3通道,0x0003
for (i = 0; i < 0x3600; i++); // Delay for 1.2V ref startup
LCD_init();//初始化液晶
tishi();
LCD_clear();
_EINT(); // Enable general interrupts
SD16CCTL0 |= SD16SC; // Set bit to start conversion,Start conversion
//***************************提示和输入************************************
//*******************************采样*********************************
while(1){
P6DIR=0X00;
adcout=0;
for(i=0;i<8;i++)adcout=adcout+results[i];
adcout=adcout>>3; //取均值,滤波
trans_volt(adcout);
}
}
#pragma vector=SD16_VECTOR
__interrupt void SD16ISR(void)
{
static unsigned int index = 0;
switch (SD16IV)
{
case 2: // SD16MEM Overflow,data too large
break;
case 4: // SD16MEM0 IFG,ADC normal
results[index] = SD16MEM0; // Save CH0 results (clears IFG)
if (++index == Num_of_Results)
{
index = 0; // SET BREAKPOINT HERE
}
break;
}
}
void trans_volt(unsigned int adcout){
unsigned long temp;
unsigned char t1;
unsigned char ptr[4];
temp=adcout;
temp=(temp<<7)-(temp<<3);
temp=temp>>16;
temp-=60;
if(temp>limit)voltoverwarn();
P1DIR=0x30;
ptr[0]=temp/100;
t1=temp-(ptr[0]*100);
ptr[1]=t1/10;
t1=t1-ptr[1]*10;
ptr[2]=t1/1;
LCD_write_chinese_string(4,1,12,6,10,0);
LCD_write_number(21,3,ptr[0]);
LCD_write_char('.');
LCD_write_number(35,3,ptr[1]);
LCD_write_number(42,3,ptr[2]);
LCD_write_char('V');
}
void key(void){
P1DIR=0x00;
P6DIR=0xf0;
teg1: P6OUT=0x00;
if(((P1IN&BIT2)==0)||((P1IN&BIT3)==0)||((P1IN&BIT6)==0)||((P1IN&BIT7)==0)){
for(int i=0;i<5000;i++);
P6OUT=~BIT4;
if(((P1IN&BIT2)==0)){entervolt[j]='0';goto out;}
else if(((P1IN&BIT3)==0)){entervolt[j]='4';goto out;}
else if(((P1IN&BIT6)==0)){entervolt[j]='8';goto out;}
else if(((P1IN&BIT7)==0)){option=12;goto out;}
else{
P6OUT=~BIT5;
if(((P1IN&BIT2)==0)){entervolt[j]='1';goto out;}
else if(((P1IN&BIT3)==0)){entervolt[j]='5';goto out;}
else if(((P1IN&BIT6)==0)){entervolt[j]='9';goto out;}
else if(((P1IN&BIT7)==0)){option=13;goto out;}
else{
P6OUT=~BIT6;
if(((P1IN&BIT2)==0)){entervolt[j]='2';goto out;}
else if(((P1IN&BIT3)==0)){entervolt[j]='6';goto out;}
else if(((P1IN&BIT6)==0)){entervolt[j]='.';goto out;}
else if(((P1IN&BIT7)==0)){option=14;goto out;}
else {
P6OUT=~BIT7;
if(((P1IN&BIT2)==0)){entervolt[j]='3';goto out;}
else if(((P1IN&BIT3)==0)){entervolt[j]='7';goto out;}
else if(((P1IN&BIT6)==0)){entervolt[j]='\n';goto out;}
else if(((P1IN&BIT7)==0)){option=15;goto out;}
}
}
}
goto teg1;
}
else{
goto teg1;
}
out:
if(select==0){
if(j==0)
{
LCD_write_english_string(0,3,"****");
LCD_write_char(entervolt[0]);
}
else if(j==4)
{LCD_write_english_string(50,3,"V****");
for(i=0;i<0xffff;i++);
for(i=0;i<0xffff;i++);
}
else
LCD_write_char(entervolt[j]);
}
else
return;
}
void voltoverwarn(void){
P6DIR|=BIT0+BIT1+BIT2+BIT3;
P6OUT|=BIT0+BIT1+BIT2+BIT3;
}
void tishi(void){
while(1){
start:
LCD_clear();
LCD_write_chinese_string(15,0,12,3,24,0);
LCD_write_english_string(6,2,"K12");
LCD_write_char(':');
LCD_write_chinese_string(24,2,12,4,27,0);
LCD_write_char(':');
LCD_write_chinese_string(6,4,12,2,31,0);
LCD_write_english_string(22,4," K15");
LCD_write_chinese_string(50,4,12,2,34,0);
optionnumber=(optionnumber+1)%3;
while(1){
key();
for(i=0;i<0xffff;i++);//软件消抖
if(option==12){
LCD_clear();
LCD_write_chinese_string(6,2,12,5,36,0);
for(i=0;i<0xffff;i++);
for(i=0;i<0xffff;i++);
goto start;
}
else if(option==13){
goto continue1;
}
else if(option==14){
LCD_clear();
LCD_write_chinese_string(6,2,12,5,36,0);
for(i=0;i<0xffff;i++);
for(i=0;i<0xffff;i++);
goto start;
}
else if(option!=15) continue;
else
if(optionnumber==1){
LCD_clear();
LCD_write_chinese_string(15,0,12,3,24,0);
LCD_write_english_string(6,2,"K13");
LCD_write_char(':');
LCD_write_chinese_string(24,2,12,5,41,0);
//LCD_write_char(':');
LCD_write_chinese_string(6,4,12,2,31,0);
LCD_write_english_string(22,4," K15");
LCD_write_chinese_string(50,4,12,2,34,0);
optionnumber=(optionnumber+1)%3;
}
else if(optionnumber==2){
LCD_clear();
LCD_write_chinese_string(15,0,12,3,24,0);
LCD_write_english_string(6,2,"K14");
LCD_write_char(':');
LCD_write_chinese_string(24,2,12,5,46,0);
//LCD_write_char(':');
LCD_write_chinese_string(6,4,12,2,31,0);
LCD_write_english_string(22,4," K15");
LCD_write_chinese_string(50,4,12,2,34,0);
optionnumber=(optionnumber+1)%3;
}
else
goto start;
}
continue;
}
continue1:
select=0;
LCD_clear();
LCD_write_chinese_string(3,1,12,7,17,0);
tag:
for(j=0;j<5;j++){
key();
for(i=0;i<0xffff;i++);
}
if(entervolt[4]=='\n')
{
LCD_clear();
limit=(entervolt[2]-48)*10+(entervolt[3]-48);
}
else{
LCD_clear();
goto tag;
}
}