模拟了个串口,发送数据有时不对

2019-10-16 04:45发布

void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;   
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//使能
//  TIM3 中断嵌套设计
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 0;//此参数可以是一个值在3和0之间。
NVIC_Init(&NVIC_InitStructure);
//  TIM2 中断嵌套设计
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 1;
NVIC_Init(&NVIC_InitStructure);
//USART1 中断向量设置
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;//设置中断
NVIC_InitStructure.NVIC_IRQChannelPriority = 2;//设置优先级   
NVIC_Init(&NVIC_InitStructure);
//EXTI0中断向量配置   
NVIC_InitStructure.NVIC_IRQChannel = EXTI4_15_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 3;
NVIC_Init(&NVIC_InitStructure);
}
void TIM3_Configuration(void)
{
TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
//T=(TIM_Period+1)*(TIM_Prescaler+1)/TIMxCLK
TIM_DeInit(TIM3);  //复位TIM2定时器
TIM_TimeBaseStructure.TIM_Period = 1249;           // 自动重装载寄存器周期的值(计数值)
TIM_TimeBaseStructure.TIM_Prescaler = 15; //时钟预分频数
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//向上计数模式
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
TIM_ClearFlag(TIM3, TIM_FLAG_Update);           // 清除溢出中断标志
TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);
//TIM_Cmd(TIM3, ENABLE);
}
void TIM3_IRQHandler(void)//虚拟串口
{
static uint64_t Voltagedata_Data;
static uint32_t check_data;
static uint8_t i;
static uint8_t battery_Numbering;
if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)
{
  TIM3->SR = (uint16_t)~TIM_FLAG_Update;// 清除溢出中断标志  
  
  if (uart8_tx_flag)//发送,
  {
   if (uart8_tx_bit == 0)//是起始位
   {
    uart8_TXD_PORT_WriteLow;//起始位
   }
   else
   {
    if (uart8_tx_bit < 9)
    {
     if ((uart8_tx_byte & 0x1)==0x1)//数据位为1
     {
      uart8_TXD_PORT_WriteHigh;
     }
     else
     {
      uart8_TXD_PORT_WriteLow;
     }
     uart8_tx_byte = uart8_tx_byte >> 1;
    }
    else
    {
     if (uart8_tx_bit == 9)
     {
      uart8_TXD_PORT_WriteHigh;//停止位
     }
     if (uart8_tx_bit == 10)
     {
      uart8_tx_flag = 0;
      uart8_tx_bit = 0;
      TIM3->CR1 &= (uint16_t)(~((uint16_t)TIM_CR1_CEN)); //停止定时器
     }
    }
   }
   // printf("uart8_tx_bit %d ", uart8_tx_bit);
   uart8_tx_bit++;
  }
}
}
void uart8_send_byte(uint8_t byte)
{
uart8_tx_flag = 1;
uart8_tx_bit = 0;
uart8_tx_byte = byte;
TIM3->CNT = 0; //计数器置零,
TIM3->CR1 |= TIM_CR1_CEN;//启动定时器
while (uart8_tx_flag == 1)  //检查是否发送完毕
{;
};
}

模拟了个串口,发送数据有时不对,表现为该为为高电平时有时会马上又变低,导致数据出错。
求解。
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。