打印

IIC读RTC出现问题了

[复制链接]
1439|9
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
jiahy|  楼主 | 2014-3-26 18:11 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

int PCF8563Read(unsigned char address,unsigned char *pbuf,int number)
{
         int i;
         //
         // Specify slave address  
         // 0xA3  --> read
         // 0xA2  --> write
         //
         I2CMasterSlaveAddrSet(I2C_MASTER_BASE, 0x51, false);
         //
         // Place the character to be sent in the data register
         //
         I2CMasterDataPut(I2C_MASTER_BASE,address);
         //
         // Initiate send of character from Master to Slave
         //
         I2CMasterControl(I2C_MASTER_BASE,  I2C_MASTER_CMD_BURST_SEND_START);
         //
         // Delay until transmission completes
         //
         while(I2CMasterBusy(I2C_MASTER_BASE));
         //指定从机地址
        I2CMasterSlaveAddrSet(I2C_MASTER_BASE, 0x51, true);
         //Initiate send character from Slave to Master
         // 读取数据命令
         I2CMasterControl(I2C_MASTER_BASE,  I2C_MASTER_CMD_BURST_RECEIVE_START);
         
        //
         // Delay until transmission completes
         //
         for(i = 0; i < number-1; i++)
         {
                 while(I2CMasterBusy(I2C_MASTER_BASE));
                 *pbuf++ = (unsigned char)I2CMasterDataGet(I2C_MASTER_BASE);
                 I2CMasterControl(I2C_MASTER_BASE,  I2C_MASTER_CMD_BURST_RECEIVE_CONT);
         }
         //发送一个停止位
         while(I2CMasterBusy(I2C_MASTER_BASE));
         *pbuf++ = (unsigned char)I2CMasterDataGet(I2C_MASTER_BASE);
         I2CMasterControl(I2C_MASTER_BASE, I2C_MASTER_CMD_BURST_RECEIVE_FINISH);
         while(I2CMasterBusBusy(I2C_MASTER_BASE));         return i;
}

相关帖子

沙发
jiahy|  楼主 | 2014-3-26 18:11 | 只看该作者
使用IIC外挂一个RTCCF8563T,程序运行了好几天,发现出现CPU停在最后的一个while循环

使用特权

评论回复
板凳
huangchui| | 2014-3-26 18:13 | 只看该作者
先确定硬件问题还是软件问题

使用特权

评论回复
地板
jiajs| | 2014-3-26 18:14 | 只看该作者
没用过LM3S6965,等看结果。

使用特权

评论回复
5
jiahy|  楼主 | 2014-3-26 18:16 | 只看该作者
补充一下:
IIC只挂一个设备,就PCF8563T。

使用特权

评论回复
6
午夜粪车| | 2014-3-26 18:16 | 只看该作者

#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);
}

使用特权

评论回复
7
午夜粪车| | 2014-3-26 18:16 | 只看该作者
8563的代码,参考下

使用特权

评论回复
8
wyjie| | 2014-3-26 18:18 | 只看该作者
很强大的各位啊,不管是用哪个,第一要先保证I2C总线可以用的啊

使用特权

评论回复
9
shimx| | 2014-3-26 18:19 | 只看该作者
是啊,不然通讯都不同的,两边的路再好也是过不了河啊

使用特权

评论回复
10
jiahy|  楼主 | 2014-3-26 18:22 | 只看该作者
好的,那我就结贴了哈

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

862

主题

9861

帖子

4

粉丝