[mw_shl_code=c,true] GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd ( RCC_APB2Periph_GPIOA, ENABLE );//使能GPIOA时钟
RCC_APB1PeriphClockCmd ( RCC_APB1Periph_USART2,ENABLE );//使能USART2时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; //PA1端口配置
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //PA2
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_3;//PA3
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空输入
GPIO_Init(GPIOA, &GPIO_InitStructure);
RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2,ENABLE);//复位串口2
RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2,DISABLE);//停止复位
USART_InitStructure.USART_BaudRate = bound;
USART_InitStructure.USART_WordLength = USART_WordLength_9b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;//一个停止位
USART_InitStructure.USART_Parity = USART_Parity_Even;///奇偶校验位
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;//收发模式
USART_Init(USART2, &USART_InitStructure); //初始化串口
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; //使能串口2中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; //先占优先级2级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //从优先级3级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能外部中断通道
NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器
USART_ITConfig( USART2, USART_IT_RXNE, ENABLE );//开启中断
[mw_shl_code=c,true] /******************************************************************************************************************/[/mw_shl_code]
[mw_shl_code=c,true] float temp;[/mw_shl_code]
[mw_shl_code=c,true] u16 mantissa;
u16 fraction;
NVIC_InitTypeDef NVIC_InitStructure;
temp = ( (float)(36*1000000) ) / (bound*16);//得到USARTDIV
mantissa = temp; //得到整数部分
fraction = (temp-mantissa)*16; //得到小数部分
mantissa <<= 4;
mantissa += fraction;
RCC->APB2ENR |= 1<<2; //使能PORTA口时钟
GPIOA->CRL &= 0XFFFF000F; //IO状态设置
GPIOA->CRL |= 0X00008B30; //IO状态设置
RCC->APB1ENR |= 1<<17; //使能串口时钟
RCC->APB1RSTR |= 1<<17; //复位串口2
RCC->APB1RSTR &= ~(1<<17);//停止复位
USART2->BRR = mantissa; // 波特率设置
USART2->CR1 |= 0X340C; //1位停止
USART2->CR1 |= 1<<8; //校验错误中断使能
USART2->CR1 |= 1<<5; //接收缓冲区非空中断使能
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; //使能串口2中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; //先占优先级2级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //从优先级2级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能外部中断通道
NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器
USART_ITConfig( USART2, USART_IT_RXNE, ENABLE );//开启中断[/mw_shl_code]
USART_Cmd(USART2, ENABLE); //使能串口 [/mw_shl_code]
485发送:PA1控制方向,用寄存器设置时没发现问题,用库函数时板子偶尔卡住,好像接收不到,等一下(一两秒)再发又正常了。 |