完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
/**************************************************************************//**
* @file main.c * @version V3.00 * $Revision: 2 $ * $Date: 14/12/25 10:24a $ * @brief * Show how to set I2C in Slave mode and receive the data from Master. * This sample code needs to work with I2C_Master. * @note * Copyright (C) 2014 Nuvoton Technology Corp. All rights reserved. * ******************************************************************************/ #include #include "M0518.h" #define PLLCON_SETtiNG SYSCLK_PLLCON_50MHz_XTAL #define PLL_CLOCK 50000000 uint32_t slave_buff_addr; uint8_t g_u8SlvData[256]; uint8_t g_au8RxData[3]; /*---------------------------------------------------------------------------------------------------------*/ /* Global variables */ /*---------------------------------------------------------------------------------------------------------*/ volatile uint8_t g_u8DeviceAddr; volatile uint8_t g_au8TxData[3]; volatile uint8_t g_u8RxData; volatile uint8_t g_u8DataLen; typedef void (*I2C_FUNC)(uint32_t u32Status); static I2C_FUNC s_I2C0HandlerFn = NULL; /*---------------------------------------------------------------------------------------------------------*/ /* I2C0 IRQ Handler */ /*---------------------------------------------------------------------------------------------------------*/ void I2C0_IRQHandler(void) { uint32_t u32Status; u32Status = I2C_GET_STATUS(I2C0); if(I2C_GET_TIMEOUT_FLAG(I2C0)) { /* Clear I2C0 Timeout Flag */ I2C_ClearTimeoutFlag(I2C0); } else { if(s_I2C0HandlerFn != NULL) s_I2C0HandlerFn(u32Status); } } /*---------------------------------------------------------------------------------------------------------*/ /* I2C TRx Callback Function */ /*---------------------------------------------------------------------------------------------------------*/ void I2C_SlaveTRx(uint32_t u32Status) { if(u32Status == 0x60) /* Own SLA+W has been receive; ACK has been return */ { g_u8DataLen = 0; I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_SI_AA); } else if(u32Status == 0x80) /* Previously address with own SLA address Data has been received; ACK has been returned*/ { g_au8RxData[g_u8DataLen] = (unsigned char) I2C_GET_DATA(I2C0); g_u8DataLen++; if(g_u8DataLen == 2) { slave_buff_addr = (g_au8RxData[0] << 8) + g_au8RxData[1]; } if(g_u8DataLen == 3) { g_u8SlvData[slave_buff_addr] = g_au8RxData[2]; g_u8DataLen = 0; } I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_SI_AA); } else if(u32Status == 0xA8) /* Own SLA+R has been receive; ACK has been return */ { I2C_SET_DATA(I2C0, g_u8SlvData[slave_buff_addr]); slave_buff_addr++; I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_SI_AA); } else if(u32Status == 0xC0) /* Data byte or last data in I2CDAT has been transmitted Not ACK has been received */ { I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_SI_AA); } else if(u32Status == 0x88) /* Previously addressed with own SLA address; NOT ACK has been returned */ { g_u8DataLen = 0; I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_SI_AA); } else if(u32Status == 0xA0) /* A STOP or repeated START has been received while still addressed as Slave/Receiver*/ { g_u8DataLen = 0; I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_SI_AA); } else { /* TO DO */ printf("Status 0x%x is NOT processed\n", u32Status); } } void SYS_Init(void) { /*---------------------------------------------------------------------------------------------------------*/ /* Init System Clock */ /*---------------------------------------------------------------------------------------------------------*/ /* Enable Internal RC 22.1184MHz clock */ CLK_EnableXtalRC(CLK_PWRCON_OSC22M_EN_Msk); /* Waiting for Internal RC clock ready */ CLK_WaitClockReady(CLK_CLKSTATUS_OSC22M_STB_Msk); /* Switch HCLK clock source to Internal RC and HCLK source divide 1 */ CLK_SetHCLK(CLK_CLKSEL0_HCLK_S_HIRC, CLK_CLKDIV_HCLK(1)); /* Enable external XTAL 12MHz clock */ CLK_EnableXtalRC(CLK_PWRCON_XTL12M_EN_Msk); /* Waiting for external XTAL clock ready */ CLK_WaitClockReady(CLK_CLKSTATUS_XTL12M_STB_Msk); /* Set core clock as PLL_CLOCK from PLL */ CLK_SetCoreClock(PLL_CLOCK); /* Enable UART module clock */ CLK_EnableModuleClock(UART0_MODULE); /* Enable I2C0 module clock */ CLK_EnableModuleClock(I2C0_MODULE); /* Select UART module clock source */ CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART_S_HXT, CLK_CLKDIV_UART(1)); /*---------------------------------------------------------------------------------------------------------*/ /* Init I/O Multi-function */ /*---------------------------------------------------------------------------------------------------------*/ /* Set GPB multi-function pins for UART0 RXD and TXD */ SYS->GPB_MFP = SYS_GPB_MFP_PB0_UART0_RXD | SYS_GPB_MFP_PB1_UART0_TXD; /* Set GPA multi-function pins for I2C0 SDA and SCL */ SYS->GPA_MFP = SYS_GPA_MFP_PA8_I2C0_SDA | SYS_GPA_MFP_PA9_I2C0_SCL; } void UART0_Init() { /*---------------------------------------------------------------------------------------------------------*/ /* Init UART */ /*---------------------------------------------------------------------------------------------------------*/ /* Reset IP */ SYS_ResetModule(UART0_RST); /* Configure UART0 and set UART0 Baudrate */ UART_Open(UART0, 115200); } void I2C0_Init(void) { /* Open I2C module and set bus clock */ I2C_Open(I2C0, 100000); /* Get I2C0 Bus Clock */ printf("I2C clock %d Hz\n", I2C_GetBusClockFreq(I2C0)); /* Set I2C 4 Slave Addresses */ I2C_SetSlaveAddr(I2C0, 0, 0x15, 0); /* Slave Address : 0x15 */ I2C_SetSlaveAddr(I2C0, 1, 0x35, 0); /* Slave Address : 0x35 */ I2C_SetSlaveAddr(I2C0, 2, 0x55, 0); /* Slave Address : 0x55 */ I2C_SetSlaveAddr(I2C0, 3, 0x75, 0); /* Slave Address : 0x75 */ /* Set I2C 4 Slave Addresses Mask */ I2C_SetSlaveAddrMask(I2C0, 0, 0x01); I2C_SetSlaveAddrMask(I2C0, 1, 0x04); I2C_SetSlaveAddrMask(I2C0, 2, 0x01); I2C_SetSlaveAddrMask(I2C0, 3, 0x04); /* Enable I2C interrupt */ I2C_EnableInt(I2C0); NVIC_EnableIRQ(I2C0_IRQn); } void I2C0_Close(void) { /* Disable I2C0 interrupt and clear corresponding NVIC bit */ I2C_DisableInt(I2C0); NVIC_DisableIRQ(I2C0_IRQn); /* Disable I2C0 and close I2C0 clock */ I2C_Close(I2C0); CLK_DisableModuleClock(I2C0_MODULE); } /*---------------------------------------------------------------------------------------------------------*/ /* Main Function */ /*---------------------------------------------------------------------------------------------------------*/ int32_t main(void) { uint32_t i; /* Unlock protected registers */ SYS_UnlockReg(); /* Init System, IP clock and multi-function I/O */ SYS_Init(); /* Init UART0 for printf */ UART0_Init(); /* Lock protected registers */ SYS_LockReg(); /* This sample code sets I2C bus clock to 100kHz. Then, Master accesses Slave with Byte Write and Byte Read operations, and check if the read data is equal to the programmed data. */ printf("+------------------------------------------------------_-+\n"); printf("| M0518 I2C Driver Sample Code(Slave) for access Slave |\n"); printf("| |\n"); printf("| I2C Master (I2C0) <---> I2C Slave(I2C0) |\n"); printf("+-------------------------------------------------------_+\n"); printf("Configure I2C0 as a slave.\n"); printf("The I/O connection for I2C0:\n"); printf("I2C0_SDA(PA.8), I2C0_SCL(PA.9)\n"); /* Init I2C0 */ I2C0_Init(); /* I2C enter no address SLV mode */ I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_SI_AA); for(i = 0; i < 0x100; i++) { g_u8SlvData = 0; } /* I2C function to Slave receive/transmit data */ s_I2C0HandlerFn = I2C_SlaveTRx; printf("\n"); printf("I2C Slave Mode is Running.\n"); while(1); } 为何可以设置这么多地址。 |
|
相关推荐
1个回答
|
|
I2C的地址是由硬件设置的,它可以使多个设备共享同一条I2C总线。M051的I2C可以设置多个地址,可以连接多个设备到同一条I2C总线上。这些设备可以被访问,即Master可以通过发送正确的地址来选择访问哪个设备。因此,这些地址的设置为多设备协同工作提供了方便。
|
|
|
|
只有小组成员才能发言,加入小组>>
3260 浏览 9 评论
2940 浏览 16 评论
3442 浏览 1 评论
8943 浏览 16 评论
4035 浏览 18 评论
1073浏览 3评论
557浏览 2评论
const uint16_t Tab[10]={0}; const uint16_t *p; p = Tab;//报错是怎么回事?
550浏览 2评论
用NUC131单片机UART3作为打印口,但printf没有输出东西是什么原因?
2285浏览 2评论
NUC980DK61YC启动随机性出现Err-DDR是为什么?
1844浏览 2评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-5 23:21 , Processed in 1.140824 second(s), Total 79, Slave 59 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号