需要使用串口三通信,但是程序没有报错。就是实现不了功能。发送不了数据。菜鸟求解代码使用的是串口三,写代码时失误了写成了uart2.c
#include "led.h"
#include "delay.h"
#include "key.h"
#include "sys.h"
#include "usart.h"
#include "uart2.h"
int main(void)
{
u8 key;
u16 t;
u16 len;
// u16 times=0;
// u16 receive_buf[70];
int a=1;
delay_init(); //Ñóê±oˉêy3õê¼»ˉ
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //éèÖÃNVICÖD¶Ï·Ö×é2:2λÇàÕ¼óÅÏ輶£¬2λÏìó|óÅÏ輶
uart_init(9600); //′®¿ú3õê¼»ˉÎa115200
uart3_init(9600);
LED_Init(); //LED¶Ë¿ú3õê¼»ˉ
KEY_Init(); //3õê¼»ˉóë°′¼üᬽóμÄó2¼t½ó¿ú
while(1)
{
LED1=!LED1;
delay_ms(1000);
USART_SendData(USART3, USART3_RX_BUF[0]);//Ïò′®¿ú1·¢Ëíêy¾Y
USART_SendData(USART3, a);//Ïò′®¿ú3·¢Ëíêy¾Y
while(USART_GetFlagStatus(USART3,USART_FLAG_TC)!=SET);//μè′y·¢Ëí½áêø
}
}
#include "sys.h"
#include "uart2.h"
#include "delay.h"
#include "led.h"
#include "usart.h"
#if SYSTEM_SUPPORT_OS
#include "includes.h" //ucos ê1óÃ
#endif
#if EN_USART3_RX //èç1ûê1Äüá˽óêÕ
//′®¿ú1ÖD¶Ï·tÎñ3ìDò
//×¢òa,¶áè¡USARTx->SRÄü±üÃaÄaÃûÆäÃîμÄ′íÎó
u8 USART3_RX_BUF[USART_REC_LEN]; //½óêÕ»o3å,×î′óUSART_REC_LEN¸ö×Ö½ú.
//½óêÕ×′ì¬
//bit15£¬ ½óêÕíê3é±êÖ¾
//bit14£¬ ½óêÕμ½0x0d
//bit13~0£¬ ½óêÕμ½μÄóDD§×Ö½úêyÄ¿
u16 USART3_RX_STA=0; //½óêÕ×′쬱ê¼Ç
void uart3_init(u32 bound)
{
//GPIO¶Ë¿úéèÖÃ
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);//ê1ÄüGPIOA,Dê±Öó
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE);//ê1ÄüUSART2ê±Öó
USART_DeInit(USART3); //¸′λ′®¿ú3
//USART1_TX GPIOA2
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //PA.2
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¸′óÃíÆíìêä3ö
GPIO_Init(GPIOB, &GPIO_InitStructure);//3õê¼»ˉGPIOA.9
//USART1_RX GPIOA33õê¼»ˉ
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;//PA3
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//¸¡¿Õêäèë
// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //éÏà-êäèë
GPIO_Init(GPIOB, &GPIO_InitStructure);//3õê¼»ˉGPIOA.10
//Usart1 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í¨μàê1Äü
NVIC_Init(&NVIC_InitStructure); //¸ù¾YÖ¸¶¨μÄ2Îêy3õê¼»ˉVIC¼Ä′æÆ÷
//USART 3õê¼»ˉéèÖÃ
USART_InitStructure.USART_BaudRate = bound;//′®¿ú2¨ìØÂê
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//×Ö3¤Îa8λêy¾Y¸ñê½
USART_InitStructure.USART_StopBits = USART_StopBits_1;//ò»¸öí£Ö1λ
USART_InitStructure.USART_Parity = USART_Parity_No;//ÎTÆæżD£Ñéλ
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//ÎTó2¼têy¾Yá÷¿ØÖÆ
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //êÕ·¢Ä£ê½
USART_Init(USART3, &USART_InitStructure); //3õê¼»ˉ′®¿ú1
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);//¿aÆô′®¿ú½óêüÖD¶Ï
USART_Cmd(USART3, ENABLE); //ê1Äü′®¿ú1
USART_ClearFlag(USART3, USART_FLAG_TC);
}
void USART3_IRQHandler(void) //′®¿ú1ÖD¶Ï·tÎñ3ìDò
{
u8 Res;
if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET) //½óêÕÖD¶Ï(½óêÕμ½μÄêy¾Y±ØDëêÇ0x0d 0x0a½áÎ2)
{
Res =USART_ReceiveData(USART3); //¶á衽óêÕμ½μÄêy¾Y
switch(Res)
{
case 'A':
// delay_ms(100);
// printf("
Äú·¢ËíμÄÏûÏ¢Îa:
");
LED0=!LED0;
delay_ms(100);
}
USART3_RX_BUF[USART3_RX_STA]=Res ;
USART3_RX_STA++;
if(USART3_RX_STA>100)USART3_RX_STA=0;//½óêÕêy¾Y′íÎó,ÖØD¿aê¼½óêÕ
// if((USART2_RX_STA&0x8000)==0)//½óêÕÎ′íê3é
// {
// if(USART2_RX_STA&0x4000)//½óêÕμ½áË0x0d
// {
// if(Res!=0x0a)USART2_RX_STA=0;//½óêÕ′íÎó,ÖØD¿aê¼
// else USART2_RX_STA|=0x8000; //½óêÕíê3éáË
// }
// else //»1ûêÕμ½0X0D
// {
// if(Res==0x0d)USART2_RX_STA|=0x4000;
// else
// {
// USART2_RX_BUF[USART2_RX_STA&0X3FFF]=Res ;
// USART2_RX_STA++;
// if(USART2_RX_STA>(USART_REC_LEN-1))USART2_RX_STA=0;//½óêÕêy¾Y′íÎó,ÖØD¿aê¼½óêÕ
// }
// }
// }
}
#if SYSTEM_SUPPORT_OS //èç1ûSYSTEM_SUPPORT_OSÎaÕ棬ÔòDèòaÖ§3ÖOS.
OSIntExit();
#endif
}
#endif
#ifndef __USART_N
#define __USART_N
#include "stdio.h"
#include "sys.h"
#define USART_REC_LEN 200 //¶¨òå×î′ó½óêÕ×Ö½úêy 200
#define EN_USART3_RX 1 //ê1Äü£¨1£©/½ûÖ1£¨0£©′®¿ú1½óêÕ
extern u8 USART3_RX_BUF[USART_REC_LEN]; //½óêÕ»o3å,×î′óUSART_REC_LEN¸ö×Ö½ú.Ä©×Ö½úÎa»»DD·û
extern u16 USART3_RX_STA; //½óêÕ×′쬱ê¼Ç
//èç1ûÏë′®¿úÖD¶Ï½óêÕ£¬Çë2»òa×¢êíòÔÏÂo궨òå
void uart3_init(u32 bound);
void uart_init(u32 bound);
#endif
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //PA.2
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¸′óÃíÆíìêä3ö
GPIO_Init(GPIOB, &GPIO_InitStructure);//3õê¼»ˉGPIOA.9
//USART1_RX GPIOA33õê¼»ˉ
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;//PA3
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//¸¡¿Õêäèë
// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //éÏà-êäèë
GPIO_Init(GPIOB, &GPIO_InitStructure);//3õê¼»ˉGPIOA.10
我使用的是串口3。配置的是串口三的pb10和pb11。战舰V3的板子
一周热门 更多>