houyawei 发表于 2024-6-29 14:51:12

我想用一线通讯,输出来的为什么不是方波

本来是个stm的部分代码,我想把它逻辑移植到stc上。但是stm的输出的是方波完全是高低电平,我输出的是山谷带尖。
还在学习中,望大佬指教一下。
转过来后时间周期差不多


#include "comm\STC32G.h"
#include "stdio.h"
#include "intrins.h"

typedef         unsigned char        u8;
typedef         unsigned int        u16;
typedef         unsigned long        u32;

#define MAIN_Fosc      12000000UL

/****************************** 用户定义宏 ***********************************/
#define Timer0_Reload   (65536UL -(MAIN_Fosc / 1000))       //Timer 0 中断频率, 1000次/秒

#if (MAIN_Fosc >= 40000000L)
    #define                usrNOP()    _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_()
#elif (MAIN_Fosc >= 36000000L)
    #define                usrNOP()    _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_()
#elif (MAIN_Fosc >= 30000000L)
    #define                usrNOP()    _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_()
#elif (MAIN_Fosc >= 24000000L)
    #define                usrNOP()    _nop_();_nop_();_nop_();_nop_();_nop_();_nop_()
#elif (MAIN_Fosc >= 20000000L)
    #define                usrNOP()    _nop_();_nop_();_nop_();_nop_();_nop_()
#elif (MAIN_Fosc >= 18000000L)
    #define                usrNOP()    _nop_();_nop_();_nop_();_nop_()
#elif (MAIN_Fosc >= 12000000L)
    #define                usrNOP()    _nop_();_nop_();_nop_()
#elif (MAIN_Fosc >= 6000000L)
    #define                usrNOP()    _nop_();_nop_()
#else
    #define                usrNOP()    _nop_()
#endif

sbit DQ = P1^3;
u8 USB_databuf = {0x9E, 0xA0, 0x00, 0x00, 0xFA, 0x00, 0x00, 0x00, 0x00, 0x84};
u8 upan_runstep = 0;
/*****************************************************************************/

/*************本地常量声明    **************/


/*************本地变量声明    **************/
bit B_1ms;          //1ms标志
u16 msecond,second;
/*************本地函数声明    **************/
u8 Reverse_Byte(u8 dat);
u8 CanReadReg(u8 addr);
void delay_us(u8 us);
void Upan_Txd_Function(u8 devicestatus);
void Upan_Rxd_Function(void);
void Upan_Txd_FristStart(void);
void WriteBit(u8 State);
void Start_Bit(void);
void End_Bit(void);
void WriteByte(u8 dat);
void Delay100Xus(u8 count);        //@12.000MHz


/********************* 主函数 *************************/
void main(void)
{
    WTST = 0;//设置程序指令延时参数,赋值为0可将CPU执行指令的速度设置为最快
    EAXFR = 1; //扩展寄存器(XFR)访问使能
    CKCON = 0; //提高访问XRAM速度

    P0M0 = 0x00; P0M1 = 0x00;
    P1M0 = 0x00; P1M1 = 0x00;
    P2M0 = 0x00; P2M1 = 0x00;
    P3M0 = 0x00; P3M1 = 0x00;
    P4M0 = 0x00; P4M1 = 0x00;
    P5M0 = 0x00; P5M1 = 0x00;
    P6M0 = 0x00; P6M1 = 0x00;
    P7M0 = 0x00; P7M1 = 0x00;

        P35=0;
        second=0;
       
        AUXR = 0x80;    //Timer0 set as 1T, 16 bits timer auto-reload,
    TH0 = (u8)(Timer0_Reload / 256);
    TL0 = (u8)(Timer0_Reload % 256);
    ET0 = 1;    //Timer0 interrupt enable
    TR0 = 1;    //Tiner0 run
    EA = 1;   //打开总中断
       
   while(1)
    {
      if(B_1ms)   //1ms到
      {
            B_1ms = 0;
            if(++msecond >= 150)   
            {
                P35=~P35;
                                Upan_Txd_FristStart();
                                Upan_Txd_Function(1);

               
            }
      }
    }
}

