#define BSP_I2C_MODULE
#include <bsp.h>
/*
*********************************************************************************************************
* LOCAL DEFINES
*********************************************************************************************************
*/
#define LM3SXXXX_BASE_I2C0 0x40020000uL
#define LM3SXXXX_BASE_I2C1 0x40021000uL
/*
*********************************************************************************************************
* REGISTER BIT DEFINES
*********************************************************************************************************
*/
/*********************************************************************
总线地址分配
*********************************************************************/
#define PCF8563ADDR 0x51 //PCF8563器件地址,默认为1010001x
/*********************************************************************
PCF8563寄存器定义
*********************************************************************/
#define PCF8563CTRL0 0x00 //PCF8563控制状态寄存器0
#define PCF8563CTRL1 0x01 //PCF8563控制状态寄存器1
#define PCF8563SECOND 0x02 //PCF8563秒(欠压)寄存器
#define PCF8563MINUTE 0x03 //PCF8563分钟寄存器
#define PCF8563HOUR 0x04 //PCF8563小时寄存器
#define PCF8563DAY 0x05 //PCF8563日寄存器
#define PCF8563WEEK 0x06 //PCF8563星期寄存器
#define PCF8563MONTH 0x07 //PCF8563月(世纪)寄存器
#define PCF8563YEAR 0x08 //PCF8563年寄存器
/*
*********************************************************************************************************
* LOCAL CONSTANTS
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* LOCAL DATA TYPES
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* LOCAL TABLES
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* LOCAL GLOBAL VARIABLES
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* LOCAL FUNCTION PROTOTYPES
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* LOCAL CONFIGURATION ERRORS
*********************************************************************************************************
*/
void PCF8563Write(unsigned char addr,unsigned char data)
{
// Specify slave address 指定从机地址
I2CMasterSlaveAddrSet(I2C1_MASTER_BASE, PCF8563ADDR, false);
// Place the character to be sent in the data register
// 发送子地址
I2CMasterDataPut(I2C1_MASTER_BASE, addr);
// Initiate send of character from Master to Slave
// 设置主机状态
I2CMasterControl(I2C1_MASTER_BASE, I2C_MASTER_CMD_BURST_SEND_START);
// Delay until transmission completes
while(I2CMasterBusy(I2C1_MASTER_BASE));
// Place the character to be sent in the data register
I2CMasterDataPut(I2C1_MASTER_BASE, data);
// Initiate send of character from Master to Slave
// 发送数据命令
I2CMasterControl(I2C1_MASTER_BASE, I2C_MASTER_CMD_BURST_SEND_CONT);
// Delay until transmission completes
while(I2CMasterBusy(I2C1_MASTER_BASE));
// 发送结束命令
I2CMasterControl(I2C1_MASTER_BASE, I2C_MASTER_CMD_BURST_SEND_FINISH);
// 等待命令发送结束
while(I2CMasterBusy(I2C1_MASTER_BASE));
}
unsigned long PCF8563Read(unsigned char address)
{
unsigned long data;
// Specify slave address 指定从机地址
I2CMasterSlaveAddrSet(I2C1_MASTER_BASE, PCF8563ADDR, false);
// Place the character to be sent in the data register
I2CMasterDataPut(I2C1_MASTER_BASE,address);
// Initiate send of character from Master to Slave
I2CMasterControl(I2C1_MASTER_BASE, I2C_MASTER_CMD_BURST_SEND_START);
// Delay until transmission completes
while(I2CMasterBusy(I2C1_MASTER_BASE));
//指定从机地址
I2CMasterSlaveAddrSet(I2C1_MASTER_BASE, PCF8563ADDR, true);
//Initiate send character from Slave to Master
// 读取数据命令
I2CMasterControl(I2C1_MASTER_BASE, I2C_MASTER_CMD_SINGLE_RECEIVE);
// Delay until transmission completes
while(I2CMasterBusy(I2C1_MASTER_BASE));
//get the data
data=I2CMasterDataGet(I2C1_MASTER_BASE);
//发送一个停止位
//I2CMasterControl(I2C1_MASTER_BASE, I2C_MASTER_CMD_BURST_RECEIVE_FINISH);
//while(I2CMasterBusBusy(I2C1_MASTER_BASE));
//UARTprintf("read value: %d\n", data);
return data;
}
void BSP_RTCInit(void)
{
unsigned long ulVal;
// Enable the peripherals used by this example.
SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C1);//开启I2C1模块
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);//开启GPIOA模块(PA6+PA7)
// 管脚功能及参数配置
GPIOPinConfigure(GPIO_PA6_I2C1SCL);//配置PA6复选功能为I2C1SCL
GPIOPinConfigure(GPIO_PA7_I2C1SDA);//配置PA7复选功能为I2C1SDA
GPIOPinTypeI2C(GPIO_PORTA_BASE,
GPIO_PIN_6 | GPIO_PIN_7);//针对PA6、PA7配置管脚电气参数
// Initialize Master and Slave
ulVal = SysCtlClockGet();
I2CMasterInitExpClk(I2C1_MASTER_BASE, ulVal, false);
//I2CMasterEnable(I2C1_MASTER_BASE);
}
void BSP_GetTime(DT * time)
{
time->Second = PCF8563Read(PCF8563SECOND) & 0x7F;
time->Minute = PCF8563Read(PCF8563MINUTE) & 0x7F;
time->Hour = PCF8563Read(PCF8563HOUR) & 0x3F;
time->Day = PCF8563Read(PCF8563DAY) & 0x3F;
time->Week = PCF8563Read(PCF8563WEEK) & 0x07;
time->Month = PCF8563Read(PCF8563MONTH) & 0x1F;
time->Year = PCF8563Read(PCF8563YEAR);
}
void BSP_InitTime(void)
{
PCF8563Write(PCF8563MINUTE, 0x18);
PCF8563Write(PCF8563HOUR, 0x08);
PCF8563Write(PCF8563DAY, 0x05);
PCF8563Write(PCF8563WEEK, 0x06);
PCF8563Write(PCF8563MONTH, 0x11);
PCF8563Write(PCF8563YEAR, 0x11);
}
|