Read and write MMA7455 program on STC89C52

Publisher:真瓷堂Latest update time:2017-11-08 Source: eefocusKeywords:STC89C52  MMA7455 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

main.c

 

#include 
#include "mytype.h"
#include "iic.h"
#include "mma7455.h"



uint8 X,Y,Z;
void main()
{
 uint8 i,temp;
 EA=1; //Open interrupt
 EX1=1; //Open external interrupt INIT1
 
  //Initialize IIC bus
 IIC_init();


 //Write data
  IIC_start();
 IIC_write_byte(MMA7455_ADDER_WRITE); //1. Address MMA7455 on IIC bus
 IIC_respons();
 IIC_write_byte(MMA7455_Mode_Control_Register); //2. Address mode setting register
 IIC_respons();
 IIC_write_byte(0x05); //3. Write mode register data
 IIC_respons();
 IIC_stop();

 //Write data
  IIC_start();
 IIC_write_byte(MMA7455_ADDER_WRITE); //1. Address MMA7455 on the IIC bus
 IIC_respons();
 IIC_write_byte(MMA7455_Mode_Control_Register); //2. Address the range detection setting register
 IIC_respons();
 IIC_write_byte(MMA7455_2G_Measurement_Mode); //3. Set the range to 2G, detection mode. Write the range detection setting register configuration data
 IIC_respons();
 IIC_stop();


  while(1)
 {
  //Read X-axis data
  IIC_start();
  IIC_write_byte(MMA7455_ADDER_WRITE); //1. IIC address addressing
  IIC_respons();
  IIC_write_byte(MMA7455_READ_X); //2. X-axis data register addressing
  IIC_respons();
  IIC_start(); //3.
  IIC_write_byte(MMA7455_ADDER_READ); //Change read and write direction
  IIC_respons();
  X=IIC_read_byte(); //4.Read data
  IIC_stop();
  //Read Y-axis data
  IIC_start();
  IIC_write_byte(MMA7455_ADDER_WRITE); //1. IIC address addressing
  IIC_respons();
  IIC_write_byte(MMA7455_READ_Y); //2. X-axis data register addressing
  IIC_respons();
  IIC_start(); //3.
  IIC_write_byte(MMA7455_ADDER_READ); //Change read and write direction
  IIC_respons();
  Y=IIC_read_byte(); //4.Read data
  IIC_stop();
  //Read X-axis data
  IIC_start();
  IIC_write_byte(MMA7455_ADDER_WRITE); //1. IIC address addressing
  IIC_respons();
  IIC_write_byte(MMA7455_READ_X); //2. X-axis data register addressing
  IIC_respons();
  IIC_start(); //3.
  IIC_write_byte(MMA7455_ADDER_READ); //Change read and write direction
  IIC_respons();
  Z=IIC_read_byte(); //4.Read data
  IIC_stop();
 }
}

 


iic.c

 

 

#include
#include
#include "iic.h"
#include "mytype.h"

#define  NOP()   _nop_()  
#define  _Nop()  _nop_()  



void IIC_start() 
{
 SDA=1;        
 _Nop();
 SCL=1;
 _Nop();       
 _Nop();
 _Nop();
 _Nop();
 _Nop();   
 SDA=0;        
 _Nop();       
 _Nop();
 _Nop();
 _Nop();
 _Nop();      
 SCL=0;      
 _Nop();
 _Nop();
}

void IIC_stop()  
{
SDA=0;     
_Nop();      
SCL=1;     
_Nop();
_Nop();
_Nop();
_Nop();
_Nop();
SDA=1;     
_Nop();
_Nop();
_Nop();
_Nop();
_Nop();
_Nop();
_Nop();
_Nop();
}


void IIC_init()  //
{
 SDA=1;
 SCL=1;
}

void IIC_write_byte(int8 adder)
{
 uint8 i,temp;
 temp=adder;
 for(i=0;i<8;i++)
 {
  temp=temp<<1;
  SDA=CY;
  SCL=1;              
  _Nop();
  _Nop();            
  _Nop();
  _Nop();
  _Nop();
  SCL=0;
 }
    _Nop();
    _Nop();
    SDA=1;               
    _Nop();
    _Nop();
    SCL=1;
    _Nop();
    _Nop();
    _Nop();
}

uint8 IIC_read_byte()
{
 uint8 i,j,k;
 SCL=0;
    _Nop();
    _Nop();
    _Nop();
    _Nop();
    _Nop();
    _Nop();
    _Nop();
    _Nop();
    _Nop();
    _Nop();
 SDA=1;
    _Nop();
    _Nop();
    _Nop();
    _Nop();
    _Nop();
    _Nop();
    _Nop();
    _Nop();
    _Nop();
    _Nop();
 for(i=0;i<8;i++)
 {
  SCL=1;
     _Nop();
     _Nop();
     _Nop();
  j=SDA;
  k=(k<<1)|j;
  SCL=0;
     _Nop();
     _Nop();
     _Nop();
     _Nop();
     _Nop();
     _Nop();
     _Nop();
     _Nop();
     _Nop();
     _Nop();

 }
    _Nop();
    _Nop();
    _Nop();
 return k;

}

