标题:
atmega128单片机双串口使用
[打印本页]
作者:
chingha
时间:
2019-12-14 15:14
标题:
atmega128单片机双串口使用
/ICC-AVR application builder
// Target : M128
// Crystal: 3.6864Mhz
#include
#include
//定义相关的数据变量
unsigned int DATA_NUM1,DATA_NUM2; //定义接收的数据个数
unsigned char BIT_SEND; //定义发送标志位 =0 表示需要发送 =1 表示不需要发送
unsigned char DATA1[400];
unsigned char DATA2[330];
void port_init(void)
{
PORTA = 0x00;
DDRA = 0x00;
PORTB = 0x38;
DDRB = 0x38;
PORTC = 0x00; //m103 output only
DDRC = 0x00;
PORTD = 0x00;
DDRD = 0x00;
PORTE = 0x00;
DDRE = 0x00;
PORTF = 0x00;
DDRF = 0x00;
PORTG = 0x00;
DDRG = 0x00;
}
//UART0 initialize
// desired baud rate: 9600
// actual: baud rate:9600 (0.0%)
// char size: 8 bit
// parity: Disabled
void uart0_init(void)
{
UCSR0B = 0x00; //disable while setting baud rate
UCSR0A = 0x00;
UCSR0C = 0x06;
UBRR0L = 0x17; //set baud rate lo
UBRR0H = 0x00; //set baud rate hi
UCSR0B = 0xF8;
}
#pragma interrupt_handler uart0_rx_isr:19
void uart0_rx_isr(void)
{
//uart has received a character in UDR
unsigned char i,j;;
DATA1[DATA_NUM1]=UDR0;
DATA_NUM1+=1;
}
#pragma interrupt_handler uart0_udre_isr:20
void uart0_udre_isr(void)
{
//character transferred to shift register so UDR is now empty
}
#pragma interrupt_handler uart0_tx_isr:21
void uart0_tx_isr(void)
{
//character has been transmitted
}
//UART1 initialize
// desired baud rate:4800
// actual baud rate:4800 (0.0%)
// char size: 7 bit
// parity: Disabled
//void uart1_init(void)
//{
// UCSR1B = 0x00; //disable while setting baud rate
// UCSR1A = 0x00;
// UCSR1C = 0x0C;
// UBRR1L = 0x2F; //set baud rate lo
// UBRR1H = 0x00; //set baud rate hi
// UCSR1B = 0xF8;
//}
//UART1 initialize
// desired baud rate:4800
// actual baud rate:4800 (0.0%)
// char size: 7 bit
// parity: Even
/*
void uart1_init(void)
{
UCSR1B = 0x00; //disable while setting baud rate
UCSR1A = 0x00;
UCSR1C = 0x2C;
UBRR1L = 0x5F; //set baud rate lo
UBRR1H = 0x00; //set baud rate hi
UCSR1B = 0x68;
}
*/
//UART1 initialize
// desired baud rate:4800
// actual baud rate:4800 (0.0%)
// char size: 7 bit
// parity: Even
void uart1_init(void)
{
UCSR1B = 0x00; //disable while setting baud rate
UCSR1A = 0x00;
UCSR1C = 0x2C;
UBRR1L = 0x2F; //set baud rate lo
UBRR1H = 0x00; //set baud rate hi
UCSR1B = 0xf8;
}
#pragma interrupt_handler uart1_rx_isr:31
void uart1_rx_isr(void)
{
//uart has received a character in UDR
}
#pragma interrupt_handler uart1_udre_isr:32
void uart1_udre_isr(void)
{
//character transferred to shift register so UDR is now empty
}
#pragma interrupt_handler uart1_tx_isr:33
void uart1_tx_isr(void)
{
//character has been transmitted
}
//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();
uart0_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 USART0_Transmit(unsigned char data)
{
//等待发送缓冲器为空
while(!(UCSR0A&(1<<udre0)));
UDR0=data;
}
void USART1_Transmit(unsigned char data)
{
//等待发送缓冲器为空
while(!(UCSR1A&(1<<udre1)));
UDR1=data;
}
//**********************************************
//分析数据 出现数据测量失误的情况 就发这个数据
//**********************************************
void DATA_fenxi1()
{
DATA2[0]=0X7F; //起始位
DATA2[1]=0X02;
DATA2[2]=0X0D;
DATA2[3]=0X0A;
}</udre1)));
</udre0)));
//*************************************
//分析完整的数据
//*************************************
void DATA_fenxi()
{
DATA2[0]=0X7F; //起始位
DATA2[1]=0X02;
DATA2[2]=0X0D;
DATA2[3]=0X0A;
}
//*************************************
//延时子程序 延时0.5s
//*************************************
void DEL_05S()
{
unsigned char i,j;
for(i=0;i<250;i++)
{
for(j=0;j<200;j++)
{;}
}
}
//主程序 用来控制相关的协议变化
void main()
{
unsigned int i;
DATA_NUM1=0X00;
DATA_NUM2=0x00;
init_devices();
///////////////////////
//for(i=0;i<315;i++)
// {
// DATA1[i]=0x32;
// }
// DATA_fenxi();
///////////////////////
START:
DATA_NUM1=0;
DEL_05S();
DEL_05S();
PORTB=0B11011110; //单片机无问题红灯亮
DEL_05S();
DEL_05S();
/////////////////////////////////
// for(i=0;i<315;i++)
// {
// PORTB=0B11010110; //发送数据 红灯和绿灯亮
// USART1_Transmit(DATA2[i]);
// }
// goto START;
/////////////////////////////////
if(DATA_NUM1>0){
while(DATA_NUM1!=363)
{
unsigned char i;
PORTB=0B11001110; //接受数据状态 红灯亮 黄灯亮
for(i=0;i<80;i++)
{
DEL_05S();
if(DATA_NUM1>=362)
{goto FENXI;}
}
if(DATA_NUM1>=362)
{goto FENXI;}
else
{
if(DATA_NUM1<=300)
{goto FENXI1;}
}
}
FENXI:
DEL_05S();
DEL_05S();
DATA_NUM1=0x00;
DATA_fenxi();
//USART1_Transmit(DATA1[0]);
for(i=0;i<315;i++)
{
PORTB=0B11010110; //发送数据 红灯和绿灯亮
USART1_Transmit(DATA2[i]);
}
goto START;
goto START;
goto START;
FENXI1:
DATA_NUM1=0X00;
DATA_fenxi1();
for(i=0;i<315;i++)
{
PORTB=0B11010110; //发送数据 红灯和绿灯亮
USART1_Transmit(DATA2[i]);
}
goto START;
goto START;
goto START;
}
goto START;
}
复制代码
atmega 128 双串口使用.docx
2019-12-14 15:13 上传
点击文件名下载附件
下载积分: 黑币 -5
17.63 KB, 下载次数: 7, 下载积分: 黑币 -5
atmega 128 双串口使用
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1