概述:本综合系统包含矩阵键盘控制LED,串口控制LED,RTC时间显示,温度显示和超声波测距五个基本模块,菜单在LCD1602上显示。属于比较基本的stm32的综合系统。
个人通过面包板和杜邦线比较简易地搭建了这个综合系统,其他原料为:LCD1602显示屏,stm32f103c8t6最小系统板,USB转TTL及其驱动,超声波模块,4*4矩阵键盘。
杂乱的外表:
文件配置:
接线:
//矩阵键盘
PA0-PA3与R1~R4PA4~PA7与C1~C4
//串口
TXD与PA10
RXD与PA9
//LCD1602
VSS与GND
VDD与5V
V0与GND
RS与PA8
RW与PA11
EN与PA12
D4与PB8
D5~D7与PB5~PB7
A与3.3V
K与GND
//超声波传感模块
PB0与Ttig
PB1与Echo
main.c部分代码#include "stm32f10x.h"
#include "delay.h"
#include "rtc.h"
#include "sys.h"
#include "usart.h"
#include "LCD1602.h"
#include "keyscan.h"
#include "tsensor.h"
u8 num,ch,flag,rtcflag=0,led,rtcnum,overflow=0;
u8 tempch[5],distch[3],rtcch[4];
u16 adct,tempz; //存放AD转换的结果
float tempv,tempt;
void gpio_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOA,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_1;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
void nvic_init(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
}
void time_init(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
TIM_TimeBaseStructure.TIM_Period = 4999; //x/(72M/prescaler)=0.025
TIM_TimeBaseStructure.TIM_Prescaler = 359;
TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
TIM_Cmd(TIM3, DISABLE);
TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);
}
u8 recedata(void)
{
if(USART_GetFlagStatus(USART1, USART_FLAG_RXNE)==1)
{
ch = USART_ReceiveData(USART1);
}
return ch;
}
void get_rtcch(rtcnum)
{
u8 rtcnumm;
rtcnumm=get_rtcnum(rtcnum);
rtcch[0]=rtcnumm/1000+0x30;
rtcch[1]=(rtcnumm/100)%10+0x30;
rtcch[2]=(rtcnumm/10)%10+0x30;
rtcch[3]=rtcnumm%10+0x30;
if(rtcch[0]=='0'&&rtcch[1]=='0')
{
rtcch[0]=rtcch[2];
rtcch[1]=rtcch[3];
rtcch[2]=' ';
rtcch[3]=' ';
}
}
void mainmenu(void)
{
LCD_init();
LCD_write_string(1,1,"1LED 2USART 3RTC");
LCD_write_string(1,2,"4TMP 5SON");
}
void auxmenu(int key1)
{
switch(key1)
{
case 1:
LCD_init();
LCD_write_string(1,1,"KEY CONTROL LED");
LCD_write_string(1,2,"1KS 2MS 3CL *Q");
break;
case 2:
LCD_init();
LCD_write_string(1,1,"UART CONTROL LED");
LCD_write_string(1,2,"A-K B-M C-L *Q");
break;
case 3:
LCD_init();
LCD_write_string(1,1,"#RTC");
LCD_write_string(1,2,"*-QUIT");
break;
case 4:
LCD_init();
LCD_write_string(1,1,"#TEMP");
LCD_write_string(1,2,"*-QUIT");
break;
case 5:
LCD_init();
LCD_write_string(1,1,"#SON");
LCD_write_string(1,2,"*-QUIT");
break;
case 31:
LCD_init();
LCD_write_string(1,1,"min:");
get_rtcch(5);
LCD_write_string(5,1,rtcch);
LCD_write_string(9,1,"sec:");
get_rtcch(6);
LCD_write_string(13,1,rtcch);
LCD_write_string(1,2,"C-Change *-QUIT");
break;
case 32:
LCD_init();
LCD_write_string(1,1,"mon:");
get_rtcch(2);
LCD_write_string(5,1,rtcch);
LCD_write_string(8,1,"date:");
get_rtcch(3);
LCD_write_string(13,1,rtcch);
LCD_write_string(1,2,"C-Change *-QUIT");
break;
case 41:
LCD_init();
LCD_write_string(1,1,"TEMP:");
LCD_write_string(8,1,tempch);
LCD_write_string(13,1," ^C ");
LCD_write_string(1,2,"*QUIT");
break;
case 51:
LCD_init();
LCD_write_string(1,1,"Distance:");
LCD_write_string(10,1,distch);
LCD_write_string(13,1," ");
LCD_write_string(1,2,"*QUIT");
break;
default:
break;
}
}
void flash_fast(void)
{
while(1)
{
PCout(13)=0;
delay_ms(50);
PCout(13)=1;
delay_ms(50);
num=keyscan();
ch=recedata();
if(num==12||ch=='Q')
{
break;
}
}
}
void flash_slow(void)
{
while(1)
{
PCout(13)=0;
delay_ms(500);
PCout(13)=1;
delay_ms(500);
num=keyscan();
ch=recedata();
if(num==12||ch=='Q')
{
break;
}
}
}
void led_cl(void)
{
while(1)
{
PCout(13)=0;
num=keyscan();
ch=recedata();
if(num==12||ch=='Q')
{
break;
}
}
}
u16 dist_sensor(void)
{
u16 time_count;
float distance;
PBout(0)=1;
delay_us(10);
PBout(0)=0;
while(PBin(1)==0&&overflow==0);
TIM_Cmd(TIM3, ENABLE);
TIM_SetCounter(TIM3,0x00);
while(PBin(1)==1&&overflow==0);
TIM_Cmd(TIM3, DISABLE);
time_count=TIM_GetCounter(TIM3);
distance=time_count/200.0*17;
if(overflow==1)
{
distance=999;
overflow=0;
}
return distance;
}
void TIM3_IRQHandler(void)
{
if(TIM_GetITStatus(TIM3, TIM_IT_Update) == 1)
{
TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
overflow=1;
}
}
void ledctr(void)
{
num = keyscan();
if(num==1)
{
flash_fast();
}
if(num==2)
{
flash_slow();
}
if(num==3)
{
led_cl();
}
PCout(13)=1;
}
void usartctr(void)
{
ch=recedata();
if(ch=='A')
{
flash_fast();
}
if(ch=='B')
{
flash_slow();
}
if(ch=='C')
{
led_cl();
}
PCout(13)=1;
}
void get_temp(void)
{
num=keyscan();
if(num==11)
{
while(1)
{
adct=T_Get_Temp(); //获取温度的数字量
tempv=adct*3.3/4096; //获得电压量3.3为基准电压,4096为2的12次方
tempt=(1.43-tempv)/0.0043+25-120; //获取温度摄氏度值,1.43为25度时的温度值,0.0043为温度/VSENSE的平均斜率
tempz=tempt*100;
tempch[0]=tempt/10+0x30;
tempch[1]=(tempz/100)%10+0x30;
tempch[2]='.';
tempch[3]=(tempz/10)%10+0x30;
tempch[4]=tempz%10+0x30;
auxmenu(41);
delay_ms(200);
num=keyscan();
if(num==12)
{
break;
}
}
}
}
void get_rtc()
{
num=keyscan();
if(num==11)
{
while(1)
{
if(rtcflag==0)
{
auxmenu(31);
}
else
{
auxmenu(32);
}
delay_ms(200);
num=keyscan();
if(num==13)
{
rtcflag=~rtcflag;
}
if(num==12)
{
break;
}
}
}
}
void get_son()
{
u16 dist;
num=keyscan();
if(num==11)
{
while(1)
{
dist=dist_sensor();
delay_ms(500);
distch[0]=dist/100+0x30;
distch[1]=(dist-100*(dist/100))/10+0x30;
distch[2]=dist%10+0x30;
auxmenu(51);
delay_ms(200);
num=keyscan();
if(num==12)
{
break;
}
}
}
}
void quit(int key2)
{
ch=recedata();
num=keyscan();
if(ch=='Q'||num==12)
{
ch=recedata();
num=keyscan();
if(ch=='Q'||num==12)
{
PCout(13)=1;
ch='s';
num=0xff;
mainmenu();
flag=1;
}
else
{
ch='s';
num=0xff;
auxmenu(key2);
flag=0;
}
}
}
int main()
{
delay_init();
gpio_init();
uart_init(9600);
T_Adc_Init();
time_init();
nvic_init();
RTC_Init();
RTC_Set(2020,12,15,9,40,00);
mainmenu();
while(1)
{
flag=0;
ch=recedata();
num=keyscan();
if(num==1||ch=='1')
{
auxmenu(1);
while(1)
{
num=keyscan();
ledctr();
quit(1);
if(flag==1)
{
break;
}
else
{
continue;
}
}
}
if(num==2||ch=='2')
{
auxmenu(2);
while(1)
{
ch=recedata();
num=keyscan();
usartctr();
quit(2);
if(flag==1)
{
break;
}
else
{
continue;
}
}
}
if(num==3||ch=='3')
{
auxmenu(3);
delay_ms(1000);
while(1)
{
ch=recedata();
num=keyscan();
get_rtc();
quit(3);
if(flag==1)
{
break;
}
else
{
continue;
}
}
}
if(num==4||ch=='4')
{
auxmenu(4);
delay_ms(1000);
while(1)
{
ch=recedata();
num=keyscan();
get_temp();
quit(4);
if(flag==1)
{
break;
}
else
{
continue;
}
}
}
if(num==5||ch=='5')
{
auxmenu(5);
delay_ms(1000);
while(1)
{
ch=recedata();
num=keyscan();
get_son();
quit(5);
if(flag==1)
{
break;
}
else
{
continue;
}
}
}
}
}
具体工程欢迎下载附件文件。
全部资料51hei下载地址:
mainSystem.7z
(188.51 KB, 下载次数: 52)
|