本帖最后由 donkey89 于 2009-9-4 22:59 编辑
我用的是M48,用在485通信上面
串口的接收完毕中断代码:
ISR(USART_RX_vect)
{
rcc = UDR0;
ui = 1;
rcc = 'k'; //UDR0;
}
uint8_t uart_getc(void)
{
uint8_t vr;
cbi(RD_PORT, RD_PIN);
vr = rcc;
if (ui)
rcc = 0;
return vr;
}
int main(void)
{
uint8_t vc;
//cli();
rcc = 0; //'t';
ui = 0;
sbi(DDRC, RD_PIN);
sbi(RD_PORT, RD_PIN);
//uart 初始化
UBRR0H = 0;
UBRR0L = 25; //9600 baud 6MHz:38 4MHz:25
UCSR0B = _BV(RXEN0) | _BV(TXEN0) | _BV(RXCIE0);
UCSR0C = (1 << UCSZ00) | (1 << UCSZ01); //8位数据,一个停止位
uart_putc('a');
uart_putc('b');
uart_putc('c');
uart_putc('d');
uart_putc('e');
uart_putc('f');
uart_putc('g');
cbi(RD_PORT, RD_PIN);
sei();
while (1)
{
//cbi(RD_PORT,RD_PIN);
vc = uart_getc();
if (vc == 'c')
uart_putc('w');
else if (vc == 'b')
uart_putc('y');
else if (vc == 'q')
break;
else if (ui)
uart_putc(vc);
}
sbi(RD_PORT, RD_PIN);
} |