N76E003 <========IIC========> 脱落仪模块(IIC Slave)
IIC 模拟部分代码:
bsp_iic.c
#include "bsp_iic.h"
unsigned char SDA_READ(void)
{
if(SDA_RD)
return 1;
else
return 0;
}
void delay_10us(unsigned int dly)
{
Delay10us(dly);
}
void IIC_Start(void)
{
SDA_H();
SCL_H();
delay_10us(RW_DELAY);
SDA_L();
delay_10us(RW_DELAY);
SCL_L();
}
void IIC_Stop(void)
{
SDA_L();
SCL_L();
delay_10us(RW_DELAY);
SCL_H();
delay_10us(RW_DELAY);
SDA_H();
delay_10us(RW_DELAY);
}
void IIC_Ack(void)
{
SCL_L();
SDA_L();
delay_10us(RW_DELAY);
SCL_H();
delay_10us(RW_DELAY);
SCL_L();
}
void IIC_NAck(void)
{
SCL_L();
SDA_H();
delay_10us(RW_DELAY);
SCL_H();
delay_10us(RW_DELAY);
SCL_L();
}
unsigned char IIC_Wait_Ack(void)
{
unsigned char ucErrTime = 0;
SDA_H();
delay_10us(1);
SCL_H();
delay_10us(1);
while(SDA_READ()) {
ucErrTime++;
if(ucErrTime > 250) {
IIC_Stop();
return 1;
}
}
SCL_L();
return 0;
}
void IIC_WriteByte(unsigned char SendByte)
{
unsigned char t;
SCL_L(); // Set CLK as low to start
delay_10us(RW_DELAY);
for (t = 0; t < 8; t++) {
if ((SendByte & 0x80) >> 7) {
SDA_H();
} else {
SDA_L();
}
SendByte <<= 1;
delay_10us(RW_DELAY);
SCL_H();
delay_10us(RW_DELAY);
SCL_L();
delay_10us(RW_DELAY);
}
}
unsigned char IIC_ReadByte(unsigned char ack)
{
unsigned char i, receive = 0;
SCL_L();
for (i = 0; i < 8; i++) {
delay_10us(RW_DELAY);
SCL_H();
receive <<= 1;
if (SDA_READ()) {
receive++;
}
delay_10us(RW_DELAY);
SCL_L();
delay_10us(RW_DELAY);
}
if (!ack) {
IIC_NAck();
} else {
IIC_Ack();
}
return receive;
}
void write_add(unsigned char slaveaddr,unsigned char regaddr,unsigned char dat)
{
IIC_Start();
IIC_WriteByte(slaveaddr | IIC_WR);
IIC_Wait_Ack();
IIC_WriteByte(regaddr);
IIC_Wait_Ack();
delay_10us(10);
IIC_WriteByte(dat);
IIC_Wait_Ack();
IIC_Stop();
}
unsigned char read_add(unsigned char slaveaddr,unsigned char regaddr)
{
unsigned char dat;
IIC_Start();
IIC_WriteByte(slaveaddr | IIC_WR);
IIC_Wait_Ack();
IIC_WriteByte(regaddr);
IIC_Wait_Ack();
IIC_Stop();
//delay_10us(10);
IIC_Start();
IIC_WriteByte(slaveaddr | IIC_RD);
IIC_Wait_Ack();
dat=IIC_ReadByte(0);
IIC_Stop();
return dat;
}
u8 IIC_WriteToSlave(u8 DevAddr, u8 RegAddr, u8 len, u8 *WData)
{
u8 i;
IIC_Start();
IIC_WriteByte( DevAddr|0x00);//???,???????
if(IIC_Wait_Ack()) //1 Ack Failed, 0 Ack Success
return 1;
IIC_WriteByte(RegAddr);//???
if(IIC_Wait_Ack()) //1 Ack Failed, 0 Ack Success
return 2;
for(i = 0; i < len; i++)
{
IIC_WriteByte(WData[i]);//???
if(IIC_Wait_Ack()) //1 Ack Failed, 0 Ack Success
return 3;
}
IIC_Stop();
return 4;
}
u8 IIC_ReadFromSlave(u8 DevAddr, u8 RegAddr, u8 len, u8 *RData)
{
IIC_Start();
IIC_WriteByte( DevAddr|0x00);//???,???????
if(IIC_Wait_Ack()) //1 Ack Failed, 0 Ack Success
return 1;
IIC_WriteByte(RegAddr);//???
if(IIC_Wait_Ack()) //1 Ack Failed, 0 Ack Success
return 2;
IIC_Start();
IIC_WriteByte( DevAddr|0x01);//???,???????
if(IIC_Wait_Ack()) //1 Ack Failed, 0 Ack Success
return 3;
while(len)
{
if(len == 1)
{
*RData = IIC_ReadByte(0); //???,??????nAck
}
else
{
*RData = IIC_ReadByte(1); //??????????????Ack
}
len--;
RData++;
}
IIC_Stop();
return 4;
}
//================TEST====================
#define SLAVE_ADDR 0xAA
void IIC_TEST(void)
{
// SDA_H();
// SCL_H();
// delay_10us(10);
// SDA_L();
// SCL_L();
// delay_10us(10);
u8 res;
u8 temp_arr_pitch[2];
u8 temp_arr_roll[2];
u8 temp_arr_yaw[2];
signed short pitch, roll,yaw;
res = IIC_ReadFromSlave(SLAVE_ADDR, 0x3D, 2, temp_arr_pitch);
delay_10us(300);
// //dbg(0, ("res = %bxrn",res));
res = IIC_ReadFromSlave(SLAVE_ADDR, 0x3E, 2, temp_arr_roll);
res = IIC_ReadFromSlave(SLAVE_ADDR, 0x3F, 2, temp_arr_yaw);
//
// pitch = (temp_arr_pitch[0]<<8) | temp_arr_pitch[1]; //??????X -90~90
// roll = (temp_arr_roll[0]<<8) | temp_arr_roll[1]; //??o???Y -180~180
// yaw = (temp_arr_yaw[0]<<8) | temp_arr_yaw[1]; //Dy¡Áa??Z -180~180
//
// //dbg(0, ("pitch: %+6d, roll:%+6d, yaw:%+6drn",pitch, roll, yaw));
}
bsp_iic.h
#ifndef __BSP_IIC_H
#define __BSP_IIC_H
#include "N76E003.h"
#include "Common.h"
#include "Delay.h"
/*<-------------------N76E003 Ä£Äâ IIC------------------>*/
typedef unsigned char u8;
/**
* P00 SCL
* P01 SDA
*/
#define SDA_RD P01 //SDA READ
#define SDA_H() (P01 = 1) //SDA HIGH
#define SDA_L() (P01 = 0) //SDA LOW
#define SCL_H() (P00 = 1) //SCL HIGH
#define SCL_L() (P00 = 0) //SCL LOW
#define RW_DELAY 1 //unit 10us
#define IIC_WR 0 //IIC Write cmd
#define IIC_RD 1 //IIC Read cmd
unsigned char SDA_READ(void);
void delay_10us(unsigned int dly);
void IIC_Start(void);
void IIC_Stop(void);
void IIC_Ack(void);
void IIC_NAck(void);
unsigned char IIC_Wait_Ack(void);
void IIC_WriteByte(unsigned char SendByte);
unsigned char IIC_ReadByte(unsigned char ack);
void write_add(unsigned char slaveaddr,unsigned char regaddr,unsigned char dat);
unsigned char read_add(unsigned char slaveaddr,unsigned char regaddr);
u8 IIC_WriteToSlave(u8 DevAddr, u8 RegAddr, u8 len, u8 *WData);
u8 IIC_ReadFromSlave(u8 DevAddr, u8 RegAddr, u8 len, u8 *RData);
void IIC_TEST(void);
#endif /* __BSP_IIC_H */
main.c的调用:
void main (void)
{
Set_All_GPIO_Quasi_Mode; // Define in Function_define.h
InitialUART0_Timer3(115200);
set_CLOEN;
while(1)
{
IIC_TEST();
}
}
IIC clk速度设置到最快:
上一篇:N76E003 学习三 按键控制LED亮灭
下一篇:N76E003 单片机 IIC 软模拟
推荐阅读最新更新时间:2024-11-08 18:56
设计资源 培训 开发板 精华推荐
- NCV33174DTBR2G 直流耦合反相放大器最大输出摆幅的典型应用,采用单 +5V 电源
- LT1963A 系列的典型应用 - 1.5A、低噪声、快速瞬态响应 LDO 稳压器
- 分立功放 mos输出版1969
- 使用 MaxLinear, Inc 的 SPX385AN-1.2/TR 的参考设计
- LTC3562 演示板、I2C 四路同步降压型 DC/DC 稳压器
- NCV2002SN1T1G 1V语音带滤波器典型应用电路
- DC2237A,使用 LTM8064 CVCC 源/灌降压模块稳压器的演示板,7.5V = VIN = 58V,Vout = 5V @ ±6A
- LTC2938IMS ±5V 电源监视器的典型应用电路,看门狗已禁用且未使用的输入为高电平
- STEVAL-CBL012V1,用于双 LNB 电源和控制 IC DiSEqC 2.0 的评估板,基于 LNBH26
- 具有 PWM 调光功能的 NSV50350AST3G LED 灯串的典型应用
- 6nm EUV工艺 联发科全新5G芯片将杀到:60万跑分
- 公共场所Wi-Fi如何连?电信科普:6点要求保平安
- 三星发布了Exynos 1080旗舰芯片:5纳米EUV工艺 vivo首发
- 苹果M1芯片MacBook Pro跑分解密:单核1714,多核6802
- 韩媒:台积电5nm产能满载,三星或成苹果M1芯片代工
- 新专利显示苹果计划让Apple Glass舒适地提供了沉浸式体验
- vivo WATCH发布,圆形表盘强调设计 价格1299元
- 华米Amazfit GTR 2评测:时尚背后的一整套健康体系
- LG入门级新机W11在Google Play现身,联发科P22+3GB内存
- 华米发布GT系列两款新表:新增血氧检测 售价为999元