我试图找出如何与attiny 85和我坚持一个问题。
我用rgb led条来表示位(1像素-1位,我发现很方便)。所有的代码是:
#define F_CPU 8388608UL // 8MH
#include <avr/io.h>
#include <util/delay.h>
#define LED PB4
void setBitHigh(short int pin) {
PORTB |= _BV(pin); // 1 tactDuration = 125*10-9sec
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
PORTB &= ~_BV(pin); // 1 tactDuration
asm("nop");
asm("nop");
}
void setBitLow(short int pin) {
PORTB |= _BV(pin); // 1 tactDuration
asm("nop");
asm("nop");
PORTB &= ~_BV(pin); // 1 tactDuration
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
}
void trueByte(short int pin, short int intensity) {
for (int i = 7; i >= 0; i--) {
intensity & _BV(i) ? setBitHigh(pin) : setBitLow(pin);
}
}
void falseByte(short int pin) {
for (int i = 0; i < 8; i++) {
setBitLow(pin);
}
}
void setPixel(short int pin, short int r, short int g, short int b) {
g > 0 ? trueByte(pin, g) : falseByte(pin);
r > 0 ? trueByte(pin, r) : falseByte(pin);
b > 0 ? trueByte(pin, b) : falseByte(pin);
}
void indicate(int data) {
int tmp = data;
for (int i = 0; i < 8; i++) {
int intensity = (tmp & _BV(i)) > 0 ? 10 : 0;
setPixel(LED, intensity, 0, 0);
}
}
void blink(short int times) {
for (short int i = 0; i < times; i++) {
indicate(255);
_delay_ms(200);
indicate(0);
_delay_ms(200);
}
}
int main() {
DDRB = 0x00 | _BV(LED);
PORTB = 0x00;
_delay_ms(1000);
blink(3);
_delay_ms(1000);
while(true) {
indicate(PINB);
_delay_ms(50);
}
return 0;
}
字符串
我希望PINB指示灯是0
(在带没有光),但它是 Flink 的一些模式,我不能识别熟悉,但我看到它循环。只有前4位周期性地成为1
。
正如你所看到的,我使用PB 4只为LED. PB 0 -3是免费的(没有连接在所有)
我会很高兴任何想法或提示,泰。
1条答案
按热度按时间siv3szwd1#
此问题与sw无关,但与hw有关。这是未连接输入引脚的正常行为。未连接输入引脚的行为不可预测。输入必须由某个信号驱动,或用电阻将其拉至VCC或GND。上拉可以用内部电阻完成。在程序中,只需将PORTB设置为0x 0 F
字符串
了解DS MCU端口的工作原理
如果您希望PB 0 -3为0,则必须使用外部电阻(10 k Ω或类似电阻)下拉至GND。