新手上路
- 积分
- 45
- 金钱
- 45
- 注册时间
- 2019-4-4
- 在线时间
- 11 小时
|

楼主 |
发表于 2019-7-20 11:58:05
|
显示全部楼层
- <div>这是我串口3的配置程序</div><div>void ATT59_init(u32 bound)
- </div>{
- //GPIO端口设置
- GPIO_InitTypeDef GPIO_InitStructure;
- USART_InitTypeDef USART_InitStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
-
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); //使能USART3,GPIOA时钟
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE);
-
- //USART3_RX GPIOB.10
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //PA.9
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
- GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化GPIOA.9
-
- //USART3_TX GPIOB.11初始化
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;//PA10
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入
- GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化GPIOA.10
- //Usart3 NVIC 配置
- NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//抢占优先级3
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //子优先级3
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
- NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器
- //NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置中断优先级分组为组2:2位抢占优先级,2位响应优先级
- //USART 初始化设置
- USART_InitStructure.USART_BaudRate = bound;//串口波特率
- USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
- 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(USART3, &USART_InitStructure); //初始化串口3
- USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);//开启串口接受中断
- USART_Cmd(USART3, ENABLE); //使能串口3
- // USART_ClearITPendingBit(USART3, USART_IT_RXNE);
- printf("ATT59_init-ok\r\n");
- }
- uint8_t flag_send = 0;
- uint8_t Receive_buff[10] = {0,0,0,0};
- uint32_t Att_date;
- static u8 count= 0;
- void USART3_IRQHandler(void) //串口3中断服务程序
- {
- if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET)
- {
-
- Receive_buff[count] = USART_ReceiveData(USART3); //读取接收到的数据
- printf("Receive_buff[%d]:%X\r\n",count,Receive_buff[count]);
- count++;
- if(count >=4){
- // Att_date = ((uint32_t)Receive_buff[0] << 16) |((uint32_t)Receive_buff[1] << 8)|((uint32_t)Receive_buff[2]);
- count = 0;
- flag_send = 1;
- memset(Receive_buff,0,sizeof Receive_buff);
- }
- }
- // USART_ClearFlag(USART3,0);
- }
复制代码
|
|