GithubHelp home page GithubHelp logo

Comments (1)

wukongxuetang avatar wukongxuetang commented on July 20, 2024

I found the code just like I look for,hope these will be useful.

#include <C8051F310.H>
#include <math.h> //Keil library
#include <stdio.h> //Keil library
#include <INTRINS.H>
#define uchar unsigned char
#define uint unsigned int
#define WHO_AM_I 0x0F
#define CTRL_REG1 0x20
#define CTRL_REG2 0x21
#define CTRL_REG3 0x22
#define CTRL_REG4 0x23
#define CTRL_REG5 0x24
#define REFERENCE 0x25
#define OUT_TEMP 0x26
#define STATUS_REG 0x27
#define OUT_X_L 0x28
#define OUT_X_H 0x29
#define OUT_Y_L 0x2A
#define OUT_Y_H 0x2B
#define OUT_Z_L 0x2C
#define OUT_Z_H 0x2D
#define FIFO_CTRL_REG 0x2E
#define FIFO_SRC_REG 0x2F
#define INT1_CFG 0x30
#define INT1_SRC 0x31
#define INT1_TSH_XH 0x32
#define INT1_TSH_XL 0x33
#define INT1_TSH_YH 0x34
#define INT1_TSH_YL 0x35
#define INT1_TSH_ZH 0x36
#define INT1_TSH_ZL 0x37
#define INT1_DURATION 0x38

#define CRA_REG_M 0x00
#define CRB_REG_M 0x01
#define MR_REG_M 0x02
#define OUTXH_M 0X03
#define OUTXL_M 0x04
#define OUTYH_M 0x05
#define OUTYL_M 0x06
#define OUTZH_M 0x07
#define OUTZL_M 0x08
#define SR_REG_M Ox09

sbit SCL = P0^0;
sbit SDA = P0^1;
sbit TE = P2^0;

#define LSM303A_SlaveAddress 0x30
#define LSM303M_SlaveAddress 0x3C
#define SlaveAddress1 0xD2

typedef unsigned char BYTE;
typedef unsigned short WORD;

BYTE BUF[16];
int dis_data;

void delay(unsigned int k);
void Single_Write(uchar SlaveAddress,uchar REG_Address,uchar REG_data);
uchar Single_Read(uchar SlaveAddress,uchar REG_Address);
void Multiple_Read1(uchar,uchar);
//------------------------------------
void Delay5us();
//void Delay5ms();
void LSM303_Start();
void LSM303_Stop();
void LSM303_SendACK(bit ack);
bit LSM303_RecvACK();
void LSM303_SendByte(BYTE dat);
BYTE LSM303_RecvByte();
void LSM303_ReadPage();
void LSM303_WritePage();

//-----------------------------------
/*******************************/
void delay(unsigned int k)
{
unsigned int i,j;
for(i=0;i<k;i++)
{
for(j=0;j<245;j++)
{;}}
}

void Delay5us()
{
nop();nop();nop();nop();
nop();nop();nop();nop();
nop();nop();nop();nop();

}
/**************************************/
void LSM303_Start()
{
SDA = 1;
SCL = 1;
Delay5us();
SDA = 0;
Delay5us();
SCL = 0;
}

void LSM303_Stop()
{
SDA = 0;
SCL = 1;
Delay5us();
SDA = 1;
Delay5us();
}

void LSM303_SendACK(bit ack)
{
SDA = ack;
SCL = 1;
Delay5us();
SCL = 0;
Delay5us();
}

bit LSM303_RecvACK()
{
SCL = 1;
Delay5us();
CY = SDA;
SCL = 0;
Delay5us();

return CY;

}

void LSM303_SendByte(BYTE dat)
{
BYTE i;

for (i=0; i<8; i++)
{
    dat <<= 1;
    SDA = CY; 
    SCL = 1;  
    Delay5us();     
    SCL = 0;  
    Delay5us();   
}
LSM303_RecvACK();

}

BYTE LSM303_RecvByte()
{
BYTE i;
BYTE dat = 0;

SDA = 1;
for (i=0; i<8; i++)
{
    dat <<= 1;
    SCL = 1;
    Delay5us();
    dat |= SDA;           
    SCL = 0;
    Delay5us();
}
return dat;

}

