5 개의 LED 세트가 앞뒤로 "기사 라이더 (Night Rider)"스타일로 "바운스 (bounce)"하는 코드를 작성하고 있습니다. PORTB 레지스터에 글을 쓸 때, LED를 꽂은 방식이 1, 2, 4, 8, 16 일 것이라고 인식했습니다. LED가 켜지면 LED가 켜집니다. 그래서 나는 pow
함수를 사용하여 레지스터를 2의 값으로 설정하여 LED 번호 (0, 1, 2, 3, 4)로 루핑하는 것으로 생각했습니다. 그래도 제대로 작동하지 않습니다.C 및 AVR pow 기능을 LED에 적용
#include <avr/io.h>
#include <inttypes.h>
#include <math.h>
void delay(uint16_t x);
//void buttons(int b1, int b2);
int led = 0;
int inc = 1;
unsigned int ledpow = 0;
int main(void)
{
DDRB |= (1<<PORTB0); //Set PORTB0 (pin 8) as an output
DDRB |= (1<<PORTB1); //Set PORTB1 (pin 9) as an output
DDRB |= (1<<PORTB2); //Set PORTB2 (pin 10) as an output
DDRB |= (1<<PORTB3); //Set PORTB3 (pin 11) as an output
DDRB |= (1<<PORTB4); //Set PORTB4 (pin 12) as an output
DDRD &= ~(1<<PORTD3); //Set PORTD3 (pin 3) as an input
DDRD &= ~(1<<PORTD4); //Set PORTD4 (pin 4) as an input
PORTB = 0; //Disable Pull-up resistors for PORTB
PORTD = 0; //Disable Pull-up resistors for PORTD
while(1)
{
while((PIND & (1<<PORTD3)) != 0) {
//Do nothing, just pause the program
}
ledpow = pow(2,led);
PORTB = ledpow;
led = led + inc;
if ((led == 4) || (led==0)) {
inc = -inc;
}
if((PIND & (1<<PORTD4)) != 0) {
delay(50);
}
else {
delay(100);
}
}
}
void delay(uint16_t x)
{
uint16_t i,j;
for(i=0;i<x;i++)
for(j=0;j<1000;j++)
;
return;
}
왜 이것이 올바르게 작동하지 않습니까? 나는 switch/case 문으로 작업을했다. 그리고 나는 pow
함수가 PORTB = pow(2,0);
과 다른 변수 인 "led"변수를 사용하여 작동한다는 것을 테스트했습니다. 그것은 올바르게 작동했습니다.