lm3s读取rx8025代码i2c程序轮询方式
[复制链接]
#define I2C_RX8025_CALENDAR_MOUDLE
#include
#include
#define DELAY_2N 0
#define I2C_Speed 100000
#define RX8025_SLAVE_ADDRESS 0x32
static u8 bcdtobin(u8 bcd);
static u8 bintobcd(u8 bcd);
//static void delay (int cnt);
#if 0
static void delay (int cnt)
{
cnt <<= DELAY_2N;
while (cnt--);
}
#endif
static u8 bcdtobin(u8 bcd)
{
return ((bcd/16)*10 + (bcd&0x0f));
}
static u8 bintobcd(u8 bin)
{
return ((bin/10)*16+(bin%10));
}
/*******************************************************************************
* Function Name : I2C1_CAL_Init
* Description : Initializes peripherals used by the I2C Calendar Rx8025 driver.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void I2C1_RX8025CAL_Init(void)
{
SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C1);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
GPIOPinConfigure(GPIO_PA6_I2C1SCL);
GPIOPinConfigure(GPIO_PA7_I2C1SDA);
GPIOPinTypeI2C(GPIO_PORTA_BASE, GPIO_PIN_6 | GPIO_PIN_7);
I2CMasterEnable(I2C1_MASTER_BASE);
I2CMasterInitExpClk(I2C1_MASTER_BASE, SysCtlClockGet(), 0);//false
}
/*******************************************************************************
* Function Name : InitRTC
* Description : 初始化时钟芯片
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void InitRTC(void)
{
#if TURE_RX8025
#if 0
u8 tempchar[4];
tempchar[0] = 0xe0; //“0”模式设置寄存器“E、F”
tempchar[1] = 0x23; //寄存器“E”的位设置
tempchar[2] = 0x20; //寄存器“F”的位设置
I2C_Master_BufferWrite(&tempchar[0],3);
#else
u8 tempchar[2];
tempchar[0]=0x20;
tempchar[1]=0x20;
I2C_Master_BufferWriteoffset(&tempchar[0],2,0xe0);
#endif
#endif
#if DS1302
Initial_ds1302();
#endif
return;
}
/*******************************************************************************
* Function Name : SetTime
* Description : 将当前时间设置到时钟芯片(当前时间为公共变量)
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void SETTime(struct Time_Struct *time)
{
U8 tempchar[8];
#if TURE_RX8025
#if 1
tempchar[0] = 0x00; //“0”模式设置寄存器“0~F”
tempchar[1] = bintobcd(time->Second);
tempchar[2] = bintobcd(time->Minute);
tempchar[3] = bintobcd(time->Hour);
tempchar[4] = (time->Week >= 7)?0:time->Week;
tempchar[5] = bintobcd(time->Day);
tempchar[6] = bintobcd(time->Month);
tempchar[7] = bintobcd(time->Year);
#else
// tempchar[0] = 0x00; //“0”模式设置寄存器“0~F”
tempchar[0] = bintobcd(time->Second);
tempchar[1] = bintobcd(time->Minute);
tempchar[2] = bintobcd(time->Hour);
tempchar[3] = (time->Week >= 7)?0:time->Week;
tempchar[4] = bintobcd(time->Day);
tempchar[5] = bintobcd(time->Month);
tempchar[6] = bintobcd(time->Year);
#endif
I2C_Master_BufferWrite(&tempchar[0],8);
#endif
#if DS1302
Wirte_DS1302 (WRITE_PROTECT,0x00); //禁止写保护
tempchar[0] = bintobcd(time->Second);
tempchar[1] = bintobcd(time->Minute);
tempchar[2] = bintobcd(time->Hour);
tempchar[3] = bintobcd(time->Day);
tempchar[4] = bintobcd(time->Month);
tempchar[5] = (time->Week >= 7)?0:time->Week;
tempchar[6] = bintobcd(time->Year);
tempchar[7] = 0x80;
DS1302_BurstWriteT(&tempchar[0]);//多字节写
#if 0 //单字节写
Wirte_DS1302 (WRITE_SECOND,tempchar[0]); //秒位初始化
Wirte_DS1302 (WRITE_MINUTE,tempchar[1]); //分钟初始化
Wirte_DS1302 (WRITE_HOUR,tempchar[2]); //小时初始化
Wirte_DS1302 (WRITE_DATE,tempchar[3]); //初始化
Wirte_DS1302 (WRITE_MONTH,tempchar[4]); //初始化
Wirte_DS1302 (WRITE_WEEK,tempchar[5]); //
Wirte_DS1302 (WRITE_YEAR,tempchar[6]); //
Wirte_DS1302 (WRITE_PROTECT,0x80); //允许写保护
#endif
#endif
return;
}
/*******************************************************************************
* Function Name : GetTime
* Description : 从时钟芯片读取当前时间.以标准的读模式读取
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void GETTime(struct Time_Struct *time)
{
u8 tempchar[8];
#if TURE_RX8025
I2C_Master_BufferRead(&tempchar[0],8);//用方式3读
time->Second = bcdtobin(tempchar[1] & 0x7f);
time->Minute = bcdtobin(tempchar[2] & 0x7f);
time->Hour = bcdtobin(tempchar[3] & 0x7f);
tempchar[4] &= 0x7;
if (tempchar[4]>=7)
tempchar[4] = 0;
time->Week = tempchar[4];
time->Day = bcdtobin(tempchar[5] & 0x3f);
time->Month = bcdtobin(tempchar[6] & 0x1f);
time->Year = bcdtobin(tempchar[7]);
return;
#endif
#if DS1302
#if 0 //可以用两种方式来读写,1中是单字节读写,一种是突发式多字节读写
tempchar[0]=Read_DS1302(0x81);
tempchar[1]=Read_DS1302(0x83);
tempchar[2]=Read_DS1302(0x85);
tempchar[3]=Read_DS1302(0x87);
tempchar[4]=Read_DS1302(0x89);
tempchar[5]=Read_DS1302(0x8b);
tempchar[6]=Read_DS1302(0x8d);
#endif
DS1302_BurstReadT(&tempchar[0]);//多字节读
time->Second = bcdtobin(tempchar[0] & 0x7f);
time->Minute = bcdtobin(tempchar[1] & 0x7f);
time->Hour = bcdtobin(tempchar[2] & 0x7f);
time->Day = bcdtobin(tempchar[3] & 0x3f);
time->Month = bcdtobin(tempchar[4] & 0x1f);
tempchar[5] &= 0x7;
if (tempchar[5]>=7)
tempchar[5] = 0;
time->Week = tempchar[5];
time->Year = bcdtobin(tempchar[6]);
return;
#endif
}
/*******************************************************************************
* Function Name : I2C_Master_BufferWrite
* Description : Send a buffer of bytes to the slave.
* Input : - pBuffer: Buffer of bytes to be sent to the slave.
* - NumByteToRead: Number of bytes to be sent to the slave.
* Output : None.
* Return : None.
*******************************************************************************/
unsigned long I2CMasterState(unsigned long ulBase)
{
unsigned long state;
//
// Check the arguments.
//
ASSERT((ulBase == I2C0_MASTER_BASE) || (ulBase == I2C1_MASTER_BASE));
//
// Get the raw error state
//
state = HWREG(ulBase + I2C_O_MCS);
return state;
}
//从头写起
void I2C_Master_BufferWrite(u8* pBuffer, u16 NumByteToWrite)
{
OSSchedLock();
while(I2CMasterBusy(I2C1_MASTER_BASE)); //如果i2c忙,则等待
I2CMasterSlaveAddrSet(I2C1_MASTER_BASE, RX8025_SLAVE_ADDRESS , false); //设置主机从地址
I2CMasterDataPut(I2C1_MASTER_BASE, 0x00);
I2CMasterControl(I2C1_MASTER_BASE, I2C_MASTER_CMD_BURST_SEND_START);//开始发送地址,后面进行第一个字节发送
while (NumByteToWrite)
{
while(I2CMasterBusy(I2C1_MASTER_BASE)); //等空闲时
I2CMasterDataPut(I2C1_MASTER_BASE, *pBuffer);
pBuffer++;
NumByteToWrite--;
I2CMasterControl(I2C1_MASTER_BASE, I2C_MASTER_CMD_BURST_SEND_CONT); //发送数据
if(NumByteToWrite==1)
{
while(I2CMasterBusy(I2C1_MASTER_BASE)); //等空闲时
I2CMasterDataPut(I2C1_MASTER_BASE, *pBuffer);
pBuffer++;
I2CMasterControl(I2C1_MASTER_BASE,I2C_MASTER_CMD_BURST_SEND_FINISH); //发送最后一个数据 后发送停止位
NumByteToWrite--;
}
}
OSSchedUnlock();
}
//写到指定的位置
void I2C_Master_BufferWriteoffset(u8* pBuffer, u16 NumByteToWrite,u8 ucoffset)
{
OSSchedLock();
while(I2CMasterBusy(I2C1_MASTER_BASE)); //如果i2c忙,则等待
I2CMasterSlaveAddrSet(I2C1_MASTER_BASE, RX8025_SLAVE_ADDRESS , false); //设置主机从地址
I2CMasterDataPut(I2C1_MASTER_BASE, ucoffset);
I2CMasterControl(I2C1_MASTER_BASE, I2C_MASTER_CMD_BURST_SEND_START);//开始发送地址,后面进行第一个字节发送
while (NumByteToWrite)
{
while(I2CMasterBusy(I2C1_MASTER_BASE)); //等空闲时
I2CMasterDataPut(I2C1_MASTER_BASE, *pBuffer);
pBuffer++;
NumByteToWrite--;
I2CMasterControl(I2C1_MASTER_BASE, I2C_MASTER_CMD_BURST_SEND_CONT); //发送数据
if(NumByteToWrite==1)
{
while(I2CMasterBusy(I2C1_MASTER_BASE)); //等空闲时
I2CMasterDataPut(I2C1_MASTER_BASE, *pBuffer);
pBuffer++;
I2CMasterControl(I2C1_MASTER_BASE,I2C_MASTER_CMD_BURST_SEND_FINISH); //发送最后一个数据 后发送停止位
NumByteToWrite--;
}
}
OSSchedUnlock();
}
/*******************************************************************************
* Function Name : I2C_Master_BufferRead
* Description : Reads buffer of bytes from the slave.
* Input : - pBuffer: Buffer of bytes to read from the slave.
* - NumByteToRead: Number of bytes to be read by the Master.
* - NumByteToRead >= 3.
* Output : None.
* Return : None.
*******************************************************************************/
void I2C_Master_BufferRead(u8* pBuffer, u16 NumByteToRead)
{
OSSchedLock();
while(I2CMasterState(I2C1_MASTER_BASE) & I2C_MCS_BUSY);
I2CMasterSlaveAddrSet(I2C1_MASTER_BASE, RX8025_SLAVE_ADDRESS| (0x00 >> 8), false);
I2CMasterDataPut(I2C1_MASTER_BASE, 0x00);
I2CMasterControl(I2C1_MASTER_BASE, I2C_MASTER_CMD_SINGLE_SEND);
while(I2CMasterBusy(I2C1_MASTER_BASE)); //如果i2c忙,则等待
I2CMasterSlaveAddrSet(I2C1_MASTER_BASE,0x32, true);
I2CMasterControl(I2C1_MASTER_BASE, I2C_MASTER_CMD_BURST_RECEIVE_START);
while(I2CMasterBusy(I2C1_MASTER_BASE)); //如果i2c忙,则等待
I2CMasterDataGet(I2C1_MASTER_BASE);
I2CMasterControl(I2C1_MASTER_BASE, I2C_MASTER_CMD_BURST_RECEIVE_CONT);
while(NumByteToRead)
{
if(NumByteToRead!=1)
{
while(I2CMasterBusy(I2C1_MASTER_BASE)); //如果i2c忙,则等待
*pBuffer++ = I2CMasterDataGet(I2C1_MASTER_BASE);
NumByteToRead--;
I2CMasterControl(I2C1_MASTER_BASE, I2C_MASTER_CMD_BURST_RECEIVE_CONT);
if(NumByteToRead==1)
{
while(I2CMasterState(I2C1_MASTER_BASE) & I2C_MCS_BUSY); //如果i2c忙,则等待
*pBuffer++ = I2CMasterDataGet(I2C1_MASTER_BASE);
NumByteToRead--;
I2CMasterControl(I2C1_MASTER_BASE, I2C_MASTER_CMD_BURST_RECEIVE_FINISH);
}
}
else
{
while(I2CMasterState(I2C1_MASTER_BASE) & I2C_MCS_BUSY); //如果i2c忙,则等待
*pBuffer++ = I2CMasterDataGet(I2C1_MASTER_BASE);
NumByteToRead--;
I2CMasterControl(I2C1_MASTER_BASE, I2C_MASTER_CMD_BURST_RECEIVE_FINISH);
}
}
OSSchedUnlock();
}