}
voidLED_GPIO_Config()
{
GPIO_InitTypeDefGPIO_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
}
voidSysTick_Init()
{
if(SysTick_Config(SystemCoreClock/1000))
{
while
(1);
}
SysTick->CTRL&=~SysTick_CTRL_ENABLE_Msk;//关闭滴答定时器
//SysTick->CTRL|=SysTick_CTRL_ENABLE_Msk;//开启滴答定时器
}
voidDelay_ms(__IOu32nTime)
{
TimingDelay=nTime;
SysTick->CTRL|=SysTick_CTRL_ENABLE_Msk;//打开
while(TimingDelay!
=0);
}
voidRCC_Config(void)//配置时钟
{
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE);//DMA
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1|RCC_APB2Periph_GPIOC,ENABLE);//ADC1andGPIOC
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA,ENABLE);//USART
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD,ENABLE);//使能PD端口时钟LED
}
voidGPIO_Config(void)
{
GPIO_InitTypeDefGPIO_InitStructure;
/***ConfigPA.01(ADC1)***/
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AIN;
GPIO_Init(GPIOC,&GPIO_InitStructure);
/***ConfigLED***/
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;//推挽输出
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;//IO速度50MHz
GPIO_Init(GPIOD,&GPIO_InitStructure);//根据设定参数初始化GPIOB.5
/***ConfigUSART***/
/*ConfigureUSART1Tx(PA.09)asalternatefunctionpush-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);
/*ConfigureUSART1Rx(PA.10)asinputfloating*/
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA,&GPIO_InitStructure);
}
voidDMA_Config(void)
{
/*DMAchannel1configuration*/
DMA_InitTypeDefDMA_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);
/*EnableDMAchannel1*/
DMA_Cmd(DMA1_Channel1,ENABLE);
}
voidADC1_Config(void)
{ADC_InitTypeDefADC_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);
/*ADC1regularchannel16configuration*/
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);
}
voidUSART1_Config(void)
{
USART_InitTypeDefUSART_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);//启动串口
}
staticuint8_tUSART_Scanf(uint32_tvalue)//字符串读取函数
{
uint32_tindex=0;
uint32_ttmp[2]={0,0};
while(index<2)
{
/*LoopuntilRXNE=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);
return0xFF;
}
returnindex;
}
voidTime_Regulate(rtc_time*tm)//时间设定函数
{
uint32_tTmp_HH=0xFF,Tmp_MI=0xFF,Tmp_SS=0xFF;
uint32_tTmp_up=0xff,Tmp_low=0xff;
printf("\r\n设定温度范围");
printf("\r\n输入温度上限:
");
while(Tmp_up==0xFF)
{
Tmp_up=USART_Scanf(99);
}
printf("\n\r温度上限为%0.2dC\n\r",Tmp_up);
upper_bound=Tmp_up;
//-------------------
printf("\r\n输入温度下限:
");
while(Tmp_low==0xFF)
{
Tmp_low=USART_Scanf(99);
}
printf("\n\r温度下限为%0.2dC\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;
}
intfputc(intch,FILE*f)//重定向函数
{
USART_SendData(USART1,(unsignedchar)ch);
//while(!
(USART1->SR&USART_FLAG_TXE));
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!
=SET);
return(ch);
}
/*****************************主函数***********************************************/
intmain(void)
{
#ifdefDEBUG
#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当前温度:
%02dC温度上限:
%02dC温度下限:
%02dC\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);
}
elseif(((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"
/*Privatefunctions---------------------------------------------------------*/
voiddisplay(void)
{
unsignedcharad_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;
}
elseif(ad_data{
ad_value_min=ad_data;
}
elseif(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-