简单程序尚有不足请指教
单片机源程序如下:
- #include "delay.h"
- #include "sys.h"
- #include "stm32f10x.h"
- #include "RTC_Time.h"
- #include <stdio.h>
- #include "SPI_TFT.h"
- #include "Image.h"
- #include "timer.h"
- #include "exti.h"
- #include "usart.h"
- #define uchar unsigned char
- #define uint unsigned int
-
- #define BUFLEN 50
- #define BUFLEN2 50
- #define BUFLEN2 50
- #define HEADLEN 5
- #define LEN_OFFSET 2
- extern u16 backing_up,distance_measurement;
- u8 flag_m1=0,flag_m2; //清屏标志位
- //unsigned int Color = YELLOW; // 前景颜色
- //unsigned int Color_BK = BLUE; // 背景颜色
- extern int shifenwei,baifenwei,gewei,shiwei,baiwei;
- extern u8 mode_number;
- extern int number,man;
- extern u16 key_flag,show_flag_2;
-
- extern float pinjun;
- extern u16 b,a,show_flag_1,flag_measure,flag_over;
- extern int shifenwei,baifenwei,gewei,shiwei,baiwei;
- uchar head[HEADLEN] = {0xfd,0x00,0x00,0x01,0x01};
- uchar speed[HEADLEN] = {0x5B,0x74,0x30,0x5D,0xB8};
- uchar text[BUFLEN] = {"距离目标 厘米"};
- uchar text2[BUFLEN] = {"距离过近"};
- uchar text3[BUFLEN] = {"超出检测范围"};
- uchar sound[6] = {0x5B,0x76,0x31,0x30,0x5D,0x84};
- void Speech(uchar *buf, uchar len);
- int main(void)
- {
- SystemInit();
- NVIC_Configuration();
- delay_init();
- uart_init(9600);
- InitLcd();
- TIM3_Int_Init(39999,72); //测距最远距离2m 此时计数器溢出 计数器每走一步1us 记满40ms
- EXTIX_Init();
- LCD_clear(BLACK);
- Color_BK = BLACK;
- start_show();
- while(1)
- {
- mode_chose();
- if(mode_number%2==1)
- { flag_m1++;
- if(flag_m1==1)LCD_clear(BLACK);
- else{ Reversing();
- man++;
- GPIO_SetBits(GPIOB,GPIO_Pin_0);
- delay_us(20);
- GPIO_ResetBits(GPIOB,GPIO_Pin_0);
- delay_ms(100);
- }
- if(flag_over==1) //检测一次结束
- {
- number++;
- flag_over=0;
- if(number>4)
- {
- number=0;
- show_flag_2=1;
- }
- }
- if(show_flag_2==1) //测距显示
- {
- if(show_flag_1==1)
- {
- show_flag_1=0;
- juli_date_show(); //数据显示函数
- show_flag_2=0;
- }
- }
- }
- if(mode_number%2==0)
- { flag_m1=0;
- if(flag_measure==1)
- { man++;
- flag_m2++;
- if( flag_m2==1) LCD_clear(BLACK);
- else{
- ultrasonic_ranging_show();
- GPIO_SetBits(GPIOB,GPIO_Pin_0);
- delay_us(20);
- GPIO_ResetBits(GPIOB,GPIO_Pin_0);
- delay_ms(100);
- flag_measure=0;
- }
- }
- if(flag_over==1) //检测一次结束
- {
- number++;
- flag_measure=1;
- flag_over=0;
- if(number>4)
- {
- number=0;
- flag_measure=0;
- show_flag_2=1;
- }
- }
- if(show_flag_2==1) //测距显示
- {
- if(show_flag_1==1)
- {
- show_flag_1=0;
- juli_date_show(); //数据显示函数
- if(pinjun<3) Speech(text2,25);
- if(pinjun>200) Speech(text3,25);
- if(pinjun>3 && pinjun<200)
- {
- sprintf(text + 9,"%5.2f",pinjun);
- Speech(text,25);
- }
- delay_ms(20);
- show_flag_2=0;
- flag_m1=0;
- }
- }
- }
- }
-
- }
-
- /*****************************************************************
- * 名 称: Speech()
- * 功 能: 语音播报陈程序
- * 入口参数: 无
- * 出口参数: 无
- *****************************************************************/
- void Speech(uchar *buf, uchar len)
- {
- uchar i = 0;
- uchar xor = 0x00;
- head[LEN_OFFSET] = len+ 3;
- for(i = 0; i < HEADLEN; i++)
- {
- xor ^= head[i];
- USART_SendData(USART1, head[i]);//UART_Trans(head[i]);
- delay_ms(2);
- }
- for(i = 0; i < len; i++)
- {
- xor ^= buf[i];
- USART_SendData(USART1, buf[i]);//UART_Trans(buf[i]);
- delay_ms(2);
- }
- USART_SendData(USART1, xor);//UART_Trans(xor);
- }
复制代码
所有资料51hei提供下载:
TFT oled 2.rar
(330.25 KB, 下载次数: 78)
|