| 
 
初级会员 
 
	积分77金钱77 注册时间2013-7-31在线时间13 小时 | 
 
 发表于 2019-12-31 11:14:25
|
显示全部楼层 
| 非常感谢楼主解惑,LED可以亮了。不过又碰到一个问题,串口可以打印出来。但是不能接收,看了很久,找不出哪里有问题
  
 /* USART初始化 */
 void USART1_init(uint32_t baud)
 {
 GPIO_InitTypeDef GPIO_InitStructure;
 USART_InitTypeDef USART_InitStructure;
 NVIC_InitTypeDef NVIC_InitStruct;
 
 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);  //使能GPIOA的时钟
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);//使能USART的时钟
 /* USART1的端口配置 */
 GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_1);//配置PA9成第二功能引脚        TX
 GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_1);//配置PA10成第二功能引脚  RX
 
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
 GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
 GPIO_Init(GPIOA, &GPIO_InitStructure);
 
 /* USART1的基本配置 */
 USART_InitStructure.USART_BaudRate = baud;              //波特率
 USART_InitStructure.USART_WordLength = USART_WordLength_8b;
 USART_InitStructure.USART_StopBits = USART_StopBits_1;
 USART_InitStructure.USART_Parity = USART_Parity_No;
 USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
 USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
 
 USART_Init(USART1, &USART_InitStructure);
 USART_Cmd(USART1, ENABLE);                             //使能USART1
 USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);           //使能接收中断
 
 /* USART1的NVIC中断配置 */
 NVIC_InitStruct.NVIC_IRQChannel = USART1_IRQn;
 NVIC_InitStruct.NVIC_IRQChannelPriority = 0x02;
 NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
 NVIC_Init(&NVIC_InitStruct);
 }
 void USART2_init(uint32_t bound){
 GPIO_InitTypeDef GPIO_InitStructure;
 USART_InitTypeDef USART_InitStructure;
 NVIC_InitTypeDef NVIC_InitStruct;
 
 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);  //使能GPIOA的时钟
 RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);//使能USART的时钟
 
 GPIO_PinAFConfig(GPIOA,GPIO_PinSource2,GPIO_AF_1);//重映射
 GPIO_PinAFConfig(GPIOA,GPIO_PinSource3,GPIO_AF_1);
 
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;//USART1_TX | USART1_RX
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
 GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
 GPIO_Init(GPIOA, &GPIO_InitStructure);
 
 USART_InitStructure.USART_BaudRate = bound;//波特率
 USART_InitStructure.USART_WordLength = USART_WordLength_8b;//8位数据
 USART_InitStructure.USART_StopBits = USART_StopBits_1;//1位停止位
 USART_InitStructure.USART_Parity = USART_Parity_No;//无奇偶校验
 USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流
 USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;//收发模式
 USART_Init(USART2,&USART_InitStructure);
 
 USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);//开启接收中断
 USART_Cmd(USART2,ENABLE);//使能串口1
 
 /* USART2的NVIC中断配置 */
 NVIC_InitStruct.NVIC_IRQChannel = USART2_IRQn;
 NVIC_InitStruct.NVIC_IRQChannelPriority = 0x01;
 NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
 NVIC_Init(&NVIC_InitStruct);
 }
 
 
 void USART1_IRQHandler(void)                        //串口1中断服务程序
 {
 u8 Res;
 //        delay_us(5);
 if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)  //接收中断(接收到的数据必须是0x0d 0x0a结尾)
 {
 Res =USART_ReceiveData(USART1);        //读取接收到的数据
 //                printf("RES = %d",Res);
 if((USART_RX_STA&0x8000)==0)//接收未完成
 {
 if(USART_RX_STA&0x4000)//接收到了0x0d
 {
 if(Res!=0x0a)USART_RX_STA=0;//接收错误,重新开始
 else USART_RX_STA|=0x8000;        //接收完成了
 }
 else //还没收到0X0D
 {
 if(Res==0x0d)USART_RX_STA|=0x4000;
 else
 {
 USART_RX_BUF[USART_RX_STA&0X3FFF]=Res ;
 USART_RX_STA++;
 if(USART_RX_STA>(USART_REC_LEN-1))USART_RX_STA=0;//接收数据错误,重新开始接收
 }
 }
 }
 }
 }
 
 int main(void)
 {
 u32 adcx;
 float temp;
 u16 i;
 u32 len;
 delay_init();
 USART1_init(9600);
 USART2_init(9600);
 LED_Init();
 while(1)
 {
 //            printf("STM32F030 TEST");
 LED_ON_OFF();
 delay_ms(1000);
 if(USART_RX_STA&0x8000)//串口1收到数据,向串口2发送数据
 {
 len=USART_RX_STA&0x3fff;//得到此次接收到的数据长度
 for(i=0;i<len;i++)
 {
 USART_SendData(USART2, USART_RX_BUF);//向串口2发送数据
 while(USART_GetFlagStatus(USART2,USART_FLAG_TC)!=SET);//等待串口2发送结束
 }
 USART_RX_STA=0;
 }
 }
 }
 
 | 
 |