61.
62.
63. //-------------------------------------------------------
64. //串口初始化;
65. voidUSART_initial(void)
66. {
67. UBRRH=0X00;
68. UBRRL=51;//f=8MHz;设置波特率:9600:51;19200:25;
69. UCSRB=(1<<RXEN)|(1<<TXEN);//接收器与发送器使能;
70. UCSRC=(1<<URSEL)|(1<<UCSZ0)|(1<<UCSZ1);//设置帧格式:8个数据位,1
个停止位;
71.
72. UCSRB|=(1<<RXCIE);//USART 接收中断使能
73. }
74.
75.
76. //-------------------------------------------------------
77. //串口发送数据;
78. voidUSART_Transmit(unsignedchardata)
79. {
80. while(!(UCSRA&(1<<UDRE)));//等待发送缓冲器为空;
81. UDR=data;//将数据放入缓冲器,发送数据;
82. }
83.
84.
85. //-------------------------------------------------------
86. //串口接收数据中断,确定数据输出的状态;
87. #pragmainterrupt_handlerUSART_Receive_Int:12
88. staticcharUSART_State;
89. voidUSART_Receive_Int(void)
90. {
91. USART_State=UDR;//USART_Receive();
92. }