Atmega128USART测试代码

来源:互联网 发布:java银行项目需求分析 编辑:程序博客网 时间:2024/06/06 14:04

/**************************************
UART转USB,UART通信程序
CPU:ATmega128
晶振:11.0592
编写:fjh
2007-9-20
************************************/
#include <iom128v.h>
#include <macros.h>

#define uchar unsigned char
#define uint  unsigned int

void uart0_init(void);
void uart1_init(void);
void uart0_send(uchar c);
void uart1_send(uchar c);
uchar Databuf[60];
volatile uchar count=0;
volatile uchar flag=1;
/******************端口初始化***************************/
void port_init(void)
{ //端口设置
 PORTA = 0x00;//LCD口,总线低八位地址
 DDRA  = 0x00;
 
 PORTB = 0x00;//SPI接口
 DDRB  = 0xff;
 
 PORTC = 0x00; //总线高八位地址
 DDRC  = 0x00;
 
 PORTD = 0x00;//USART1接口
 DDRD  = 0x00;
 
 PORTE = 0x0f;//键盘口
 DDRE  = 0xf0;
 
 PORTF = 0x00;//LED口
 DDRF  = 0xff;
 
 PORTG = 0xff;
 DDRG  = 0xff;
}
/***************串口0初始化********************/
void uart0_init(void)
{
 UCSR0B = 0x00; //disable while setting baud rate
 UCSR0A = 0x00;
 UCSR0C = 0x06;
 UBRR0L = 0x47; //baud rate=9600
 UBRR0H = 0x00; //
 UCSR0B = 0x98;
}
/********************************************/
/***************串口1初始化********************/
void uart1_init(void)
{
 UCSR1B = 0x00; //disable while setting baud rate
 UCSR1A = 0x00;
 UCSR1C = 0x06;
 UBRR1L = 0x47; //baud rate=9600
 UBRR1H = 0x00; //
 UCSR1B = 0x98;
}
/********************************************/
/**************发送采用查询方式**************/
void uart0_send(uchar c)
{
  while( !(UCSR0A & (1<<UDRE0)) );
 UDR0=c;
}
/********************************************/
/**************发送采用查询方式**************/
void uart1_send(uchar c)
{
  while( !(UCSR1A & (1<<UDRE1)) );
 UDR1=c;
}
/********************************************/
/**************接收采用中断******************/
#pragma interrupt_handler uart0_rx_isr:19
void uart0_rx_isr(void)
{
 uchar temp=0;
 temp=UDR0;
 uart0_send(temp);
}
/********************************************/
/**************接收采用中断******************/
#pragma interrupt_handler uart1_rx_isr:31
void uart1_rx_isr(void)
{
 uchar temp=0;
 
 temp=UDR1;
      uart1_send(temp);
}
/********************************************/
void main()
{
  uchar i=0;
  CLI();
  port_init();
  uart0_init();
  uart1_init();
  for(i=0;i<60;i++)
   Databuf[i]=0;
  SEI();
  while(1)
  {
   }
}

原创粉丝点击