2440 driver MMA7455L source program

Publisher:技术掌门Latest update time:2020-07-12 Source: 51heiKeywords:Driver  MMA7455L Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

The microcontroller source program is as follows:

/****************************************************************

File name: Main.c

Function description: Main program

********************************************************************/

#define GLOBAL_CLK 1


#include

#include

#include "def.h"

#include "option.h"

#include "2440addr.h"

#include "2440lib.h"

#include "2440slib.h"

#include "Uart.h"

void SPI_Init(void);

void WriteByte_MMA7455L(U8 addr,U8 dat);

U8 ReadByte_MMA7455L(U8 addr);                                               

static U32 cpu_freq;       

static U32 UPLL;               

static void cal_cpu_bus_clk(void)

{

        U32 val;

        U8 m, p, s;       

        val = rMPLLCON; //MPLLCON power-on initial value is 0x00096030

        m = (val>>12)&0xff; //The result is 0x96=150

        p = (val>>4)&0x3f; //The result is 0x03=3

        s = val&3; //The result is 0

        //(m+8)*FIN*2 Do not exceed 32 digits!

        FCLK = ((m+8)*(FIN/100)*2)/((p+2)*(1<        val = rCLKDIVN;

        m = (val>>1)&3;

        p = val&1;       

        val = rCAMDIVN;

        s = val>>8;       

        switch (m) {

        case 0:

                HCLK = FCLK;

                break;

        case 1:

                HCLK = FCLK>>1;

                break;

        case 2:

                if(s&2)

                        HCLK = FCLK>>3;

                else

                        HCLK = FCLK>>2;

                break;

        case 3:

                if(s&1)

                        HCLK = FCLK/6;

                else

                        HCLK = FCLK/3;

                break;

        }       

        if(p)

                PCLK = HCLK>>1;

        else

                PCLK = HCLK;       

        if(s&0x10)

                cpu_freq = HCLK;

        else

                cpu_freq = FCLK;               

        val = rUPLLCON;

        m = (val>>12)&0xff;

        p = (val>>4)&0x3f;

        s = val&3;

        UPLL = ((m+8)*FIN)/((p+2)*(1<        UCLK = (rCLKDIVN&8)?(UPLL>>1):UPLL;

}


void Main(void)

{

        U8 key,sta;                       

        int i;                               

        U32 mpll_val = 0;

        i = 2 ;       

        switch ( i ) {

        case 0: //200

                key = 12;

                mpll_val = (92<<12)|(4<<4)|(1);

                break;

        case 1: //300

                key = 13;

                mpll_val = (67<<12)|(1<<4)|(1);

                break;

        case 2: //400M

                key = 14;

                mpll_val = (92<<12)|(1<<4)|(1);

                break;

        case 3: //440!!!

                key = 14;

                mpll_val = (102<<12)|(1<<4)|(1);

                break;

        default:

                key = 14;

                mpll_val = (92<<12)|(1<<4)|(1);

                break;

        }

        //rMPLLCON is assigned 0x5c011

        ChangeMPllValue((mpll_val>>12)&0xff, (mpll_val>>4)&0x3f, mpll_val&3);

        ChangeClockDivider(key, 12); //key=14

        cal_cpu_bus_clk();

       

        Port_Init(); //IO port initialization

        SPI_Init(); //SPI initialization

        MMA7455L_INIT(); //MMA7455L initialization

        Uart_Init(); //Serial port initialization

        Uart_Select(0);

        Delay(5); //Delay 10ms to let IIC and SPI get ready

        Uart_Printf("CLKCON=%x!",rCLKCON);

        Uart_Printf("Start sending data!nn");       

        //Write configuration data 0x05 to the 0x16 register of MMA7455L

        WriteByte_MMA7455L(0x16,0x05);

        //Read back the configuration value of register 0x16 to determine whether the SPI communication is successful.

        sta = ReadByte_MMA7455L (0x16);   

        Uart_Printf("sta=0x%x",sta);

        while(1)

        {


        }

}


void WriteByte_MMA7455L(U8 addr,U8 dat)

{

        int n;

        //Set chip select CS to low to enable MMA7455 in SPI mode

        rGPFDAT &= 0xef;   

        Delay(1);

       

        Uart_Printf("The data of SPI status register is 0x%x!n",rSPSTA0);

        while(!(rSPSTA0&0x01==0x01)) ;

        //High position 1 write operation

        rSPTDAT0 = 0x80|((addr & 0x3f)<<1);  

        Uart_Printf("The data of SPI status register is 0x%x!n",rSPSTA0);

        Uart_Printf("Write operation address sent successfully!n");       

        for(n=0;n<20;n++);       

        Uart_Printf("The data of SPI status register is 0x%x!n",rSPSTA0);

        while(!(rSPSTA0&0x01==0x01)) ;

        rSPTDAT0 = dat; //write data

        Uart_Printf("The data of SPI status register is 0x%x!n",rSPSTA0);

        Uart_Printf("Write operation data sent successfully!n");       

        for(n=0;n<20;n++);       

        // Set chip select CS to high level to end SPI communication

        rGPFDAT |= 0xff;  

        Delay(1);

}


U8 ReadByte_MMA7455L(U8 addr)

{

        int n;

        U8 Re_Dat;

        //Set chip select CS to low level to enable MMA7455L in SPI mode

        rGPFDAT &= 0xef;   

        Delay(1);       

        Uart_Printf("The data of SPI status register is 0x%x!n",rSPSTA0);

        while(!(rSPSTA0&0x01==0x01)) ;

        //Read operation high clear 0

        rSPTDAT0 = (addr & 0x3f)<<1;   

        Uart_Printf("The data of SPI status register is 0x%x!n",rSPSTA0);

        Uart_Printf("Read operation address sent successfully!n");       

        for(n=0;n<20;n++);       

        Uart_Printf("The data of SPI status register is 0x%x!n",rSPSTA0);

        while(!(rSPSTA0&0x01==0x01));

        Re_Dat = rSPRDAT0;

        Uart_Printf("The data of SPI status register is 0x%x!n",rSPSTA0);       

        for(n=0;n<20;n++);

        // Set chip select CS to high level to end SPI communication

        rGPFDAT |= 0xff;

        Delay(1);

         //Return read data

        return Re_Dat;

}

//MMA7455L initialization

void MMA7455L_INIT(void )

{

   MMA_QUEUE.MMA_HEAD=0;

   MMA_QUEUE.MMA_CUR=0;  

   SPI_WriteREG(0x16,0x05); //Level mode

   delay_nus(10);

   SPI_WriteREG(0x18,0x20);

   delay_nus(10);

   SPI_WriteREG(0x19,0x00);

   delay_nus(10);

   SPI_WriteREG(0x1a,0x00);

   delay_nus(10);

  }

//SPI initialization

void SPI_Init(void)

{       

        //Activate the SPI bit of the clock controller

        rCLKCON |= 1 << 18;

        rSPCON0 = (0<<6)|(0<<5)|(1<<4)|(1<<3)|(0<<2)|(0<<1)|1;

        //Query mode; SCK enable; host mode; clock low level is valid; format A; normal mode;

        //The SPI transmission baud rate of MMA7455 can reach up to 4MHz when the operating voltage is less than 2.4V, and up to 8MHz when it is greater than 2.4V

        //SPI baud rate is 50MHz/2/(24+1)=1MHz

        rSPPRE0 = 24;         

}

Keywords:Driver  MMA7455L Reference address:2440 driver MMA7455L source program

Previous article:Proteus simulation ARM7 LPC2101 AD converter source program
Next article:S3C2440 microcontroller external interrupt experiment

Latest Microcontroller Articles
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号