/************************这是串口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<
}
/*串口0单字符发送,查询方式*/
void Uart0_sendbyte(uchar i)
{
while (!(UCSR0A & (1<
}
/*串口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<
}
/*串口0单字符发送,查询方式*/
void Uart1_sendbyte(uchar i)
{
while (!(UCSR1A & (1<
}
/*串口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<
}
uchar SPI_Receive(void)
{
/* 等待接收结束 */
while(!(SPSR & (1<
/* 返回数据 */
return SPDR;
}
void main()
{
unsigned char pa,i;
init_devices();
LCD1602_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<
uchar SPI_Receive(void)
{
/* 等待接收结束 */
while(!(SPSR & (1<
return SPDR;
}
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 (imp
// 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[i]+0x30);
}
for(i=0;i<4;i++)
{
LCD1602_sendbyte(iDat,table2[i]+0x30);
}
}
}
}
上一篇:AVR(ATMEGA_I2C,WTI功能)外部EEPROM_AT24C64读写
下一篇:用单片机(AVR)比较准确地读出PS2鼠标位移(X,Y轴位移)
推荐阅读最新更新时间:2024-03-16 15:16