新手上路
- 积分
- 33
- 金钱
- 33
- 注册时间
- 2018-8-13
- 在线时间
- 7 小时
|
8金钱
//初始化
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);//ê1ÄüGPIOA/USARTê±Öó
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);//1|Äü¸′óÃIOê±Öóê1Äü
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10; //
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //
GPIO_Init(GPIOA,&GPIO_InitStructure); //
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9; //
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; //
GPIO_Init(GPIOA,&GPIO_InitStructure); //
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; //
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_SetBits(GPIOA, GPIO_Pin_8);//默认发送模式
USART_ClearFlag(USART1,USART_FLAG_TC);//
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)
{
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
USART_ClearITPendingBit(USART1, USART_IT_RXNE);
USART1_Buffer[uart_p]=USART_ReceiveData(USART1);
uart_p++;
}
}
//串口发送数据
void RS485_Send_Data(u8 byte[])
{
u8 i;
GPIO_SetBits(GPIOA, GPIO_Pin_8);
for(i=0;i<8;i++)
{
USART_SendData(USART1,byte[i]);
while( USART_GetFlagStatus(USART1,USART_FLAG_TC)!= SET);
}
GPIO_ResetBits(GPIOA, GPIO_Pin_8);
delay_ms(2000);//等待接收中断
}
我刚开始是用的232接收和发送 通过串口助手测试成功了 但现在换成了485(相同串口,只是多了控制脚),就发现只能发送,中断接收不能实现了,应该是程序没有进入中断,一直无法解决 求大神帮忙
|
|