⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 基于AVR的超声波测距程序
💻 C
字号:
#include <avr/io.h>
#include <avr/wdt.h>
#include <stdio.h>
#include <avr/delay.h>
#include <avr/signal.h>
#include "MyDef.h"
#include "ds1820.h"
#include "Ultrasound.h"
#include "Uart.h"

void IO_INIT(void);  //端口初始化

/////////////////////////////////////////////////////////////

int main(void)       //程序入口
{
	uchar i;
	wdt_disable();
	IO_INIT();

#ifdef Debug	
		
	Uart_Init();
	printf("\nSystem Started ......\n");
	
#endif	

	while(!DS1820CRCDETECT()); //DS1820检测

	sei();	

	while(1)
		{
			GetValuableDistant();//获取距离有效值
#ifdef Debug			
			printf("\n"); 
#endif
		}
}

/////////////////////////////////////////////////////////////

void IO_INIT(void)
{
	MCUCR=_BV(ISC01); //外部中断0设置为下降沿触发

	DDRB|=_BV(P0); 	 //DS1820输出

	DDRD&=~_BV(P2);  
	PORTD|=_BV(P2);  //外部中断0引脚内部上拉
	
	TCCR1B=_BV(WGM12);//|_BV(CS11);//采用8分频 这样的话 TCNT1的计数时基为 1us
	
	OCR1A=20000;  //TCNT1 计数上限 设置超声波接收超时 这里设置 20ms
	
	TIMSK|=_BV(OCIE1A);//开启溢出中断  超时中断
	
	TCCR0=_BV(COM00)|_BV(WGM01)|_BV(CS00);  //T0 使用 CTC模式 不预分频
	
	OCR0=99;//PWM 输出 40000Hz
	//OCR0=104; //PWM 输出 38000Hz
}







⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -