本帖最后由 SYDZ__顺 于 2012-5-15 18:16 编辑
arduino学习笔记21数字骰子实验 前几次做了数码管实验和按键控制LED的实验,通过实验大家已经学会了两种器件的基本用法。这次使用数码管与按键进行组合,做一个简易数字骰子。
数字筛子的基本原理是数码管快速在1-8之间循环跳动,当按下按键时,数码管定格在当前的数字上,不再跳动。
先看一下接线图
1. //设置控制各段的数字IO脚 2. int a=7; 3. int b=6; 4. int c=5; 5. int d=11; 6. int e=10; 7. int f=8; 8. int g=9; 9. int dp=4; 10. 11. //显示数字1 12. void digital_1(void) 13. { 14. unsigned char j; 15. digitalWrite(c,LOW);//给数字5引脚低电平,点亮c段 16. digitalWrite(b,LOW);//点亮b段 17. for(j=7;j<=11;j++)//熄灭其余段 18. digitalWrite(j,HIGH); 19. digitalWrite(dp,HIGH);//熄灭小数点DP段 20. } 21. //显示数字2 22. void digital_2(void) 23. { 24. unsigned char j; 25. digitalWrite(b,LOW); 26. digitalWrite(a,LOW); 27. for(j=9;j<=11;j++) 28. digitalWrite(j,LOW); 29. digitalWrite(dp,HIGH); 30. digitalWrite(c,HIGH); 31. digitalWrite(f,HIGH); 32. } 33. //显示数字3 34. void digital_3(void) 35. { 36. unsigned char j; 37. digitalWrite(g,LOW); 38. digitalWrite(d,LOW); 39. for(j=5;j<=7;j++) 40. digitalWrite(j,LOW); 41. digitalWrite(dp,HIGH); 42. digitalWrite(f,HIGH); 43. digitalWrite(e,HIGH); 44. } 45. //显示数字4 46. void digital_4(void) 47. { 48. digitalWrite(c,LOW); 49. digitalWrite(b,LOW); 50. digitalWrite(f,LOW); 51. digitalWrite(g,LOW); 52. digitalWrite(dp,HIGH); 53. digitalWrite(a,HIGH); 54. digitalWrite(e,HIGH); 55. digitalWrite(d,HIGH); 56. } 57. //显示数字5 58. void digital_5(void) 59. { 60. unsigned char j; 61. for(j=7;j<=9;j++) 62. digitalWrite(j,LOW); 63. digitalWrite(c,LOW); 64. digitalWrite(d,LOW); 65. digitalWrite(dp,HIGH); 66. digitalWrite(b,HIGH); 67. digitalWrite(e,HIGH); 68. } 69. //显示数字6 70. void digital_6(void) 71. { 72. unsigned char j; 73. for(j=7;j<=11;j++) 74. digitalWrite(j,LOW); 75. digitalWrite(c,LOW); 76. digitalWrite(dp,HIGH); 77. digitalWrite(b,HIGH); 78. } 79. //显示数字7 80. void digital_7(void) 81. { 82. unsigned char j; 83. for(j=5;j<=7;j++) 84. digitalWrite(j,LOW); 85. digitalWrite(dp,HIGH); 86. for(j=8;j<=11;j++) 87. digitalWrite(j,HIGH); 88. } 89. //显示数字8 90. void digital_8(void) 91. { 92. unsigned char j; 93. for(j=5;j<=11;j++) 94. digitalWrite(j,LOW); 95. digitalWrite(dp,HIGH); 96. } 97. void setup() 98. { 99. int i; 100. for(i=4;i<=11;i++) 101. { 102. pinMode(i,OUTPUT);//设置4~11口为输出模式 103. } 104. } 105. void loop() 106. { 107. while(1) 108. { 109. digital_1();//显示数字1 110. while(analogRead(0)>1000);//如果读到模拟0口的值1000则说明有按键按下 111. delay(100);//延时200ms 112. digital_2(); 113. while(analogRead(0)>1000); 114. delay(100); 115. digital_3(); 116. while(analogRead(0)>1000); 117. delay(100); 118. digital_4(); 119. while(analogRead(0)>1000); 120. delay(100); 121. digital_5(); 122. while(analogRead(0)>1000); 123. delay(100); 124. digital_6(); 125. while(analogRead(0)>1000); 126. delay(100); 127. digital_7(); 128. while(analogRead(0)>1000); 129. delay(100); 130. digital_8(); 131. while(analogRead(0)>1000); 132. delay(100); 133. } 134. } 复制代码 实验的效果如下
|