Lpc1788io Mouth Simulated IIC

Source: Internet
Author: User

#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); Initializing the IO port of the IIC

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 one 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 an ACK signal

#endif

#include "myiic.h"

The drive is designed for simulating IIC operations

void My_iic_init(void) //initialization of IIC IO port

{

LPC_SC, Pconp |= (1<<);

Lpc_iocon-p0_27 = 0x00; Select Gpio function, disable hysteresis not reverse normal push-pull

Lpc_iocon, p0_27 |= (1<<8); Related to pin configuration modification according to the actual pin

Lpc_iocon-p0_28 = 0x00; Select Gpio function, disable hysteresis not reverse normal push-pull

Lpc_iocon, p0_28 |= (1<<8); Related to pin configuration modification according to the actual pin

P0dir(+) = 1; SCL output

P0dir(+) = 1; SDA output

P0high(+) = 1; Initialization is high level

P0high(+) = 1;

}

void My_iic_start(void) //Send IIC start signal

{

Iic1_sda_out(); SDA Line output

P0high(+) =1; SDA High

P0high(+) =1; SCL High

Delayus(5);

P0low(+) =1; SDA Low

Delayus(5);

P0low(+) =1; SCL Low

}

void My_iic_stop(void) //Send IIC stop signal

{

Iic1_sda_out(); SDA Line output

P0high(+) =1; SCL High

P0low(+) =1; SDA Low

Delayus(5);

P0high(+) =1; Send I²c bus end signal SDA high

Delayus(5);

}

void My_iic_sendbyte(u8 txd) //iic send a byte

{

U8 T;

Iic1_sda_out();

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

{

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

P0high(+) = 1;

Else

P0low(+) = 1;

Txd<<=1;

P0high(+) =1; SCL High

Delayus(5);

P0low(+) =1; SCL Low

Delayus(5);

}

}

U8 my_iic_readbyte(void) //iic reads one byte

{

unsigned char i,receive=0;

Iic1_sda_in(); SDA set to input

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

{

Receive<<=1;

P0high(+) =1; SCL High

Delayus(5);

if (IIC1_READ_SDA)receive+ +;

P0low(+) =1; SCL Low

Delayus(5);

}

return receive;

}

U8 my_iic_waitack(void) //IIC wait for ACK signal

{

U8 ucerrtime=0;

Iic1_sda_out();

P0high(+) =1; SDA High

Delayus(5);

Iic1_sda_in(); SDA set to input

P0high(+) =1; SCL High

Delayus(5);

while (iic1_read_sda)

{

Ucerrtime+ +;

Delayus(1);

if (ucerrtime>)

{

My_iic_stop();

return 1;

}

}

P0low(+) =1; Clock output SCL Low

Delayus(5);

return 0;

}

void My_iic_ack(void) //IIC sends an ACK signal

{

Iic1_sda_out();

P0low(+) =1; SDA Low

Delayus(2);

P0high(+) =1; SCL High

Delayus(5);

P0low(+) =1; SCL Low

Delayus(5);

}

void My_iic_nack(void) //IIC does not send an ACK signal

{

Iic1_sda_out();

P0high(+) =1; SDA High

Delayus(2);

P0high(+) =1; SCL High

Delayus(5);

P0low(+) =1; SCL Low

Delayus(5);

}

Lpc1788io Mouth Simulated IIC

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.