📄 aaa.c
字号:
#include"Heads.h"
#define STATE_IDLE 0
#define STATE_WRITE_FINAL 1
#define STATE_SEND_ACK 2
#define STATE_WAIT_ACK 3
#define STATE_READ_FIRST 4
#define STATE_READ_FINAL 5
#define STATE_READ_WAIT 6
static volatile unsigned char g_uc_Data_Tx;
static volatile unsigned char g_uc_Data_Rx1;
static volatile unsigned char g_uc_State = STATE_IDLE;
// Write to the Atmel device.
void I2C_RX8025_Write(unsigned char uc_Data, unsigned char uc_Offset)
{
g_uc_Data_Tx = uc_Data;
g_uc_State = STATE_WRITE_FINAL;
I2CMasterSlaveAddrSet(I2C_MASTER_BASE, 0x32, false);
I2CMasterDataPut(I2C_MASTER_BASE, uc_Offset);
I2CMasterControl(I2C_MASTER_BASE, I2C_MASTER_CMD_BURST_SEND_START);
while(g_uc_State != STATE_IDLE)
{
}
}
void I2C_RX8025_Read(unsigned char uc_Offset)
{
g_uc_State = STATE_READ_FIRST;
I2CMasterSlaveAddrSet(I2C_MASTER_BASE, 0x32, false);
I2CMasterDataPut(I2C_MASTER_BASE, uc_Offset);
I2CMasterControl(I2C_MASTER_BASE, I2C_MASTER_CMD_BURST_SEND_START);
while(g_uc_State != STATE_IDLE)
{
}
}
int main(void)
{
// Set the clocking to run directly from the crystal.
SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN | SYSCTL_XTAL_6MHZ);
// Enable the peripherals used by this example.
SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
// Enable processor interrupts.
IntMasterEnable();
// Configure the appropriate pins to be I2C instead of GPIO.
GPIOPinTypeI2C(GPIO_PORTB_BASE, GPIO_PIN_2 | GPIO_PIN_3);
// Initialize the I2C master.
I2CMasterInit(I2C_MASTER_BASE, true); //400K BPS
// Enable the I2C interrupt.
IntEnable(INT_I2C);
// Enable the I2C master interrupt.
I2CMasterIntEnable(I2C_MASTER_BASE);
// Config RX8025 CLOCK--------------------------------------
I2C_RX8025_Write(0x20, 0xe0);
I2C_RX8025_Write(0x20, 0xf0);
I2C_RX8025_Write(0x00, 0x70);
I2C_RX8025_Write(0x00, 0x50);
I2C_RX8025_Write(0x08, 0x00); //sec
I2C_RX8025_Write(0x08, 0x10); //min
I2C_RX8025_Write(0x08, 0x20); //hour
I2C_RX8025_Write(0x08, 0x40); //day
I2C_RX8025_Write(0x08, 0x50); //month
I2C_RX8025_Write(0x08, 0x60); //year
while(1)
{
I2C_RX8025_Read(0x00); //Sec
I2C_RX8025_Read(0x10); //Min
I2C_RX8025_Read(0x20); //Hour
I2C_RX8025_Read(0x40); //Day
I2C_RX8025_Read(0x50); //Month
I2C_RX8025_Read(0x60); //Year
}
}
void I2C_IntHandler(void)
{
I2CMasterIntClear(I2C_MASTER_BASE);
switch(g_uc_State)
{
case STATE_IDLE:
{
break;
}
case STATE_WRITE_FINAL:
{
I2CMasterDataPut(I2C_MASTER_BASE, g_uc_Data_Tx);
I2CMasterControl(I2C_MASTER_BASE, I2C_MASTER_CMD_BURST_SEND_FINISH);
g_uc_State = STATE_SEND_ACK;
break;
}
case STATE_SEND_ACK:
{
I2CMasterSlaveAddrSet(I2C_MASTER_BASE, 0x32, true);
I2CMasterControl(I2C_MASTER_BASE, I2C_MASTER_CMD_SINGLE_RECEIVE);
g_uc_State = STATE_WAIT_ACK;
break;
}
case STATE_WAIT_ACK:
{
if(I2CMasterErr(I2C_MASTER_BASE) == I2C_MASTER_ERR_NONE)
{
I2CMasterDataGet(I2C_MASTER_BASE);
g_uc_State = STATE_IDLE;
break;
}
}
case STATE_READ_FIRST:
{
I2CMasterSlaveAddrSet(I2C_MASTER_BASE, 0x32, true);
I2CMasterControl(I2C_MASTER_BASE, I2C_MASTER_CMD_BURST_RECEIVE_START);
g_uc_State = STATE_READ_FINAL;
break;
}
case STATE_READ_FINAL:
{
g_uc_Data_Rx1 = I2CMasterDataGet(I2C_MASTER_BASE);
I2CMasterControl(I2C_MASTER_BASE, I2C_MASTER_CMD_BURST_RECEIVE_FINISH);
g_uc_State = STATE_READ_WAIT;
break;
}
case STATE_READ_WAIT:
{
I2CMasterDataGet(I2C_MASTER_BASE);
g_uc_State = STATE_IDLE;
break;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -