stm8红外避障程序下载:
stm8红外避障程序.zip
(411.73 KB, 下载次数: 9)
- /*****************************************************************
- * 实验名称及内容:趣味实验之壁障检测
- * 配套书籍:《深入浅出STM8单片机入门、进阶与应用实例》
- * 交流QQ群: 305348768
- * 实验平台及编程人员:思修电子工作室"祥云小王子"开发版 Long
- ******************************************************************/
- #include "iostm8s208mb.h"//主控芯片的头文件
- #include "stdio.h"//需要使用printf()函数故而包含该头文件
- /************************常用数据类型定义*************************/
- #define u8 uint8_t
- #define u16 uint16_t
- #define u32 uint32_t
- typedef unsigned char uint8_t;
- typedef unsigned short uint16_t;
- typedef unsigned long uint32_t;
- /*************************端口/引脚定义区域***********************/
- #define Obstacle_evading PE_IDR_IDR6 //连接至避障传感器引脚
- /**************************函数声明区域***************************/
- void delay(u16 Count); //延时函数声明
- void UART3_Init(void); //串口3初始化函数
- void UART3_SendByte(u8 data); //串口3发送单个字符函数
- int putchar(int ch); //发送字符重定向函数
- /**************************主函数区域*****************************/
- void main(void)
- {
- CLK_CKDIVR=0x00; //选定HSI时钟源,配置频率为16MHz
- delay(10); //延时等待时钟稳定
- UART3_Init(); //初始化串口3
- delay(50);
- PE_DDR_DDR6=0; //配置PE6端口为输入模式
- PE_CR1_C16=1; //配置PE6端口为弱上拉输入模式
- PE_CR2_C26=1; //使能PE6端口外部中断
- while(1) //死循环
- {
- if(Obstacle_evading==0)//检测到障碍物
- {
- //printf("<I>18"); //提示音1
- //delay(1000);
- printf("<G>前方有障碍物\n");
- delay(1000);
- while(!Obstacle_evading);//直到远离障碍物
- }
- }
- }
- /****************************************************************/
- //延时函数delay(),有形参Count用于控制延时函数执行次数,无返回值
- /****************************************************************/
- void delay(u16 Count)
- {
- u8 i,j;
- while (Count--)//Count形参控制延时次数
- {
- for(i=0;i<50;i++)
- for(j=0;j<20;j++);
- }
- }
- /****************************************************************/
- //初始化函数UART3_Init(),无形参和返回值
- /****************************************************************/
- void UART3_Init(void)
- {
- //1.设定通信数据位数,此处设定为8位数据位,无校验位
- UART3_CR1 = 0x00;
- //2.设定通信停止位位数,此处设定为1位停止位
- UART3_CR3 = 0x00;
- //3.配置通信波特率参数,此处配置为9600bps(16MHz频率下)
- UART3_BRR2 = 0x03;
- UART3_BRR1 = 0x68;
- //4.使能发送和接收功能
- UART3_CR2 = 0x0C;
- }
- /****************************************************************/
- //发送单字符函数UART3_SendByte(),有形参data,无返回值
- /****************************************************************/
- void UART3_SendByte(u8 data)
- {
- UART3_DR=data;//发送数据到UART3数据寄存器
- while (!(UART3_SR & 0x80));//等待发送完毕
- }
- /****************************************************************/
- //发送字符重定向函数putchar(),有形参ch,有返回值
- /****************************************************************/
- int putchar(int ch)
- {
- while((UART3_SR&0x80)==0x00);
- UART3_SendByte((u8)ch);//将Printf内容发往串口
- return (ch);
- }
复制代码
|