rs232로 usart 통신 코딩중인데
32bit 입력을 (8비트 4개) UDR 레지스터로 들어와서 RX_DATA에 저장한 뒤
다시 8비트 4개를 32비트로 가공해서 TX_DATA에 저장후 UDR레지스터로 내보내려는 코딩중인데
잘 안되네요..
#include <mega2560.h>
#include <stdio.h>
#include <interrupt.h>
unsigned char rx_flag = 0;
unsigned char rx_cnt = 0;
unsigned long int r = 0;
unsigned long int t = 0;
interrupt [USART1_RXC] void usart1_rx(void)
{
unsigned char r0;
r0 = UDR1;
r<<=8;
r|=r0;
rx_cnt++;
if(rx_cnt > 4)
{
rx_cnt = 0;
t = r;
}
}
void tx_data(unsigned char t)
{
while(!(UCSR1A&0x20));
UDR1 = t;
}
void main()
{
unsigned char t0;
DDRD = 0xFB;
UCSR1A = 0x00;
UCSR1B = 0x98;
UCSR1C = 0x06;
UBRR1H = 0;
UBRR1L = 51;
#asm("sei");
while(1)
{
if(rx_flag)
{
rx_flag = 0;
t0 = t>>24;
tx_data(t0);
t0 = t>>16;
tx_data(t0);
t0 = t>>8;
tx_data(t0);
t0 = t;
tx_data(t0);
}
}
}
부족한 부분과 수정할 부분 지적좀 부탁드려요ㅠㅠ 인터럽트로 작성하려합니다..!
안녕하세요.
먼저 RX인터럽트가 잘 들어 오는지 먼저 확인하는게 좋을거 같습니다.
아래의 형식으로 받은 데이터를 바로 돌려 보내는 게 잘 되는지부터 확인해 보시고
4바이트 작업을 해보세요.~~