找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2450|回复: 1
收起左侧

51MCU常用延时,16进制转10进制程序等

[复制链接]
ID:701952 发表于 2020-3-3 20:17 | 显示全部楼层 |阅读模式
常用函数:
    1. 延时函数;
    2. HexToDec(unsigned char uHex);//16进制转10进制
    3. DecToHex(unsigned char uDec);//10进制转16进制
     4.时钟框架;

单片机源程序如下:
  1. //#include <reg51.h>
  2. #include <absacc.h>
  3. #include <intrins.h>
  4. #include <math.h>
  5. #include <absacc.h>
  6. #include <string.h>

  7. #include "..\\INC\\ISD51_U1.h"
  8. #include "..\\INC\\basic.h"

  9. #include "..\\INC\\SST89.h"
  10. #include "..\\INC\\ProcessCommu.H"
  11. #include "..\\INC\\CommunicationExt.h"

  12. idata struct SSysTimer sSysTimerV;

  13. //extern xdata struct SSysTime SSysTimeV;                         /* 系统时间 */

  14. bit        bNet1CpuStateFlag = 0;
  15. bit        bNet2CpuStateFlag = 0;
  16. bit        bNet3CpuStateFlag = 0;
  17. bit bDisplayCpuStateFlag = 0;


  18. void delay(unsigned int dtime)          // 1000 is equal 1000ms when crystal is 36MHz
  19. {
  20.     unsigned char i;


  21.         ISDdisable();

  22.     while((dtime--)!=0)
  23.     {
  24.          for(i=0;i<246;i++)
  25.          {
  26.              _nop_();
  27.              _nop_();
  28.              _nop_();
  29.              _nop_();
  30.          }
  31.     }

  32. //        ISDenable();

  33. }


  34. void InitSys(void)                                 /*init 8031*/
  35. {
  36.         WDTC = 0;                                                                           //禁止Watchdog

  37.         WDTD = -100;                                                                           //禁止Watchdog

  38.         //unsigned int timer2_baud;
  39.         // ISD51 makes use of Uart1 or Uart2 based on header file & object file used
  40.         T2CON  = 0x34;                // Use Timer 2 as baudrate generator
  41.                                                 // This sets Uart1 to use timer2 baud rate
  42.     PCON  |= 0x0C;                 // Set UART2 to user timer2 baud rate
  43. //        RCAP2L = 0xE0;                // 19200  at 20M  0xE0
  44.         RCAP2L = 0xC0;                // 19200  at 40M  0xC0,19200
  45.         RCAP2H = 0xff;
  46.         SCON   = 0x50;      // enable first serial UART & receiver
  47.        
  48.         EX0   =0;
  49.         EX1   =0;

  50.         ET1   =0;

  51.         TR1   =1;
  52.        
  53.         IE = IE & 0x18;       
  54.         EA = 1;             // 开放中断

  55.         TMOD = 0x12;                             //T0 :自动载入 ; T1:16 定时器

  56.         TH0 = 0xFC;                        //old is 0xFD
  57.         TL0 = 0xFF;
  58.         IT1 = 1;
  59.         EX1 = 0;                                                                //禁止IAP中断
  60.         EPCA = 1;                                                                //使能PCA中断

  61. //中断优先级
  62.         IP  = 0x40;                                                                //PCA中断优先级最高
  63.         IPH = 0x18;                                                                //串口和定时器1中断优先级次高

  64.         CCAPM0 = 0;
  65.         CCAPM1 = 0;
  66.         CCAPM2 = 0;
  67.         CCAPM3 = 0;
  68.         CCAPM4 = 0;

  69.         TR0 = 1;

  70.         TR1   =1;
  71.         ET1   =0;
  72.         ET0   =0;

  73. }

  74. void ProcessSystimer(unsigned char uVal)
  75. {
  76.         static unsigned char uNum = 0;
  77.         code unsigned char uAdjustNum[3]={15,15,5};
  78.         static unsigned char uIndex=0;

  79. /*
  80.         uNum=uNum+ uVal;

  81.         if(uNum<uAdjustNum[uIndex])
  82.                 return;
  83.         uNum = uNum-uAdjustNum[uIndex];
  84.         uIndex++;
  85.         if(uIndex>=3)
  86.                 uIndex=0;
  87. */


  88.         uIndex++;
  89.         if(uIndex==3)
  90.                 uIndex = 0;
  91.         else
  92.                 return;



  93.     sSysTimerV.uMillisecond++;

  94.     if(sSysTimerV.uMillisecond>=1000)
  95.     {
  96.                 sSysTimerV.uMillisecond = 0;
  97.         sSysTimerV.uSecond++;
  98.         if(sSysTimerV.uSecond>=200)
  99.         {
  100.             sSysTimerV.uSecond = 0;
  101.         }

  102.     }

  103. }

  104. unsigned char HexToDec(unsigned char uHex)
  105. {
  106.     unsigned char uTempVal;

  107.     uTempVal = (uHex/16)*10+uHex%16;

  108.     return uTempVal;

  109. }

  110. unsigned char DecToHex(unsigned char uDec)
  111. {
  112.     unsigned char uTempVal;

  113.     uTempVal = (uDec/10)*16+uDec%10;

  114.     return uTempVal;

  115. }


  116. unsigned int GetTimeInterval(struct STimeInterval * ptsTimeInterval)
  117. {
  118.          xdata unsigned int iCurrentTime;

  119.      iCurrentTime = GetCurrentTime();
  120.      if( iCurrentTime >= ptsTimeInterval[0].OldTime)
  121.          ptsTimeInterval[0].Interval = iCurrentTime - ptsTimeInterval[0].OldTime;
  122.      else
  123.          ptsTimeInterval[0].Interval = 20000 + iCurrentTime - ptsTimeInterval[0].OldTime;

  124.      return ptsTimeInterval[0].Interval;

  125. }


  126. unsigned int GetCurrentTime()
  127. {
  128.          xdata  struct SSysTimer sCurrentTimeV;

  129.      sCurrentTimeV.uSecond      = sSysTimerV.uSecond;
  130.      sCurrentTimeV.uMillisecond = sSysTimerV.uMillisecond/10;

  131. //ISDwait();

  132.      sCurrentTimeV.uTime = sCurrentTimeV.uSecond*100;
  133.      sCurrentTimeV.uTime = sCurrentTimeV.uTime + sCurrentTimeV.uMillisecond;

  134.      return sCurrentTimeV.uTime;

  135. }
复制代码

所有资料51hei提供下载:
basic常用函数.zip (2.14 KB, 下载次数: 12)


回复

使用道具 举报

ID:923440 发表于 2021-7-15 20:28 | 显示全部楼层
这16进制转10进制的程序也不对吧
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表