⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 uart.txt

📁 基于avr 的mega128/64 的串口通讯程序
💻 TXT
字号:
//ICC-AVR application builder : 2009-3-24 15:18:21
// Target : M128
// Crystal: 7.3728Mhz

#include <iom128v.h>
#include <macros.h>

unsigned char huangwei[]={0xAA, 0x53 ,0x01, 0x4C, 0x01 ,0x73 ,0x48, 0x55 ,0x41 ,0x4E ,0x47, 0x20 ,0x57 ,0x45 ,0x49 ,0xCC, 0x33 ,0xC3, 0x3C,0x00};
void port_init(void)
{
 PORTA = 0x00;
 DDRA  = 0x00;
 PORTB = 0x00;
 DDRB  = 0x00;
 PORTC = 0x00; //m103 output only
 DDRC  = 0x00;
 PORTD = 0x00;
 DDRD  = 0x00;
 PORTE = 0x00;
 DDRE  = 0x00;
 PORTF = 0x00;
 DDRF  = 0x00;
 PORTG = 0x00;
 DDRG  = 0x00;
}

//UART1 initialize
// desired baud rate:115200
// actual baud rate:115200 (0.0%)
// char size: 8 bit
// parity: Disabled
void uart1_init(void)
{
 UCSR1B = 0x00; //disable while setting baud rate
 UCSR1A = 0x00;
 UCSR1C = 0x06;
 UBRR1L = 0x03; //set baud rate lo
 UBRR1H = 0x00; //set baud rate hi
 UCSR1B = 0x08;
}

//call this routine to initialize all peripherals
void init_devices(void)
{
 //stop errant interrupts until set up
 CLI(); //disable all interrupts
 XDIV  = 0x00; //xtal divider
 XMCRA = 0x00; //external memory
 port_init();
 uart1_init();

 MCUCR = 0x00;
 EICRA = 0x00; //extended ext ints
 EICRB = 0x00; //extended ext ints
 EIMSK = 0x00;
 TIMSK = 0x00; //timer interrupt sources
 ETIMSK = 0x00; //extended timer interrupt sources
 SEI(); //re-enable interrupts
 //all peripherals are now initialized
}
//
void delay(int j)
{int k, i; 
for(i=0;i<=j;i++);
   for(k=0;k<=2000;k++);
   }


void sendchar(unsigned char i)
{
 while (!(UCSR1A&(1<<UDRE1)));
 UDR1=i;
 while(!(UCSR1A&(1<<TXC1)));
 UCSR1A|=BIT(6);
}

void sendword(unsigned int m)
{
 while(!(UCSR1A&(1<<UDRE1)));
 UDR1=(unsigned char)m;
 while(!(UCSR1A&(1<<TXC1)));
 UCSR1A|=BIT(6);
 UDR1=(unsigned char)(m>>8);
  while(!(UCSR1A&(1<<TXC1)));
 UCSR1A|=BIT(6);
 return;
}

void sendstr(unsigned char *p)
{
 while(*p)
 {
 sendchar(*p);
 p++;
 } 
}
void main(void)
{
 init_devices();
 //insert your functional code here...
 DDRD|=BIT(3);
 PORTD|=BIT(3);
/* sendchar(0xaa);
 sendchar(0x53);
  sendchar(0x01);
 sendchar(0x4c);
 sendchar(0x01);
sendchar(0x73);
  sendchar(0x48);
 sendchar(0x55);
  sendchar(0x41);
 sendchar(0x4e);
 sendchar(0x47);
 sendchar(0x20);
  sendchar(0x57);
 sendchar(0x45);
 sendchar(0x49);
 sendchar(0xcc);
 sendchar(0x33);
  sendchar(0xc3);
sendchar(0x3c);*/
sendstr(huangwei);

 
 
}


⌨️ 快捷键说明

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