我使用的STM32芯片:STM32F103ZET6
我们使用的STM32库版本:V3.5.0
注意:
I2cSoft.h:
#ifndef __AOBO_stm32f10x_I2c_H_
#define __AOBO_stm32f10x_I2c_H_
#include "stm32f10x.h"
#include "Gpio.h"
namespace stm32f10x{
class I2cSoft{
public:
I2cSoft(Gpio *sda, Gpio *scl);
void initialize(void);
int singleWrite(u8 SlaveAddress,u8 REG_Address,u8 REG_data);
int singleRead(u8 SlaveAddress,u8 REG_Address);
int multRead(u8 SlaveAddress,u8 REG_Address,u8 * ptChar,u8 size);
private:
Gpio *SDA, *SCL;
void delay(void);
int start(void);
void stop(void);
void ack(void);
void noAck(void);
int waitAck(void);
void sendByte(u8 SendByte);
u8 readByte(void);
};
}
#endif
i2cSoft.cpp
#include "I2cSoft.h"
using namespace stm32f10x;
I2cSoft::I2cSoft(Gpio *sda, Gpio *scl):SDA(sda), SCL(scl){
}
void I2cSoft::initialize(void){
}
void I2cSoft::delay(void)
{
}
int I2cSoft::start(void)
{
SDA->high(); SCL->high(); delay();
if(SDA->islow())return 0;
SDA->low(); delay();
if(SDA->ishigh()) return 0;
SDA->low(); delay();
return 1;
}
void I2cSoft::stop(void)
{
SCL->low(); delay();
SDA->low(); delay();
SCL->high(); delay();
SDA->high(); delay();
}
void I2cSoft::ack(void)
{
SCL->low(); delay();
SDA->low(); delay();
SCL->high(); delay();
SCL->low(); delay();
}
void I2cSoft::noAck(void)
{
SCL->low(); delay();
SDA->high(); delay();
SCL->high(); delay();
SCL->low(); delay();
}
int I2cSoft::waitAck(void)
{
SCL->low(); delay();
SDA->high(); delay();
SCL->high(); delay();
if(SDA->ishigh()){
SCL->low(); delay(); return 0;
}
SCL->low(); delay();
return 1;
}
void I2cSoft::sendByte(u8 SendByte)
{
u8 i=8;
while(i--){
SCL->low(); delay();
if(SendByte&0x80) SDA->high();
else SDA->low();
SendByte<<=1; delay();
SCL->high(); delay();
}
SCL->low();
}
u8 I2cSoft::readByte(void)
{
u8 i=8;
u8 ReceiveByte=0;
SDA->high();
while(i--){
ReceiveByte<<=1;
SCL->low(); delay();
SCL->high(); delay();
if(SDA->ishigh()){
ReceiveByte|=0x01;
}
}
SCL->low();
return ReceiveByte;
}
int I2cSoft::singleWrite(u8 SlaveAddress,u8 REG_Address,u8 REG_data) {
if(!start())return 0;
sendByte(SlaveAddress);
if(!waitAck()) {stop(); return 0;}
sendByte(REG_Address );
waitAck();
sendByte(REG_data);
waitAck();
stop();
return 1;
}
int I2cSoft::singleRead(u8 SlaveAddress,u8 REG_Address){
unsigned char REG_data;
if(!start())return 0;
sendByte(SlaveAddress);
if(!waitAck()){
stop(); return 0;
}
sendByte((u8) REG_Address); waitAck();
start();
sendByte(SlaveAddress+1); waitAck();
REG_data= readByte();
noAck();
stop();
return REG_data;
}
int I2cSoft::multRead(u8 SlaveAddress,u8 REG_Address,u8 * ptChar,u8 size){
uint8_t i;
if(size < 1) return 0;
if(!start()) return 0;
sendByte(SlaveAddress);
if(!waitAck()){
stop(); return 0;
}
sendByte(REG_Address); waitAck();
start();
sendByte(SlaveAddress+1); waitAck();
for(i=1;i<size; i++){
*ptChar++ = readByte();
ack();
}
*ptChar++ = readByte();
noAck();
stop();
return 1;
}
main.cpp
#include "stm32f10x.h"
#include "Gpio.h"
#include "I2cSoft.h"
using namespace stm32f10x;
int main(void)
{
Gpio scl(PA,5);
Gpio sda(PA,6);
I2cSoft i2c(&sda, &scl);
while(true)
{
}
}
搞定
你可以到这里下载我已经做好的 STM32 C++ I2c(Soft)类:
百度云 链接:http://pan.baidu.com/s/1bpbZ2MV 密码:esam
也可以在CSDN里面下载:http://download.csdn.net/detail/github_35160620/9626553
小结:
下一讲,我们来使用 C++ 语言,创建一个 STM32 的 硬件 I2c 类。
|
请发表评论