//初始化配置
void RS485_Init(void)
{
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_USART1,ENABLE);//ʹÄÜGPIOA/USARTʱÖÓ
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);//¹¦Äܸ´ÓÃIOʱÖÓʹÄÜ
USART_Cmd(USART1, ENABLE);//ʹÄÜUSART
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10; //RX½ÓÊÕÒý½Å
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //¸¡¿ÕÊäÈë
GPIO_Init(GPIOA,&GPIO_InitStructure); //ÅäÖÃ IO
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9; //TX·¢ËÍÒý½Å
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¸´ÓÃÍÆÍìÊä³ö
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; //Êä³öËÙ¶È
GPIO_Init(GPIOA,&GPIO_InitStructure); //ÅäÖÃ IO
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; //485¿ØÖÆÒý½Å
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //ÍÆÍìÊä³ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_ResetBits(GPIOA, GPIO_Pin_8);//ĬÈϽÓÊÕģʽ
USART_ClearFlag(USART1,USART_FLAG_TC);//Çå³ý´®¿Ú´¦Àí±ê־λ
USART_ClearITPendingBit(USART1, USART_IT_RXNE);//Çå³ý´®¿ÚÖжϱê־λ
USART_StructInit(&USART_InitStructure);
USART_Init(USART1,&USART_InitStructure);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //ÖжÏʹÄÜ
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority =1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
//中断接收
void USART1_IRQHandler(void)
{
u8 RX_dat;
if(USART_GetITStatus(USART1,USART_IT_RXNE)==SET)//USART_IT_RXNE:????
{
RX_dat=USART_ReceiveData(USART1);
GPIO_SetBits(GPIOA,GPIO_Pin_8);
delay_ms(1);
USART_SendData(USART1,RX_dat);
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE) == RESET){} //???????????
delay_ms(2);
GPIO_ResetBits(GPIOA,GPIO_Pin_8);
}
如题 我之前用串口助手测试232通信 这个测试程序通过了 但换成485通信时(相同串口,只是多了一个控制引脚),程序就实现不了了 |