|
適用于需要代碼想法的人,
此例程為中斷實(shí)現(xiàn)
單片機(jī)源程序如下:
- #include "sys.h"
- #include "delay.h"
- #include "usart.h"
- #include "led.h"
- #include "lcd.h"
- #include "timer.h"
- #include "wave.h"
- int main(void)
- {
- u8 t=20;//trig引腳高電平持續(xù)時(shí)間
- float Distance;
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//設(shè)置系統(tǒng)中斷優(yōu)先級(jí)分組2
- delay_init(168); //初始化延時(shí)函數(shù)
- uart_init(115200); //初始化串口波特率為115200
- LED_Init(); //初始化LED
- Wave_SRD_Init();//初始化超聲波
- TIM3_Int_Init(9998,7199);// APB1定時(shí)器時(shí)鐘84M,分頻系數(shù)8400,所以84M/8400=10KHZ的技術(shù)頻率,記一次數(shù)0.1ms
- LCD_Init(); //初始化LCD FSMC接口
- while(Wave_SRD_Strat(t))
- {
- LCD_ShowString(60,140,200,16,16,"HC_SR04 IS ERROR");
- delay_ms(200);
- LCD_Fill(60,330,239,130+16,WHITE);
- delay_ms(200);
- }
- LCD_ShowString(60,160,200,16,16,"HC_SR04 IS OK");
- POINT_COLOR=BLUE;
- LCD_ShowString(60,180,200,16,16,"Distance: cm");
- while(1)
- {
- Wave_SRD_Strat(t);
- Distance = TIM_GetCounter(TIM3)*340/200.0;
- if(Distance>0)
- {
- LCD_ShowNum(60+70,180,Distance,6,16);//顯示測(cè)量距離
- delay_ms(10);
- }
- LED1=!LED1;
- delay_ms(2000);
- }
- }
復(fù)制代碼
所有資料51hei提供下載:
F407超聲波實(shí)驗(yàn).7z
(338.23 KB, 下載次數(shù): 64)
2021-7-8 05:26 上傳
點(diǎn)擊文件名下載附件
|
評(píng)分
-
查看全部評(píng)分
|