单片机型号说明:IAP15F2K61S2
新建工程时单片机型号选择STC15F2K60S2
51单片机矩阵键盘代码
1. 2. void Delay_ms(int xms) //15单片机延时函数 3. { 4. int i; 5. long a; 6. for (i = xms; i > 0; i --) 7. for(a = 810; a > 0; a --); 8. } 9. 10. unsigned char KeyJZ_16(void) 11. { 12. unsigned char KeyNumber; 13. unsigned char temp; 14. 15. //第一列 16. P3 = 0x7F; //P37 = 0,其他是1,检测第一列 17. temp = P3; //按键检测,输入 18. temp &= 0x0F; 19. if(temp != 0x0F) 20. { 21. Delay_ms(20); //延时20ms来确认是否真正按下按键 22. temp = P3; 23. temp &= 0x0F;//检测是第一列哪个按键 24. if(temp != 0x0F) 25. { 26. temp = P3; 27. switch (temp) 28. { 29. case 0x7E : KeyNumber = 7; break; 30. case 0x7D : KeyNumber = 6; break; 31. case 0x7B : KeyNumber = 5; break; 32. case 0x77 : KeyNumber = 4; break; 33. } 34. while(temp != 0x0f) 35. { 36. temp = P3; 37. temp &= 0x0F; //因为这条,所以松手检测才写成 temp != 0x0f 38. } 39. } 40. 41. } 42. 43. //第二列 44. P3 = 0xBF; 45. temp = P3; 46. temp &= 0x0F; 47. if(temp != 0x0F) 48. { 49. Delay_ms(20); 50. temp = P3; 51. temp &= 0x0F; 52. if(temp != 0x0F) 53. { 54. temp = P3; 55. switch (temp) 56. { 57. case 0xBE : KeyNumber = 11; break; 58. case 0xBD : KeyNumber = 10; break; 59. case 0xBB : KeyNumber = 9; break; 60. case 0xB7 : KeyNumber = 8; break; 61. } 62. while(temp != 0x0f) 63. { 64. temp = P3; 65. temp &= 0x0F; //因为这条,所以松手检测才写成 temp != 0x0f 66. } 67. } 68. } 69. 70. //第三列 71. P3 = 0xDF; 72. temp = P3; 73. temp &= 0x0F; 74. if(temp != 0x0F) 75. { 76. Delay_ms(20); 77. temp = P3; 78. temp &= 0x0F; 79. if(temp != 0x0F) 80. { 81. temp = P3; 82. switch (temp) 83. { 84. case 0xDE : KeyNumber = 15; break; 85. case 0xDD : KeyNumber = 14; break; 86. case 0xDB : KeyNumber = 13; break; 87. case 0xD7 : KeyNumber = 12; break; 88. } 89. while(temp != 0x0f) 90. { 91. temp = P3; 92. temp &= 0x0F; //因为这条,所以松手检测才写成 temp != 0x0f 93. } 94. } 95. } 96. 97. //第四列 98. P3 = 0xEF; 99. temp = P3; 100. temp &= 0x0F; 101. if(temp != 0x0F) 102. { 103. Delay_ms(20); 104. temp = P3; 105. temp &= 0x0F; 106. if(temp != 0x0F) 107. { 108. temp = P3; 109. switch (temp) 110. { 111. case 0xEE : KeyNumber = 19; break; 112. case 0xED : KeyNumber = 18; break; 113. case 0xEB : KeyNumber = 17; break; 114. case 0xE7 : KeyNumber = 16; break; 115. } 116. while(temp != 0x0f) 117. { 118. temp = P3; 119. temp &= 0x0F; //因为这条,所以松手检测才写成 temp != 0x0f 120. } 121. } 122. } 123. 124. return KeyNumber; 125. }
本程序烧写到51单片机应该是正常使用的,手头没有51开发板了,暂时没测试
这个程序烧写到15单片机,前两列按键不能正常使用
只需要在判断第一列按键那部分程序,前面加上P42 = 0;
在判断第二列按键那部分程序,前面加上P44 = 0;
因为这两列接线是P42和P44
矩阵按键部分代码(新写法)
1. 2. void Delay_ms(int xms) //15单片机延时函数 3. { 4. int i; 5. long a; 6. for (i = xms; i > 0; i --) 7. for(a = 810; a > 0; a --); 8. } 9. 10. unsigned char KeyJZ_16(void) 11. { 12. unsigned char KeyNumber = 0; 13. unsigned char temp; 14. 15. //第一列 16. P3 = 0x7F; //P37 = 0,其他是1,检测第一列 17. P4 = 0xEF; //P44 = 0; P42 = 1; //15单片机第一列占用的引脚 18. temp = P3; //按键检测,输入 19. temp &= 0x0F; 20. if(temp != 0x0F) 21. { 22. Delay_ms(20); //延时20ms来确认是否真正按下按键 23. temp = P3; 24. temp &= 0x0F;//检测是第一列哪个按键 25. if(temp != 0x0F) 26. { 27. temp = P3; 28. switch (temp) 29. { 30. case 0x7E : KeyNumber = 7; break; 31. case 0x7D : KeyNumber = 6; break; 32. case 0x7B : KeyNumber = 5; break; 33. case 0x77 : KeyNumber = 4; break; 34. } 35. while(temp != 0x0f) 36. { 37. temp = P3; 38. temp &= 0x0F; //因为这条,所以松手检测才写成 temp != 0x0f 39. } 40. } 41. 42. } 43. 44. //第二列 45. P3 = 0xBF; 46. P4 = 0xFB; //P42 = 0; P44 = 1; //15单片机第二列占用的引脚 47. temp = P3; 48. temp &= 0x0F; 49. if(temp != 0x0F) 50. { 51. Delay_ms(20); 52. temp = P3; 53. temp &= 0x0F; 54. if(temp != 0x0F) 55. { 56. temp = P3; 57. switch (temp) 58. { 59. case 0xBE : KeyNumber = 11; break; 60. case 0xBD : KeyNumber = 10; break; 61. case 0xBB : KeyNumber = 9; break; 62. case 0xB7 : KeyNumber = 8; break; 63. } 64. while(temp != 0x0f) 65. { 66. temp = P3; 67. temp &= 0x0F; //因为这条,所以松手检测才写成 temp != 0x0f 68. } 69. } 70. } 71. 72. //第三列 73. P3 = 0xDF; 74. P4 = 0xFF; 75. temp = P3; 76. temp &= 0x0F; 77. if(temp != 0x0F) 78. { 79. Delay_ms(20); 80. temp = P3; 81. temp &= 0x0F; 82. if(temp != 0x0F) 83. { 84. temp = P3; 85. switch (temp) 86. { 87. case 0xDE : KeyNumber = 15; break; 88. case 0xDD : KeyNumber = 14; break; 89. case 0xDB : KeyNumber = 13; break; 90. case 0xD7 : KeyNumber = 12; break; 91. } 92. while(temp != 0x0f) 93. { 94. temp = P3; 95. temp &= 0x0F; //因为这条,所以松手检测才写成 temp != 0x0f 96. } 97. } 98. } 99. 100. //第四列 101. P3 = 0xEF; 102. temp = P3; 103. temp &= 0x0F; 104. if(temp != 0x0F) 105. { 106. Delay_ms(20); 107. temp = P3; 108. temp &= 0x0F; 109. if(temp != 0x0F) 110. { 111. temp = P3; 112. switch (temp) 113. { 114. case 0xEE : KeyNumber = 19; break; 115. case 0xED : KeyNumber = 18; break; 116. case 0xEB : KeyNumber = 17; break; 117. case 0xE7 : KeyNumber = 16; break; 118. } 119. while(temp != 0x0f) 120. { 121. temp = P3; 122. temp &= 0x0F; //因为这条,所以松手检测才写成 temp != 0x0f 123. } 124. } 125. } 126. 127. return KeyNumber; 128. }
测试键盘是否好使
1. #include <STC15F2K60S2.H> 2. 3. unsigned char KeyNum; 4. 5. void main() 6. { 7. P2 = 0xA0; P0 = 0x00; P2 = 0x80; P0 = 0xFF; //初始化 8. while(1) 9. { 10. KeyNum = KeyJZ_16(); 11. if(KeyNum != 0) 12. { 13. P00 = !P00; //每次按键按下P00状态都会取反 14. } 15. 16. } 17. }
一定要加初始化呀,这次忘了,按键根本不好使