以下是我的超声波程序,无论怎么弄都接收不了。请各大神帮忙挖掘错误?谢谢!(小弟是初学者)
#define F_CPU 1000000UL
#include
#include
#define uint unsigned int
#define uchar unsigned char
#define ulong unsigned long
const char table[]="L= . cm"; //整数部分显示三位,小数部分显示两位
const char table1[10]={"0123456789"};
uchar ge,shi,bai;//自定义寄存器
#define RS_1 PORTA |=(1<
#define RS_0 PORTA &=~(1<
#define RW_1 PORTA|=(1<
#define RW_0 PORTA &=~(1<
#define EN_1 PORTA|=(1<
#define EN_0 PORTA &=~(1<
#define TX_0 PORTD&=~(1<
#define TX_1 PORTD|=(1<
uint temp1,temp2;
uint t;//时间变量
uint s=0;//距离变量
uchar count =0;//溢出标志位
/////////////////写指令////////////////////
void write_com(uchar com)
{
_delay_ms(20); //忙则等待
RS_0;
RW_0;
EN_1;
PORTC= com;
_delay_ms(20);
EN_0;
}
/////////写数据/////////////
void write_dat(uchar dat)
{
_delay_ms(20);//忙则等待
RS_1;
RW_0;
EN_1;
PORTC= dat;
_delay_ms(20);
EN_0;
}
////////写显示///////////
void write_char()
{
write_com(0x80 + 0x02);
write_dat(table1[bai]);
_delay_ms(5);
write_com(0x80 + 0x03);
write_dat(table1[shi]);
_delay_ms(5);
write_com(0x80 + 0x05);
write_dat(table1[ge]);
_delay_ms(5);
}
/////////////清屏///////
void clear()
{
write_com(0x01);
_delay_ms(20);
}
////////1602的初始化/////////
void lcd_init( const char b[])
{uchar i;
write_com(0x38); //显示模式设置
_delay_ms(10);
write_com(0x38);
_delay_ms(10);
write_com(0x38);
_delay_ms(10);
write_com(0x38);
write_com(0x08); // 显示关闭
write_com(0x01); //显示清屏
write_com(0x06); //显示光标移动设置
_delay_ms(20);
write_com(0x0C); //显示开及光标设置
for(i=0;i<8;i++)
{
write_dat(b); //显示"L= . cm"
_delay_ms(5);
}
}
void Conut(void)
{
s=t*1.7;
bai=s/100;
shi=s%100/10;
ge=s%1000%10;
}
void icp_init()
{
DDRB=0XFF;
PORTB=0XFF;
_delay_ms(60);
PORTB=0X00;
TCCR1A = 0x00;
TCNT1 = 0x00;
TCCR1B=0xC2;//输入捕获上升沿触发,8分频
TIMSK=0x20;//打开输入捕捉器中断使能
//SREG |=0x80;
}
void StartModule()//启动模块
{
TX_1;//启动一次模块
_delay_us(20);
TX_0;
_delay_us(20);
write_char();
}
void port_init()
{
DDRC=0XFF; //io口A设为输出
DDRA|=(1<
DDRD|=(1<
DDRD&=~(1<
}
int main()
{
port_init();
lcd_init(table);//初始化
_delay_ms(30);
icp_init();
while(1)
{
StartModule();
Conut() ; //计算
_delay_ms(100); //80MS
}
}
ISR(TIMER1_CAPT_vect) //数 字1
{
count++;
switch(count)
{
case 1:
temp1=ICR1;
TCCR1B=0x82;
break;
case 2:
temp2=ICR1;
TCCR1B=0xC2;
if(temp2>=temp1) t=temp2-temp1;
else t=65536-temp1+temp2;
count=0;
break;
default: ;
}
}