/*---------------------------------------------------------------------------------------------------------*/
/* */
/* Copyright(c) 2020 nuvoton Technology Corp. All rights reserved. */
/* */
/*---------------------------------------------------------------------------------------------------------*/
//***********************************************************************************************************
// Website: http://www.nuvoton.com
// E-Mail : MicroC-8bit@nuvoton.com
// Date : Jan/21/2020
//***********************************************************************************************************
//***********************************************************************************************************
// File Function: ML51
//***********************************************************************************************************
#include "MS51_8K.H"
/* For printf code only. Disable this define to reduce code size. */
//#define print_function
//***********************************************************************************************************
// File Function: MS51 I2C demo code, Slave Address of 24LC64 = 0xA0
//
// ____________ ______________
// | | SDA | |
// | |<-------->| |
// | | | |
// | MS51(M) | | 24LC64(S) |
// | | SCL | |
// | |--------->| |
// |____________| |_____________|
//
// Microchip I2C EEPROM 24xx64 (64K Bit) is used as the slave device.
// The page size are 32Byte. Total are 256 page.
// If verification passes, Port3 will show 0x78. If there is any failure
// occured during the progress, Port3 will show 0x00.
//***********************************************************************************************************
#define I2C_CLOCK 2
#define EEPROM_SLA 0xA0
#define EEPROM_WR 0
#define EEPROM_RD 1
#define EEPROM_PAGE_SIZE 32
#define PAGE_NUMBER 4
#define ERROR_CODE 0x78
#define TEST_OK 0x00
bit I2C_Reset_Flag;
//========================================================================================================
void Init_I2C(void)
{
P03_OPENDRAIN_MODE; /*I2C_SCL*/
P04_OPENDRAIN_MODE; /*I2C_SDA*/
set_P0S_3;
set_P0S_4;
/* Set I2C clock rate */
I2CLK = I2C_CLOCK;
/* Enable I2C */
set_I2CON_I2CEN;
}
//========================================================================================================
void I2C_SI_Check(void)
{
if (I2STAT == 0x00)
{
I2C_Reset_Flag = 1;
set_I2CON_STO;
clr_I2CON_SI;
if(I2CON|CLR_BIT3)
{
clr_I2CON_I2CEN;
set_I2CON_I2CEN;
clr_I2CON_SI;
clr_I2CON_I2CEN;
}
}
}
void One_Page_Read(UINT8 u8PageNumber,UINT8 u8DAT)
{
UINT8 u8Count;
UINT16 u16Address;
u16Address = (UINT16)u8PageNumber*32;
/* Step1 */
set_I2CON_STA; /* Send Start bit to I2C EEPROM */
clr_I2CON_SI;
while (!(I2CON&SET_BIT3));
if (I2STAT != 0x08) /* 0x08: A START condition has been transmitted*/
{
I2C_Reset_Flag = 1;
#ifdef print_function
printf("\nI2C 'Send STA' error");
#endif
goto Read_Error_Stop;
}
/* Step2 */
I2DAT = (EEPROM_SLA | EEPROM_WR); /* Send (SLA+W) to EEPROM */
clr_I2CON_STA; /* Clear STA and Keep SI value in I2CON */
clr_I2CON_SI;
while (!(I2CON&SET_BIT3));
if (I2STAT != 0x18) /* 0x18: SLA+W has been transmitted; ACK has been received */
{
I2C_Reset_Flag = 1;
#ifdef print_function
printf("\nI2C 'Send SLA+W' error");
#endif
goto Read_Error_Stop;
}
/* Step3 */
I2DAT = HIBYTE(u16Address); /* Send I2C EEPROM's High Byte Address */
clr_I2CON_SI;
while (!(I2CON&SET_BIT3));
if (I2STAT != 0x28) /* 0x28: Data byte in S1DAT has been transmitted; ACK has been received */
{
I2C_Reset_Flag = 1;
#ifdef print_function
printf("\nI2C 'Send I2C High Byte Address' error");
#endif
goto Read_Error_Stop;
}
/* Step4 */
I2DAT = LOBYTE(u16Address); /* Send I2C EEPROM's Low Byte Address */
clr_I2CON_SI;
while (!(I2CON&SET_BIT3));
if (I2STAT != 0x28) /* 0x28: Data byte in S1DAT has been transmitted; ACK has been received */
{
I2C_Reset_Flag = 1;
#ifdef print_function
printf("\nI2C 'Send I2C Low Byte Address' error");
#endif
goto Read_Error_Stop;
}
/* Step5 */
set_I2CON_STA; /* Repeated START */
clr_I2CON_SI;
while (!(I2CON&SET_BIT3));
if (I2STAT != 0x10) /* 0x10: A repeated START condition has been transmitted */
{
I2C_Reset_Flag = 1;
#ifdef print_function
printf("\nI2C 'Send STA' error");
#endif
goto Read_Error_Stop;
}
/* Step6 */
clr_I2CON_STA; /* Clear STA and Keep SI value in I2CON */
I2DAT = (EEPROM_SLA | EEPROM_RD); /* Send (SLA+R) to EEPROM */
clr_I2CON_SI;
while (!(I2CON&SET_BIT3));
if (I2STAT != 0x40) /* 0x40: SLA+R has been transmitted; ACK has been received */
{
I2C_Reset_Flag = 1;
#ifdef print_function
printf("\nI2C 'Send SLA+R' error");
#endif
goto Read_Error_Stop;
}
/* Step7 */ /* Verify I2C EEPROM data */
for (u8Count = 0; u8Count <EEPROM_PAGE_SIZE; u8Count++)
{
set_I2CON_AA; /* Set Assert Acknowledge Control Bit */
clr_I2CON_SI;
while (!(I2CON&SET_BIT3));
if (I2STAT != 0x50) /* 0x50:Data byte has been received; NOT ACK has been returned */
{
I2C_Reset_Flag = 1;
#ifdef print_function
printf("\nI2C 'No Ack' error");
#endif
goto Read_Error_Stop;
}
if (I2DAT != u8DAT) /* Send the Data to EEPROM */
{
I2C_Reset_Flag = 1;
#ifdef print_function
printf("\nI2C 'Read data' error");
#endif
goto Read_Error_Stop;
}
u8DAT = ~u8DAT;
}
/* Step8 */
clr_I2CON_AA; /* Send a NACK to disconnect 24xx64 */
clr_I2CON_SI;
while (!(I2CON&SET_BIT3));
if (I2STAT != 0x58) /* 0x58:Data byte has been received; ACK has been returned */
{
I2C_Reset_Flag = 1;
#ifdef print_function
printf("\nI2C 'Ack' error");
#endif
goto Read_Error_Stop;
}
/* Step9 */
clr_I2CON_SI;
set_I2CON_STO;
while (I2CON|CLR_BIT4) /* Check STOP signal */
{
I2C_SI_Check();
if (I2C_Reset_Flag)
goto Read_Error_Stop;
}
Read_Error_Stop:
if (I2C_Reset_Flag)
{
I2C_SI_Check();
#ifdef print_function
printf("\nI2C Read error, test stop");
#endif
I2C_Reset_Flag = 0;
}
}
//========================================================================================================
void One_Page_Write(UINT8 u8PageNumber,UINT8 u8DAT)
{
UINT8 u8Count;
UINT16 u16Address;
u16Address = (UINT16)u8PageNumber*32;
/* Step1 */
set_I2CON_STA;
没有合适的资源?快使用搜索试试~ 我知道了~
资源详情
资源评论
资源推荐
收起资源包目录
MS51FB9AE_MS51XB9AE_MS51XB9BE BSP包 官网2022最新例程包含ISP (758个子文件)
STARTUP.A51 5KB
STARTUP.A51 5KB
STARTUP.A51 5KB
I2C_EEPROM.c 14KB
I2C_EEPROM.c 14KB
I2C_EEPROM.c 13KB
IAP.c 13KB
IAP.c 13KB
IAP.c 13KB
Table_Address.c 12KB
Table_Address.c 12KB
Table_Address.c 12KB
Table_Data .c 12KB
Table_Data .c 12KB
Table_Data .c 12KB
main_autosize_wdtdis.c 9KB
main_autosize_wdtdis.c 9KB
main_autosize.c 9KB
ISP_I2C_main_autosize_wdtdis.c 9KB
ISP_I2C_main_autosize_wdtdis.c 9KB
ISP_I2C_main_autosize_wdtdis.c 9KB
ISP_UART1_main.c 9KB
main.c 9KB
ISP_UART1_main.c 9KB
sys.c 8KB
delay.c 8KB
delay.c 8KB
I2C_M.c 8KB
I2C_M.c 8KB
I2C_M.c 7KB
I2C_M.c 7KB
I2C_M.c 7KB
delay.c 7KB
I2C_TO_M.c 7KB
pwm0.c 6KB
pwm123.c 6KB
fclk.c 6KB
ISP_I2C.c 6KB
ISP_I2C.c 6KB
ISP_I2C.c 6KB
I2C_M.c 6KB
I2C_M.c 6KB
I2C_M.c 6KB
timer.c 6KB
SPI_Flash_RW.c 6KB
SPI_Flash_RW.c 6KB
SPI_Flash_RW.c 6KB
timer.c 6KB
sys.c 5KB
pwm.c 5KB
pwm.c 5KB
sys.c 5KB
gpio.c 5KB
isr.c 5KB
isp_uart0.c 5KB
isp_uart0.c 5KB
isp_uart1.c 5KB
isr.c 5KB
isr.c 5KB
isp_uart1.c 5KB
isp_uart1.c 5KB
isp_uart0.c 5KB
eeprom.c 5KB
i2c.c 5KB
i2c.c 5KB
i2c.c 5KB
I2C_TO_S.c 5KB
I2C_S.c 5KB
I2C_S.c 5KB
eeprom.c 4KB
eeprom.c 4KB
uart.c 4KB
SPI_Slave_Int.c 4KB
SPI_Slave_Int.c 4KB
uart.c 4KB
SPI_Slave_Polling.c 4KB
SPI_Slave_Polling.c 4KB
SPI_Slave_Polling.c 4KB
SPI_Slave_Int.c 4KB
eeprom_sprom.c 4KB
eeprom_sprom.c 4KB
eeprom_sprom.c 4KB
SPI_Master_Po.c 4KB
SPI_Master_Po.c 4KB
SPI_Master_Po.c 4KB
uart.c 4KB
IAP_SPROM.c 4KB
IAP_SPROM.c 4KB
IAP_SPROM.c 4KB
I2C_S.c 4KB
I2C_S.c 4KB
I2C_S.c 4KB
PWM123_GP_INT.c 4KB
ADC_BG_VDD.c 4KB
ADC_BG_VDD.c 4KB
PWM0_Deadtime.C 3KB
adc.c 3KB
PWM_Deadtime.C 3KB
PWM_Deadtime.C 3KB
Timer0_reg.c 3KB
共 758 条
- 1
- 2
- 3
- 4
- 5
- 6
- 8
wangheng1888
- 粉丝: 3
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0