[mw_shl_code=c,true]void UART2_Init()
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
// NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB1Periph_USART2|
RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/*NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2 ;//ÇàÕ¼óÅÏ輶3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //×óóÅÏ輶3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQí¨μàê1Äü
NVIC_Init(&NVIC_InitStructure); //¸ù¾YÖ¸¶¨μÄ2Îêy3õê¼»ˉVIC¼Ä′æÆ÷*/
USART_InitStructure.USART_BaudRate = 9600;//′®¿ú2¨ìØÂê[mw_shl_code=c,true]u16 Ultrasonic_Ranging()
{
u16 temp;
// u32 range;
USART_SendData(USART2,0x55);
PCout(13)=0;
while(USART_GetFlagStatus(USART2,USART_FLAG_TC)!=SET);//μè′y·¢Ëí½áêø
while(USART_GetFlagStatus(USART2,USART_FLAG_RXNE)==RESET);
temp=USART_ReceiveData(USART2);
temp=temp<<8;
while(USART_GetFlagStatus(USART2,USART_FLAG_RXNE)==RESET);
temp|=USART_ReceiveData(USART2);
return temp;
}[/mw_shl_code]
然后是测距
[mw_shl_code=applescript,true]USART_InitStructure.USART_WordLength = USART_WordLength_8b;//×Ö3¤Îa8λêy¾Y¸ñê½
USART_InitStructure.USART_StopBits = USART_StopBits_1;//ò»¸öí£Ö1λ
USART_InitStructure.USART_Parity = USART_Parity_No;//ÎTÆæżD£Ñéλ
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//ÎTó2¼têy¾Yá÷¿ØÖÆ
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //êÕ·¢Ä£ê½
USART_Init(USART2, &USART_InitStructure); //3õê¼»ˉ′®¿ú1
//USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);//¿aÆô′®¿ú½óêüÖD¶Ï
//USART_ITConfig(USART2, USART_IT_TC, ENABLE);
USART_Cmd(USART2, ENABLE); //ê1Äü′®¿ú1
}[/mw_shl_code][/mw_shl_code]
主函数
[mw_shl_code=c,true]#include "Ultrasonic.h"
#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "LED.h"
extern u16 distance;
void usart1_send_char(u8 c)
{
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)==RESET);
USART_SendData(USART1,c);
}
int main()
{
u16 range;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
delay_init();
uart_init(115200);
Ultrasonic_Init();
LED_Init();
while(1)
{
range=Ultrasonic_Ranging();
usart1_send_char(range>>8);
usart1_send_char(range);
PCout(13)=!PCout(13);
delay_ms(500);
}
}
[/mw_shl_code]
不知道为什么一直卡死在测距函数里,好像串口的发送完成标志位一直都没被标志,请各位帮我看一下,不胜感激!!
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>