51单片机与PC机的串口通信,含有CRC校验
}
}
/*串口1初始化*/
void uart1_init(void)
{
UCSR1C=_BV(UCSZ11)|_BV(UCSZ10);//11.0592 9600 UBRR1H=0x00;
UBRR1L=0x47;
UCSR1B=_BV(RXEN1)|_BV(RXCIE1);
UCSR1B|=_BV(TXEN1);
}
void uart1send(uchar a)
{
UCSR1A|=_BV(UDRE1);
while(!(UCSR1A&_BV(UDRE1)));
PORTD&=~_BV(5);
UDR1=p[a];
_delay_ms(5);
PORTD|=_BV(5);
}
void main(void)
{
uint m;
uart1_init();
DDRA=0xff;
DDRB=0xff;
DDRD=0x38;
PORTD=0xff;
sei();
while(1)
{
m=crc16(p,6);
PORTA=m>>8;
PORTB=m;
p[6]=m>>8;
p[7]=m;