各位大神们:我现在想使用STM32F103的RS232串口触摸屏的RS232串口进行通信,通信协议是modbus, 数据格式是9600bps, 8位数据,1位停止位,现在STM32和触摸屏不能通信,请大神们帮我看看是我的程序哪里出问题了,该怎么办????
我写的定时器程序:
void TIM2_Configuration(void) //定時2ms
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); //配置RCC,使能TIM2
TIM_TimeBaseStructure.TIM_Prescaler = 7199; //時鐘預分頻數
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上計數
TIM_TimeBaseStructure.TIM_Period = 14; //自動重裝載寄存器周期的值
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //時間分割值
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); //初始化定時器2
TIM_ClearFlag(TIM2, TIM_FLAG_Update); //清除標志
}
/*******************************************/
/*******************************************/
/*******************************************/
void Timer2_enable(void)
{
TIM_ClearFlag(TIM2, TIM_FLAG_Update); //清除標志
TIM_SetCounter(TIM2, 0x00); //設置寄存器值為0
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE); //使能中斷
TIM_Cmd(TIM2, ENABLE); //開啟定時器
}
/*******************************************/
/*******************************************/
/*******************************************/
void Timer2_disable(void)
{
// TIM_ITConfig(TIM2, TIM_IT_Update |TIM_IT_Trigger, DISABLE);
TIM_ITConfig(TIM2, TIM_IT_Update, DISABLE);
TIM_Cmd(TIM2, DISABLE);
}
/*******************************************/
/*******************************************/
/*******************************************/
void TIM2_ITHandle(void)
{
if(TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET) //檢測是否發生溢出更新事件
{
TIM_ClearITPendingBit(TIM2, TIM_IT_Update); //清除更新標志位
// checkComm0Modbus();
if(receTimeOut<2)
{
receTimeOut++;
}
else
{
Timer2_disable();
receCount = 0;
bitflag = 1;
}
}
}
我写的串口中断程序:
void USART_Configuration(u32 baudrate)
{
USART_InitTypeDef USART_InitStructure;
USART_ClockInitTypeDef USART_ClockInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_ClockInitStructure.USART_Clock = USART_Clock_Disable; //時鐘低電平活動
USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low; //時鐘低脈沖
USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge; //時鐘第二個邊沿進行數據撲獲
USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable; //最后一位數據的時鐘脈沖不縱SCLK輸出
USART_ClockInit(USART1, &USART_ClockInitStructure);
USART_InitStructure.USART_BaudRate = baudrate;
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_ITConfig(USART1, USART_IT_RXNE, ENABLE); //接收中斷
USART_ITConfig(USART1, USART_IT_TC, ENABLE); //傳送完成中斷
USART_Cmd(USART1, ENABLE);
}
/*******************************************/
/*******************************************/
/*******************************************/
void USART_ITHandle(void)
{
if(USART_GetITStatus(USART1, USART_IT_RXNE)== SET) //接收中斷
{
USART_ClearITPendingBit(USART1, USART_IT_RXNE); //清除接收中斷位
if(receTimeOut>1)
{
receCount = 0;
Timer2_enable();
receTimeOut = 0;
}
receBuf[receCount] = USART_ReceiveData(USART1);
receCount++;
Timer2_enable();
receCount &= 0x0f;
}
else if(USART_GetITStatus(USART1, USART_IT_TC) == SET) //檢驗發送完成中斷
{
USART_ClearFlag(USART1, USART_FLAG_TC); //清除發送完成標志位
if(sendPosi < sendCount)
{
sendPosi++;
USART_SendData(USART1, sendBuf[sendPosi]);
}
else
{
receCount = 0;
}
}
}
|