本帖最后由 比神乐 于 2023-2-16 10:44 编辑
昨天搞了一下AT24C04,谁知搞完21ic网站打不开了,今天补上。原理图
主程序代码:#include "SC_Init.h" // MCU initialization header file, including all firmware library header files
#include "SC_it.h"
#include "..\Drivers\SCDriver_list.h"
#include "HeadFiles\SysFunVarDefine.h"
/**************************************Generated by EasyCodeCube*************************************/
USCI3_HandleInfoDef* USCI3_HANDLE;
uint8_t pData=235;
uint8_t temp=0;
/*************************************.Generated by EasyCodeCube.************************************/
/*****************************************************************************************************
* Function Name: main
* Description : This function implements main function.
* Arguments : None
* Return Value : None
******************************************************************************************************/
void main(void)
{
uint8_t bai,shi,ge;
/*<Generated by EasyCodeCube begin>*/
/*<UserCodeStart>*//*<SinOne-Tag><36>*/
IcResourceInit();
/*<UserCodeEnd>*//*<SinOne-Tag><36>*/
/*<UserCodeStart>*//*<SinOne-Tag><101>*/
OLED_Init();
/*<UserCodeEnd>*//*<SinOne-Tag><101>*/
/*<UserCodeStart>*//*<SinOne-Tag><102>*/
OLED_Clear();
/*<UserCodeEnd>*//*<SinOne-Tag><102>*/
OLED_ShowCHinese( 20 , 0 , 0 );
OLED_ShowCHinese( 38 , 0 , 1 );
OLED_ShowCHinese( 56 , 0 , 2 );
OLED_ShowCHinese( 74 , 0 , 3 );
OLED_ShowCHinese( 92 , 0 , 4 );
/*<UserCodeStart>*//*<SinOne-Tag><4>*/
OLED_ShowCHinese( 0 , 2 , 5 );
OLED_ShowCHinese( 16 , 2 , 6 );
OLED_ShowCHinese( 32 , 2 , 7 );
OLED_ShowCHinese( 48 , 2 , 8 );
OLED_ShowChar(64,2,':',16);
/*****MainLoop*****/
OLED_ShowCHinese( 0 , 4 , 9 );
OLED_ShowCHinese( 16 , 4 , 10 );
OLED_ShowCHinese( 32 , 4 , 7 );
OLED_ShowCHinese( 48 , 4 , 8 );
OLED_ShowChar(64,4,':',16);
//USCI3_TWI_Master_Transmit(USCI3_HANDLE,0,&pData,1,1000);
AT24C04_Write_Byte(0,235);
OLED_ShowChar(80,2,'2',16);
OLED_ShowChar(88,2,'3',16);
OLED_ShowChar(96,2,'5',16);
//USCI3_TWI_Master_Receive(USCI3_HANDLE, 0, &temp, 1, 1000);
temp=AT24C04_Read_Byte(0);
bai=(temp/100)+0x30;
shi=(temp%100/10)+0x30;
ge=(temp%10)+0x30;
OLED_ShowChar(80,4,bai,16);
OLED_ShowChar(88,4,shi,16);
OLED_ShowChar(96,4,ge,16);
while(1)
{
/*<UserCodeStart>*//*<SinOne-Tag><103>*/
/*<UserCodeEnd>*//*<SinOne-Tag><103>*/
/*<UserCodeStart>*//*<SinOne-Tag><104>*/
//OLED_ShowString( 10,2,CharData,16);
/*<UserCodeEnd>*//*<SinOne-Tag><104>*/
/*<UserCodeStart>*//*<SinOne-Tag><105>*/
//OLED_ShowNum( 40,4,202212,6,16);
/*<UserCodeEnd>*//*<SinOne-Tag><105>*/
/*<Begin-Inserted by EasyCodeCube for Condition>*/
}
/*<UserCodeEnd>*//*<SinOne-Tag><4>*/
/*<Generated by EasyCodeCube end>*/
}
初始化程序:
#include "SC_Init.h"
#include "SC_it.h"
#include "..\Drivers\SCDriver_list.h"
#include "HeadFiles\SysFunVarDefine.h"
/**************************************Generated by EasyCodeCube*************************************/
void IcResourceInit()
{
/*<UserCodeStart>*//*<SinOne-Tag><37>*/
SC_Init(); /***MCU init***/
/*<UserCodeEnd>*//*<SinOne-Tag><37>*/
}
/*****************************************************
*函数名称: SC_Init
*函数功能: MCU初始化函数
*入口参数:void
*出口参数:void
*****************************************************/
void SC_Init(void)
{
SC_GPIO_Init();
SC_OPTION_Init();
SC_USCI0_Init();
SC_USCI3_Init();
/*write initial function here*/
EA = 1;
}
/*****************************************************
*函数名称: SC_USCI0_Init
*函数功能: USCI0初始化函数
*入口参数:void
*出口参数:void
*****************************************************/
void SC_USCI0_Init(void)
{
GPIO_Init(GPIO0, GPIO_PIN_5,GPIO_MODE_IN_PU);
USCI0_TWI_GeneralCallCmd(ENABLE);
USCI0_TWI_AcknowledgeConfig(ENABLE);
USCI0_ITConfig(ENABLE,LOW);
USCI0_TWI_MasterCommunicationRate(USCI0_TWI_32);
USCI0_TWI_SlaveClockExtension(DISABLE);
USCI0_TWI_Slave_Init(0x00);
USCI0_TWI_Cmd(ENABLE);
/*USCI0_Init write here*/
}
/*****************************************************
*函数名称: SC_USCI3_Init
*函数功能: USCI3初始化函数
*入口参数:void
*出口参数:void
*****************************************************/
void SC_USCI3_Init(void)
{
/*USCI3_Init write here*/
GPIO_Init(GPIO2, GPIO_PIN_6,GPIO_MODE_IN_PU);
USCI3_TWI_GeneralCallCmd(ENABLE);
USCI3_TWI_AcknowledgeConfig(ENABLE);
USCI3_ITConfig(ENABLE,LOW);
//USCI3_ITConfig(DISABLE,LOW);
USCI3_TWI_MasterCommunicationRate(USCI3_TWI_32);
USCI3_TWI_SlaveClockExtension(DISABLE);
USCI3_TWI_Slave_Init(0x00);
USCI3_TWI_Cmd(ENABLE);
}
AT24C04读写函数:
void AT24C04_Write_Byte(uint8_t Addr,uint8_t Data)
{
uint16_t i;
Select_USCI3();//选择控制USCI3
US3CON1 |= 0x20;
while((US3CON0&0X07)!=0x00);
US3CON3=0xA0;
//while((US3CON1&0X80)==0x00);
while((US3CON0&0X07)!=0x00);
US3CON3=Addr;
while((US3CON1&0X80)==0x00);
//US3CON0&=0xBF;
//while((US3CON0&0X07)!=0x00);
US3CON3=Data;
while((US3CON1&0X40)==0x00);
US3CON0&=0xBF;
//while((US3CON0&0X07)!=0x00);
US3CON1 |= 0x10;
for(i=0;i<20000;i++);
//US3CON3=0; //清标志位
}
uint8_t AT24C04_Read_Byte(uint8_t Addr)
{
uint8_t Data;
Select_USCI3();//选择控制USCI3
US3CON1 |= 0x20;
while((US3CON0&0X07)!=0x00);
US3CON3=0xA0;
while((US3CON0&0X07)!=0x00);
US3CON3=Addr;
while((US3CON0&0X07)!=0x00);
US3CON1 |= 0x20;
while((US3CON0&0X07)!=0x00);
US3CON3=0xA1;
while((US3CON0&0X07)!=0x00);
while((US3CON0&0X40)==0x00);
US3CON0&=0XBF;
Data=US3CON3;
US3CON0 &= 0XF7;
US3CON1 |= 0x10;
return Data;
}
/******************* (C) COPYRIGHT 2022 RDS Microelectronics *****END OF FILE****/
效果图:
补充SDA是P26,SCL是P27.图片有点不清楚,在我手机上很清楚,传上去看不清了。写读都是235.
|