• #循循渐进学51单片机#函数进阶与按键#not.7


    1、掌握单片机最小系统三要素电路设计规则。

    1)

    2)

    3)


    2、掌握函数间相互调用的方法和规则。

    函数的形式参数和实际参数


    实参:在调用一个有参数的函数时,函数名后边括号中的参数。


    形参:在被调用的函数进行定义时,括号内的参数叫做形式参数。


    1、函数定义中指定的形参,在未发生调用时不占用内存,函数调用时分配内存单元,调用结束释放内存。


    2,或者复杂的表达式,但是要求必须有确定的值,在调用时传递给形参。c=add(1,a+b);


    3、参必须制定数据类型,和变量定义一样。


    4参和实参的数据类型应该相同或者赋值兼容。


    5、主调函数在调用函数之前,应对被调函数做原型声明。


    6、实参向形参的数据传递是单向传递,不能有形参再回传给实参。 


    3、会独立按键和矩阵按键的电路设计方法和软件编程思路。

    1)

    2)

    3)

    4)按键消抖方法,遍历八个字节,当八个字节在连续时间字节相同,则判定按钮按下

    5)按钮按下与否的判断,就是跟按钮前一刻的值判断,当前值与前一刻的值不同,则按钮按下。


    4一按实现一个数码管数字从F~0递减的变化程序。

    1. #include <REGX52.H>
    2. sbit addr0 = P1^0;
    3. sbit addr1 = P1^1;
    4. sbit addr2 = P1^2;
    5. sbit addr3 = P1^3;
    6. sbit ENLED = P1^4;
    7. sbit KeyIn1 = P2^4;
    8. sbit KeyIn2 = P2^5;
    9. sbit KeyIn3 = P2^6;
    10. sbit KeyIn4 = P2^7;
    11. sbit KeyOut1 = P2^3;
    12. sbit KeyOut2 = P2^2;
    13. sbit KeyOut3 = P2^1;
    14. sbit KeyOut4 = P2^0;
    15. unsigned char code LedChar[] = { //ÊýÂë¹ÜÏÔʾ×Ö·ûת»»±í
    16. 0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8,
    17. 0x80, 0x90, 0x88, 0x83, 0xC6, 0xA1, 0x86, 0x8E
    18. };
    19. bit i = 1,Key = 1;
    20. unsigned int cnt = 0;
    21. void main()
    22. {
    23. EA = 1;
    24. KeyOut4 = 0;
    25. ENLED = 0;
    26. addr3 = 1;
    27. TMOD = 0x01;
    28. TH0 = 0xF8;
    29. TL0 =0xcb;
    30. addr1 = 0;
    31. addr2 = 0;
    32. addr0 = 0;
    33. ET0 = 1;
    34. TR0 = 1;
    35. P0 = LedChar[cnt];
    36. while(1)
    37. {
    38. if(Key != i)
    39. {
    40. if(i == 0)
    41. {
    42. P0 = LedChar[cnt];
    43. cnt++;
    44. if(cnt >= 15)
    45. {
    46. cnt = 0;
    47. }
    48. }
    49. i = Key;
    50. }
    51. }
    52. }
    53. void InterruptTimer0() interrupt 1
    54. {
    55. static unsigned int arr = 0xff;
    56. TH0 = 0xF8;
    57. TL0 =0xcb;
    58. arr = (arr << 1) | KeyIn2;
    59. if(arr == 0xff)
    60. {
    61. Key = 1;
    62. }
    63. else if(arr == 0x00)
    64. {
    65. Key = 0;
    66. }
    67. else {}
    68. }


    5、用矩阵按键做一个简易减法计算器。

    1. #include <REGX52.H>
    2. sbit addr0 = P1^0;
    3. sbit addr1 = P1^1;
    4. sbit addr2 = P1^2;
    5. sbit addr3 = P1^3;
    6. sbit ENLED = P1^4;
    7. sbit KeyIn1 = P2^4;
    8. sbit KeyIn2 = P2^5;
    9. sbit KeyIn3 = P2^6;
    10. sbit KeyIn4 = P2^7;
    11. sbit KeyOut1 = P2^3;
    12. sbit KeyOut2 = P2^2;
    13. sbit KeyOut3 = P2^1;
    14. sbit KeyOut4 = P2^0;
    15. unsigned char code LedChar[] = {
    16. 0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8,
    17. 0x80, 0x90, 0x88, 0x83, 0xC6, 0xA1, 0x86, 0x8E
    18. };
    19. unsigned char LedBuff[6] = {
    20. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
    21. };
    22. unsigned char code KeyCodeMap[4][4] = {
    23. { 0x31, 0x32, 0x33, 0x26 },
    24. { 0x34, 0x35, 0x36, 0x25 },
    25. { 0x37, 0x38, 0x39, 0x28 },
    26. { 0x30, 0x1B, 0x0D, 0x27 }
    27. };
    28. unsigned char KeySta[4][4] = {
    29. {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}
    30. };
    31. void KeyDriver();
    32. void main()
    33. {
    34. EA = 1;
    35. ENLED = 0;
    36. addr3 = 1;
    37. TMOD = 0x01;
    38. TH0 = 0xFC;
    39. TL0 = 0x67;
    40. ET0 = 1;
    41. TR0 = 1;
    42. LedBuff[0] = LedChar[0];
    43. while (1)
    44. {
    45. KeyDriver();
    46. }
    47. }
    48. void ShowNumber(long num)
    49. {
    50. signed char i;
    51. unsigned char sign;
    52. unsigned char buf[6];
    53. if (num < 0)
    54. {
    55. sign = 1;
    56. num = -num;
    57. }
    58. else
    59. {
    60. sign = 0;
    61. }
    62. for (i=0; i<6; i++)
    63. {
    64. buf[i] = num % 10;
    65. num = num / 10;
    66. }
    67. for (i=5; i>=1; i--)
    68. {
    69. if (buf[i] == 0)
    70. LedBuff[i] = 0xFF;
    71. else
    72. break;
    73. }
    74. if (sign != 0)
    75. {
    76. if (i < 5)
    77. {
    78. LedBuff[i+1] = 0xBF;
    79. }
    80. }
    81. for ( ; i>=0; i--)
    82. {
    83. LedBuff[i] = LedChar[buf[i]];
    84. }
    85. }
    86. void KeyAction(unsigned char keycode)
    87. {
    88. static char oprt = 0;
    89. static long result = 0;
    90. static long addend = 0;
    91. if ((keycode>=0x30) && (keycode<=0x39))
    92. {
    93. addend = (addend*10)+(keycode-0x30);
    94. ShowNumber(addend);
    95. }
    96. else if (keycode == 0x26)
    97. {
    98. oprt = 0;
    99. result = addend;
    100. addend = 0;
    101. ShowNumber(addend);
    102. }
    103. else if (keycode == 0x28)
    104. {
    105. oprt = 1;
    106. result = addend;
    107. addend = 0;
    108. ShowNumber(addend);
    109. }
    110. else if (keycode == 0x0D)
    111. {
    112. if (oprt == 0)
    113. {
    114. result += addend;
    115. }
    116. else
    117. {
    118. result -= addend;
    119. }
    120. addend = 0;
    121. ShowNumber(result);
    122. }
    123. else if (keycode == 0x1B)
    124. {
    125. addend = 0;
    126. result = 0;
    127. ShowNumber(addend);
    128. }
    129. }
    130. void KeyDriver()
    131. {
    132. unsigned char i, j;
    133. static unsigned char backup[4][4] = {
    134. {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}
    135. };
    136. for (i=0; i<4; i++)
    137. {
    138. for (j=0; j<4; j++)
    139. {
    140. if (backup[i][j] != KeySta[i][j])
    141. {
    142. if (backup[i][j] != 0)
    143. {
    144. KeyAction(KeyCodeMap[i][j]);
    145. }
    146. backup[i][j] = KeySta[i][j];
    147. }
    148. }
    149. }
    150. }
    151. void KeyScan()
    152. {
    153. static unsigned char add = 0;
    154. unsigned char j ;
    155. static unsigned char keybuf[4][4] = {
    156. {0xFF, 0xFF, 0xFF, 0xFF}, {0xFF, 0xFF, 0xFF, 0xFF},
    157. {0xFF, 0xFF, 0xFF, 0xFF}, {0xFF, 0xFF, 0xFF, 0xFF}
    158. };
    159. keybuf[add][0] = (keybuf[add][0] << 1) | KeyIn1;
    160. keybuf[add][1] = (keybuf[add][1] << 1) | KeyIn2;
    161. keybuf[add][2] = (keybuf[add][2] << 1) | KeyIn3;
    162. keybuf[add][3] = (keybuf[add][3] << 1) | KeyIn4;
    163. for(j = 0;j <4;j++)
    164. {
    165. if( (keybuf[add][j] & 0x0F) == 0x00 )
    166. {
    167. KeySta[add][j] = 0;
    168. }
    169. else if((keybuf[add][j] & 0x0F) == 0x0f)
    170. {
    171. KeySta[add][j] = 1;
    172. }
    173. }
    174. add++;
    175. add = add & 0x03;
    176. switch(add)
    177. {
    178. case 0:KeyOut4 = 1;KeyOut1 = 0;break;
    179. case 1:KeyOut1 = 1;KeyOut2 = 0;break;
    180. case 2:KeyOut2 = 1;KeyOut3 = 0;break;
    181. case 3:KeyOut3 = 1;KeyOut4 = 0;break;
    182. default:break;
    183. }
    184. }
    185. void LedScan()
    186. {
    187. static unsigned char i = 0;
    188. P0 = 0xff;
    189. switch(i)
    190. {
    191. case 0:addr2 = 0;addr1 = 0;addr0 = 0;i++;P0 = LedBuff[0];break;
    192. case 1:addr2 = 0;addr1 = 0;addr0 = 1;i++;P0 = LedBuff[1];break;
    193. case 2:addr2 = 0;addr1 = 1;addr0 = 0;i++;P0 = LedBuff[2];break;
    194. case 3:addr2 = 0;addr1 = 1;addr0 = 1;i++;P0 = LedBuff[3];break;
    195. case 4:addr2 = 1;addr1 = 0;addr0 = 0;i++;P0 = LedBuff[4];break;
    196. case 5:addr2 = 1;addr1 = 0;addr0 = 1;i=0;P0 = LedBuff[5];break;
    197. default:break;
    198. }
    199. }
    200. void InterruptTimer0() interrupt 1
    201. {
    202. TH0 = 0xFC;
    203. TL0 = 0x67;
    204. LedScan();
    205. KeyScan();
    206. }

  • 相关阅读:
    如何发布一个 NPM 包
    【数据结构与算法】第十五篇:快速,希尔排序
    深入理解Java虚拟机(第3版)学习笔记——后端编译与优化(超详细)
    JS如何判断对象为空?以及各自的缺点。
    单商户商城系统功能拆解34—应用中心—分销应用
    JAVA 实现《五子棋单机版》游戏
    uni-app:多种方法写入图片路径
    《MongoDB入门教程》第13篇 限制返回结果数量
    Oracle修改varchar类型为clob时,报错:ORA-22858
    PyTorch(四)数据转换与构建神经网络
  • 原文地址:https://blog.csdn.net/2301_77479336/article/details/132856508