欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

N76E003 模拟IIC 读写陀螺仪模块

程序员文章站 2022-04-18 19:56:09
...

N76E003   <========IIC========>  脱落仪模块(IIC Slave)

 

IIC 模拟部分代码:

bsp_iic.c

#include "bsp_iic.h"



unsigned char SDA_READ(void)
{
    if(SDA_RD)
        return 1;
    else
        return 0;
}

void delay_10us(unsigned int dly)
{
    Delay10us(dly);
}

void IIC_Start(void)
{
    SDA_H();
    SCL_H();
    delay_10us(RW_DELAY);
    SDA_L();
    delay_10us(RW_DELAY);
    SCL_L();
}

void IIC_Stop(void)
{
    SDA_L();
    SCL_L();
    delay_10us(RW_DELAY);
    SCL_H();
    delay_10us(RW_DELAY);
    SDA_H();
    delay_10us(RW_DELAY);
}


void IIC_Ack(void)		
{
	SCL_L();
    SDA_L();
    delay_10us(RW_DELAY);
    SCL_H();
    delay_10us(RW_DELAY);
    SCL_L();
}

void IIC_NAck(void)  
{
    SCL_L();
    SDA_H();
    delay_10us(RW_DELAY);
    SCL_H();
    delay_10us(RW_DELAY);
    SCL_L();
}

unsigned char IIC_Wait_Ack(void)
{
    unsigned char ucErrTime = 0;

    SDA_H();
    delay_10us(1);
    SCL_H();
    delay_10us(1);
    
    while(SDA_READ()) {
        ucErrTime++;
        if(ucErrTime > 250) {
            IIC_Stop();
            return 1;
        }
    }    

    SCL_L();

    return 0;
}

void IIC_WriteByte(unsigned char SendByte)
{
    unsigned char t;
      
    SCL_L(); // Set CLK as low to start
    delay_10us(RW_DELAY);
    for (t = 0; t < 8; t++) {
        if ((SendByte & 0x80) >> 7) {
            SDA_H();
        } else {
            SDA_L();
        }

        SendByte <<= 1;

        delay_10us(RW_DELAY);
        SCL_H();
        delay_10us(RW_DELAY);
        SCL_L();
        delay_10us(RW_DELAY);
    }
}

unsigned char IIC_ReadByte(unsigned char ack)
{
    unsigned char i, receive = 0;

    SCL_L();
    for (i = 0; i < 8; i++) {
        delay_10us(RW_DELAY);
        SCL_H();
        receive <<= 1;
        if (SDA_READ()) {
            receive++;
        }

        delay_10us(RW_DELAY);
        SCL_L();
        delay_10us(RW_DELAY);
    }
    
    if (!ack) {
        IIC_NAck();
    } else {
        IIC_Ack();
    }

    return receive;
}

void write_add(unsigned char slaveaddr,unsigned char regaddr,unsigned char dat)
{
	IIC_Start();
	IIC_WriteByte(slaveaddr | IIC_WR);
	IIC_Wait_Ack();
	IIC_WriteByte(regaddr);
	IIC_Wait_Ack();
    delay_10us(10);
	IIC_WriteByte(dat);
	IIC_Wait_Ack();
	IIC_Stop();
}

unsigned char read_add(unsigned char slaveaddr,unsigned char regaddr)
{
	unsigned char dat;
	IIC_Start();
	IIC_WriteByte(slaveaddr | IIC_WR);
	IIC_Wait_Ack();
	IIC_WriteByte(regaddr);
	IIC_Wait_Ack();
    IIC_Stop();
    //delay_10us(10);
	IIC_Start();
	IIC_WriteByte(slaveaddr | IIC_RD);
	IIC_Wait_Ack();
	dat=IIC_ReadByte(0);
	IIC_Stop();

	return dat;
}

u8 IIC_WriteToSlave(u8 DevAddr, u8 RegAddr, u8 len, u8 *WData)
{
    u8 i;
    
    IIC_Start();
    
    IIC_WriteByte( DevAddr|0x00);//???,???????
    if(IIC_Wait_Ack())  //1 Ack Failed, 0 Ack Success
        return 1;
    
    IIC_WriteByte(RegAddr);//???
    if(IIC_Wait_Ack())  //1 Ack Failed, 0 Ack Success
        return 2;
    
    for(i = 0; i < len; i++)
    {
        IIC_WriteByte(WData[i]);//???
        if(IIC_Wait_Ack())  //1 Ack Failed, 0 Ack Success
            return 3;
    }
    
    IIC_Stop();
    
    return 4;
}


