| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- /********************************************************************************
- **** Copyright (C), 2019, Fortior Technology Co., Ltd. ****
- ********************************************************************************
- File Name : I2C.c
- Author : Bruce HW&RD
- Date : 2019-03-27
- Description : .C file function description
- Version : 1.0
- Function List :
- Record :
- 1.Date : 2019-03-27
- Author : Bruce HW&RD
- Modification: Created file
- ********************************************************************************/
- #include "I2C.h"
- /*
- void I2C_Init_Master(void)
- {
- SetBit(P0_PU , P00);
- SetBit(P0_PU , P01);
- // I2C initial
- SetBit(I2C_CR , I2CMS); //主从模式选择 0-->Slave Mode 1-->Host Mode
- ClrBit(I2C_CR , I2CSPD1); //00-->100kHz 01-->400kHz
- SetBit(I2C_CR , I2CSPD0); //10-->1MHz 11-->RSV
- ClrBit(I2C_CR , I2CIE); //中断使能 0-->Disable 1-->Enable
- I2C_ID = 0xD0;
- SetBit(I2C_ID , GC); //广播呼叫使能 0-->Disable 1-->Enable
- SetBit(I2C_CR , I2CEN); //I2C使能 0-->Disable 1-->Enable
- }
- void I2C_ID_Start(bool rw)
- {
- if(rw)
- SetBit(I2C_SR , DMOD);
- else
- ClrBit(I2C_SR , DMOD);
- SetBit(I2C_SR , I2CSTA); //1-->发送START或RESTART和地址字节
- while(!ReadBit(I2C_SR , STR));
- ClrBit(I2C_SR , STR);
- }
- void I2C_Addr_Write(unsigned char addr,unsigned char wdata)
- {
- I2C_ID_Start(0); // ID write
- I2C_DR = addr; // Slave reg addr.
- while(!ReadBit(I2C_SR , STR));
- ClrBit(I2C_SR , STR);
- I2C_DR = wdata; // Slave reg data.
- while(!ReadBit(I2C_SR , STR));
- ClrBit(I2C_SR , STR);
- SetBit(I2C_SR , I2CSTP); //1-->发送STOP
- }
- unsigned char I2C_Addr_Read(char addr)
- {
- unsigned char rd_data;
- //write process
- I2C_ID_Start(0); // write+ID
- I2C_DR = addr; // write reg. addr
- while(!ReadBit(I2C_SR , STR));
- ClrBit(I2C_SR , STR);
- // SetBit(I2C_SR , I2CSTP); //1-->发送STOP
- //read process
- I2C_ID_Start(1); // read+ID
- while(!ReadBit(I2C_SR , STR));
- ClrBit(I2C_SR , STR);
- rd_data = (unsigned char)(I2C_DR); // read data
- SetBit(I2C_SR , NACK); //第9位发送NACK
- SetBit(I2C_SR , I2CSTP); //1-->发送STOP
- return(rd_data);
- }*/
|