主函数就这四句 Uart4_SendByte(0x01); Uart4_SendByte(0x02);Uart4_SendByte(0X03);Uart4_SendByte(0X04); 仿真电脑端收不到数据,停止仿真。让板子自己跑,电脑收到数据。 数据为下面的图片 另外波特率 停止位 数据位配置的一样。配置的程序如下:
#include "delay.h"
#include "uart4.h"
#include "stdarg.h"
#include "stdio.h"
#include "string.h"
#include "timer6.h"
__align(8) u8 UART4_TX_BUF[UART4_MAX_SEND_LEN]; //????,??USART2_MAX_SEND_LEN??
#ifdef UART4_RX_EN //???????
//???????
u8 UART4_RX_BUF[UART4_MAX_RECV_LEN]; //????,??USART2_MAX_RECV_LEN???.
vu16 UART4_RX_STA=0;
void UART4_IRQHandler(void)
{
u8 res;
if(USART_GetITStatus(UART4, USART_IT_RXNE) != RESET)//?????
{
res =USART_ReceiveData(UART4);
if((UART4_RX_STA&(1<<15))==0)//????????,??????,?????????
{
if(UART4_RX_STA<UART4_MAX_RECV_LEN) //???????
{
TIM_SetCounter(TIM6,0);//?????
if(UART4_RX_STA==0)
TIM_Cmd(TIM6, ENABLE); //?????6
UART4_RX_BUF[UART4_RX_STA++]=res; //???????
}
else
{
UART4_RX_STA|=1<<15; //????????
}
}
}
}
#endif
//???IO ??2
//bound:???
void uart4_init(u32 bound)
{
NVIC_InitTypeDef NVIC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE); //??GPIOA??
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4,ENABLE); //??USART4??
// USART_DeInit(UART4); //????3
/*************************?????? ***************************/
// GPIO_PinAFConfig(GPIOA,GPIO_PinSource0,GPIO_AF_USART2); //GPIOB11???USART3
// GPIO_PinAFConfig(GPIOA,GPIO_PinSource1,GPIO_AF_USART2); //GPIOB10???USART3
/*************************????????************************/
GPIO_PinAFConfig(GPIOA,GPIO_PinSource0,GPIO_AF_UART4); //GPIOA0???UART4
GPIO_PinAFConfig(GPIOA,GPIO_PinSource1,GPIO_AF_UART4); //GPIOA1???UART4
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1; //GPIOB11?GPIOB10???
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//????
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //??50MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //??????
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //??
GPIO_Init(GPIOA,&GPIO_InitStructure); //???GPIOB11,?GPIOB10
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_No;//??????
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//????????
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //????
USART_Init(UART4, &USART_InitStructure); //?????4
USART_Cmd(UART4, ENABLE); //????
USART_ITConfig(UART4, USART_IT_RXNE, ENABLE);//????
NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2 ;//?????0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //????0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ????
NVIC_Init(&NVIC_InitStructure); //??????????VIC???
//TIM6_Int_Init(100-1,8400-1);
TIM6_Int_Init(100-1,8400-1); //100ms??
UART4_RX_STA=0; //??
TIM_Cmd(TIM6, DISABLE); //?????6
}
//??2,printf ??
//???????????USART3_MAX_SEND_LEN??
void u4_printf(char* fmt,...)
{
u16 i,j;
va_list ap;
va_start(ap,fmt);
vsprintf((char*)UART4_TX_BUF,fmt,ap);
va_end(ap);
i=strlen((const char*)UART4_TX_BUF);//?????????
for(j=0;j<i;j++)//??????
{
while(USART_GetFlagStatus(UART4,USART_FLAG_TC)==RESET); //????,??????
USART_SendData(UART4,UART4_TX_BUF[j]);
}
}
//??4??????
void Uart4_SendByte(u8 temp)
{
USART_SendData(UART4,temp);
while(USART_GetFlagStatus(UART4, USART_FLAG_TXE)==RESET);//??????
}
//???fputc??
/*?????UART4,???USART4*/
//int fputc(int ch, FILE *f)
//{
// while((UART4->SR&0X40)==0); //????,??????
// UART4->DR = (u8) ch;
// return ch;
//}
void Uart4_SendNumByte(u8 *p,u8 num)
{
u8 t;
for(t=0;t<num;t++)
{
USART_SendData(UART4,*p);
while(USART_GetFlagStatus(UART4, USART_FLAG_TXE)==RESET);//μè′y·¢Ëííê3é
p++;
}
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
不对可能波特率配置有问题
一周热门 更多>