串口查询输入输出.c

来自「AVR单片机通用异步收发器UART操作」· C语言 代码 · 共 68 行

C
68
字号

// Target : M162
// Crystal: 8.0000Mhz

#include <iom162v.h>
#include <macros.h>
unsigned char DATA;

//字符输出函数
void u_putchar(unsigned char c)
{while(!(UCSR0A&(1<<UDRE0)));
 UDR0=c;
 }
 void put_s(unsigned char *ptr)
{ while(*ptr)
  {u_putchar(*ptr++);
  }
   u_putchar(0x0D);
   u_putchar(0x0A);   //输出字符串 
}

 //字符输入函数
 unsigned char u_getchar(void)
{ while(!(UCSR0A&(1<<RXC0)));

  return UDR0;
 }
 void uart0_init(void)
{
 UCSR0B = 0x00; //disable while setting baud rate
 UCSR0A = 0x00; //disable while setting baud rate
 UBRR0L  =0x33; //set baud rate
 UBRR0H = 0x00;
 UCSR0C = BIT(URSEL0) | 0x06;
 UCSR0A = 0x00; //enable
 UCSR0B = (1<<RXEN0)|(1<<TXEN0) ;//enable
}
 void init_devices(void)
{
 //stop errant interrupts until set up
 CLI(); //disable all interrupts


 uart0_init();

 MCUCR= 0x00; 
 EMCUCR = 0x00;

 TIMSK= 0x00; //timer interrupt sources
 ETIMSK=0x00;
 GICR= 0x00;
 PCMSK0=0x00;
 PCMSK1=0x00;
 SEI(); //re-enable interrupts
 //all peripherals are now initialized
}
void main(void)
 { unsigned char i;
   init_devices();
   while(1)
   {
    i=u_getchar();
	u_putchar(i);
	u_putchar(0x05);
	u_putchar(0xaa);
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?