uint8 IIC_respons()
{

    _Nop();
    _Nop();
    _Nop();
    _Nop();
    _Nop();
    _Nop();
    _Nop();
    _Nop();
    if(SDA==1)
 {
  return 0;
 }
    SCL=0;
    _Nop();
    _Nop();
 return 1;
}


iic.h

 

#ifndef __IIC_H_
#define __IIC_H_

#include

sbit SDA = P2^1; //PIN 13 of MA7455 is data
sbit SCL = P2^0; //PIN 14 of MA7455 is clock

void IIC_delay();
void IIC_init();
void IIC_start();
void IIC_stop();

void IIC_write_byte(uint8);
uint8 IIC_read_byte();
uint8 IIC_respons();

 

mma7455.c

 

#include
#include
#include "iic.h"
#include"mma7455.h"

#define NOP() _nop_()  
#define _Nop() _nop_()  

void IIC_adder_write_byte(int8 adder,int8 rw)
{
 uint8 i,temp;
 temp=adder;
 temp=temp<<1; //Shift left one bit from device address,
 temp=temp|rw; //Read and write one bit later

 IIC_delay();
 for(i=0;i<8;i++)
 {
  temp=temp<<1;
  SDA=CY;
  SCL=1;              
  _Nop();
  _Nop();            
  _Nop();
  _Nop();
  _Nop();
  SCL=0;
 }
    _Nop();
    _Nop();
    SDA=1;               
    _Nop();
    _Nop();
    SCL=1;
    _Nop();
    _Nop();
    _Nop();
}

 

mma7455.h

 

#ifndef __MMA7455_H__
#define __MMA7455_H__

#define MMA7455_ADDER_WRITE 0x3A //0x1D shift left, add a write bit 0 at the end, get 0x3A
#define MMA7455_ADDER_READ 0x3B //0x1D shift left, add a read bit 1 at the end, get 0x3B
#define MMA7455_Mode_Control_Register 0x16
#define MMA7455_WHOAMI 0X0F
#define MMA7455_2G_Measurement_Mode 0x05
#define MMA7455_READ_X 0x06 //Read 8bits X-axis data address
#define MMA7455_READ_Y 0x07 //Read 8bits Y-axis data address
#define MMA7455_READ_Z 0x08 //Read 8bits Y-axis data address

void IIC_adder_write_byte(int8,int8);


mytype.h

#ifndef _MY_TYPE_H__
#define _MY_TPYE_H__

#define uint8 unsigned char 
#define uint16 unsigned short int
#define uint32 unsigned long int
#define uint64 unsigned long long int

#define int8 signed char
#define int16 signed short int
#define int32 signed long int
#define int64 signed long long int

#endif


Keywords:STC89C52  MMA7455 Reference address:Read and write MMA7455 program on STC89C52

Previous article:DMA demo for STM32, USART
Next article:Solution to unsuccessful stm32 serial communication

Recommended ReadingLatest update time:2024-11-16 23:35

8051 single chip microcomputer (STC89C52) eight-segment digital tube stably displays 0 ~ 7
On the basis of the program that displays 0~7 in turn, the delay provided by the delay() function is greatly reduced to achieve a short visual stay effect. In this case, the brightness and contrast of the digital tube are somewhat lower than when it is displayed in turn. In addition, if you want to perceive the existe
[Microcontroller]
STC89C52 MCU UART AD DA test experiment
///////////////////////////////////////////////////// ////////////////////////////  Functions implemented: Adjust the potentiometer labeled AD0 on the experimental board to change the voltage, and         transmit the data to the microcontroller through the acquisition of PCF8591. The microcontroller processes the co
[Microcontroller]
STC89C52RC 8-bit running light program
The circuit is very simple, so you don't need to draw it. Just connect a resistor to the P0 port and then connect 8 LED lights. #include reg52.h //header file /***********Macro definition***********************/ #define uint unsigned int #define uchar unsigned char /*********Array definition and assignment************
[Microcontroller]
What is the difference between the microcontroller STC89C52 and STC89C52RC?
STC89C52RC is an enhanced MCS-51 microcontroller from Hongjing Company. Compared with Atmel's AT89C52, it has the following advantages: (1) Supports STC's 2-wire download method, making program downloading more convenient; (2) Supports 6T mode (in 6T mode, 6 clock cycles are one machine cycle) (3) I
[Microcontroller]
Design of intelligent lighting control system based on STC89C52 microcontroller
At present, most of the lighting systems in teaching buildings and student dormitories in universities in my country are controlled by timing, which causes problems such as a large waste of electric energy and inflexible lighting modes. Based on the 51 microcontroller, this article realizes intelligent and dynamic con
[Microcontroller]
Design of intelligent lighting control system based on STC89C52 microcontroller
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号