首页 | 新闻 | 新品 | 文库 | 方案 | 视频 | 下载 | 商城 | 开发板 | 数据中心 | 座谈新版 | 培训 | 工具 | 博客 | 论坛 | 百科 | GEC | 活动 | 主题月 | 电子展
返回列表 回复 发帖

AVR(ATMEGA128)输入捕捉功能

AVR(ATMEGA128)输入捕捉功能

用于测量三个脉冲的时间差·~~~ 原理是分别读取每个脉冲来时定时器内的计算值
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);
    }
  }
   }
}

以下是主机部份
#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();
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<<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  ATMEGA128  输入捕捉功能
返回列表