📄 fm31xx.c
字号:
//***************************************************
#include "DSP28_Device.h"
//**************************************************
struct
{
unsigned int second;
unsigned int minute;
unsigned int hour;
unsigned int week;
unsigned int date;
unsigned int month;
unsigned int year;
}RtcRegs;
unsigned int buffer[32];
/////////////////////////////////////////////////////////////
#define IIC_WRITE 0xa0
#define IIC_READ 0xa1
#define REG_READ 0xd1
#define REG_WRITE 0xd0
//==================================================
#define WRITEPROTECT 0x0b
#define WDTTIME 0x0a
#define WDTFLAG 0x09
#define YEAR 0x08
#define MOUNTH 0x07
#define DAY 0x06
#define DATE 0x05
#define HOUR 0x04
#define MINUTE 0x03
#define SECOND 0x02
#define CONTROL 0x01
#define FLAGS 0x00
//================================================
#define SDA GpioDataRegs.GPADAT.bit.GPIOA0
#define SCL GpioDataRegs.GPADAT.bit.GPIOA2
#define PFO GpioDataRegs.GPADAT.bit.GPIOA1
#define SDA_INPUT() {GpioMuxRegs.GPADIR.all &= 0xfffe;}//SDA作为输入口
#define SDA_OUTPUT() {GpioMuxRegs.GPADIR.all |= 0x0001;}//SDA作为输出口
//================================
void Start(void)
{
EALLOW;
SDA_OUTPUT()
SDA = 1;DelayUs(1);
SCL = 1;DelayUs(1);
SDA = 0;DelayUs(1);
EDIS;
}
//=================================
void Stop(void)
{
EALLOW;
SDA_OUTPUT()
SDA = 0;DelayUs(1);
SCL = 1;DelayUs(1);
SDA = 1;DelayUs(1);
EDIS;
}
//=================================
void WriteByte(unsigned int Wdata)//写一个字节(写int型数据的低位字节,高字节忽略)
{
unsigned int i;
EALLOW;
SDA_OUTPUT()
SCL = 0;DelayUs(1);
for(i=0x80;i!=0;i>>=1)
{
if(Wdata & i) SDA = 1;
else SDA = 0;
DelayUs(1);
SCL = 1;DelayUs(1);
SCL = 0;DelayUs(1);
}
EDIS;
}
//==================================
unsigned int ReadByte(void)//读一个字节(放入int型数据的低位字节,高字节清零)
{
unsigned int i;
unsigned int RetValue=0;
EALLOW;
SDA_INPUT()
SCL = 0;DelayUs(1);
for(i=0;i<8;i++)
{
RetValue <<= 1;
SCL = 1;DelayUs(1);
RetValue += SDA;DelayUs(1);
SCL = 0;DelayUs(1);
}
EDIS;
return RetValue;
}
//====================================
void SlaveAck(void)//查询从机FM31应答信号
{
unsigned int i=10;
EALLOW;
SDA_INPUT()
SCL = 1;DelayUs(1);
while(SDA&&(i--));
{
DelayUs(1);
}
SCL = 0;DelayUs(1);
EDIS;
}
//====================================
void MasterAck(void)//主机应答
{
EALLOW;
SDA_OUTPUT()
SDA = 0;DelayUs(1);
SCL = 1;DelayUs(1);
SCL = 0;DelayUs(1);
EDIS;
}
//====================================
void MasterNoAck(void)//主机无应答
{
EALLOW;
SDA_OUTPUT()
SDA = 1;DelayUs(1);
SCL = 1;DelayUs(1);
SCL = 0;DelayUs(1);
EDIS;
}
//====================================
unsigned int ReadFRAM(unsigned int Address)//单字节读
{
unsigned int RetValue;
Start();
WriteByte(IIC_WRITE);
SlaveAck();
WriteByte(Address>>8);
SlaveAck();
WriteByte(Address & 0x00ff);
SlaveAck();
Start();
WriteByte(IIC_READ);
SlaveAck();
RetValue = ReadByte();
MasterNoAck();
Stop();
return RetValue;
}
//=====================================
void WriteFRAM(unsigned int Address,unsigned int Wdata)//单字节写
{
Start();
WriteByte(IIC_WRITE);
SlaveAck();
WriteByte(Address>>8);
SlaveAck();
WriteByte(Address & 0x00ff);
SlaveAck();
Wdata &= 0x00ff;
WriteByte(Wdata);
SlaveAck();
Stop();
}
//=====================================
unsigned int ReadReg(unsigned int RegAdd)
{
unsigned int RetValue;
Start();
WriteByte(REG_WRITE);
SlaveAck();
RegAdd &= 0x001f;
WriteByte(RegAdd);
SlaveAck();
Start();
WriteByte(REG_READ);
SlaveAck();
RetValue = ReadByte();
MasterNoAck();
Stop();
return RetValue;
}
//=====================================
void WriteReg(unsigned int RegAdd,unsigned int Wdata)
{
Start();
WriteByte(REG_WRITE);
SlaveAck();
RegAdd &= 0x001f;
WriteByte(RegAdd);
SlaveAck();
WriteByte(Wdata);
SlaveAck();
Stop();
}
//====================================
void Read_array(unsigned int Address,unsigned int number/*16bit字数*/)//数据读入buffer
{
unsigned int i;
unsigned int RetValue_LSB,RetValue_MSB;
Start();
WriteByte(IIC_WRITE);
SlaveAck();
WriteByte(Address>>8);
SlaveAck();
WriteByte(Address & 0x00ff);
SlaveAck();
Start();
WriteByte(IIC_READ);
SlaveAck();
for(i=0;i<number;i++)
{
RetValue_LSB = ReadByte();
MasterAck();
RetValue_MSB = ReadByte();
if(i<(number-1)) MasterAck();
else MasterNoAck();
buffer[i] = (RetValue_MSB<<8)+RetValue_LSB;
}
Stop();
}
//=====================================
void Write_array(unsigned int Address,unsigned int number/*16bit字数*/)//写buffer数据
{
unsigned int i;
Start();
WriteByte(IIC_WRITE);
SlaveAck();
WriteByte(Address>>8);
SlaveAck();
WriteByte(Address & 0x00ff);
SlaveAck();
for(i=0;i<number;i++)
{
WriteByte(buffer[i] & 0x00ff);
SlaveAck();
WriteByte(buffer[i]>>8);
SlaveAck();
}
Stop();
}
//=======================================
void Initiation_fm31(void)
{
WriteReg(WRITEPROTECT,0x00);//无写保护,VTP=2.6V
WriteReg(WDTTIME,0x1f); //看门狗关闭,禁止看门狗计数器
WriteReg(WDTFLAG,0x0a); //清除复位标志,看门狗计数器复位
WriteReg(CONTROL,0x00); //启动时钟
}
//=========================================
void WriteRTC(void)
{
WriteReg(FLAGS,0x02); //允许写操作
WriteReg(YEAR,0x09); //写时间寄存器 09年2月7日星期六12时34分56秒
WriteReg(MOUNTH,0x02);
WriteReg(DAY,0x07);
WriteReg(DATE,0x06);
WriteReg(HOUR,0x12);
WriteReg(MINUTE,0x34);
WriteReg(SECOND,0x56);
WriteReg(FLAGS,0x00); //装载时间寄存器
}
//========================================
void ReadRTC(void)
{
unsigned int rtc[7];
WriteReg(FLAGS,0x01); //捕捉时间寄存器,允许读时间操作
rtc[0] = ReadReg(YEAR);
rtc[1] = ReadReg(MOUNTH);
rtc[2] = ReadReg(DAY);
rtc[3] = ReadReg(DATE);
rtc[4] = ReadReg(HOUR);
rtc[5] = ReadReg(MINUTE);
rtc[6] = ReadReg(SECOND);
WriteReg(FLAGS,0x00); //返回0,准备下次读时间
RtcRegs.year = ((rtc[0]>>4)*10) + (rtc[0] & 0xf);
RtcRegs.month = ((rtc[1]>>4)*10) + (rtc[1] & 0xf);
RtcRegs.date =((rtc[2]>>4)*10) + (rtc[2] & 0xf);
RtcRegs.week = rtc[3];
RtcRegs.hour = ((rtc[4]>>4)*10) + (rtc[4] & 0xf);
RtcRegs.minute =((rtc[5]>>4)*10) + (rtc[5] & 0xf);
RtcRegs.second = ((rtc[6]>>4)*10) + (rtc[6] & 0xf);
}
//=================================================
//=================================================
//end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -