zyk12345 发表于 2024-11-28 20:34:01

你们的32G会因串口而复位么【已解决】

本帖最后由 zyk12345 于 2024-11-30 16:48 编辑

在测试串口数据是发现MCU自动复位

换了根数据线好了!!!!!!!!!!!!!!!!!!

接受缓冲区是1024,但这才120+就会复位
低于120个字符,则正常。

接受缓冲区是1024,但这才120+就会复位
低于120个字符,则正常。

定时器#include "Timer.h"
#include "Led.h"
#include "Usart1.h"
void Timer_Init()
{
    TMOD = 0x00;
    TL0 = (65536-50000)%256;               
    TH0 = (65536-50000)/256;                  
    TR0 = 1;
    ET0 = 1;
    EA = 1;
    T0x12=0;
}
void Timer0() interrupt 1
{   
    if(Usart1_IsRecv>1)
    {
      Usart1_IsRecv-=1;
      if(Usart1_IsRecv==1)
      {
      Usart1_IsRecv=0;
      Usart1_data.IsEnd=1;
      }      
    }
}


串口1实现#include "Usart1.h"
#include "intrins.h"
USART1_Data Usart1_data;
bit busy;
int Usart1_IsRecv=0;
unsigned int wptr;
unsigned int rptr;
void Usart1_Init()
{
    P3M0 = 0x00;
    P3M1 = 0x00;

    SCON = 0x50;
    T2L = BRT;
    T2H = BRT>>8;
    S1BRT = 1;
    T2x12 = 1;
    T2R = 1;
    wptr = 0x00;
    rptr = 0x00;
    busy=0;
    ES =1;
    EA =1;
}
void Usart1_ClearData(void)
{
    Usart1_IsRecv=0;
    for(rptr=0;rptr<BUFFSIZE;rptr++)
    {
      Usart1_data.Buffer=0;
    }
    wptr=0;
    Usart1_data.IsEnd=0;
}
void Usart1_Send(char dat)
{
    while(busy);
    busy=1;
    SBUF = dat;
}
void Usart1_SendChars(char *str)
{
    while(*str)
    {
      Usart1_Send(*str++);
    }
}
void Usart1_Isr() interrupt 4
{
    Usart1_IsRecv++;
    Usart1_data.IsEnd=0;
    if(TI)
    {
      TI=0;
      busy=0;
    }
    if(RI)
    {
      RI=0;
      
      Usart1_data.Buffer = SBUF;
      wptr++;
      if(wptr>BUFFSIZE)
      {
            wptr=0;
      }
    }
}


main函数#include <STC32G.H>
#include "intrins.h"
#include "Led.h"
#include "Delay.h"
#include "Timer.h"
#include "Usart1.h"
#include <stdio.h>
#include <string.h>
char Data;
int c=0;
int main()
{
    EAXFR = 1;
    CKCON = 0x00;
    WTST = 0x00;
    Usart1_Init();
    Timer_Init();

    Usart1_SendChars("Timer-USART1 Test!\r\n");
    while(1)
    {      
      if(Usart1_data.IsEnd)
      {
            if(strcmp(Usart1_data.Buffer,"LEDON")==0)
            {
                Led_On(Led_1);
                Usart1_SendChars("Led On!\r\n");
                Usart1_ClearData();
            }
            else if(strcmp(Usart1_data.Buffer,"LEDOFF")==0)
            {
                Led_Off(Led_1);
                Usart1_SendChars("Led Off!\r\n");
                Usart1_ClearData();
            }
            else
            {
                sprintf(Data,"STC32G12K128:%s\r\n",Usart1_data.Buffer);
                Usart1_SendChars(Data);
                Usart1_ClearData();
            }            
      }

    }
    return 0;
}

神农鼎 发表于 2024-11-28 21:07:38

用户程序区问题,收对,收错,溢出,
MCU是不知到的,是你用户程序知到,是你用户程序的反应

21cnsound 发表于 2024-11-28 21:25:41

代码贴出来,让大家看看是用户程序哪里出问题了

AI-32位8051 发表于 2024-11-28 21:45:33

本帖最后由 AI-32位8051 于 2024-11-28 21:46 编辑

1:加几个NOP试试?
2:参考【新提醒】单片机运行时自动复位 | 已解决,程序问题 - 老鸟反刍/吐槽,新手乐园,毕业设计 国芯技术交流网站 - AI32位8051交流社区https://www.stcaimcu.com/forum.php?mod=viewthread&tid=10381&highlight=%E8%87%AA%E5%8A%A8%E5%A4%8D%E4%BD%8D&page=1&extra=#pid102096
页: [1]
查看完整版本: 你们的32G会因串口而复位么【已解决】