标题:
AVR串口通信程序RS232头文件
[打印本页]
作者:
yuchengze
时间:
2016-10-20 14:53
标题:
AVR串口通信程序RS232头文件
下面是ATMEGA128的
头文件
/************************这是串口0******************************/
/*串口0的初始化*/
void Uart0_init(void)
{
UCSR0B = 0x00; //禁止发送和接收
UCSR0A = 0x02; //倍速异步模式USX0=1
UBRR0L=(Crystal/8/Baud0-1)%256; //根据数据手册的计算法
UBRR0H=(Crystal/8/Baud0-1)/256; //如果不是倍速则(Crystal/16/Baud0-1)
UCSR0B=(1<<RXEN0)|(1<<TXEN0)|(1<<RXCIE0); 发送接收使能,接收中断使能
UCSR0C=(1<<UCSZ01)|(1<<UCSZ00); 8位数据,ucsz02,01,00共同确定
DDRE|=BIT(1); //设置发送口为输出
}</UCSZ01)|(1<</RXEN0)|(1<<TXEN0)|(1<
/*串口0单字符发送,查询方式*/
void Uart0_sendbyte(uchar i)
{
while (!(UCSR0A & (1<<UDRE0))); *="" 等待发送缓冲器为空*=""
UDR0 = i; /* 要发送的数据*/
}
/*串口0字符串发送*/
void Uart0_sendstring(uchar *p)
{
while(*p)
{
Uart0_sendbyte(*p);
p++;
}
}
/*串口0接收中断*/
#pragma
interrupt
_handler Uart0_rx:19
void Uart0_rx(void)
{
UCSR0B&=~BIT(RXCIE0);//先关接收中断
R_dat0=UDR0; //用变量接收串口1收到的数据
Flag0=1;
UCSR0B|=BIT(RXCIE0);//重开接收中断
}
/***************************这是串口1******************************/
/*串口1的初始化*/
void Uart1_init(void)
{
UCSR1B = 0x00; //禁止发送和接收
UCSR1A = 0x02; //倍速异步模式USX0=1
UBRR1L=(Crystal/8/Baud1-1)%256; //根据数据手册的计算法
UBRR1H=(Crystal/8/Baud1-1)/256; //如果不是倍速则(Crystal/16/Baud1-1)
UCSR1B=(1<<RXEN1)|(1<<TXEN1)|(1<<RXCIE1); 发送接收使能,接收中断使能
UCSR1C=(1<<UCSZ11)|(1<<UCSZ10); 8位数据,ucsz12,11,10共同确定
DDRD|=BIT(3); //设置发送口为输出
}</UCSZ11)|(1<</RXEN1)|(1<<TXEN1)|(1<
/*串口0单字符发送,查询方式*/
void Uart1_sendbyte(uchar i)
{
while (!(UCSR1A & (1<<UDRE1))); *="" 等待发送缓冲器为空*=""
UDR1 = i; /* 要发送的数据*/
}
/*串口1字符串发送*/
void Uart1_sendstring(uchar *p)
{
while(*p)
{
Uart1_sendbyte(*p);
p++;
}
}
/*串口1接收中断*/
#pragma interrupt_handler Uart1_rx:31
void Uart1_rx(void)
{
UCSR1B&=~BIT(RXCIE1);//先关接收中断
R_dat1=UDR1; //用变量接收串口1收到的数据
Flag1=1;
UCSR1B|=BIT(RXCIE1);//重开接收中断
}
以下是主机部份
#include
#include
#define uchar unsigned char
#define uint unsigned int
#include "delay.h"
#include "lcd.h"
void port_init(void);
//SPI initialize
// clock rate: 1843199hz
void spi_init(void)
{
SPCR = 0x74; //setup SPI
SPSR = 0x00; //setup SPI
DDRB=0X07;
PORTB|=0X08;
}
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
spi_init();
SEI(); //re-enable interrupts
}
uchar SPI_Transmit(char cData)
{
/* 启动数据传输 */
SPDR = cData;
/* 等待传输结束 */
while(!(SPSR & (1<<SPIF)));
return SPDR;
}</SPIF)));
uchar SPI_Receive(void)
{
/* 等待接收结束 */
while(!(SPSR & (1<<SPIF)))
;
/* 返回数据 */
return SPDR;
}</SPIF)))
void main()
{
unsigned char pa,i;
init_devices();
LCD
1602
_initial();
LCD1602_sendbyte(iDat,1+0x30 );
// while(1)
//{
for(i=0;i<2;i++)
{
LCD1602_gotoXY(1, 2);
//SPI_Transmit(0x01);
//pa=SPI_Receive();
LCD1602_sendbyte(iDat,SPI_Transmit(0x01)+0x30 );
}//delayms(1000);
//}
}
以下是从机部份
#include
#include
#define uchar unsigned char
#define uint unsigned int
#include "delay.h"
#include "lcd.h"
void port_init(void);
//SPI initialize
// clock rate: 1843199hz
void spi_init(void)
{
SPCR = 0x64; //setup SPI
SPSR = 0x00; //setup SPI
DDRB=0X08;
PORTB|=0X07;
}
//call this routine to initialize all peripherals
void init_devices(void)
{
CLI(); //disable all interrupts
port_init();
spi_init();
SEI(); //re-enable interrupts
}
void SPI_Transmit(char cData)
{
/* 启动数据传输 */
SPDR = cData;
/* 等待传输结束 */
while(!(SPSR & (1<<SPIF)));
}</SPIF)));
uchar SPI_Receive(void)
{
/* 等待接收结束 */
while(!(SPSR & (1<<SPIF)));
/* 返回数据 */
return SPDR;
}</SPIF)));
void main()
{ unsigned char pp,i;
init_devices();
LCD1602_initial();
LCD1602_sendbyte(iDat,2+0x30 );
//while(1)
//{
for(i=0;i<2;i++)
{
LCD1602_gotoXY(1, 2);
pp=SPI_Receive();
SPI_Transmit(0x03);
LCD1602_sendbyte(iDat,pp+0x30 );
}
//delayms(1001);
//}
}
}
用于测量三个脉冲的时间差·~~~
原理是分别读取每个脉冲来时定时器内的计算值
uint rise_time1,rise_time2,rise_time3;
uchar rise_flag;
uchar over_flow;
uint piancha1,piancha2;
uchar table1[5];
uchar table2[5];
#pragma interrupt_handler timer0_ovf_isr:17
void timer0_ovf_isr(void)
{
over_flow++;
}
#pragma interrupt_handler timer1_capt_isr:12
void timer1_capt_isr(void)
{
static uchar cishu;
cishu++;
if(cishu==1)
rise_time1=ICR1;
else if(cishu==2)
rise_time2=ICR1;
else if(cishu==3)
{ rise_time3=ICR1; rise_flag=1;}
//timer 1 input capture event, read (int)value in ICR1 using;
// value=ICR1L; //Read low byte first (important)
// value|=(int)ICR1H << 8; //Read high byte and shift into top byte
}
void main()
{
uchar i;
TCCR1B=0X42;
TIMSK=0X24;
rise_flag=0;
LCD1602_initial();
LCD1602_gotoXY(1,2);
LCD1602_sendbyte(iDat,'a');
SEI();
TIMSK|=BIT(4);
while(1)
{
if(rise_flag==1)
{
piancha1=(rise_time2-rise_time1);
piancha2=(rise_time3-rise_time2);
table1[0]=piancha1/10000;
table1[1]=piancha1%10000/1000;
table1[2]=piancha1%1000/100;
table1[3]=piancha1%100/10;
table1[4]=piancha1%10;
table2[0]=piancha2/10000;
table2[1]=piancha2%10000/1000;
table2[2]=piancha2%1000/100;
table2[3]=piancha2%100/10;
table2[4]=piancha2%10;
LCD1602_gotoXY(2,1);
for(i=0;i<4;i++)
{
LCD1602_sendbyte(iDat,table1
+0x30);
}
for(i=0;i<4;i++)
{
LCD1602_sendbyte(iDat,table2
+0x30);
}
}
}
}
关键字:
AVR
串口通信
RS232
头文件
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/)
Powered by Discuz! 7.0.0