안녕하세요
컴퓨터 keyboard를 통해 led제어 를 usart통신으로하려합니다
그런데 하이퍼터미널까지는 했는데요
제어가 되지않습니다
그냥 불만 들어옵니다..
어떤 포트에는 불이 들어오지 않지만 어떤 포트에는 불이 들어옵니다
무엇이 잘못된건지몰라서 질문 올립니다
혹시 소스보시고 아시는분은 빨리 답변좀 해주세요ㅠ
#include <avr/io.h>
#include <avr/interrupt.h>
#include "d:/WinAVR-20100110/avr/include/avr/iom128.h"
#include <stdio.h>
void tx0Char(char message);
void tx1Char(char message);
//int putchar(char c);
//int getchar(void);
void port_init(void);
void uart0_init(void);
void uart1_init(void);
void init_devices(void);
void delay(int n);
static int Putchar(char c, FILE *stream);
static int Putchar(char c, FILE *stream)
{
tx0Char(c);
tx1Char(c);
return 0;
}
/*
// printf 함수에서 사용할 putchar 에 UART0와 UART1으로
// 데이터를 보내도록 하였다.
int putchar(char c)
{
tx0Char(c);
tx1Char(c);
return c;
}*/
// UART0 을 이용한 출력
void tx0Char(char message)
{
while (((UCSR0A>>UDRE0)&0x01) == 0) ; // UDRE, data register empty
UDR0 = message;
}
// UART1 을 이용한 출력
void tx1Char(char message)
{
while (((UCSR1A>>UDRE1)&0x01) == 0) ; // UDRE, data register empty
UDR1 = message;
}
void USART_Transmit(unsigned char data)
{
/*wait for empty transmit buffer*/
while(!(UCSR0A & (1<<UDRE)));
/*put data into buffer, sends the data*/
UDR0 = data;
}
unsigned char USART_Receive(void)
{
int toggle=1;
char temp;
int shift = 0;
/*wait for data to be received*/
while(!(UCSR0A & (1<<RXC)));
/*Get and return received data from buffer*/
temp = UDR0 & 0xff;
return temp;
}
void port_init(void)
{
PORTA = 0x00;
DDRA = 0x83;//83
PORTB = 0x00;
DDRB = 0x00;
PORTC = 0x00; //m103 output only
DDRC = 0x00;
PORTD = 0x00;
DDRD = 0x00;
PORTE = 0x02;
DDRE = 0x00;
PORTF = 0x00;
DDRF = 0xff;//ff
PORTG = 0x00;
DDRG = 0x03;//03
}
//UART0 initialize
// desired baud rate: 9600
// actual: baud rate:9615 (0.2%)
// char size: 8 bit
// parity: Disabled
void uart0_init(void)
{
UCSR0B = 0x00; //disable while setting baud rate
UCSR0A = 0x00;
UCSR0C = 0x06;
UBRR0L = 0x67; //set baud rate lo
UBRR0H = 0x00; //set baud rate hi
UCSR0B = 0x18;
}
//UART1 initialize
// desired baud rate:9600
// actual baud rate:9615 (0.2%)
// char size: 8 bit
// parity: Disabled
void uart1_init(void)
{
UCSR1B = 0x00; //disable while setting baud rate
UCSR1A = 0x00;
UCSR1C = 0x06;
// UBRR1L = 0x2F; //set baud rate lo 7.3728 MHz
// UBRR1L = 0x47; //set baud rate lo 11.0592 Mhz
UBRR1L = 0x67; //set baud rate lo 16Mhz
UBRR1H = 0x00; //set baud rate hi
UCSR1B = 0x18;
}
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
cli(); //disable all interrupts
XDIV = 0x00; //xtal divider
XMCRA = 0x00; //external memory
port_init();
uart0_init(); // UART 0 초기화
uart1_init(); // UART 1 초기화
fdevopen(Putchar,0);
MCUCR = 0x00;
EICRA = 0x00; //extended ext ints
EICRB = 0x00; //extended ext ints
EIMSK = 0x00;
TIMSK = 0x00; //timer interrupt sources
ETIMSK = 0x00; //extended timer interrupt sources
sei(); //re-enable interrupts
//all peripherals are now initialized
}
// 시간 지연 함수
void delay(int n)
{
volatile int i,j;
for(i=1;i<n;i++)
{
for(j=1;j<600;j++);
}
}
int main (void)
{
init_devices();
volatile unsigned char receive = 0;
//insert your functional code here...
printf("\n\r\ntesting...\n\r");
printf("printf 테스트 프로그램...\n\r");
printf("1. LED_ON / OFF 제어(q,w)\n");
printf("2. 커텐온오프(a,s)\n");
printf("3. 보안 온오프(z,x)\n");
printf(" : ");
do
{
receive = USART_Receive();
switch (receive)
{
case 'q' :
PORTD = 0xff;
break;
case 'w' : /*js :빠져서추가*/
PORTD = 0x00;
break;
case 'a' :
PORTA = PORTA | 0x01; // 0000 0001
delay(100);
PORTA = PORTA & 0xfe;
break;
case 's' :
PORTA = PORTA | 0x02; // 0000 0010
delay(100);
PORTA = PORTA & 0xfd; // 1111 1101
break;
case 'z' :
// PORTA_temp = PORTA_temp | 0x04; // 0000 0100
PORTA = PORTA | 0x04;
break;
case 'x' :
PORTA = PORTA & 0xfc; // 1111 1011
break;
default : break;
}
}while(1);
return 0;
}
이소스를 한가히 분석하고 답을 드리긴 쉽지않겠죠.. 기본적으로 PC에서 보내는 직렬신호는 들어오는지..문자는 수신이 됬는지는 확인하셨나요?