调试串口过程,发现一个问题。串口能正常接收数据,但不能发送数据。串口的初始化是按照原子的例程来的。
最开始调试过程中,一切都很正常,添加了一些数据处理的代码后,串口不能发送数据。
代码出下:
void uart_init(u32 bound)
{
//GPIO???úéè??
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE);
//USART_DeInit(USART1);
//USART1_TX PA.9
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //?′ó?í?íìê?3?
GPIO_Init(GPIOA, &GPIO_InitStructure); //3?ê??ˉPA9
//USART1_RX PA.10
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//????ê?è?
GPIO_Init(GPIOA, &GPIO_InitStructure); //3?ê??ˉPA10
//Usart1 NVIC ????
NVIC_InitStructure.NVIC_IRQChannel = USART1_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;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
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(USART1, &USART_InitStructure); //3?ê??ˉ′??ú
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//?a???D??
USART_Cmd(USART1, ENABLE); //ê1?ü′??ú
}
#if EN_USART1_RX //è?1?ê1?üá??óê?
void USART1_IRQHandler(void) //′??ú1?D??·t??3ìDò
{
u8 t;
u8 Res;
#ifdef OS_TICKS_PER_SEC //è?1?ê±?ó?ú??êy?¨ò?á?,?μ?÷òaê1ó?ucosIIá?.
OSIntEnter();
#endif
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) //?óê??D??(?óê?μ?μ?êy?Y±?D?ê?0x0d 0x0a?á?2)
{
Res =USART_ReceiveData(USART1);//(USART1->DR); //?áè??óê?μ?μ?êy?Y
if((USART_RX_STA&0x8000)==0) //?óê??′íê3é
{
if(state_machine == 0)
{
if(Res == 0x5A) //??í· μúò??? 5A
state_machine = 1;
else
state_machine = 0;
}
else if(state_machine == 1)//??í· μú?t?? A5
{
if(Res == 0xA5)
state_machine = 2;
else
state_machine = 0;
}
else if(state_machine == 2) //êy?Y3¤?è
{
len_count = 0; //êy?Y?óê???êy?÷
date_len = Res; //êy?Y3¤?è
state_machine = 3;
}
else if(state_machine == 3) //ê?·??aμ????á??á?
{
if(0x80<= Res <= 0x84) //?yè·μ?μ????á??á?
{
USART_RX_BUF[len_count++]=Res;
state_machine = 4;
}
else
state_machine = 0;
}
else if(state_machine == 4)
{
USART_RX_BUF[len_count++] = Res;
if(len_count-1 == date_len-1)
{
USART_RX_STA|=0x8000; //?óê?íê3éá?
state_machine = 0;
}
}
}
}
#ifdef OS_TICKS_PER_SEC //è?1?ê±?ó?ú??êy?¨ò?á?,?μ?÷òaê1ó?ucosIIá?.
OSIntExit();
#endif
}
#endif
这个是串口程序,初始化是完全按照例程进行。
void dw4827_write_variable_register(u16 addr,u16 data)
{
u16 send_data[10];
int i=0;
u16 temp;
send_data[0]=0x5A;
send_data[1]=0xA5;
send_data[2]=0x05;
send_data[3]=0x82;
temp = addr;
send_data[4]=temp>>8;
send_data[5]=addr&0x00ff;
temp = data;
send_data[6]=temp>>8;
send_data[7]=data&0x00ff;
USART_ClearFlag(USART1,USART_FLAG_TC);
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE) == RESET)
{
}
for (i=0;i<8;i++)
{
//USART1->DR=send_data;
USART_SendData(USART1,send_data);
while((USART1->SR&0X40)==0);
}
}
这个是串口数据发送。
void correct_play()
{
float coefficient;
u16 temp,temp1;
u16 shiche;
u16 lingdian;
u16 flow;
float xishu;
shiche = 1000;
temp = AT24CXX_ReadOneByte(104); // 1000 0
temp = temp<<8;
temp1 = AT24CXX_ReadOneByte(105);
lingdian = temp + temp1;
temp = AT24CXX_ReadOneByte(106)<<8;
xishu = temp + AT24CXX_ReadOneByte(107);
shiche = Get_Adc_Average(ADC_Channel_14,10);
flow = xishu*(shiche - lingdian);
flow = getavg(Sum_Buff1,10,flow,num1);
num1 ++;
if(num1 == 10)
num1 = 0;
dw4827_write_variable_register(0x0004,xishu);
dw4827_write_variable_register(0x0005,flow);
dw4827_write_variable_register(0x0006,shiche);
}
这数据一些简单处理。
标红部分,如果注释掉,串口就不能正常发送数据,接收正常。
如果不注释,串口一切正常。
这个是因为什么造成的,检查了两天,找不到原因。
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>