小波123 发表于 2012-3-30 15:18:22

超声波测距求指导

最近在做超声波测距,现在还没有效果,我单独产生40khz的方波效果很好,但是加在1602显示里面就有较大的毛刺和跳动,希望大家指出那里不对,在这里不胜感谢!!!!!



#include <reg52.h>
#include <math.h>
#include <intrins.h>

typedef unsigned char uchar;
typedef unsigned int uint;
uint time, distance, j = 0 ,k = 0, flag = 0;

void delay(uint z)
{
        uint x,y;
        for(x=z;x>0;x--)
                for(y=110;y>0;y--);
}


void timer0init(void)
{       
        P1 = 0;                       // 清P0口
        TMOD = 0x00;                                  // T0使用定时模式,工作模式0,无门控位
        TH0 = 0xFF;                                  // 为T0填入初值,定时时间25ms
        TL0 = 0xB5;
        TR0 = 1;                                    // 启动T0
        ET0 = 1;                                    // 允许定时器0中断
        EA = 1;                                            // CPU开放中断
}



/********** T0溢出中断处理函数 **********/
void timer0_int () interrupt 1 using 2                // T0溢出中断,使用工作组2
{
        TH0 = 0XFF;                                                                    // 重新填入初值
        TL0 = 0xB5;
        P1 ^= 0x01;                                                                    // P1.0取反,产生方波
        k++;
        if(k>=16)                                                                        //超声波脉冲个数控制(为赋值的一半)//
        {
                k=0;
                TR1=0;
                ET1=0;
                for(j=200;j>0;j--);                                                //延时避开盲区//
                for(j=200;j>0;j--);
                for(j=200;j>0;j--);
                EX0=1;                                                                        //开启外部中断0//
        }
                  
}                                                                                                                                                                                       

/***************外部中断0程序************/
void PINT0() interrupt 0
{
        TR0=0;
        TR1=0;
        ET1=0;
        EA=0;
        EX0=0;
        flag=1;                                                                                        //接收成功标志//
        time=TH0;
        time=time*256+TL0;
        time=time-120;                                                                        //补偿软件或硬件带来的误差//
        distance=time*0.017;               
}




/****************************** LCD1602 ****************************
*********************************************************************/
#define lcd_data_port P0                                         //定义与1602LCD 的数据口的单片机口线

sbit lcdrs = P2^0;                                                         //定义与1602LCD 的RS(第4脚)连接的口线



sbit lcden = P2^1;                                                        //定义与1602LCD 的EN(第6脚)连接的口线,第5脚R/W 端直接接地
uchar code table[]="welcome to";
uchar code table1[]="T:";
uchar code table2[]="m";                                        //温度符号
uchar code table3[]="0123456789";
uchar code table4[]=".";
void write_data(uchar date);
void write_com(uchar com);
void init();
                               



void write_com(uchar com)                  
{
        lcdrs=0;
        P0=com;                                                  
        delay(5);
        lcden=1;                                          
        delay(5);
        lcden=0;                                             
}



void write_data(uchar date)                       
{
        lcdrs=1;                                       
        P0=date;
        delay(5);
        lcden=1;
        delay(5);
        lcden=0;
}


void init()                                          
{
        lcden=0;                               
        write_com(0x38);                  
       
        write_com(0x0e);                  
       
        write_com(0x06);               
       
        write_com(0x01);                  
                      

}

void main()
{
        uint i, a, b, c, e, total, ta;

        float t;


        timer0init();
       
           init();
              
       
        write_com(0x80+0x03);                                                                      
        for(i = 0; i < 10; i++)
        {
                write_data(table);          
                delay(10);                       
       
        }       
        write_com(0xc0+0x02);                                                                      
        for(i = 0; i <2; i++)
        {
                write_data(table1);          
                delay(10);                       
       
        }




        while (1)
        {       

                           total = 0;
                        for(i = 0; i <30; i++)
                        {
                                ta = distance;
                                total =total + ta;
                       
                        }
                        t = total / 30;       
       
                  
                //        t = 33 - total / 2500 ;
                        a = (uint)t / 1000 % 10;
                        b = (uint) t / 100 % 10;
                        c = (uint) t / 10 %10;
                        e = (uint) t % 10;
                       
                        write_com(0xc0+0x06);
               
                        write_data(table3);       
                        write_data(table3);       
                        write_data(0xa5);
               
                        write_data(table3);       
       
                        write_data(table3);
              

                        write_com(0xc0+0x0d);
                        write_data(0xdf);
                        write_data(table2);        
        }
       
}

jssd 发表于 2012-3-30 15:21:16

毛刺和程序有什么关系?
页: [1]
查看完整版本: 超声波测距求指导