ATMEGA328P Arduino Uno:通过AVR编程调整LED亮度 int-led_-pin=9; int占空比=63//以%计 内部频率=16000//以赫兹为单位 int freq_clk=16000000//以赫兹为单位 int预分频器=1; 无效设置(){ //将端口B引脚(D9)设置为OC1A的输出,以覆盖正常端口功能 DDRB=DDRB|(1
ATMEGA328P Arduino Uno:通过AVR编程调整LED亮度 int-led_-pin=9; int占空比=63//以%计 内部频率=16000//以赫兹为单位 int freq_clk=16000000//以赫兹为单位 int预分频器=1; 无效设置(){ //将端口B引脚(D9)设置为OC1A的输出,以覆盖正常端口功能 DDRB=DDRB|(1,arduino,avr,Arduino,Avr,DDRB是一个8位寄存器,其中没有第9位。 您将led\u pin变量设置为9,因此操作DDRB=DDRB|(1如果您想使用AVR端口,您不能使用Arduino pin码。反之亦然。1我理解,但在我做的其他一些示例中,我能够正常初始化pin码,而不是访问端口本身。但无论如何,感谢您的反馈谢谢您的反馈。我想我们可以同时使用Arduino pin码和AVR寄存器位可以互换。但您的解释清楚地表明,在AVR编程中,只有寄存器位可用于编程端口引脚。 int led_pin = 9; int duty_cy
DDRB
是一个8位寄存器,其中没有第9位。
您将
led\u pin
变量设置为9,因此操作DDRB=DDRB|(1如果您想使用AVR端口,您不能使用Arduino pin码。反之亦然。1我理解,但在我做的其他一些示例中,我能够正常初始化pin码,而不是访问端口本身。但无论如何,感谢您的反馈谢谢您的反馈。我想我们可以同时使用Arduino pin码和AVR寄存器位可以互换。但您的解释清楚地表明,在AVR编程中,只有寄存器位可用于编程端口引脚。
int led_pin = 9;
int duty_cycle = 63; //in %
int freq_OC = 16000; //in Hz
int freq_clk = 16000000; //in Hz
int prescaler = 1;
void setup() {
//setting the port B pin (D9) as output for OC1A to override the normal port functionality
DDRB = DDRB|(1 << led_pin);
//clearing the counter/timer registers
TCCR1A = 0;
TCCR1B = 0;
//setting the prescaler 1
TCCR1B = TCCR1B|(1 << CS10);
//setting the mode 14 fast PWM
TCCR1B = TCCR1B|((1 << WGM13)|(1 << WGM12));
TCCR1A = TCCR1A|(1 << WGM11);
//setting the fast PWM in non-inverting mode
TCCR1A = TCCR1A|(1 << COM1A1);
ICR1 = (freq_clk/(freq_OC*prescaler)) - 1;
OCR1A = ICR1/(100/duty_cycle);
}
void loop() {
}
DDRB = DDRB|(1 << PB1);