c++ Attiny85 PINB随机 Flink

6psbrbz9  于 2024-01-09  发布在  Flink
关注(0)|答案(1)|浏览(271)

我试图找出如何与attiny 85和我坚持一个问题。
我用rgb led条来表示位(1像素-1位,我发现很方便)。所有的代码是:

  1. #define F_CPU 8388608UL // 8MH
  2. #include <avr/io.h>
  3. #include <util/delay.h>
  4. #define LED PB4
  5. void setBitHigh(short int pin) {
  6. PORTB |= _BV(pin); // 1 tactDuration = 125*10-9sec
  7. asm("nop");
  8. asm("nop");
  9. asm("nop");
  10. asm("nop");
  11. asm("nop");
  12. PORTB &= ~_BV(pin); // 1 tactDuration
  13. asm("nop");
  14. asm("nop");
  15. }
  16. void setBitLow(short int pin) {
  17. PORTB |= _BV(pin); // 1 tactDuration
  18. asm("nop");
  19. asm("nop");
  20. PORTB &= ~_BV(pin); // 1 tactDuration
  21. asm("nop");
  22. asm("nop");
  23. asm("nop");
  24. asm("nop");
  25. asm("nop");
  26. }
  27. void trueByte(short int pin, short int intensity) {
  28. for (int i = 7; i >= 0; i--) {
  29. intensity & _BV(i) ? setBitHigh(pin) : setBitLow(pin);
  30. }
  31. }
  32. void falseByte(short int pin) {
  33. for (int i = 0; i < 8; i++) {
  34. setBitLow(pin);
  35. }
  36. }
  37. void setPixel(short int pin, short int r, short int g, short int b) {
  38. g > 0 ? trueByte(pin, g) : falseByte(pin);
  39. r > 0 ? trueByte(pin, r) : falseByte(pin);
  40. b > 0 ? trueByte(pin, b) : falseByte(pin);
  41. }
  42. void indicate(int data) {
  43. int tmp = data;
  44. for (int i = 0; i < 8; i++) {
  45. int intensity = (tmp & _BV(i)) > 0 ? 10 : 0;
  46. setPixel(LED, intensity, 0, 0);
  47. }
  48. }
  49. void blink(short int times) {
  50. for (short int i = 0; i < times; i++) {
  51. indicate(255);
  52. _delay_ms(200);
  53. indicate(0);
  54. _delay_ms(200);
  55. }
  56. }
  57. int main() {
  58. DDRB = 0x00 | _BV(LED);
  59. PORTB = 0x00;
  60. _delay_ms(1000);
  61. blink(3);
  62. _delay_ms(1000);
  63. while(true) {
  64. indicate(PINB);
  65. _delay_ms(50);
  66. }
  67. return 0;
  68. }

字符串
我希望PINB指示灯是0(在带没有光),但它是 Flink 的一些模式,我不能识别熟悉,但我看到它循环。只有前4位周期性地成为1
正如你所看到的,我使用PB 4只为LED. PB 0 -3是免费的(没有连接在所有)
我会很高兴任何想法或提示,泰。

siv3szwd

siv3szwd1#

此问题与sw无关,但与hw有关。这是未连接输入引脚的正常行为。未连接输入引脚的行为不可预测。输入必须由某个信号驱动,或用电阻将其拉至VCC或GND。上拉可以用内部电阻完成。在程序中,只需将PORTB设置为0x 0 F

  1. int main() {
  2. DDRB = 0x00 | _BV(LED);
  3. PORTB = 0x0F;

字符串
了解DS MCU端口的工作原理
如果您希望PB 0 -3为0,则必须使用外部电阻(10 k Ω或类似电阻)下拉至GND。

相关问题