uart._c
来自「AVR系列被广泛应用的单片机MEGA8开发(原理图)及例程」· _C 代码 · 共 140 行
_C
140 行
#include <iom8v.h>
#include <macros.h>
#include "Uart.h"
void uart0_RX_Enable(void)
{
UCSRB = 0xE8; //disable while setting baud rate
}
void uart0_RX_Disable(void)
{
UCSRB = 0xF8; //disable while setting baud rate
}
void uart0_init(void)
{
UCSRB = 0x00; //disable while setting baud rate
UCSRA = 0x00;
UCSRC = 0x86;
UBRRL = 0x0B; //set baud rate lo
UBRRH = 0x00; //set baud rate hi
UCSRB = 0xF8;
}
#pragma interrupt_handler uart0_rx_isr:12
void uart0_rx_isr(void)
{ unsigned char tempdata;
tempdata = UDR; /* read the received data */
PutChar( tempdata);
// PutChar(0x0a);
// PutChar(0x0d);
PutStr("富友勤 ATMEGA8 DEMO");
PutStr("Start IAP");
if(tempdata=='I')
{ CLI();
UCSRB = 0x00;
asm("JMP 0x1C00") ;
}
}
#pragma interrupt_handler uart0_udre_isr:13
void uart0_udre_isr(void)
{
//character transferred to shift register so UDR is now empty
}
#pragma interrupt_handler uart0_tx_isr:14
void uart0_tx_isr(void)
{
}
int PutStr(char *s)
{
while(*s)
{while ( !(UCSRA & (1<<UDRE)) )
; /* wait for empty transmit buffer */
UDR = *s; /* start transmittion */
s++;
}
while ( !(UCSRA & (1<<UDRE)) )
; /* wait for empty transmit buffer */
UDR = 0x0a; /* start transmittion */
//while ( !(UCSRA & (1<<UDRE)) )
; /* wait for empty transmit buffer */
//UDR = 0x0d; /* start transmittion */
return 1;
}
void PutInt(unsigned int intdata)
{
unsigned char i,tempdata,tempdata1,tempdata2,tempdata3,tempdata4,tempdata5;
unsigned char firstzero=false;
for(i=0;i<5;i++)
{
tempdata=intdata%10;
intdata=intdata/10;
switch(i)
{
case 0:
tempdata1=tempdata;
break;
case 1:
tempdata2=tempdata;
break;
case 2:
tempdata3=tempdata;
break;
case 3:
tempdata4=tempdata;
break;
case 4
tempdata5=tempdata;
break;
default:
break;
}
}
if(tempdata5!=0)
{
PutChar(tempdata5+48);
firstzero=true;
}
if((tempdata4!=0)||firstzero)
{
PutChar(tempdata4+48);
firstzero=true;
}
if((tempdata3!=0)||firstzero)
{
PutChar(tempdata3+48);
firstzero=true;
}
if((tempdata2!=0)||firstzero)
{
PutChar(tempdata2+48);
}
PutChar(tempdata1+48);
PutChar(0x0a);
//PutChar(0x0d);
}
void PutChar(unsigned char c)
{
while ( !(UCSRA & (1<<UDRE)) )
;
UDR = c;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?