STN8 串口发送数据出错

2019-07-19 21:45发布



数据发送错误求帮助~!
本身,我是想要通过调试,发送123456789,但是上面显示乱码,不知道具体是什么原因引起的,另外,上图中读数据在串口打开之后就一直在跳~R:1518870---不知道什么原因
我的STM8是用STVD编写的
程序如下:
//发送
void UART_Send(INT8U *Buffer, INT16U Length)
{
INT16U i = 0;
for(;i<Length;i++)
  
UART1_SendChar(Buffer);
}

//接收
INT8U UART_Recive(void)
{
INT8U UART1_Re_Buf;
while ((UART1_SR & 0x20) == 0x00);
UART1_Re_Buf=UART1_DR;
return UART1_Re_Buf;

}



   //串口发送字符
   void UART1_SendChar(unsigned char ch)
{
  while((UART1_SR & 0x80) == 0x00);  // 若发送寄存器不空,则等待

     UART1_DR = ch;         // 将要发送的字符送到数据寄存器
}
//中断
@far @interrupt void UART1_Recv_IRQHandler (void)
{
  unsigned char ch;
if(UART1_SR&(1<<5))//接收到的字符 
{
     ch = UART1_DR;          // 读入接收到的字符 

  }
}


//extern @far @interrupt void TIM5_UPD_OVF_IRQHandler (void);
//extern @far @interrupt void TIM6_UPD_OVF_IRQHandler (void);

struct interrupt_vector const _vectab[] = {
    {0x82, (interrupt_handler_t)_stext}, /* reset */
    {0x82, NonHandledInterrupt}, /* trap  */
    {0x82, NonHandledInterrupt},       /* irq0  */
    {0x82, NonHandledInterrupt}, /* irq1  */
    {0x82, NonHandledInterrupt}, /* irq2  */
    {0x82, NonHandledInterrupt}, /* irq3  */
    {0x82, NonHandledInterrupt}, /* irq4  */
    {0x82, NonHandledInterrupt}, /* irq5  */
    {0x82, NonHandledInterrupt}, /* irq6  */
    {0x82, NonHandledInterrupt}, /* irq7  */
    {0x82, NonHandledInterrupt}, /* irq8  */
    {0x82, NonHandledInterrupt}, /* irq9  */
    {0x82, NonHandledInterrupt}, /* irq10 */
    {0x82, NonHandledInterrupt}, /* irq11 */
    {0x82, NonHandledInterrupt}, /* irq12 */
    {0x82, NonHandledInterrupt}, /* irq13 */
    {0x82, NonHandledInterrupt}, /* irq14 */
    {0x82, NonHandledInterrupt}, /* irq15 */
    {0x82, NonHandledInterrupt}, /* irq16 */
    {0x82, NonHandledInterrupt}, /* irq17 */
    {0x82, UART1_Recv_IRQHandler}, /* irq18 */
    {0x82, NonHandledInterrupt}, /* irq19 */
    {0x82, NonHandledInterrupt}, /* irq20 */
    {0x82, NonHandledInterrupt}, /* irq21 */
    {0x82, NonHandledInterrupt}, /* irq22 */
 {0x82, NonHandledInterrupt},/* irq23 */
    {0x82, NonHandledInterrupt}, /* irq24 */
    {0x82, NonHandledInterrupt}, /* irq25 */
    {0x82, NonHandledInterrupt}, /* irq26 */
    {0x82, NonHandledInterrupt}, /* irq27 */
    {0x82, NonHandledInterrupt}, /* irq28 */
    {0x82, NonHandledInterrupt}, /* irq29 */
};


//主程序
while (1)
 { 
   B5_OUT = 1;                      //4.2V电压截止
PA1_OUT = 1;                      //3.3V电压使能
PD3_OUT = 0;                     // LED1
PD2_OUT = 1;                      //LED2
DelayMS(100);                     //
PD2_OUT = 0;                      //LED2
PD3_OUT = 1;                     //LED1
DelayMS(100);
P_485 = 1;
for(i = 0;i<64;i++)
  {
 UART1_DR = Buffer[64];
while((UART1_SR&0x40)==0);
  }
printf(" ");//
}





友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
3条回答
正点原子
1楼-- · 2019-07-20 00:58
 精彩回答 2  元偷偷看……
ddong610
2楼-- · 2019-07-20 01:45
回复【2楼】正点原子:
---------------------------------
    //启动时钟配置
void SysClkInit(void)
{
       CLK_SWR=0xe1; //HSI为主时钟源
// CLK_SWR=0xb4; //HSE为主时钟源
       CLK_CKDIVR=0x00;//CPU时钟0分频,系统时钟0分频
       CLK_CSSR=0x01;//时钟安全监测使能
    // CLK_SWCR=0x02;//使能自动时钟切换
}

 //串口配置
 void UART1_Init(void)
{   
  
  UART1_CR2 = 0;           // 禁止UART发送和接收
  UART1_CR1 = 0;           // b5 = 0,允许UART,禁止奇偶校验
                         //一个起始位,8个数据位
UART1_CR3 = 0;           // 1个停止位
                            

  UART1_BRR2 = 0x03;
  UART1_BRR1 = 0x68;     // 实际的波特率分频系数为0683(1667)
// 对应的波特率为16000000/1667=9600
                                    
  UART1_CR2 = 0x02C;               // 允许发送,接收
                                //允许接收中断
}


我是这么设置的,感觉没问题~
正点原子
3楼-- · 2019-07-20 04:54
回复【3楼】ddong610:
---------------------------------
试试就知道有没有问题了。

一周热门 更多>