ADS_LPC2103 development board I2C test experiment

Publisher:rnm888Latest update time:2015-11-13 Source: eefocusKeywords:ADS Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
#include "config.h" //Related header files



#define C02MasterAddr 0xa0 //C02 device address

#define C02SlaveAddr 0 //Starting address for reading and writing data

#define LED (1 << 19) //LED IO number

#define LED2 (1 << 20)

#define AA (1 << 2) //Definition of each bit of I2CONSET

#define SI (1 << 3)
#define STO (1 << 4)
#define STA (1 << 5)
#define I2EN (1 << 6)

#define WRMode 0 //Write mode

#define RDMode 1 //Read mode


#define lastbyte 1 //Is it the last byte




uint8 I2C_buf1 = 5;
uint8 I2C_buf2 = 0;
//Delay function

void Delay(uint32 dly)
{
    uint32 i;
    
    for(; dly > 0; dly--)
    {
     for(i = 0; i < 50000; i++);
    }
}

//I2C initialization function

void I2C_Init(uint32 Fi2c)
{
    if(Fi2c > 400000)
    {
     Fi2c = 400000;
    }
    
    PINSEL0 = (PINSEL0 & (~0xf0)) | 0x50;//Here is to let P02, P03 select the second function, that is, I2C bus

    
    I2SCLH = (Fpclk/Fi2c + 1) / 2;//Set I2C speed to 400K

    I2SCLL = (Fpclk/Fi2c) / 2;
    
    I2CONCLR = 0x2c; //Master transmission mode clears STA, AA, SI

    I2CONSET = 0x40;//Use I2C bus

    
// I2CONCLR = STA|AA|SI;

// I2CONSET = I2EN;

    
}
//I2C bus start function

void I2C_Start(void)
{
    I2CONSET = STA;//set start condition

    do{}while(0x08 != I2STAT);//A start condition has been transmitted

    I2CONCLR = STA;//clear start condition

}

//I2C bus stop function

void I2C_Stop(void)
{
    I2CONSET = STO; //set STO condition

    I2CONCLR = SI; //clear SI condition

}
//write byte function

void WriteByte(uint8 data)
{
    I2DAT = data;// data transmitted

    I2CONCLR = SI;//clear SI condition

}
//wirte I2C bus address mode is RDMode or WRMode

void WriteAddr(uint8 Mode)
{
    WriteByte(C02MasterAddr + Mode);//transmite the I2C address and R/W mode

    
    if(Mode)
    {
        do{}while(0x40 != I2STAT);//SLA+R has been transmitted;ACK has been received

    }
    else
    {
        do{}while(0x18 != I2STAT);//SLA+W has been transmitted;ACK has been received

    }
}
//write data to I2C

void WriteData(uint8 data)
{
    WriteByte(data);// data is will transmitted data

    do{}while(0x28 != I2STAT);//Data byte in I2DAT has been transmitted; ACK has been received

}
//read i2c byte

uint8 ReadByte(uint8 last)
{
    if(last)
    {
        I2CONCLR = AA;
        I2CONCLR = SI;
        
        do{} while(0x58 != I2STAT);//data byte has been received;NOT ACK has been returned

    }
    else
    {
        I2CONSET = AA;//transmited ACK

        I2CONCLR = SI;
        do{}while(0x50 != I2STAT);//data byte has been received;ACK has been returned.

    }    
    
    return I2DAT;//return read data

}
//write data into 24c02

void WriteC02(void)
{
//    uint8 i;//define unsigned char i

    I2C_Start();
    WriteAddr(WRMode);
    WriteData(C02SlaveAddr);//the data stor address

    
    WriteData(I2C_buf1);
    I2C_Stop();
}

//read I2C data

void ReadC02(void)
{
//    uint8 i;

    I2C_Start();
    WriteAddr(WRMode);
    WriteData(C02SlaveAddr);
    I2C_Stop();
    
    I2C_Start();
    WriteAddr(RDMode);
    
    I2C_buf2 = ReadByte(lastbyte);
    
    I2C_Stop();
}

int main(void)
{
    
    PINSEL0 = 0x00000000;
    PINSEL1 = 0x00000000;
    
    IODIR = LED|LED2;
    IOSET = LED|LED2;

    
    I2C_Init(400000);
    
    WriteC02();
    Delay(1000);
    ReadC02();
    
    if(I2C_buf2 == I2C_buf1)
    {
    while(1){
        IOCLR = LED;//correct LED1 light

        Delay(50);
        IOSET = LED;
        Delay(50);
        }
    }
    else
    {
        while(1)
        {
            IOCLR = LED2;//error LED2 light

            Delay(50);
         IOSET = LED2;
            Delay(50);
        }
    }
    
    


return 0;    
}
Keywords:ADS Reference address:ADS_LPC2103 development board I2C test experiment

Previous article:ADS_LPC2103 development board SPI 4-bit digital tube test experiment
Next article:First Example of IAR ARM Based on LPC2131: LED Blinking

Recommended ReadingLatest update time:2024-11-16 09:01

BOE and Changhong jointly release 8K ADS Pro high-end TV, opening a new era of true color immersive display
On August 8, Changhong TV held a new product launch conference "Opening a New 8K Vision", launching a new series of high-end new products - Changhong Q9K Max TV . Equipped with BOE's world-first 75-inch to 110-inch full-series 8K 120Hz giant screens and the industry-leading high-end ADS Pro display solution, it has ac
[Home Electronics]
Huawei starts mass production of "L4" ahead of schedule
After six years of secret research and development, with a team of more than 2,000 people, Huawei has finally revealed its trump card in the field of autonomous driving. The "advanced" autonomous driving solution called ADS will be available on mass-produced cars in Q1 2022, allowing consumers to achieve autonomous
[Automotive Electronics]
Huawei starts mass production of
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号