基于51单片机的GY-26驱动程序

版权声明:本文为博主原创文章,欢迎大家转载! https://blog.csdn.net/Chamico/article/details/82823542

大一的时候需要用到GY-26模块配合调试机器人转向,当时花了不少的功夫才把GY-26模块成功驱动,希望我的代码能给大家一些帮助,格式和注释写的不规范的地方希望大家多多理解,这是大一的时候的作品。下面给大家列出一些核心代码,

I2C 通信协议

///////////毫秒级延时
void delay_ms(uint t)
{
    uint i,j;
    for(i=0;i<t;i++)
        for(j=0;j<114;j++);
}

/////////// 10 微秒级延时
void delay_us(uint t)
{
    while(t--);
}


//////////////////////////////////
//////////////////////////////////
//////////////////////////////////
//LCD1602

//////////// 查忙函数
bit check_busy()
{
    bit result;
    RS = 0;
    RW = 1;
    EN = 1;
    delay_us(1);
    result = (bit)(P0&0x80);
    EN = 0;
    return result;
}

/////////// 写命令
void write_com(uchar com)
{
    while(check_busy());    //查忙
    
    RS = 0;
    RW = 0;
    EN = 0;            //高脉冲
    port = com;
    delay_ms(5);
    EN = 1;
    delay_ms(5);
    
    EN = 0;            //置 0
    delay_ms(5);
}

//////////// 写数据
void write_date(uchar date)
{
    while(check_busy());
    port = date;
    RS = 1;
    RW = 0;
    EN = 0;        //高脉冲
    delay_ms(5);
    EN = 1;
    delay_ms(5);
    
    EN = 0;        //置 0
    delay_ms(5);
}

/////////// 初始化
void init()
{
    delay_ms(15);
    write_com(0x38);    //不检测忙信号
    delay_ms(5);
    write_com(0x38);    //不检测忙信号
    delay_ms(5);
    write_com(0x38);    //不检测忙信号
    write_com(0x01);    //清屏
    write_com(0x06);    //模式设置
    write_com(0x0c);    //显示开关控制
}
//////////////////////////////////
//////////////////////////////////
//////////////////////////////////
// IIC

扫描二维码关注公众号,回复: 3319371 查看本文章

void Delay10us()
{
    unsigned char a,b;
    for(b=1;b>0;b--)
        for(a=2;a>0;a--);

}

void I2cStart()
{
    SDA=1;
    Delay10us();
    SCL=1;
    Delay10us();//建立时间是SDA保持时间>4.7us
    SDA=0;
    Delay10us();//保持时间是>4us
    SCL=0;            
    Delay10us();        
}


void I2cStop()
{
    SDA=0;
    Delay10us();
    SCL=1;
    Delay10us();//建立时间大于4.7us
    SDA=1;
    Delay10us();        
}

unsigned char I2cSendByte(unsigned char dat)
{
    unsigned char a=0,b=0;//最大255,一个机器周期为1us,最大延时255us。        
    for(a=0;a<8;a++)//要发送8位,从最高位开始
    {
        SDA=dat>>7;     //起始信号之后SCL=0,所以可以直接改变SDA信号
        dat=dat<<1;
        Delay10us();
        SCL=1;
        Delay10us();//建立时间>4.7us
        SCL=0;
        Delay10us();//时间大于4us        
    }
    SDA=1;
    Delay10us();
    SCL=1;
    while(SDA)//等待应答,也就是等待从设备把SDA拉低
    {
        b++;
        if(b>200)     //如果超过2000us没有应答发送失败,或者为非应答,表示接收结束
        {
            SCL=0;
            Delay10us();
            return 0;
        }
    }
    SCL=0;
    Delay10us();
     return 1;        
}


unsigned char I2cReadByte()
{
    unsigned char a=0,dat=0;
    SDA=1;            //起始和发送一个字节之后SCL都是0
    Delay10us();
    for(a=0;a<8;a++)//接收8个字节
    {
        SCL=1;
        Delay10us();
        dat<<=1;
        dat|=SDA;
        Delay10us();
        SCL=0;
        Delay10us();
    }
    return dat;        
}


uchar GY_26Write(unsigned char dat,unsigned char addr)
{
    I2cStart();
    I2cSendByte(0xe0);//发送写器件地址
    I2cSendByte(addr);//发送要写入内存地址
    I2cSendByte(dat);    //发送数据
    I2cStop();
    return 0;
}

unsigned char GY_26Read(unsigned char addr)
{
    unsigned char num;
    I2cStart();
    I2cSendByte(0xE0); //发送写器件地址
    I2cSendByte(addr); //发送要读取的地址
    I2cStart();
    I2cSendByte(0xE1); //发送读器件地址
    num=I2cReadByte(); //读取数据
    I2cStop();
    return num;    

}

完整代码下载地址(需要积分),也可以留言邮箱地址,看到留言后会发过去。

https://download.csdn.net/download/chamico/10683783

猜你喜欢

转载自blog.csdn.net/Chamico/article/details/82823542