1.main
#include "stm32f10x.h"
#include "usart.h"
#include "adc.h"
#include "key.h"
#include "lcd.h"
#include "i2c.h"
void ADDPro(void);
void SUBPro(void);
void KEYPro(void);
void LCDPro(void);
u8 state,keyvalue,level,flag;
u32 height;
float volt;
u32 TH[3]={
10,25,30};
unsigned char pucStr[16];
unsigned char pucStr1[16];
unsigned char Str1[16];
u32 ntime=0;
u8 temp;
int main(void)
{
STM3210B_LCD_Init();
KEYInit();
USARTInit();
ADCInit();
SysTick_Config(8000);
i2c_init();
LCD_Clear(White);
LCD_SetTextColor(Blue);
LCD_SetBackColor(White);
TH[0]=Read(0xff);
TH[1]=Read(0xfe);
TH[2]=Read(0xfd);
temp=level;
while(1)
{
KEYPro();
LCDPro();
}
}
void KEYPro(void)
{
keyvalue=KEYScan();
switch(keyvalue)
{
case 1:
if(state==0)
state=1;
else
state=0;
break;
case 2:
if(++state==4)
state=1;
break;
case 3:
ADDPro();
break;
case 4:
SUBPro();
break;
}
}
void ADDPro(void)
{
switch(state)
{
case 1:
TH[0]+=5;
if(TH[0]>=100)
TH[0]=0;
Write(0xff,TH[0]);
break;
case 2:
TH[1]+=5;
if(TH[1]>=100)
TH[1]=0;
Write(0xfe,TH[1]);
break;
case 3:
TH[2]+=5;
if(TH[2]>=100)
TH[2]=0;
Write(0xfd,TH[2]);
break;
}
}
void SUBPro(void)
{
switch(state)
{
case 1:
TH[0]-=5;
if(TH[0]<=0)
TH[0]=95;
Write(0xff,TH[0]);
break;
case 2:
TH[1]-=5;
if(TH[1]<=0)
TH[1]=95;
Write(0xfe,TH[1]);
break;
case 3:
TH[2]-=5;
if(TH[2]<=0)
TH[2]=95;
Write(0xfd,TH[2]);
break;
}
}
void LCDPro(void)
{
height=Height();
switch(state)
{
case 0:
LCD_DisplayStringLine(Line2," LEQUID LEVEL ");
sprintf((char*)pucStr, " Height:%.2dcm ",height);
LCD_DisplayStringLine(Line4, pucStr);
sprintf((char*)pucStr, " ADCVolt:%.2fV ",volt);
LCD_DisplayStringLine(Line5, pucStr);
if(height<=10)
level=1;
if((height>=10)&&(height<=20))
level=2;
if((height>=20)&&(height<=30))
level=3;
if(height>30)
level=4;
switch(level)
{
case 1:
LCD_DisplayStringLine(Line6," Level:1 ");break;
case 2:
LCD_DisplayStringLine(Line6," Level:2 ");break;
case 3:
LCD_DisplayStringLine(Line6," Level:3 ");break;
case 4:
LCD_DisplayStringLine(Line6," Level:4 ");break;
}
break;
case 1:
case 2:
case 3:
LCD_SetBackColor(White);
LCD_DisplayStringLine(Line2," TH SETTING ");
if(state==1)LCD_SetBackColor(Red);
sprintf((char*)pucStr, " TH1:%.dcm ",TH[0]);
LCD_DisplayStringLine(Line4, pucStr);
LCD_SetBackColor(White);
if(state==2)LCD_SetBackColor(Red);
sprintf((char*)pucStr, " TH2:%.dcm ",TH[1]);
LCD_DisplayStringLine(Line5, pucStr);
LCD_SetBackColor(White);
if(state==3)LCD_SetBackColor(Red);
sprintf((char*)pucStr, " TH3:%.dcm ",TH[2]);
LCD_DisplayStringLine(Line6, pucStr);
LCD_SetBackColor(White);break;
}
}
void SysTick_Handler(void)
{
ntime++;
if(ntime==1000)
{
ntime=0;
volt=ADCAver();
if(temp!=level)
{
if(temp<level)
{
flag=1;
temp=level;
}
else if(temp>level)
{
flag=2;
temp=level;
}
}
switch(flag)
{
case 1:
sprintf((char*)pucStr1, "A:H%dcm+L%d+U\r\n ",height,level);
USART_SendString(pucStr1);break;
case 2:
sprintf((char*)pucStr1, "A:H%dcm+L%d+D\r\n ",height,level);
USART_SendString(pucStr1);break;
}
}
}
void USART2_IRQHandler(void)
{
u8 temp=0;
if(USART_GetFlagStatus(USART2,USART_FLAG_RXNE)!=RESET);
{
USART_ClearITPendingBit(USART2,USART_IT_RXNE);
temp=USART_ReceiveData(USART2);
if(temp=='C')
{
sprintf((char*)Str1, "C:H%dcm+L%d\r\n ",height,level);
USART_SendString(Str1);
}
else if(temp=='S')
{
sprintf((char*)Str1, "S:TL%dcm+TM%dcm+TH%dcm\r\n ",TH[0],TH[1],TH[2]);
USART_SendString(Str1);
}
}
}
2.usart
#include "stm32f10x.h"
#include "usart.h"
#include "lcd.h"
void USARTInit(void)
{
USART_InitTypeDef u;
GPIO_InitTypeDef g;
NVIC_InitTypeDef v;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
g.GPIO_Pin = GPIO_Pin_2;//TX
g.GPIO_Mode = GPIO_Mode_AF_PP;
g.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &g);
g.GPIO_Pin = GPIO_Pin_3;//RX
g.GPIO_Mode = GPIO_Mode_IN_FLOATING;
g.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &g);
u.USART_BaudRate=9600;
u.USART_WordLength=8;
u.USART_StopBits=USART_StopBits_1;
u.USART_Parity=USART_Parity_No;
u.USART_Mode=USART_Mode_Rx | USART_Mode_Tx;//写入和读取模式都开启
u.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//不需要硬件控制
USART_Init(USART2, &u);
v.NVIC_IRQChannel=USART2_IRQn;
v.NVIC_IRQChannelPreemptionPriority=2;
v.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&v);
USART_Cmd(USART2,ENABLE);
USART_ITConfig( USART2, USART_IT_RXNE,ENABLE);
}
void USART_SendString(unsigned char *str)
{
uint8_t index = 0;
do
{
USART_SendData(USART2,str[index]);
while(USART_GetFlagStatus(USART2,USART_FLAG_TXE) == RESET);
index++;
}
while(str[index] != 0); //检查字符串结束标志
}
3.adc
#include "stm32f10x.h"
#include "adc.h"
void ADCInit(void)
{
ADC_InitTypeDef a;
GPIO_InitTypeDef g;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE);
g.GPIO_Pin = GPIO_Pin_0;
g.GPIO_Mode = GPIO_Mode_AIN;
g.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOB, &g);
a.ADC_Mode=ADC_Mode_Independent;
a.ADC_NbrOfChannel=8;
a.ADC_DataAlign=ADC_DataAlign_Right;
a.ADC_ScanConvMode = DISABLE;//扫描模式不开启
a.ADC_ContinuousConvMode = DISABLE;//练习扫描不开启
a.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;//触发不开启
ADC_Init(ADC1,&a);
ADC_RegularChannelConfig(ADC1,ADC_Channel_8,1,ADC_SampleTime_13Cycles5);
ADC_Cmd(ADC1,ENABLE);
ADC_ResetCalibration(ADC1);
while(ADC_GetResetCalibrationStatus(ADC1));
ADC_StartCalibration(ADC1);
while(ADC_GetCalibrationStatus(ADC1));
}
float ReadADC(void)
{
float ADCvalue;
ADC_SoftwareStartConvCmd(ADC1,ENABLE);
while(ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC)==0);
ADCvalue=ADC_GetConversionValue(ADC1);
ADCvalue=ADCvalue*3.3/0xfff;
return ADCvalue;
}
float ADCAver(void)
{
u8 i,j;
float Aver1[10]={
0};
float Aver;
for(i=0;i<10;i++)
{
Aver1[i]=ReadADC();
}
for(j=0;j<10;j++)
{
Aver1[0]+=Aver1[j];
}
Aver=Aver1[0]/10;
return Aver;
}
u32 Height(void)
{
u32 Height;
Height= ADCAver()*(100/3.3);
return Height;
}