void Single_Write(uchar SlaveAddress,uchar REG_Address,uchar REG_data)
{
LSM303_Start();
LSM303_SendByte(SlaveAddress);
LSM303_SendByte(REG_Address);
LSM303_SendByte(REG_data);
LSM303_Stop();
}

uchar Single_Read(uchar SlaveAddress,uchar REG_Address)
{ uchar REG_data;
LSM303_Start();
LSM303_SendByte(SlaveAddress);
LSM303_SendByte(REG_Address);
LSM303_Start();
LSM303_SendByte(SlaveAddress+1);
REG_data=LSM303_RecvByte();
LSM303_SendACK(1);
LSM303_Stop();
return REG_data;
}

void Multiple_read1(uchar SlaveAddress,uchar ST_Address)
{ uchar i;
LSM303_Start();
LSM303_SendByte(SlaveAddress);
LSM303_SendByte(ST_Address);
LSM303_Start();
LSM303_SendByte(SlaveAddress+1);
for (i=0; i<6; i++)
{
BUF[i] = LSM303_RecvByte();
if (i == 5)
{
LSM303_SendACK(1);
}
else
{
LSM303_SendACK(0);
}
}
LSM303_Stop();
}
void I2C_SendByte(unsigned char addr , unsigned char dat8)
{
unsigned char i;
SDA = 1;
SCL = 1;
Delay5us();
SDA = 0;
Delay5us();
SCL = 0;
addr = (addr << 1) & 0xfe;
for(i = 8 ; i > 0 ; i --)
{
SCL = 0;
Delay5us();
SDA = (addr >> (i - 1 )) & 0x01;
Delay5us();
SCL = 1;
Delay5us();
}

    SCL = 0;
    SDA = 0;
    Delay5us();
    SCL = 1;
    Delay5us();
    Delay5us();
   
    //if(SDA == 0) I2C_SendByte(addr,dat8);
    //dat8 = 0xff;
    for(i = 8 ; i > 0 ; i --)
    {
            SCL = 0;
            Delay5us();
            SDA = (dat8 >> (i - 1 )) & 0x01;
            Delay5us();
            SCL = 1;
            Delay5us();
    }
    SCL = 0;
    SDA = 0;
    Delay5us();
    SCL = 1;
    Delay5us();
    Delay5us();
    SCL = 0;

SDA = 0;
SCL = 1; 
Delay5us(); 
SDA = 1;
Delay5us(); 

}

//******************
void Init_LSM303A()
{
Single_Write(LSM303A_SlaveAddress,0x20,0x27);
}

//初始化LSM303M(磁场)
void Init_LSM303M()
{
Single_Write(LSM303M_SlaveAddress,0x02,0x00); //
}

//**************************************************************
void Port_IO_Init()
{
P0MDOUT = 0x10;
XBR0 = 0x01;
XBR1 = 0x40;
P1MDOUT = 0x40;
P1 =0xE3;

}
void Oscillator_Init()
{
OSCICN = 0x83;
}

// Initialization function for device,
// Call Init_Device() from your main program
void UART0_Init (void)
{
SCON0 |= 0x10;
CKCON = 0x08;
TH1 = 0x96;
TL1 = TH1;
TMOD = 0x20;
TR1 = 1;
TI0 = 1;
/*EA = 1;
ES0 = 1; */
}
void Init_Device(void)
{
Port_IO_Init();
Oscillator_Init();
UART0_Init();
TE = 1;
}

//unsigned char ii;
void hs(int j)
{
j = (0x01<<j);
I2C_SendByte(0x70,j);
//Multiple_Read1(LSM303A_SlaveAddress,0xA8);
Multiple_Read1(LSM303M_SlaveAddress,0x03);
BUF0 =
//Multiple_readL3G4200D(0xA8);
}

void main()
{
int i,j;
PCA0MD &= ~0x40;
delay(5);
Init_Device();
P1 = 0;
for(i=0;i<=7;i++)
{
j = (0x01<<i);
I2C_SendByte(0x70,j);
//Init_LSM303A();
Init_LSM303M();
}
while(1)
{
for(i=0;i<=7;i++)
{
hs(i);
Frame_Send(i,&BUF);
}
}
}

from ch55xduino.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.