我用mosbus poll发送01 03 00 01 00 02 95 CB,也就是读取从机ID1的两个寄存器数值,但是返回只有板子上电的时候返回FF,也就是说没有反应,我用的是串口与TIM2中断管理收发数据,当我把tim2禁止的时候还会返回一次正确的数据。程序是根据其他程序改的,我用的是stm32f103ve,我觉着问题应该出现在中断或是初始配置里面,但是改了5天无果,在这请教各位高手,下面是我的部分程序:
//主函数
int main(void)
{
// RCC_Configuration();
Init_Config();
while (1)
{
if(Uart0_rev_comflag==0x01)
{
Uart0_rev_comflag=0x00;
ParseRecieve();//解包函数,参照网友的程序,应该没问题。
}
}
}
//中断
void TIM2_IRQHandler(void)
{
int j;
if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
{
/* Clear TIM2 Capture compare interrupt pending bit */
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
TIM_ClearITPendingBit(TIM2, TIM_FLAG_Update);
// TIM_SetCounter(TIM2, 0);
TIM_ITConfig(TIM2,TIM_IT_Update,DISABLE);
TIM_Cmd(TIM2,DISABLE); //关掉定时器2
recenum=0;
//sendnum=1;
Uart0_rev_comflag=1;//接收完毕一帧,置位标志位,通知主函数调用接收处理函数
USART_ITConfig(USART1, USART_IT_RXNE, DISABLE);
//USART_Cmd(USART1, DISABLE);//关闭串口
for(j=0;j<9;j++) aRxBuff[j]=0x00; //清除接收缓存数据
}
}
void USART1_IRQHandler(void)
{
//接收中断,SR寄存器的RXNE位为1时,表示收到数据可以读出。
if(USART_GetITStatus(USART1,USART_IT_RXNE)!=RESET)
{
USART_ClearITPendingBit(USART1,USART_IT_RXNE);
if(Uart0_rev_comflag != 1)
{
if(recenum < 9)
{
aRxBuff[recenum]= USART_ReceiveData(USART1) ;
// while (!(USART1->SR & USART_FLAG_RXNE));
recenum++;
}
else if(recenum==9)
{
recenum=0;
}
//TIM_SetCounter(TIM2, 0);
TIM_ClearFlag(TIM2, TIM_FLAG_Update);
/* TIM4 enable counter */
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
TIM_Cmd(TIM2, ENABLE);
}
}
if(USART_GetITStatus(USART1,USART_IT_TXE)!=RESET)
{
USART_ClearITPendingBit(USART1,USART_IT_TXE);
if(sendnum<(nTxBuff+1))
{
USART_SendData(USART1, aTxBuff[sendnum]);
// while (!(USART1->SR & USART_FLAG_TXE));
sendnum++;
}
else if(sendnum==(nTxBuff+1))
{
int i;
sendnum=1;
USART_ITConfig(USART1,USART_IT_TXE,DISABLE); //gai
for(i=0;i<18;i--)
{
aTxBuff[i]=0x00;
}
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE); //gai
}
}
//溢出-如果发生溢出需要先清空SR的溢出位,再读DR寄存器 则可清除不断入中断的问题
if(USART_GetFlagStatus(USART1,USART_FLAG_ORE)!=RESET)
{
USART_ClearFlag(USART1,USART_FLAG_ORE); //清溢出位
USART_ReceiveData(USART1); //读DR
}
}
//初始化配置
void USART_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
USART_ClockInitTypeDef USART_ClockInitStructure;
/*A9 USART_TX*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //推挽输出-TX A2复用功能推挽输出
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* A10 USART1_Rx */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入-RX A3为浮空输入
GPIO_Init(GPIOA, &GPIO_InitStructure);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE) ;
//19200 8BIT 0 1 RX TX
USART_InitStructure.USART_BaudRate = 9600;
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_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_ITConfig(USART1,USART_IT_RXNE,ENABLE); //设定中断方式为接收中断
USART_Cmd(USART1, ENABLE); //使能串口1
}
void Timer_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
/*初始化为默认值*/
TIM_DeInit(TIM2);
// TIM_InternalClockConfig(TIM2);
/* TIM2 clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
/* TIM2做定时器,基础设置*/
TIM_TimeBaseStructure.TIM_Period =400; //计数值:400 定时4ms
TIM_TimeBaseStructure.TIM_Prescaler =720; //预分频,除数:720, 100KHz
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //时钟分频因子为1
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); // Time base configuration
/*预先清除更新中断位*/
TIM_ClearFlag(TIM2, TIM_FLAG_Update);
// TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
//TIM_SetCounter(TIM2, 0);
/* 配置溢出中断*/
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
TIM_Cmd(TIM2,ENABLE);
}
|
|