今天搞了一下AT24C04,模拟时序,串口一打印数值。
SCL:PC0;SDA:PC1.
原理图:
主程序代码:#include "main.h"
#include "AT24Cxx.h"
/** @addtogroup Examples
@{
*/
//volatile bool txThresholdEventReceived = false;
//volatile bool rxThresholdEventReceived = false;
/** @addtogroup USART_Printf
@{
*/
/** @addtogroup USART_Printf_Variables Variables
@{
*/
volatile uint32_t tick = 0;
u8 dt=0;
u8 td=0;
uint8_t txBuf1[] = "Read Adress0 Value = : \r\n";
uint8_t txBuf2[] = "Read Adress1 Value = : \r\n";
/**@} end of group USART_Printf_Variables */
void DelayMs(uint16_t nms)
{
uint16_t i,j;
for(i=0;i<nms;i++)
for(j=0;j<200;j++);
}
/** @addtogroup USART_Printf_Functions Functions
@{
*/
void APM_MINI_GPIOInit(void)
{
GPIO_Config_T configStruct;
/* Enable the GPIO_LED Clock */
RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_GPIOE);
/* Configure the GPIO_LED pin */
configStruct.pin = GPIO_PIN_2;
configStruct.mode = GPIO_MODE_OUT_PP;
configStruct.speed = GPIO_SPEED_50MHz;
GPIO_Config(GPIOE, &configStruct);
}
/*!
* [url=home.php?mod=space&uid=247401]@brief[/url] Main program
*
* @param None
*
* @retval None
*
*/
int main(void)
{
uint8_t i;
uint8_t bai,shi,ge;
GPIO_Config_T GPIO_ConfigStruct;
USART_Config_T USART_ConfigStruct;
APM_MINI_LEDInit(LED2);
APM_MINI_GPIOInit();
RCM_EnableAPB2PeriphClock((RCM_APB2_PERIPH_T)(RCM_APB2_PERIPH_GPIOA | RCM_APB2_PERIPH_USART1));
GPIO_ConfigStruct.mode = GPIO_MODE_AF_PP;
GPIO_ConfigStruct.pin = GPIO_PIN_9;
GPIO_ConfigStruct.speed = GPIO_SPEED_50MHz;
GPIO_Config(GPIOA, &GPIO_ConfigStruct);
USART_ConfigStruct.baudRate = 115200;
USART_ConfigStruct.hardwareFlow = USART_HARDWARE_FLOW_NONE;
USART_ConfigStruct.mode = USART_MODE_TX;
USART_ConfigStruct.parity = USART_PARITY_NONE;
USART_ConfigStruct.stopBits = USART_STOP_BIT_1;
USART_ConfigStruct.wordLength = USART_WORD_LEN_8B;
USART_Config(USART1, &USART_ConfigStruct);
USART_Enable(USART1);
SysTick_Config(SystemCoreClock / 1000);
I2C_Init();
AT24Cxx_WriteOneByte(4,125);
AT24Cxx_WriteOneByte(5,238);
dt=AT24Cxx_ReadOneByte(4);
bai=dt/100+0x30;
shi=dt%100/10+0x30;
ge=dt%10+0x30;
txBuf1[22]=bai;
txBuf1[23]=shi;
txBuf1[24]=ge;
for(i = 0; i < sizeof(txBuf1); i++)
{
while(USART_ReadStatusFlag(USART1, USART_FLAG_TXBE) == RESET);
USART_TxData(USART1, txBuf1[i]);
}
Delay();
td=AT24Cxx_ReadOneByte(5);
bai=td/100+0x30;
shi=td%100/10+0x30;
ge=td%10+0x30;
txBuf2[22]=bai;
txBuf2[23]=shi;
txBuf2[24]=ge;
for(i = 0; i < sizeof(txBuf2); i++)
{
while(USART_ReadStatusFlag(USART1, USART_FLAG_TXBE) == RESET);
USART_TxData(USART1, txBuf2[i]);
}
while(1)
{
Delay();
APM_MINI_LEDToggle(LED2);
}
}
/*!
* @brief Delay
*
* @param None
*
* @retval None
*
*/
void Delay(void)
{
tick = 0;
while(tick < 500);
}
AT24C04.C:
#include "I2C.h"
#define AT24Cxx_Address 0xA0
/**********************************************************************************************************
* 函 数 名: AT24Cxx_ReadOneByte
* 功能说明: AT24Cxx_ReadOneByte读一个字节
* 形 参:无
* 返 回 值: 无
**********************************************************************************************************/
u8 AT24Cxx_ReadOneByte(u8 addr)
{
u8 temp=0;
I2C_Start();
I2C_Send_Byte(AT24Cxx_Address);//器件地址+数据地址
I2C_Wait_Ack();
I2C_Send_Byte(addr);//双字节是数据地址低位
//单字节是数据地址低位
I2C_Wait_Ack();
I2C_Start();
I2C_Send_Byte(AT24Cxx_Address+1);
I2C_Wait_Ack();
temp=I2C_Read_Byte(0); // 0 代表 NACK
I2C_Stop();
return temp;
}
/**********************************************************************************************************
* 函 数 名: AT24Cxx_WriteOneByte
* 功能说明: AT24Cxx_WriteOneByte 写一个字节
* 形 参:无
* 返 回 值: 无
**********************************************************************************************************/
void AT24Cxx_WriteOneByte(u8 addr,u8 dt)
{
I2C_Start();
I2C_Send_Byte(AT24Cxx_Address);//器件地址+数据地址
I2C_Wait_Ack();
I2C_Send_Byte(addr);//双字节是数据地址低位
//单字节是数据地址低位
I2C_Wait_Ack();
I2C_Send_Byte(dt);
I2C_Wait_Ack();
I2C_Stop();
delay_ms(50);
}
效果图:
|