Yet another otaku

某ロリコン的自白


  • 首頁
  • 歸檔
  • 分類
  • 標籤
  • 連結
  • 關於
  •    

© 2025 SgDylan

Theme Typography by Makito

Proudly published with Hexo

使用Python脚本计算单片机纯软件延时计数值

發佈於 2016-06-30 評論 编程  python mcu 

编写单片机程序时,经常为了保证时序使用软件延时程序。
以下是一种精确计算软件延时的方法(强迫症福音)。

使用方法:
假设python脚本保存为calc.py
使用时使用命令 python calc.py [延时秒值] 即可
例如延时0.5秒,即 python calc.py 0.5

8051汇编版:

单片机汇编代码

DEL:
    MOV R7,#10 ;最外层循环,对应计算结果Z值
    DEL1:
        MOV R6,#50 ;中层循环,对应计算结果Y值
        DEL2:
            MOV R5,#100 ;最里层循环,对应计算结果X值
            DJNZ R5,$
        DJNZ R6,DEL2
    DJNZ R7,DEL1 RET 

延时计算程序

#Function: 12 + (1 + (1 + 3 u) y) z == 1000000 (1*10^6)

import sys

def main(args):
    FinTime = 2
    FinX = 0
    FinY = 0
    FinZ = 0
    TotalCircle = 0
    Offset = 100
    try:
        FinTime = float(args[0])
        print('Your delay time: {}'.format(args[0]))
    except:
        print("Need more argument")
        return
    if len(args)>1:
        Offset = float(args[1])
        print('Force offset: {}'.format(args[1]))
    for RZ in range (0,255):
        for RY in range (0,255):
            for RX in range (0,255):
                ROffset = (12+(1+(1 +3*RX)*RY)*RZ)-FinTime*1000000
                #print('Now: X={} Y={} Z={}'.format(RX,RY,RZ))
                if abs(ROffset) <= abs(Offset):
                    Offset = ROffset
                    FinX = RX
                    FinY = RY
                    FinZ = RZ
    TotalCircle = (12+(1+(1 +3*FinX)*FinY)*FinZ)
    print('Finial X={}\nFinial Y={}\nFinial Z={}'.format(str(FinX),str(FinY),str(FinZ)))
    print ("Finial Offset: {}\nTotal Circle: {}".format(str(Offset),str(TotalCircle)))

if __name__ == "__main__":
    print("\nThis program could only calculate three degree pure-software-loop.")
    print("Max loop: 49225974, About 49 seconds")
    main(sys.argv[1:]) 

C语言版(默认使用Keil编译器O8优化下的结果)

单片机C51代码

void delay_time(unsigned char count0,unsigned char count1,unsigned char count2){
    unsigned char i,j,n;
    for(i;i<count0;i++){
        for(j;j<count1;j++){
            for(n;n<count2;n++);
        }
    }
}

延时计算程序

import sys

def main(args):
    FinTime = 2
    FinX = 0
    FinY = 0
    FinZ = 0
    TotalCircle = 0
    Offset = 100
    try:
        FinTime = float(args[0])
        print('Your delay time: {}'.format(args[0]))
    except:
        print("Need more argument")
        return
    if len(args)>1:
        Offset = float(args[1])
        print('Force offset: {}'.format(args[1]))
    for RZ in range (0,255):
        for RY in range (0,255):
            for RX in range (0,255):
                ROffset = (16+(1+(1 +3*RX)*RY)*RZ)-FinTime*1000000
                #print('Now: X={} Y={} Z={}'.format(RX,RY,RZ))
                if abs(ROffset) <= abs(Offset):
                    Offset = ROffset
                    FinX = RX
                    FinY = RY
                    FinZ = RZ
    TotalCircle = (16+(1+(1 +3*FinX)*FinY)*FinZ)
    print('Finial X={}\nFinial Y={}\nFinial Z={}'.format(str(FinX),str(FinY),str(FinZ)))
    print ("Finial Offset: {}\nTotal Circle: {}".format(str(Offset),str(TotalCircle)))

if __name__ == "__main__":
    print("\nThis program could only calculate three degree pure-software-loop.")
    main(sys.argv[1:])

分享到 

 上一篇: 一种批量下载Bilibili视频的方法 下一篇: 尝试在HEXO内嵌歌曲 

© 2025 SgDylan

Theme Typography by Makito

Proudly published with Hexo