找回密码
 立即注册
搜索
热搜: 中医 针灸 咳嗽
查看: 126|回复: 0

外部中断的演示

[复制链接]

3706

主题

1

回帖

1万

积分

管理员

积分
11870
发表于 2023-2-2 17:00:52 | 显示全部楼层 |阅读模式
  1. /*外部中断的演示*/
  2. //相关引脚p33
  3. #include <reg52.h>
  4. #define Uint  unsigned int
  5. #define Uchar unsigned char
  6. #define Ulong unsigned long

  7. sbit DU = P2 ^ 6; // 数码管段选
  8. sbit WE = P2 ^ 7; // 位选

  9. /* 8位选参照表
  10. FE 1
  11. FD 2
  12. fb 3
  13. f7 4
  14. ef 5
  15. df 6
  16. bf 7
  17. 7f 8
  18. */
  19. // 段选参照表
  20. //  0x06,  //"1"
  21. //  0x5B,  //"2"
  22. //  0x4F,  //"3"
  23. //  0x66,  //"4"
  24. //  0x6D,  //"5"
  25. //  0x7D,  //"6"
  26. //  0x07,  //"7"
  27. //  0x7F,  //"8"
  28. //  0x6F,  //"9"
  29. //  0x06,  //"1"
  30. //  0x5B,  //"2"
  31. //  0x4F,  //"3"
  32. //  0x66,  //"4"
  33. //  0x6D,  //"5"
  34. //  0x7D,  //"6"
  35. //  0x07,  //"7"
  36. //  0x7F,  //"8"
  37. //  0x6F,  //"9"
  38. //  0x7C,  //"B"
  39. //  0x39,  //"C"
  40. //  0x5E,  //"D"
  41. //  0x79,  //"E"
  42. //  0x71,  //"F"
  43. //  0x76,  //"H"
  44. //  0x38,  //"L"
  45. //  0x40,  //"-"
  46. //  0x00,  //熄灭
  47. //  */

  48. // 函数声明

  49. void showval(Uchar val, Uchar WEval);
  50. void delay(Uchar z, int mod);
  51. void display(Ulong Z);
  52. void init_init0(int type);

  53. Uchar code DUtabel[] = {
  54.     0x3F,
  55.     0x06,
  56.     0x5B,
  57.     0x4F,
  58.     0x66,
  59.     0x6D,
  60.     0x7D,
  61.     0x07,
  62.     0x7F,
  63.     0x6F,
  64.     0x77,
  65.     0x7C,
  66.     0x39,
  67.     0x5E,
  68.     0x79,
  69.     0x71,
  70.     0x76,
  71.     0x38,
  72.     0x40,
  73.     0x00,
  74. };

  75. Uchar code WEtabel[] = {
  76.     0x00, // 全亮,补位选空位
  77.     0xFE,
  78.     0xFD,
  79.     0xfb,
  80.     0xf7,
  81.     0xef,
  82.     0xdf,
  83.     0xbf,
  84.     0x7f,
  85. };

  86. Ulong val = 0;
  87. void main()
  88. {
  89.     init_init0(1);

  90.     while (1) {

  91.         display(val++);
  92.     }
  93. }
  94. /*
  95. 数码管显示函数:
  96. 参数1:显示的数据0-9
  97. 参数2:需要在显示的位1-8
  98. */
  99. void showval(Uchar val, Uchar WEval)
  100. {

  101.     WE = 1;              // 打开位选锁存
  102.     P0 = WEtabel[WEval]; // 选择位选
  103.     WE = 0;              // 位锁存

  104.     DU = 1;            // 打开段选锁存器
  105.     P0 = DUtabel[val]; // 送数据
  106.     DU = 0;            // 段锁存
  107. }
  108. /*
  109. 延迟函数.
  110. 参数1:z为数量
  111. 参数2:mod为模式
  112. 1:毫秒
  113. 2:秒
  114. */
  115. void delay(Uchar z, int mod)
  116. {
  117.     Ulong i;
  118.     if (mod == 1) {
  119.         for (i = 0; i < z; i++) {

  120.             unsigned char a, b;
  121.             for (b = 102; b > 0; b--)
  122.                 for (a = 3; a > 0; a--)
  123.                     ;
  124.         }
  125.     }

  126.     else if (mod == 2) {
  127.         for (i = 0; i < z * 1000; i++) {
  128.             // 10000us //误差 -0.000000000002us
  129.             unsigned char a, b;
  130.             for (b = 102; b > 0; b--)
  131.                 for (a = 3; a > 0; a--)
  132.                     ;
  133.         }
  134.     }
  135. }
  136. /*显示长数字*/
  137. void display(Ulong Z)
  138. {

  139.     // Z长度
  140.     int Zlen = 0;
  141.     /*存储Z每个位的数字数组变量*/
  142.     int val[8];
  143.     int i;

  144.     while (Z != 0) {
  145.         // 提取n的各个数位上的数
  146.         val[Zlen++] = Z % 10;
  147.         Z /= 10;
  148.     }

  149.     for (i = 0; i < Zlen; i++) {
  150.         P0 = 0XFF; // 清除段码
  151.         showval(val[i], 8 - i);
  152.         delay(2, 1);
  153.     }
  154. }

  155. /*
  156. // 初始化中断寄存器
  157. type:中断触发类型:
  158. 0: 低电平触发
  159. 1: 跳变沿方式触发
  160. */
  161. void init_init0(int type)
  162. {
  163.     EA  = 1;
  164.     EX1 = 1;
  165.     IT1 = type;
  166. }

  167. void init2() interrupt 2 //外部中断用法
  168. {

  169.    
  170.     if(P3^3 ==0){
  171.      val = 0;  
  172.      
  173.     }

  174. }
复制代码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|私人站点 ( 冀ICP备2023028127号-2 )

GMT+8, 2025-4-19 23:30 , Processed in 0.077752 second(s), 21 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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