画了个板子 485电路如图 使用串口1, 485发送时卡死在while(USART_GetFlagStatus(USART1,USART_FLAG_TC)==RESET)
程序用的原子的
大家帮忙看看问题在哪儿??
[mw_shl_code=c,true]#include "sys.h"
#include "rs485.h"
#include "delay.h"
//////////////////////////////////////////////////////////////////////////////////
//RS485Çy¶ˉ ′úÂë
//////////////////////////////////////////////////////////////////////////////////
#if EN_USART1_RX //èç1ûê1Äüá˽óêÕ
//½óêÕ»o′æÇø
u8 RS485_RX_BUF[64]; //½óêÕ»o3å,×î′ó64¸ö×Ö½ú.
//½óêÕμ½μÄêy¾Y3¤¶è
u8 RS485_RX_CNT=0;
void USART1_IRQHandler(void)
{
u8 res;
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)//½óêÕμ½êy¾Y
{
res =USART_ReceiveData(USART1);//;¶á衽óêÕμ½μÄêy¾YUSART1->DR
if(RS485_RX_CNT<64)
{
RS485_RX_BUF[RS485_RX_CNT]=res; //¼Ç¼½óêÕμ½μÄÖμ
RS485_RX_CNT++; //½óêÕêy¾YÔö¼ó1
}
}
}
#endif
//3õê¼»ˉIO ′®¿ú1
//bound:2¨ìØÂê
void RS485_Init(u32 bound)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE); //ê1ÄüGPIOAê±Öó
RCC_APB1PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);//ê1ÄüUSART1ê±Öó
//′®¿ú1òy½Å¸′óÃó3éä
GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_USART1); //GPIOA9¸′óÃÎaUSART1
GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_USART1); //GPIOA10¸′óÃÎaUSART1
//USART1
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10; //GPIOA9óëGPIOA10
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//¸′óÃ1|Äü
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; //Ëù¶è100MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //íÆíì¸′óÃêä3ö
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //éÏà-
GPIO_Init(GPIOA,&GPIO_InitStructure); //3õê¼»ˉPA9£¬
A10
//PA11íÆíìêä3ö£¬485Ä£꽿ØÖÆ
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; //GPIOA11
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//êä3ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; //Ëù¶è100MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //íÆíìêä3ö
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //éÏà-
GPIO_Init(GPIOA,&GPIO_InitStructure); //3õê¼»ˉPA11
//USART1 3õê¼»ˉéèÖÃ
USART_InitStructure.USART_BaudRate = bound;//2¨ìØÂêéèÖÃ
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(USART1, &USART_InitStructure); //3õê¼»ˉ′®¿ú1
USART_Cmd(USART1, ENABLE); //ê1Äü′®¿ú 1
USART_ClearFlag(USART1, USART_FLAG_TC);
#if EN_USART1_RX
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//¿aÆô½óêüÖD¶Ï
//USART1 NVIC ÅäÖÃ
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;//ÇàÕ¼óÅÏ輶3
NVIC_InitStructure.NVIC_IRQChannelSubPriority =3; //×óóÅÏ輶3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQí¨μàê1Äü
NVIC_Init(&NVIC_InitStructure); //¸ù¾YÖ¸¶¨μÄ2Îêy3õê¼»ˉVIC¼Ä′æÆ÷¡¢
#endif
RS485_TX_EN=0; //ĬèÏÎa½óêÕÄ£ê½
}
//RS485·¢Ëílen¸ö×Ö½ú.
//buf:·¢ËíÇøê×μØÖ·
//len:·¢ËíμÄ×Ö½úêy(ÎaáËoí±¾′úÂëμĽóêÕÆ¥Åä,Õaàィòé2»òa3¬1y64¸ö×Ö½ú)
void RS485_Send_Data(u8 *buf,u8 len)
{
u8 t;
RS485_TX_EN=1; //éèÖÃÎa·¢ËíÄ£ê½
for(t=0;t<len;t++) //Ñ-»··¢Ëíêy¾Y
{
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)==RESET); //μè′y·¢Ëí½áêø
USART_SendData(USART1,buf[t]); //·¢Ëíêy¾Y
}
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)==RESET); //μè′y·¢Ëí½áêø
RS485_RX_CNT=0;
RS485_TX_EN=0; //éèÖÃÎa½óêÕÄ£ê½
}
//RS4852éÑˉ½óêÕμ½μÄêy¾Y
//buf:½óêÕ»o′æê×μØÖ·
//len:¶áμ½μÄêy¾Y3¤¶è
void RS485_Receive_Data(u8 *buf,u8 *len)
{
u8 rxlen=RS485_RX_CNT;
u8 i=0;
*len=0; //ĬèÏÎa0
delay_ms(10); //μè′y10ms,á¬Dø3¬1y10msûóD½óêÕμ½ò»¸öêy¾Y,ÔòèÏÎa½óêÕ½áêø
if(rxlen==RS485_RX_CNT&&rxlen)//½óêÕμ½áËêy¾Y,Çò½óêÕíê3éáË
{
for(i=0;i<rxlen;i++)
{
buf
=RS485_RX_BUF;
}
*len=RS485_RX_CNT; //¼Ç¼±¾′Îêy¾Y3¤¶è
RS485_RX_CNT=0; //Çåáã
}
}[/mw_shl_code]
一周热门 更多>