u8 IIC_ReadFromSlave(u8 DevAddr, u8 RegAddr, u8 len, u8 *RData)
{
	IIC_Start();
    
    IIC_WriteByte( DevAddr|0x00);//???,???????
    if(IIC_Wait_Ack())  //1 Ack Failed, 0 Ack Success
        return 1;
    
    IIC_WriteByte(RegAddr);//???
    if(IIC_Wait_Ack())  //1 Ack Failed, 0 Ack Success
        return 2;
    
    IIC_Start();
    IIC_WriteByte( DevAddr|0x01);//???,???????
    if(IIC_Wait_Ack())  //1 Ack Failed, 0 Ack Success
        return 3;
    
    while(len)
    {
        if(len == 1)
        {
            *RData = IIC_ReadByte(0);  //???,??????nAck
        }
        else
        {
            *RData = IIC_ReadByte(1);  //??????????????Ack
        }
        len--;
        RData++;
    }
    IIC_Stop();
    
    return 4;
}

//================TEST====================
#define SLAVE_ADDR      0xAA

void IIC_TEST(void)
{
    
//    SDA_H();
//    SCL_H();
//    delay_10us(10);   
//    SDA_L();
//    SCL_L(); 
//    delay_10us(10);  
    
    u8 res;
    u8 temp_arr_pitch[2];
    u8 temp_arr_roll[2];
    u8 temp_arr_yaw[2];
    signed short pitch, roll,yaw;
    
    res = IIC_ReadFromSlave(SLAVE_ADDR, 0x3D, 2, temp_arr_pitch);
    
    delay_10us(300); 
//    //dbg(0, ("res = %bx\r\n",res));
////    res = IIC_ReadFromSlave(SLAVE_ADDR, 0x3E, 2, temp_arr_roll);
////    res = IIC_ReadFromSlave(SLAVE_ADDR, 0x3F, 2, temp_arr_yaw);
//    
//    pitch = (temp_arr_pitch[0]<<8) | temp_arr_pitch[1];   //??????X -90~90
//    roll  = (temp_arr_roll[0]<<8)  | temp_arr_roll[1];    //??o???Y -180~180   
//    yaw   = (temp_arr_yaw[0]<<8)   | temp_arr_yaw[1];     //Dy¡Áa??Z -180~180 
//    
//    //dbg(0, ("pitch: %+6d,  roll:%+6d,   yaw:%+6d\r\n",pitch, roll, yaw));
}

bsp_iic.h

#ifndef __BSP_IIC_H
#define __BSP_IIC_H
#include "N76E003.h"
#include "Common.h"
#include "Delay.h"

/*<-------------------N76E003 Ä£Äâ IIC------------------>*/

typedef unsigned char u8;

/**
 * P00   SCL
 * P01   SDA
 */
 
#define SDA_RD              P01		        //SDA READ
#define SDA_H()             (P01 = 1)  	    //SDA HIGH
#define SDA_L()             (P01 = 0)  	    //SDA LOW

#define SCL_H()             (P00 = 1)  	    //SCL HIGH
#define SCL_L()             (P00 = 0)  	    //SCL LOW

#define RW_DELAY            1               //unit 10us

#define IIC_WR              0               //IIC Write cmd
#define IIC_RD              1               //IIC Read cmd

unsigned char SDA_READ(void);
void delay_10us(unsigned int dly);
void IIC_Start(void);
void IIC_Stop(void);
void IIC_Ack(void);
void IIC_NAck(void);
unsigned char IIC_Wait_Ack(void);
void IIC_WriteByte(unsigned char SendByte);
unsigned char IIC_ReadByte(unsigned char ack);
void write_add(unsigned char slaveaddr,unsigned char regaddr,unsigned char dat);
unsigned char read_add(unsigned char slaveaddr,unsigned char regaddr);
u8 IIC_WriteToSlave(u8 DevAddr, u8 RegAddr, u8 len, u8 *WData);
u8 IIC_ReadFromSlave(u8 DevAddr, u8 RegAddr, u8 len, u8 *RData);
void IIC_TEST(void);


#endif /* __BSP_IIC_H */

main.c的调用:

void main (void) 
{
	Set_All_GPIO_Quasi_Mode;					// Define in Function_define.h
	InitialUART0_Timer3(115200);
	set_CLOEN; 	
	
	while(1)
	{	
	
        IIC_TEST();
    }
}

 

IIC clk速度设置到最快:

22.49K

 

N76E003 模拟IIC 读写陀螺仪模块