mega8串口问题
#include <avr/io.h>#include <avr/interrupt.h> //晶振频率 #define fosc 8000000UL //波特率 #define baud 9600 void Init_Usart(void) { UCSRB=(1<<RXEN)|(1<<TXEN);//允许收发,打开接收中断 UBRRL=(fosc/16/(baud+1))%256;//设置波特率寄存器 UBRRH=(fosc/16/(baud+1))/256; UCSRC=(1<<URSEL)|(1<<UCSZ1)|(1<<UCSZ0);//8位数据+1位STOP //UCSRB = 0x98; //允许接收和发送,使能接收中断} void Send_Char(char data) { //查询方式发送 while(!(UCSRA & (1 << UDRE))); UDR = data; } void delay(unsigned int uiD){ for (; uiD; uiD--);}void poweroff(){ if(!(PINC&0x10)) { delay(30); if(!(PINC&0x10)) {while(!(PINC&0x10));PORTC|=(1<<PC3);} }} //主函数 int main(void) { DDRD |= 0x02; PORTD |= 0x00; DDRC|=(1<<DDC3); PORTC&=~(1<<PC3); PORTC|=(1<<PC4); DDRC&=~(1<<DDC4);//开关机电平检测 DDRD|= (1<<DDD5); PORTD&= ~(1<<PD5); Init_Usart(); char i=0,j=0; //总中断使能 sei(); delay(400); //发送一个字符a Send_Char('a'); while(!(PINC&0x10));//等待开机键松开 for (j=0;j<10;j++) { Send_Char('f'); } while(1) { if (i++==1) { poweroff(); } } return 0; }