这一届的试题总的来说比较简单,没有用到太多外设。考察的也是简单的逻辑。
试题部分
程序部分
我是在液晶驱动例程直接进行程序编写的,要加入stm32f10x_adc.c文件其余的用不到(也可以全部加进去但是编译速度较慢)。也可以自己建立工程文件进行代码编写。
相关驱动。
#include "mysource.h"
/*********************key&&led&&lcd--init*********/
void system_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* GPIOD Periph clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB, ENABLE);
/* Configure PD0 and PD2 in output pushpull mode */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9|GPIO_Pin_10 | GPIO_Pin_11|GPIO_Pin_12 | GPIO_Pin_13|GPIO_Pin_14 | GPIO_Pin_15;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2;
GPIO_Init(GPIOB, &GPIO_InitStructure);
STM3210B_LCD_Init();
LCD_Clear(Blue);
LCD_SetBackColor(Blue);
LCD_SetTextColor(White);
LCD_DisplayStringLine(Line2,(unsigned char *)" Main ");
LCD_DisplayStringLine(Line4,(unsigned char *)" Volt: ");
LCD_DisplayStringLine(Line6,(unsigned char *)" Status: ");
}
/***********led_show****************/
void led_control(u8 date)
{
GPIOD->ODR|=GPIO_Pin_2;
GPIOC->ODR=~(date<<8);
GPIOD->ODR&=~GPIO_Pin_2;
}
void adc_init(void)
{
ADC_InitTypeDef ADC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* ADC1 configuration ------------------------------------------------------*/
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = 1;
ADC_Init(ADC1, &ADC_InitStructure);
/* ADC1 regular channel14 configuration */
ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 1, ADC_SampleTime_55Cycles5);
/* Enable ADC1 */
ADC_Cmd(ADC1, ENABLE);
/* Enable ADC1 reset calibration register */
ADC_ResetCalibration(ADC1);
/* Check the end of ADC1 reset calibration register */
while(ADC_GetResetCalibrationStatus(ADC1));
/* Start ADC1 calibration */
ADC_StartCalibration(ADC1);
/* Check the end of ADC1 calibration */
while(ADC_GetCalibrationStatus(ADC1));
}
u16 read_adc(void)
{
u16 ad_value=0;
/* ADC1 regular channel14 configuration */
ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 1, ADC_SampleTime_55Cycles5);
/* Start ADC1 Software Conversion */
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
while(!ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC));
ad_value=ADC_GetConversionValue(ADC1);
ADC_ClearFlag(ADC1,ADC_FLAG_EOC);
ADC_SoftwareStartConvCmd(ADC1, DISABLE);
return ad_value;
}
#ifndef __MYSOURCE_H
#define __MYSOURCE_H
#include "stm32f10x.h"
#include "stdio.h"
#include "lcd.h"
#define b1 GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0)
#define b2 GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_8)
#define b3 GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1)
#define b4 GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_2)
void system_init(void);
void led_control(u8 date);
void adc_init(void);
u16 read_adc(void);
#endif
主函数
#include "stm32f10x.h"
#include "lcd.h"
#include "mysource.h"
#include "math.h"
u8 led_buff[9]={0x00,0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
u32 TimingDelay = 0;
u8 str[20];
void Delay_Ms(u32 nTime);
void key_scan(void);
void lcd_show(void);
void error_choose(void);
u8 set=0;//设置状态选择
float max_ad=2.40;//电压上限
float min_ad=1.20;//电压下限
u8 led_choose;//led选择
u8 i=1;//设置模式下高亮选择
u8 j=1;//upper led选择
u8 k=2;//lower led选择
u8 led_choose1=0x01;//upper led初始状态
u8 led_choose2=0x02;//lower led初始状态
u16 led_delay=0;//滴答定时器里led0.2s延时
u8 runled=0x00;//led的运行状态
u8 error=0;//错误警告标志
//Main Body
int main(void)
{
SysTick_Config(SystemCoreClock/1000);
system_init();
led_control(0x00);//全部关闭led
adc_init();
while(1)
{
error_choose();//判断是否设置下上下限电压选择错误
key_scan();
lcd_show();
led_control(runled);
}
}
//
void Delay_Ms(u32 nTime)
{
TimingDelay = nTime;
while(TimingDelay != 0);
}
void key_scan(void)//按键扫描
{
if(b1==0)
{
Delay_Ms(30);
if(b1==0)
{
if(error==0)//错误设置下不能返回主界面
{
LCD_Clear(Blue);
set=!set;
error=0;
}
}
while(!b1);
}
else if(b2==0)
{
Delay_Ms(30);
if(b2==0)
{
if(set==1)//高亮切换
{
i++;
if(i==5)
{
i=1;
}
}
}
while(!b2);
}
else if(b3==0)
{
Delay_Ms(30);
if(b3==0)
{
if(set==1)
{
if(i==1)
{
max_ad+=0.3;
if(max_ad>3.3)
{
max_ad=3.3;
}
}
else if(i==2)
{
min_ad+=0.3;
if(min_ad>3.3)
{
min_ad=3.3;
}
}
else if(i==3)
{
j++;
if(j==9)
{
j=8;
}
led_choose1=led_buff[j];
}
else if(i==4)
{
k++;
if(k==9)
{
k=8;
}
led_choose1=led_buff[k];
}
}
}
while(!b3);
}
else if(b4==0)
{
Delay_Ms(30);
if(b4==0)
{
if(set==1)
{
if(i==1)
{
max_ad-=0.3;
if(max_ad<0)
{
max_ad=0;
}
}
else if(i==2)
{
min_ad-=0.3;
if(min_ad<0)
{
min_ad=0;
}
}
else if(i==3)
{
j--;
if(j==0)
{
j=1;
}
led_choose1=led_buff[j];//选择upper led
}
else if(i==4)
{
k--;
if(k==0)
{
k=1;
}
led_choose1=led_buff[k];//选择lower led
}
}
}
while(!b4);
}
}
void lcd_show(void)//lcd界面展示
{
if(set==0)
{
LCD_DisplayStringLine(Line2,(unsigned char *)" Main ");
sprintf((char*)str," Volt:%1.2fV ",read_adc()*3.3/0XFFF);
LCD_DisplayStringLine(Line4,str);
if((read_adc()*3.3/0XFFF)>max_ad)
{
led_choose=3;
LCD_DisplayStringLine(Line6,(unsigned char *)" Status:Upper ");
}
else if((read_adc()*3.3/0XFFF)<min_ad)
{
led_choose=1;
LCD_DisplayStringLine(Line6,(unsigned char *)" Status:Lower ");
}
else
{
led_choose=2;
LCD_DisplayStringLine(Line6,(unsigned char *)" Status:Normal ");
}
}
else
{
LCD_DisplayStringLine(Line0,(unsigned char *)" Setting ");
if(i==1)
{
LCD_SetBackColor(White);
LCD_SetTextColor(Blue);
}
sprintf((char*)str," Max Volt:%1.1f ",max_ad);
LCD_DisplayStringLine(Line2,str);
LCD_SetBackColor(Blue);
LCD_SetTextColor(White);
if(i==2)
{
LCD_SetBackColor(White);
LCD_SetTextColor(Blue);
}
sprintf((char*)str," Min Volt:%1.1f ",min_ad);
LCD_DisplayStringLine(Line4,str);
LCD_SetBackColor(Blue);
LCD_SetTextColor(White);
if(i==3)
{
LCD_SetBackColor(White);
LCD_SetTextColor(Blue);
}
sprintf((char*)str," Upper:LD%d ",j);
LCD_DisplayStringLine(Line6,str);
LCD_SetBackColor(Blue);
LCD_SetTextColor(White);
if(i==4)
{
LCD_SetBackColor(White);
LCD_SetTextColor(Blue);
}
sprintf((char*)str," Lower:LD%d ",k);
LCD_DisplayStringLine(Line8,str);
LCD_SetBackColor(Blue);
LCD_SetTextColor(White);
}
if(error==1)
{
LCD_SetTextColor(Red);
LCD_DisplayStringLine(Line9,(unsigned char*)" Error! ");
LCD_SetTextColor(White);
}
else
{
LCD_ClearLine(Line9);
}
led_choose=0;
}
void error_choose(void)
{
if(set==1)
{
if(((max_ad>min_ad)&&fabs(max_ad-min_ad)>1e-6)&&(j!=k))
{
error=0;
}
else
{
error=1;
}
}
}
void SysTick_Handler(void)//滴答定时器初始化
{
TimingDelay--;
if(led_choose==1)
{
if(++led_delay==200)
{
led_delay=0;
runled&=~led_buff[j];
runled^=led_buff[k];
}
}
else if(led_choose==2)
{
runled&=~0xff;
}
else if(led_choose==3)
{
if(++led_delay==200)
{
led_delay=0;
runled&=~led_buff[k];
runled^=led_buff[j];
}
}
else
{
runled=0x00;//关所有led
}
}