求助:谁能给个atmega16对超声波hc-sr04测距程序。要可以用的!
答案:2 悬赏:50 手机版
解决时间 2021-03-23 21:07
- 提问者网友:椧運幽默
- 2021-03-23 12:20
求助:谁能给个atmega16对超声波hc-sr04测距程序。要可以用的!
最佳答案
- 五星知识达人网友:纵马山川剑自提
- 2021-03-23 13:54
//ICC-AVR application builder : 2011-8-25 16:45:09
// Target : M16
// Crystal: 16.000Mhz
//PD6接trig,PD7接echo,系统时钟8分频,用T1的输入补捉
#include
#include
#include "lcd1602.h"
#include "delay.h"
#define uint unsigned int
#define uchar unsigned char
uchar dist_num[6]=" mm";//存放要显示的数据
void port_init(void)
{
PORTA = 0x00;
DDRA = 0xFF;
PORTB = 0x00;
DDRB = 0x00;
PORTC = 0x03; //m103 output only
DDRC = 0xFC;
PORTD = 0xb0;
DDRD = 0xbF;
}
//TIMER1 initialize - prescale:8
// WGM: 0) Normal, TOP=0xFFFF
// desired value: 1KHz
// actual value: 1.000KHz (0.0%)
void timer1_init(void)
{
TCCR1B = 0x00; //stop
TCNT1H = 0xF8; //setup
TCNT1L = 0x30;
OCR1AH = 0x07;
OCR1AL = 0xD0;
OCR1BH = 0x07;
OCR1BL = 0xD0;
ICR1H = 0x07;
ICR1L = 0xD0;
TCCR1A = 0x00;
TCCR1B = 0xC2; //start Timer
}
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
timer1_init();
lcd_init();
MCUCR = 0x00;
GICR = 0x00;
TIMSK = 0x20; //timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
void main(void)
{
uchar i;
init_devices();
while(1)
{
PORTD|=0x80;
delay_us(20);
PORTD&=0x7f;
write_com(0x80);
for(i=0;i<6;i++)
{
write_data(dist_num[i]);
}
delay_ms(400);
}
}
#pragma interrupt_handler timer1_capt_isr:6
void timer1_capt_isr(void)
{
uint time=0;
float dist=0;
if(TCCR1B==0xC2)
{
TCNT1=0;
TCCR1B=0x82;
}
else
{
dist=TCNT1;
TCCR1B=0xC2;
if(TCNT1>60000);//超过5100mm作为错误处理
else
{
dist*=0.085;//系统时钟8分频,每记1次等于0.085mm
time=dist;//转换成整型,可以作取余运算
dist_num[0]=time/1000+48;
dist_num[1]=time%1000/100+48;
dist_num[2]=time%100/10+48;
dist_num[3]=time%10+48;
}
}
}
中断处理后数据都放在dist_num中,已经转换成asic||码。你可以用自己的显示模块显示。
// Target : M16
// Crystal: 16.000Mhz
//PD6接trig,PD7接echo,系统时钟8分频,用T1的输入补捉
#include
#include
#include "lcd1602.h"
#include "delay.h"
#define uint unsigned int
#define uchar unsigned char
uchar dist_num[6]=" mm";//存放要显示的数据
void port_init(void)
{
PORTA = 0x00;
DDRA = 0xFF;
PORTB = 0x00;
DDRB = 0x00;
PORTC = 0x03; //m103 output only
DDRC = 0xFC;
PORTD = 0xb0;
DDRD = 0xbF;
}
//TIMER1 initialize - prescale:8
// WGM: 0) Normal, TOP=0xFFFF
// desired value: 1KHz
// actual value: 1.000KHz (0.0%)
void timer1_init(void)
{
TCCR1B = 0x00; //stop
TCNT1H = 0xF8; //setup
TCNT1L = 0x30;
OCR1AH = 0x07;
OCR1AL = 0xD0;
OCR1BH = 0x07;
OCR1BL = 0xD0;
ICR1H = 0x07;
ICR1L = 0xD0;
TCCR1A = 0x00;
TCCR1B = 0xC2; //start Timer
}
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
timer1_init();
lcd_init();
MCUCR = 0x00;
GICR = 0x00;
TIMSK = 0x20; //timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
void main(void)
{
uchar i;
init_devices();
while(1)
{
PORTD|=0x80;
delay_us(20);
PORTD&=0x7f;
write_com(0x80);
for(i=0;i<6;i++)
{
write_data(dist_num[i]);
}
delay_ms(400);
}
}
#pragma interrupt_handler timer1_capt_isr:6
void timer1_capt_isr(void)
{
uint time=0;
float dist=0;
if(TCCR1B==0xC2)
{
TCNT1=0;
TCCR1B=0x82;
}
else
{
dist=TCNT1;
TCCR1B=0xC2;
if(TCNT1>60000);//超过5100mm作为错误处理
else
{
dist*=0.085;//系统时钟8分频,每记1次等于0.085mm
time=dist;//转换成整型,可以作取余运算
dist_num[0]=time/1000+48;
dist_num[1]=time%1000/100+48;
dist_num[2]=time%100/10+48;
dist_num[3]=time%10+48;
}
}
}
中断处理后数据都放在dist_num中,已经转换成asic||码。你可以用自己的显示模块显示。
全部回答
- 1楼网友:野味小生
- 2021-03-23 14:50
//icc-avr application builder : 2011-8-25 16:45:09
// target : m16
// crystal: 16.000mhz
//pd6接trig,pd7接echo,系统时钟8分频,用t1的输入补捉
#include <iom16v.h>
#include <macros.h>
#include "lcd1602.h"
#include "delay.h"
#define uint unsigned int
#define uchar unsigned char
uchar dist_num[6]=" mm";//存放要显示的数据
void port_init(void)
{
porta = 0x00;
ddra = 0xff;
portb = 0x00;
ddrb = 0x00;
portc = 0x03; //m103 output only
ddrc = 0xfc;
portd = 0xb0;
ddrd = 0xbf;
}
//timer1 initialize - prescale:8
// wgm: 0) normal, top=0xffff
// desired value: 1khz
// actual value: 1.000khz (0.0%)
void timer1_init(void)
{
tccr1b = 0x00; //stop
tcnt1h = 0xf8; //setup
tcnt1l = 0x30;
ocr1ah = 0x07;
ocr1al = 0xd0;
ocr1bh = 0x07;
ocr1bl = 0xd0;
icr1h = 0x07;
icr1l = 0xd0;
tccr1a = 0x00;
tccr1b = 0xc2; //start timer
}
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
cli(); //disable all interrupts
port_init();
timer1_init();
lcd_init();
mcucr = 0x00;
gicr = 0x00;
timsk = 0x20; //timer interrupt sources
sei(); //re-enable interrupts
//all peripherals are now initialized
}
void main(void)
{
uchar i;
init_devices();
while(1)
{
portd|=0x80;
delay_us(20);
portd&=0x7f;
write_com(0x80);
for(i=0;i<6;i++)
{
write_data(dist_num[i]);
}
delay_ms(400);
}
}
#pragma interrupt_handler timer1_capt_isr:6
void timer1_capt_isr(void)
{
uint time=0;
float dist=0;
if(tccr1b==0xc2)
{
tcnt1=0;
tccr1b=0x82;
}
else
{
dist=tcnt1;
tccr1b=0xc2;
if(tcnt1>60000);//超过5100mm作为错误处理
else
{
dist*=0.085;//系统时钟8分频,每记1次等于0.085mm
time=dist;//转换成整型,可以作取余运算
dist_num[0]=time/1000+48;
dist_num[1]=time%1000/100+48;
dist_num[2]=time%100/10+48;
dist_num[3]=time%10+48;
}
}
}
中断处理后数据都放在dist_num中,已经转换成asic||码。你可以用自己的显示模块显示。
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