概述:本綜合系統包含矩陣鍵盤控制LED,串口控制LED,RTC時間顯示,溫度顯示和超聲波測距五個基本模塊,菜單在LCD1602上顯示。屬于比較基本的stm32的綜合系統。
個人通過面包板和杜邦線比較簡易地搭建了這個綜合系統,其他原料為:LCD1602顯示屏,stm32f103c8t6最小系統板,USB轉TTL及其驅動,超聲波模塊,4*4矩陣鍵盤。
雜亂的外表:
1608362885175.jpg (6.15 MB, 下載次數: 79)
下載附件
2020-12-19 15:45 上傳
1608364513933.jpg (4.58 MB, 下載次數: 52)
下載附件
2020-12-19 15:56 上傳
文件配置:
51hei截圖20201219153416.png (82.99 KB, 下載次數: 47)
下載附件
2020-12-19 15:45 上傳
接線:
//矩陣鍵盤
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)
2020-12-19 18:34 上傳
點擊文件名下載附件
stm32綜合小系統 下載積分: 黑幣 -5
|