현재 8 개의 LED를 제어하기 위해 AVR ATMEGA328 마이크로 컨트롤러, 특히 USART 주변 장치를 사용해야하는 프로젝트를 진행 중입니다. 우리는 LED를 켜고 끄고 깜박이는 명령을 마이크로 컨트롤러에 보내야합니다. 나는 C에서 일을 할 것이라고 생각하는 프로그램을 만들었지 만, 누군가가 그것을보고 내가 가진 실수를 고치도록 도와 주길 바란다. 귀하의 도움을 크게 극명하게 될 것입니다!AVR USART 프로그래밍
* P. 명령 배열의 각 명령은 LED 배열의 해당 LED 상태와 연관됩니다. LED는 마이크로 컨트롤러의 PORTB에 연결됩니다.
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
/* Arrays that contain all input commands and LED states */
const char *commands[] = {"ON0","ON1","ON2","ON3","ON4","ON5","ON6","ON7","ON8","OFF0","OFF1","OFF2","OFF3","OFF4","OFF5","OFF6","OFF7","OFF8","BLINK0","BLINK1","BLINK2","BLINK3","BLINK4","BLINK5","BLINK6","BLINK7","BLINK8","STOP"\0}
int LEDs[28] = {0X01,0X02,0X04,0X08,0X10,0X20,0X40,0X80,0XFF,0XFE,0XFD,0XFB,0XF7,0XEF,0XDF,0XBF,0X7F,0,0X01,0X02,0X04,0X08,0X10,0X20,0X40,0X80,0XFF,0}
int i;
int j;
int Blinky(int j); // Function to execute blinking commands where j is the argument
{
PORTB = LEDs[j];
_delay_ms[250 + (j-18) * 50]; /* Calculates blinking delay times */
PORTB = 0;
_delay_ms[250 + (j-18) * 50];
}
int main(void)
{
DDRB=0XFF; // PORTB is set to output
DDRD=0X02; // Turns on the transmit pin for the USART in PORTD
/* Setup USART for 9600,N,8,1 */
USCR0B = 0X18;
USCR0C = 0X06;
UBRR0 = 51;
sei(); // Enable Global Interrupts
char input;
if(UCSR0A & 0X80) /* Reads data from the USART and assigns the contents to the character input */
input = UDR0;
j=28;
char cmd;
cmd = *commands[i];
for(i=0; i<28; i++)
{
if(input==cmd) /* If the contents of UDR0 are equal to one of the commands */
j = i;
}
while(1)
{
if(j<18)
PORTB=LEDs[j]; // Executes the "ON" and "OFF" commands
else if(j<27)
Blinky(j); // Executes the blinking command by calling the Blinky function
else if(j=27)
PORTB=0; // Executes the "STOP" command
else
PORTB=0; // Accounts for typing errors
}
return(0);
}
게시하기 전에 코드를 테스트해야합니다. 작동하지 않는다면 디버깅을 도와 줄 수 있도록 모든 세부 정보를 제공하십시오. 작동중인 경우 개선 방법에 대한 제안은 https://codereview.stackexchange.com에 게시 할 수 있습니다. –