lpc1788 io port emulation iic

Publisher:limm20032003Latest update time:2017-01-13 Source: eefocusKeywords:lpc1788 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

#ifndef __MYIIC_H_

#define __MYIIC_H_

 

#include "common.h"

#include "delay.h"

#include "debugserial.h"

 

//sda 027 scl 028

//IO direction setting

#define IIC1_SDA_IN()  P0dir(27) = 0

#define IIC1_SDA_OUT() P0dir(27) = 1

 

//IO operation function    

#define IIC1_READ_SDA P0in(27) //Input SDA

 

 

void my_iic_init(void); //Initialize IIC IO port     

 

void my_iic_start(void); //Send IIC start signal

 

void my_iic_stop(void); //Send IIC stop signal

 

void my_iic_sendByte(u8 txd); //IIC sends a byte

 

u8 my_iic_readByte(void); //IIC reads a byte

 

u8 my_iic_waitAck(void); //IIC waits for ACK signal

 

void my_iic_ack(void); //IIC sends ACK signal

 

void my_iic_nAck(void); //IIC does not send ACK signal

 

 

 

 

#endif

 

 

 

 

#include "myiic.h"

 

//This driver is for analog IIC operation

 

void my_iic_init(void) //Initialize IIC IO port 

{

    LPC_SC->PCONP |= (1<<15);

   

    LPC_IOCON->P0_27 = 0x00; //Select gpio function, disable hysteresis, non-reverse, normal push-pull

    LPC_IOCON->P0_27 |= (1<<8); //Dependent on pin configuration, modify according to actual pin

   

    LPC_IOCON->P0_28 = 0x00; //Select gpio function, disable hysteresis, non-reverse, normal push-pull

    LPC_IOCON->P0_28 |= (1<<8); //Dependent on pin configuration, modify according to actual pin

   

    P0dir(27) = 1; //scl output

    P0dir(28) = 1;//sda output

    P0high(27) = 1; //initialization is all high level

    P0high(28) = 1;

   

   

}

 

void my_iic_start(void) //Send IIC start signal

{

    IIC1_SDA_OUT(); //sda line output

    P0high(27)=1;       //SDA HIGH 

    P0high(28)=1;       //SCL HIGH

    DelayUs(5);

    P0low(27)=1;        //SDA LOW

    DelayUs(5);

    P0low(28)=1;        //SCL LOW

}

 

void my_iic_stop(void) //Send IIC stop signal

{

    IIC1_SDA_OUT(); //sda line output

    P0high(28)=1;       //SCL HIGH

    P0low(27)=1;        //SDA LOW

    DelayUs(5);

    P0high(27)=1; //Send I2C bus end signal SDA HIGH

    DelayUs(5);

}

 

 

void my_iic_sendByte(u8 txd) //IIC sends a byte

{

    u8 t;  

    IIC1_SDA_OUT();        

    for(t=0;t<8;t++)

    {             

        if(((txd&0x80)>>7))

            P0high(27) = 1;

        else

            P0low(27) = 1;

        txd<<=1;         

        P0high(28)=1;   //SCL HIGH

        DelayUs(5);

        P0low(28)=1;    //SCL LOW

        DelayUs(5);

    }  

}

 

 

u8 my_iic_readByte(void) //IIC reads a byte

{

    unsigned char i,receive=0;

    IIC1_SDA_IN(); //SDA is set as input

    for(i=0;i<8;i++ )

    {

        receive<<=1;

        P0high(28)=1;           //SCL HIGH  

        DelayUs(5);

        if(IIC1_READ_SDA)receive++;

        P0low(28)=1;            //SCL LOW

        DelayUs(5);     

    }                   

    return receive;

}

 

 

u8 my_iic_waitAck(void) //IIC waits for ACK signal

{

    u8 ucErrTime=0; 

    IIC1_SDA_OUT();

    P0high(27)=1;       //SDA HIGH 

    DelayUs(5);

    IIC1_SDA_IN(); //SDA is set as input      

    P0high(28)=1;               //SCL HIGH

    DelayUs(5); 

    while(IIC1_READ_SDA)

    {

        ucErrTime++;

        DelayUs(1);

        if(ucErrTime>250)

        {

            my_iic_stop();

            return 1;

        }

    }

    P0low(28)=1; //Clock output SCL LOW

    DelayUs(5);   

    return 0;

}

 

 

void my_iic_ack(void) //IIC sends ACK signal

{

    IIC1_SDA_OUT();

    P0low(27)=1;        //SDA LOW

    DelayUs(2);

    P0high(28)=1;       //SCL HIGH

    DelayUs(5);

    P0low(28)=1;        //SCL LOW

    DelayUs(5);

}

 

 

void my_iic_nAck(void) //IIC does not send ACK signal

{

    IIC1_SDA_OUT();

    P0high(27)=1;       //SDA HIGH

    DelayUs(2);

    P0high(28)=1;       //SCL HIGH

    DelayUs(5);

    P0low(28)=1;        //SCL LOW

    DelayUs(5);

}


Keywords:lpc1788 Reference address:lpc1788 io port emulation iic

Previous article:LPC1788 nand driver
Next article:LPC2478 hardware IIC usage

Recommended ReadingLatest update time:2024-11-16 15:02

MicroPython LPC1788 porting
Environment Construction I won’t say much about setting up the environment, just follow the official wiki to set it up. Transplantation steps illustrate Micropytho uses gcc for compilation, so the official lpcopen_2_10_lpcxpresso_ea_devkit_1788 development kit is used. Also download and install MCUXpresso IDE, whi
[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号