//========================================================================
// 函数: voiddelay_us(u8 us)
// 描述: 延时函数。
// 参数: us,要延时的us数, 这里只支持1~255us.
// 返回: none.
// 版本: VER1.0
// 日期: 2013-4-1
// 备注:
//========================================================================
void delay_us(u8 us)
{
    do{
      usrNOP();
      usrNOP();
    }while(--us);
}



/********************** Timer0 1ms中断函数 ************************/
void timer0 (void) interrupt 1
{

    B_1ms = 1;      //1ms标志
}

void Upan_Txd_Function(u8 devicestatus)
{
    u8 num = 0;
    if( upan_runstep == 8 ){
      USB_databuf = Reverse_Byte(devicestatus);
      Start_Bit();
      for(num = 0; num < 10;num ++)
      {
            WriteByte(USB_databuf);
      }
      End_Bit();
    }
}

void Upan_Txd_FristStart(void)
{

    if( upan_runstep ==0 )
    {
      P13=1;
      Delay100Xus(320);
      P13=0;
      upan_runstep++;   
    }
    else if( upan_runstep == 7 )
    {
      P13=1;
      Delay100Xus(4);
      P13=0;
      upan_runstep++;      
    }
    else if( upan_runstep == 8 )
    {
      ;      
    }
    else
    {
      upan_runstep++;
    }   
}

u8 Reverse_Byte(u8 dat)
{
    u8 temp = 0;
        u8 i ;
    for(i = 0; i < 8; i++)
    {
      temp = ((dat>>i) & 0x01) | temp;
      if(i < 7)
      {
            temp = temp << 1;
      }
    }
    return temp;
}
void Start_Bit(void)
{
    u8 num = 0;
    P13=0;
   
    for(num = 0; num < 10;num ++)
    {
         Delay100Xus(10);
    }
}

void End_Bit(void)
{
    P13=1;
   Delay100Xus(4);
    P13=0;
   Delay100Xus(12);
}

void WriteBit(u8 State)
{
    P13=1;
    if(State == 1)
    {
         Delay100Xus(12);
      P13=0;
         Delay100Xus(4);
    }
    else
    {
         Delay100Xus(4);
      P13=0;
         Delay100Xus(12);
    }
    P13=1;
}
void WriteByte(u8 dat)
{
    u8 i=0;

    for(i=0;i<8;i++)
    {
      if(dat&0x80)
      {
         WriteBit(1);
      }
      else
      {
         WriteBit(0);
      }
      dat=dat<<1;
    }
}
void Delay100Xus(u8 count)        //@12.000MHz
{
        u8 num = 0;
        for(num = 0; num < count;num ++)
        {
        unsigned long edata i;

        _nop_();
        _nop_();
        _nop_();
        i = 298UL;
        while (i) i--;
        }
}


晓飛飛 发表于 2024-6-29 15:13:26

道理很简单,你STM32的GPIO设置的模式是PP_OUT,而STC32设置的是准双向内部弱上拉,这样由于口线高电平靠弱上拉来实现,影响了上升沿的斜率,看起来就成三角波了,建议配置P13端口的内部强4k上拉,或者设置为推拉输出模式。

houyawei 发表于 2024-6-29 15:57:38

晓飛飛 发表于 2024-6-29 15:13
道理很简单,你STM32的GPIO设置的模式是PP_OUT,而STC32设置的是准双向内部弱上拉,这样由于口线高电平靠弱 ...
谢谢谢谢,改成推挽确实好了

liuzonggong 发表于 2024-6-29 17:18:28

群里有高人

hsrzq 发表于 2024-6-29 20:00:42

houyawei 发表于 2024-6-29 15:57
谢谢谢谢,改成推挽确实好了

有高手指导就是好啊!

lijun4545 发表于 2024-7-3 15:44:46

这都是模拟电路的基本常知
页: [1]
查看完整版本: 我想用一线通讯,输出来的为什么不是方波