#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);
}
Previous article:LPC1788 nand driver
Next article:LPC2478 hardware IIC usage
Recommended ReadingLatest update time:2024-11-16 15:02
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Dismantling and Renovation of Old LED Bulbs
- Emitter follower principle and typical circuit
- STM32 A certain function is put into RAM and runs without any phenomenon
- I searched Baidu for half an hour but couldn't find the answer. I used a search engine to find the answer in seconds.
- EEWORLD University Hall ---- Disassembling the front controller module of Tesla Model 3 VCFRONT
- How to use JTAG to program UCD90120A
- Creating a Superior Smart Display Experience with DLP Pico Technology
- Previous test questions of the measurement and control category of the electric competition
- instaSPIN-FOC Motor Starting
- TGF4042 Function Signal Generator Evaluation: Pulse Waveform Generation