资源描述
嵌入式系统课程设计
姓名:
班级:
学号:
目录:
一. 系统要求
二. 设计方案
三.程序流程图
四.软件设计
五.课程总结与个人体会
一、系统要求
使用STM32F103作为主控CPU设计一个温度综合测控系统,具体要求:
1、使用热敏电阻或者内部集成的温度传感器检测环境温度,每0.1秒检测一次温度,对检测到的温度进行数字滤波(可以使用平均法)。记录当前的温度值和时间。
2、使用计算机,通过串行通信获取STM32F103检测到的温度和所对应的时间。
3、使用计算机进行时间的设定。
4、使用计算机进行温度上限值和下限值的设定。
5、若超过上限值或者低于下限值,则STM32进行报警提示。
二、 设计方案
本次课程设计的要求是使用STM32F103设计一个温度测控系统,这款单片机集成了很多的片上资源,功能十分强大,我使用了以下部分来完成课程设计的要求:
1、 STM32F103内置了3个12位A/D转换模块,最快转换时间为1us。本次课程设计要求进行温度测定,于是使用了其中一个ADC对片上温度传感器的内部信号源进行转换。当有多个通道需要采集信号时,可以把ADC配置为按一定的顺序来对各个通道进行扫描转换,本设计只采集一个通道的信号,所以不使用扫描转换模式。 本设计需要循环采集电压值,所以使用连续转换模式。
2、 本次课程设计还使用到了DMA。DMA是一种高速的数据传输操作,允许在外部设备和储存器之间利用系统总线直接读写数据,不需要微处理器干预。使能ADC的DMA接口后,DMA控制器把转换值从ADC数据寄存器(ADC_DR)中转移到变量ADC_ConvertedValue中,当DMA传输完成后,在main函数中使用的ADC_ConvertedValue的内容就是ADC转换值了。
3、 STM32内部的温度传感器和ADCx_IN16输入通道相连接,此通道把传感器输出的电压值转换成数字值。STM内部的温度传感器支持的温度范围:-40到125摄氏度。利用下列公式得出温度
温度(°C) = {(V25 - VSENSE) / Avg_Slope} + 25
式中V25是 VSENSE在25摄氏度时的数值(典型值为1.42V)
Avg_Slope是温度与VSENSE曲线的平均斜率(典型值为4.3mV/C)
利用均值法对转换后的温度进行滤波,将得到的温度通过串口输出。
4、 本设计采用了USART1作为串行通信接口,来进行时间、温度的传输,以及进行时间和温度上下限的设定。
5、 当温度超过上下限时,开发板上的灯会相应亮起作为警报,使用了GPIO配置引脚。
6、 时间计时使用了systick时钟,并配置其中断,由此进行一秒定时,实现时钟的实时显示。
7、 时间设定部分参考了一个两位数字读取的函数,在进入主循环前设定参数,从而避免了在串口中断中输入只能一次性输入所有参数的弊端。
三、 程序流程图
开始
各模块初始化
设定温度
显示当前
时间温度
计时一秒
是
对应警告灯亮
判断当前温度
是否超过设定范围
否
警告灯全灭
四、 软件设计
用到的库文件:
stm32f10x_adc.h,stm32f10x_dma.h,stm32f10x_flash.h,stm32f10x_gpio.h,stm32f10x_rcc.h,stm32f10x_usart.h,misc.h
自己编写的文件:
main.c,stm32f10x_it.c,stm32f10x_it.h
main文件:
#include "stm32f10x.h"
#include "stdarg.h"
#include "stdio.h"
#define ADC1_DR_Address ((uint32_t)0x4001244C)
extern __IO u16 ADC_ConvertedValue;
extern __IO u16 calculated_temp;
__IO u16 Current_Temp;
unsigned char sec=0,min=0,hour=0;
typedef struct
{
int tm_sec;
int tm_min;
int tm_hour;
}rtc_time;
rtc_time systmtime;
__IO u16 upper_bound;
__IO u16 lower_bound;
//static uint8_t USART_Scanf(uint32_t value);
void Time_Regulate(rtc_time *tm);
unsigned int TimingDelay=0;
unsigned int KEY_ON;
unsigned int KEY_OFF;
void Delay(u32 count)
{
u32 i=0;
for(;i<count;i++);
}
void LED_GPIO_Config()
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE); // 使能PD端口时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10|GPIO_Pin_11; //LED0-->PD.8端口配置
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO 速度 50MHz
GPIO_Init(GPIOD, &GPIO_InitStructure); //根据设定参数初始化 GPIOB.5
}
void SysTick_Init()
{
if (SysTick_Config(SystemCoreClock / 1000))
{
while(1);
}
SysTick->CTRL &= ~ SysTick_CTRL_ENABLE_Msk;//关闭滴答定时器
//SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk;//开启滴答定时器
}
void Delay_ms(__IO u32 nTime)
{
TimingDelay=nTime;
SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk;//打开
while(TimingDelay != 0);
}
void RCC_Config(void)//配置时钟
{
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);//DMA
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOC, ENABLE);//ADC1 and GPIOC
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);//USART
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE); // 使能PD端口时钟 LED
}
void GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/***Config PA.01 (ADC1)***/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOC, &GPIO_InitStructure);
/***Config LED ***/
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO 速度 50MHz
GPIO_Init(GPIOD, &GPIO_InitStructure); //根据设定参数初始化 GPIOB.5
/***Config USART ***/
/* Configure USART1 Tx (PA.09) as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure USART1 Rx (PA.10) as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void DMA_Config(void)
{
/* DMA channel1 configuration */
DMA_InitTypeDef DMA_InitStructure;
DMA_DeInit(DMA1_Channel1);
DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address; /*ADC??*/
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&ADC_ConvertedValue;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_BufferSize = 16;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel1, &DMA_InitStructure);
/* Enable DMA channel1 */
DMA_Cmd(DMA1_Channel1, ENABLE);
}
void ADC1_Config(void)
{ ADC_InitTypeDef ADC_InitStructure;
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode = ENABLE ;
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
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 channel16 configuration */
ADC_RegularChannelConfig(ADC1, ADC_Channel_16, 1, ADC_SampleTime_55Cycles5);
ADC_TempSensorVrefintCmd(ENABLE);
ADC_DMACmd(ADC1, ENABLE);
ADC_Cmd(ADC1, ENABLE);
ADC_ResetCalibration(ADC1);
while(ADC_GetResetCalibrationStatus(ADC1));
ADC_StartCalibration(ADC1);
while(ADC_GetCalibrationStatus(ADC1));
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
}
void USART1_Config(void)
{
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No ;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART1, &USART_InitStructure);
// USART_ITConfig(USART1,USART_IT_RXNE,ENABLE); //接收使能
// USART_ITConfig(USART1,USART_IT_TXE,ENABLE); //发送使能
USART_Cmd(USART1,ENABLE); //启动串口
}
static uint8_t USART_Scanf(uint32_t value)//字符串读取函数
{
uint32_t index = 0;
uint32_t tmp[2] = {0, 0};
while (index < 2)
{
/* Loop until RXNE = 1 */
while (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) ==RESET)
{
}
tmp[index++] = (USART_ReceiveData(USART1));
if ((tmp[index - 1] < 0x30) || (tmp[index -1] > 0x39))
{
printf("\n\r请输入有效数字 0 到 9 -->: ");
index--;
}
}
index = (tmp[1] - 0x30) + ((tmp[0] - 0x30) * 10);
/* Checks */
if (index > value)
{
printf("\n\r请输入有效数字 0 到 %d", value);
return 0xFF;
}
return index;
}
void Time_Regulate(rtc_time *tm)//时间设定函数
{
uint32_t Tmp_HH =0xFF, Tmp_MI = 0xFF, Tmp_SS = 0xFF;
uint32_t Tmp_up = 0xff,Tmp_low = 0xff;
printf("\r\n 设定温度范围");
printf("\r\n 输入温度上限: ");
while (Tmp_up == 0xFF)
{
Tmp_up = USART_Scanf(99);
}
printf("\n\r 温度上限为 %0.2d C\n\r", Tmp_up);
upper_bound = Tmp_up;
//-------------------
printf("\r\n 输入温度下限: ");
while (Tmp_low == 0xFF)
{
Tmp_low = USART_Scanf(99);
}
printf("\n\r 温度下限为 %0.2d C\n\r", Tmp_low);
lower_bound = Tmp_low;
printf("\r\n 设定时间 ");
Tmp_HH = 0xFF;
printf("\r\n 设定小时: ");
while (Tmp_HH == 0xFF)
{
Tmp_HH = USART_Scanf(23);
}
printf("\n\r 设定小时为 %d\n\r", Tmp_HH );
tm->tm_hour= Tmp_HH;
Tmp_MI = 0xFF;
printf("\r\n 设定分钟: ");
while (Tmp_MI == 0xFF)
{
Tmp_MI = USART_Scanf(59);
}
printf("\n\r 设定分钟为 %d\n\r", Tmp_MI);
tm->tm_min= Tmp_MI;
Tmp_SS = 0xFF;
printf("\r\n 设定秒: ");
while (Tmp_SS == 0xFF)
{
Tmp_SS = USART_Scanf(59);
}
printf("\n\r 设定秒为 %d\n\r", Tmp_SS);
tm->tm_sec= Tmp_SS;
}
int fputc(int ch, FILE *f)//重定向函数
{
USART_SendData(USART1, (unsigned char) ch);
// while (!(USART1->SR & USART_FLAG_TXE));
while( USART_GetFlagStatus(USART1,USART_FLAG_TC)!= SET);
return (ch);
}
/*****************************主函数***********************************************/
int main(void)
{
#ifdef DEBUG
#endif
SysTick_Init();
LED_GPIO_Config();
RCC_Config();
GPIO_Config();
DMA_Config();
ADC1_Config();
USART1_Config();
Delay(5000);
Time_Regulate(&systmtime);
GPIO_SetBits(GPIOD, GPIO_Pin_8);
GPIO_SetBits(GPIOD, GPIO_Pin_9);
GPIO_SetBits(GPIOD, GPIO_Pin_10);
GPIO_SetBits(GPIOD, GPIO_Pin_11);
sec=systmtime.tm_sec;
min=systmtime.tm_min;
hour=systmtime.tm_hour;
while(1)
{
sec++;
if(sec==60)
{ sec=0;min++;
if(min==60)
{
min=0;hour++;
if(hour==24)
{
hour=0;
}
}
}
printf("\r\n 当前时间: %d :%d :%d \r\n", hour,min,sec);
printf("\r\n 当前温度: %02d C 温度上限:%02d C 温度下限:%02d C \r\n",Average_Temp,upper_bound,lower_bound);
GPIO_SetBits(GPIOD, GPIO_Pin_8);
GPIO_SetBits(GPIOD, GPIO_Pin_9);
GPIO_SetBits(GPIOD, GPIO_Pin_10);
GPIO_SetBits(GPIOD, GPIO_Pin_11);
if(((int)Current_Temp) > ((int)upper_bound))
{
GPIO_ResetBits(GPIOD, GPIO_Pin_8);
}
else if(((int)Current_Temp) < ((int)lower_bound))
{
GPIO_ResetBits(GPIOD, GPIO_Pin_11);
}
else{
GPIO_SetBits(GPIOD, GPIO_Pin_8);
GPIO_SetBits(GPIOD, GPIO_Pin_9);
GPIO_SetBits(GPIOD, GPIO_Pin_10);
GPIO_SetBits(GPIOD, GPIO_Pin_11);}
Delay_ms(1000);
}
}
stm32f10x_it.c文件:
/* Includes ------------------------------------------------------------------*/
#include "stm32f10x_it.h"
/* Private functions ---------------------------------------------------------*/
void display(void)
{
unsigned char ad_data,ad_value_max,ad_value_min;
ad_data=Current_Temp;
if(ad_sample_cnt==0)
{
ad_value_max=ad_data;
ad_value_min=ad_data;
}
else if(ad_data<ad_value_min)
{
ad_value_min=ad_data;
}
else if(ad_data>ad_value_max)
{
ad_value_max=ad_data;
}
ad_value_sum+=ad_data;
ad_sample_cnt++;
if(ad_sample_cnt==10)
{
ad_value_sum-=ad_value_min;
ad_value_sum-=ad_value_max;
ad_value_sum/=8;
calculated_temp=ad_value_sum;
ad_sample_cnt=0;
ad_value_min=0;
ad_value_max=0;
}
}
void SysTick_Handler(void)
{
TimingDelay--;
ADC_tempValueLocal = ADC_ConvertedValue;
//printf("\n %02d \n, ADC_ConvertedValue");
Current_Temp=(V25-ADC_tempValueLocal)/Avg_Slope+25;
temp_sum+=Current_Temp;
temp_cnt++;
if(temp_cnt>=10)
{
temp_cnt=0;
temp_sum/=10;
Average_Temp=temp_sum;
temp_sum=0;
}
//printf("\r\n The current temperature = %02d C\r\n", calculated_temp);
}
五、 课程总结与个人体会
嵌入式开发是自动化专业的主要课程之一,现实生活中,嵌入式在应用可以说得是无处不在。因此在大学中掌握嵌入式的开发技术是十分重要的,也是十分必要的。
本次使用基于Cortex-M3内核的32位ARM处理器stm32作为主控制器,设计了一种温度测控系统。
系统中,使用了ADC、DMA、温度传感器、USART、GPIO、定时器、NVIC等资源,实践了课上所学的内容,深深体会到了应用的重要性。在课程设计的过程中,为了减小干扰的影响,数据采集后,平均算法进行温度输出。并利用串口设计了简单的交互系统,虽然没有使用上位机,但也达到了比较好的效果。通过本次课程设计,着实经历到了很多想象不到的困难,自己的一些想法也不够成熟,最后还是参考了别人的解决方案,这让我深深认识到在嵌入式开发这条路上,与别人交流学习是提升自己的非常有效的方式。
在设计串口设定时间的程序时,我最开始的想法是通过USART的中断进行输入字符的识别,从而分别设定时间以及温度上下限,可是经过自己的冥思苦想还是想不出来,怎么都实现不了。无奈之下,我只好去隔壁寝室的大神那里虚心求教,在参考了他的程序之后我恍然大悟,选择了在循环之外先按顺序读取字符串的方法,顺利解决了我的问题,让我深深认识到了交流的重要性,在自己的想法不够完善时,多多了解些别人的算法对提升自己是有很大帮助的。
由于之前没有完整开发一个有较多功能系统的经历,在本次做课程设计的过程中,走了不少的弯路,也学到很多课本上没有的知识。使用库开发Stm32时,非常注重模块化的概念,不光是很多片上资源使用库文件来进行封装,自己在编写一些函数时也应该学会进行封装,其中又涉及到c语言很多之前没太注意到的地方,在开发过程中着实让我吃了不少苦头,不过幸运的是同学的指导下,我一点点解决了那些疑惑的地方,更加深入了理解了一个工程的整体结构,对模块化的思想印象深刻。这对我以后的开发将起到巨大的作用。
总之,本次的嵌入式课程设计让我收获了很多,不仅仅学习到了很多课本和课堂上学不到的东西,更重要的是学习到了库开发的思想,以及体会到了交流的重要性,同时也感谢老师这一学期来的认真授课,严谨的答疑解惑,让我认识到理论知识对开发潜移默化的作用。
20
展开阅读全文