师兄们帮忙看一下 谢谢了 麻烦了!!
程序 想实现 自发自收的功能 ,但是在调试过程中 能进入接收中断 ,但是不能发送出去,师兄们帮忙看一下是哪儿的问题
#include "sys.h"
#include "rs485.h"
#include "delay.h"
#if EN_UART4_RX //è?1?ê1?üá??óê?
//?óê??o′???
u8 RS485_RX_BUF[64]; //?óê??o3?,×?′ó64??×??ú.
//?óê?μ?μ?êy?Y3¤?è
u8 RS485_RX_CNT=0;
void UART4_IRQHandler(void)
{
u8 res;
if(USART_GetITStatus(UART4, USART_IT_RXNE) != RESET)//?óê?μ?êy?Y
{
res =USART_ReceiveData(UART4);//;?áè??óê?μ?μ?êy?YUART4->DR
if(RS485_RX_CNT<64)
{
RS485_RX_BUF[RS485_RX_CNT]=res; //?????óê?μ?μ??μ
RS485_RX_CNT++; //?óê?êy?Y???ó1
}
}
}
#endif
//3?ê??ˉIO ′??ú4
//bound:2¨ì??ê
void RS485_Init(u32 bound)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC,ENABLE); //ê1?üGPIOCê±?ó
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4,ENABLE);//ê1?üUSART4ê±?ó
//′??ú4òy???′ó?ó3é?
GPIO_PinAFConfig(GPIOC,GPIO_PinSource10,GPIO_AF_UART4); //GPIOC10?′ó??aUART4
GPIO_PinAFConfig(GPIOC,GPIO_PinSource11,GPIO_AF_UART4); //GPIOC11?′ó??aUART4
//UART4
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11; //GPIOC10ó?GPIOC11
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(GPIOC,&GPIO_InitStructure); //3?ê??ˉPA2£?PA3
//PC8í?íìê?3?£?485?£ê?????
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; //GPIOC8
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(GPIOC,&GPIO_InitStructure); //3?ê??ˉPC8
//UART4 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(UART4, &USART_InitStructure); //3?ê??ˉ′??ú4
USART_Cmd(UART4, ENABLE); //ê1?ü′??ú4
USART_ClearFlag(UART4, USART_FLAG_TC);
#if EN_UART4_RX
USART_ITConfig(UART4, USART_IT_RXNE, ENABLE);//?a???óêü?D??
//Uart4 NVIC ????
NVIC_InitStructure.NVIC_IRQChannel = UART4_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??′??÷?¢
#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(UART4,USART_FLAG_TC)==RESET); //μè′y·¢?í?áê?
USART_SendData(UART4,buf[t]); //·¢?íêy?Y
}
while(USART_GetFlagStatus(UART4,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[i]=RS485_RX_BUF[i];
}
*len=RS485_RX_CNT; //????±?′?êy?Y3¤?è
RS485_RX_CNT=0; //??á?
}
}
void RS485_test(void)
{
char rs485buf[64] ={0};
u8 t0,rxlen = 0;
if(RS485_RX_CNT > 0)
{
rxlen = RS485_RX_CNT&0x3fff;
for(t0 = 0;t0<rxlen;t0++)
{
rs485buf[t0] = RS485_RX_BUF[t0];
}
RS485_Send_Data((u8 *)rs485buf,rxlen);
RS485_RX_CNT = 0;
rxlen = 0;
}
else
{
delay_ms(10);
}
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>