Robofun 機器人論壇
標題:
ping超音波使用在avr上的問題
[打印本頁]
作者:
tom7232
時間:
2011-11-9 01:48
標題:
ping超音波使用在avr上的問題
我將ping超音波的信號角接至PORTB.3,程式如下
//////////////////////////////////////////////////////////////////////////////////////////////////////
#include <mega16.h>
#include <alcd.h>
#include <delay.h>
unsigned long dis_cal(unsigned int timer);
void lcd_scan(unsigned long distance);
unsigned char x=0;
unsigned int pwm_1=0,timer;
double distance;
interrupt [TIM0_OVF] void timer0_ovf_isr(void)
{
if(TCCR0==0x07) //00000111(計時由T0腳位輸入,正緣觸發)
{
TCCR0=0x06; //令00000110(計時由T0腳位輸入,負緣觸發)
timer=0;
x=1;
}
else if(TCCR0==0x06) //00000110(計時由T0腳位輸入,負緣觸發)
{
TCCR0=0x07; //令00000111(計時由T0腳位輸入,正緣觸發)
x=0;
}
TCNT0=0xff; //令初值255
}
interrupt [TIM1_OVF] void timer1_ovf_isr(void)
{
TCNT1H=0xff;
TCNT1L=0xD8;
pwm_1++;
if(x==1)
{
timer++;
}
PORTB.3=(!PORTB.3);
}
void main(void)
{
PORTB=0x08;
DDRB=0x00;
TCCR0=0x07; //00000111(計時由T0腳位輸入,正緣觸發)
TCNT0=0xff; //計時器0為256
TCCR1A=0x00;
TCCR1B=0x01; //00000001(無分頻)
TCNT1H=0x00;
TCNT1L=0x00;
TIMSK=0x05;
ACSR=0x80;
lcd_init(16);
#asm("sei")
while (1)
{
if(pwm_1>=0 && pwm_1<=500)
{
PINB.3=1;
}
else if(pwm_1>500 && pwm_1<=6000)
{
if(pwm_1>5000)
{
pwm_1=0;
}
else
{
PINB.3=0;
distance=dis_cal(timer);
lcd_scan(distance);
}
}
}//end while
}//end main
unsigned long dis_cal(unsigned int timer)
{
unsigned long distance;
float handle;
handle=((10*timer)/2)*2257;
distance=handle;
return distance;
}
void lcd_scan(unsigned long distance)
{
lcd_clear();
lcd_gotoxy(0,0);
lcd_putsf("Test Distance:");
lcd_gotoxy(0,1);
lcd_putchar((distance/100%10)+0x30);
lcd_gotoxy(1,1);
lcd_putchar((distance/10%10)+0x30);
lcd_gotoxy(2,1);
lcd_putchar((distance%10)+0x30);
lcd_gotoxy(3,1);
lcd_putsf("cm");
}
//////////////////////////////////////////////////////
但程式結果出來所量測距離在LCD上是不會改變的,不知原因在哪??
作者:
tom7232
時間:
2011-11-9 01:51
剛程式有點問題,這個才是。
#include <mega16.h>
#include <alcd.h>
#include <delay.h>
unsigned long dis_cal(unsigned int timer);
void lcd_scan(unsigned long distance);
unsigned char x=0;
unsigned int pwm_1=0,timer;
double distance;
interrupt [TIM0_OVF] void timer0_ovf_isr(void)
{
if(TCCR0==0x07) //00000111(計時由T0腳位輸入,正緣觸發)
{
TCCR0=0x06; //令00000110(計時由T0腳位輸入,負緣觸發)
timer=0;
x=1;
}
else if(TCCR0==0x06) //00000110(計時由T0腳位輸入,負緣觸發)
{
TCCR0=0x07; //令00000111(計時由T0腳位輸入,正緣觸發)
x=0;
}
TCNT0=0xff; //令初值255
}
interrupt [TIM1_OVF] void timer1_ovf_isr(void)
{
TCNT1H=0xff;
TCNT1L=0xD8;
pwm_1++;
if(x==1)
{
timer++;
}
PORTB.3=(!PORTB.3);
}
void main(void)
{
PORTB=0x08;
DDRB=0x00;
TCCR0=0x07; //00000111(計時由T0腳位輸入,正緣觸發)
TCNT0=0xff; //計時器0為256
TCCR1A=0x00;
TCCR1B=0x01; //00000001(無分頻)
TCNT1H=0x00;
TCNT1L=0x00;
TIMSK=0x05;
ACSR=0x80;
lcd_init(16);
#asm("sei")
while (1)
{
if(pwm_1>=0 && pwm_1<=500)
{
PINB.3=1;
}
else if(pwm_1>500 && pwm_1<=6000)
{
if(pwm_1>5000)
{
pwm_1=0;
}
else
{
PINB.3=0;
distance=dis_cal(timer);
lcd_scan(distance);
}
}
}//end while
}//end main
unsigned long dis_cal(unsigned int timer)
{
unsigned long distance;
float handle;
handle=(10*(float)timer)/58;
distance=handle;
return distance;
}
void lcd_scan(unsigned long distance)
{
lcd_clear();
lcd_gotoxy(0,0);
lcd_putsf("Test Distance:");
lcd_gotoxy(0,1);
lcd_putchar((distance/100%10)+0x30);
lcd_gotoxy(1,1);
lcd_putchar((distance/10%10)+0x30);
lcd_gotoxy(2,1);
lcd_putchar((distance%10)+0x30);
lcd_gotoxy(3,1);
lcd_putsf("cm");
}
歡迎光臨 Robofun 機器人論壇 (https://robofun.net/forum/)
Powered by Discuz! X3.2