完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
使用的是串口同方式,板子还是iCore3那块,芯片是F4的,大小异,直接上代码
distance.c #include "..includedistance.h" #include "..includesys.h" #include "..includedelay.h" #include "..includetimer3.h" #include "..includeusart.h" #include "..fwlibincstm32f4xx_gpio.h" #include "..fwlibincstm32f4xx_rcc.h" #include "..fwlibincstm32f4xx_tim.h" u8 flag = 0; u8 USART6_RX_BUF[100]; u8 USART6_RX_STA = 0; unsigned char rxbuf[6],rxempty, rxcnt,rxflag,waitflag; unsigned short int length_val; unsigned int timeout; //VCC 左2行 2/3列 //GND 左2行 1列 //RX接 左11行 2列 //TX接 左11行 3列 void USART6_init(u32 bound) { //GPIO端口设置 GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC,ENABLE); //使能GPIOA时钟 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART6,ENABLE);//使能UART4时钟 //串口6对应引脚复用映射 GPIO_PinAFConfig(GPIOC,GPIO_PinSource6,GPIO_AF_USART6); //GPIOC6复用为UART6 GPIO_PinAFConfig(GPIOC,GPIO_PinSource7,GPIO_AF_USART6); //GPIOC7复用为UART6 //UART4端口配置 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; //GPIOC6与GPIOC7 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//复用功能 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //速度50MHz GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽复用输出 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉 GPIO_Init(GPIOC,&GPIO_InitStructure); //初始化PC6,PC7 //UART4 初始化设置 USART_InitStructure.USART_BaudRate = bound;//波特率设置 USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式 USART_InitStructure.USART_StopBits = USART_StopBits_1;//一个停止位 USART_InitStructure.USART_Parity = USART_Parity_No;//无奇偶校验位 USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制 USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收发模式 USART_Init(USART6, &USART_InitStructure); //初始化串口6 USART_Cmd(USART6, ENABLE); //使能串口3 //USART_ClearFlag(UART4, USART_FLAG_TC); USART_ITConfig(USART6, USART_IT_RXNE, ENABLE);//开启相关中断 //UART4 NVIC 配置 NVIC_InitStructure.NVIC_IRQChannel = USART6_IRQn;//串口6中断通道 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;//抢占优先级3 NVIC_InitStructure.NVIC_IRQChannelSubPriority =3; //子优先级3 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能 NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器、 } void USART6_IRQHandler(void) //串口1中断服务程序 { unsigned char Res; if(USART_GetITStatus(USART6, USART_IT_RXNE) != RESET) //接收中断(接收到的数据必须是0x0d 0x0a结尾) { if(rxflag==0) { rxbuf[rxcnt++] = USART_ReceiveData(USART6);//(USART1->DR); //读取接收到的数据 if(rxcnt>2) { if(waitflag==1) { if((rxbuf[rxcnt-2]=='o')&&(rxbuf[rxcnt-1]=='k'))//串口接收距离数据格式ASCII码0mm~2000mm { waitflag=2; } } else { if((rxbuf[rxcnt-1]=='m')&&(rxbuf[rxcnt-2]=='m'))//串口接收距离数据格式ASCII码0mm~2000mm { rxflag=1; } } } else if(rxcnt>=16) { rxcnt=0; } } else { rxempty = USART_ReceiveData(USART6);//(USART1->DR); //读取接收到的数据 } } } void Send_Data_To_USART6(char dat) { while((USART6->SR&0X40)==0);//循环发送,直到发送完毕 USART6->DR = dat; } void USART_send_string(USART_TypeDef* USARTx,unsigned char *str) { while(*str) { while(!USART_GetFlagStatus(USARTx,USART_FLAG_TXE)); USART_SendData(USARTx,*str); while(!USART_GetFlagStatus(USARTx,USART_FLAG_TC)); str++; } } distance.h #ifndef __distance_h__ #define __distance_h__ #include "sys.h" #define Trig PHout(2) extern u8 flag; extern u8 USART6_RX_BUF[100]; extern u8 USART6_RX_STA; void USART6_init(u32 bound); void USART_send_string(USART_TypeDef* USARTx,unsigned char *str); void Send_Data_To_USART6(char dat); #endif main.c #include "..includeled.h" #include "..includemain.h" #include "..includeusart.h" #include "..includetimer3.h" #include "..includeled.h" #include "..includedistance.h" #include "..includesys.h" #include "..includedelay.h" #include "..includeexti.h" #include "..includekey.h" #include "..includereset.h" #include "..fwlibincstm32f4xx_rcc.h" #include "..fwlibincstm32f4xx_tim.h" #include "..fwlibincstm32f4xx_gpio.h" #include #include #include #include extern unsigned short int length_val; extern unsigned char rxbuf[6],rxempty, rxcnt,rxflag,waitflag; extern unsigned int timeout; int main(void) { delay_init(168); u8 len,t,i,x = 0; unsigned char string[20] = {"r7#rn"}; unsigned char str[] = {"s4-0#"}; led.initialize(); timer3.initialize(); EXTIX_Init(); //外部中断初始化 USART6_init(9600); usart4.initialize(9600); Send_Data_To_USART6('s'); Send_Data_To_USART6('5'); Send_Data_To_USART6('-'); Send_Data_To_USART6('1'); Send_Data_To_USART6('#'); //设为被动读取模式 delay_ms(10); rxcnt=0; usart4.printf("hello! I am iCore3!rn"); //串口4输出 // USART_send_string(UART4,string); USART_send_string(USART6,str); // delay_ms(50); USART_send_string(USART6,string); while(1) { // delay_ms(50); PIout(5) = !PIout(5); Send_Data_To_USART6('r'); Send_Data_To_USART6('6'); Send_Data_To_USART6('#');//主动读取距离命令 timeout=10000; while((rxflag==0)&&(timeout--)); //如果是主动发送只需在主程序里查询rxflag标志 if(rxflag)//接收到1组有效数据 { for(i=0; i if(rxbuf=='m') { if(rxbuf[i+1]=='m') //ASCII码转换为16进制数据,单位mm { if((i>0)&&(rxbuf[i-1]>='0')&&(rxbuf[i-1]<='9')) length_val=rxbuf[i-1]-'0'; if((i>1)&&(rxbuf[i-2]>='0')&&(rxbuf[i-2]<='9')) length_val+=(rxbuf[i-2]-'0')*10; if((i>2)&&(rxbuf[i-3]>='0')&&(rxbuf[i-3]<='9')) length_val+=(rxbuf[i-3]-'0')*100; if((i>3)&&(rxbuf[i-4]>='0')&&(rxbuf[i-4]<='9')) length_val+=(rxbuf[i-4]-'0')*1000; break; } } } rxflag = 0; rxcnt = 0; USART_send_string(UART4,rxbuf); } delay_ms(500); } } |
|
|
|
只有小组成员才能发言,加入小组>>
调试STM32H750的FMC总线读写PSRAM遇到的问题求解?
1780 浏览 1 评论
X-NUCLEO-IHM08M1板文档中输出电流为15Arms,15Arms是怎么得出来的呢?
1621 浏览 1 评论
1081 浏览 2 评论
STM32F030F4 HSI时钟温度测试过不去是怎么回事?
728 浏览 2 评论
ST25R3916能否对ISO15693的标签芯片进行分区域写密码?
1679 浏览 2 评论
1938浏览 9评论
STM32仿真器是选择ST-LINK还是选择J-LINK?各有什么优势啊?
731浏览 4评论
STM32F0_TIM2输出pwm2后OLED变暗或者系统重启是怎么回事?
570浏览 3评论
596浏览 3评论
stm32cubemx生成mdk-arm v4项目文件无法打开是什么原因导致的?
556浏览 3评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-23 16:34 , Processed in 0.872713 second(s), Total 77, Slave 60 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号