完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
电子发烧友论坛扫一扫,分享给好友
|
我想请问这个从机发送接收代码是怎么实现的
/*---------------------------------------------------------------------------------------------------------*/ /* */ /* Copyright(c) 2015 Nuvoton Technology Corp. All rights reserved. */ /* */ /*---------------------------------------------------------------------------------------------------------*/ //*********************************************************************************************************** // File Function: N76E003 I2C Slave demo code //*********************************************************************************************************** #include "N76E003.h" #include "Common.h" #include "Delay.h" #include "SFR_Macro.h" #include "Function_define.h" //*********************************************************************************************************** // N76E885-series I2C slave mode demo code, the Slave address = 0xA4 // // ____________ _____________ // | | SDA | | // | |<-------->| | // | | | | // |N76E003(M) | | N76E003(S) | // |(I2C_Master)| | (I2C_Slave) | // | | SCL | | // | |--------->| | // |____________| |_____________| // // The protocol of I2C is same the "24LC64" //*********************************************************************************************************** #define I2C_CLOCK 13 #define EEPROM_SLA 0xA4 UINT8 data_received[34], data_num = 0; //======================================================================================================== void I2C_ISR(void) interrupt 6 { switch (I2STAT) { case 0x00: STO = 1; break; case 0x60: AA = 1; //P3 = 0x60; break; case 0x68: P02 = 0; while(1); break; case 0x80: //P3 = 0x80; data_received[data_num] = I2DAT; data_num++; if (data_num == 34) AA = 0; else AA = 1; break; case 0x88: //P3 = 0x88; data_received[data_num] = I2DAT; data_num = 0; AA = 1; break; case 0xA0: //P3 = 0xA0; AA = 1; break; case 0xA8: //P3 = 0xA0; I2DAT = data_received[data_num]; data_num++; AA = 1; break; case 0xB8: //P3 = 0xB8; I2DAT = data_received[data_num]; data_num++; AA = 1; break; case 0xC0: AA = 1; break; case 0xC8: //P3 = 0xC8; AA = 1; break; } SI = 0; while(STO); } //======================================================================================================== void Init_I2C(void) { P13_Quasi_Mode; //set SCL (P13) is Quasi mode P14_Quasi_Mode; //set SDA (P14) is Quasi mode SDA = 1; //set SDA and SCL pins high SCL = 1; set_P0SR_6; //set SCL (P06) is Schmitt triggered input select. set_EI2C; //enable I2C interrupt by setting IE1 bit 0 set_EA; I2ADDR = EEPROM_SLA; //define own slave address set_I2CEN; //enable I2C circuit set_AA; } //======================================================================================================== void main(void) { Set_All_GPIO_Quasi_Mode; /* Initial I2C function */ Init_I2C(); //initial I2C circuit while (1); /* =================== */ } |
|
相关推荐
1个回答
|
|
|
sp; * This code is written to explain how to use I2C communication protocol *//*---------------------------------------------------------------------------------------------------------*/#include "N76E003.h" #include "SFR_Macro.h" #include "Function_define.h"#define SLAVE_ADDRESS 0xA0 // Address of the slave device#define I2C_CLOCK_RATE 400000 // I2C clock rate in Hz#define I2C_TIMEOUT 1000 // I2C timeout in system clock cycles#define I2C_BUFFER_SIZE 16 // Maximum I2C buffer size for receiving databit i2c_busy; // I2C busy flagbit i2c_error; // I2C error flaguint8_t i2c_buffer[I2C_BUFFER_SIZE]; // I2C buffer for receiving datauint8_t i2c_buffer_index; // Index of next byte to be received void I2C_IRQHandler(void) { uint8_t status; status = I2C0STAT; // Get I2C status register value switch (status) { case 0x60: // SLA+R has been received, ACK has been returned i2c_buffer_index = 0; i2c_busy = 1; // Set I2C busy flag i2c_error = 0; // Clear I2C error flag I2C0CON0 |= 0x01; // Set ACK bit break; case 0x80: // Data byte has been received, ACK has been returned if (i2c_buffer_index < I2C_BUFFER_SIZE) { i2c_buffer[i2c_buffer_index++] = I2C0DAT; I2C0CON0 |= 0x01; // Set ACK bit } else { i2c_error = 1; // Set I2C error flag I2C0CON0 &= ~0x04; // Send NACK I2C0CON0 |= 0x02; // Stop I2C communication i2c_busy = 0; // Clear I2C busy flag } break; case 0xA0: // STOP or repeated start condition has been detected, set SLA and R/W bits to 0 I2C0CON0 &= ~0x01; // Clear ACK bit i2c_busy = 0; // Clear I2C busy flag break; default: // Unknown status received i2c_error = 1; // Set I2C error flag I2C0CON0 &= ~0x04; // Send NACK I2C0CON0 |= 0x02; // Stop I2C communication i2c_busy = 0; // Clear I2C busy flag } I2C0SI = 0; // Clear I2C interrupt flag} void I2C_init(void) { uint32_t pclk; P12_QUASI_MODE; // Enable P1.2 as open-drain output P13_QUASI_MODE; // Enable P1.3 as open-drain output pclk = Fsys_GetMHZ() * 1000000; // Get system clock frequency CLK_PeripheralClockConfig(CLK_I2C_EN, ENABLE); // Enable I2C clock I2C0ADDR0 = SLAVE_ADDRESS << 1; // Set device address I2C0CLK = pclk / (I2C_CLOCK_RATE << 1); // Set clock rate I2C0TOC = (pclk * I2C_TIMEOUT) / (1000000 << 3); // Set timeout period I2C0CON0 = 0x80; // Enable I2C module I2C0CON1 = 0x00; // Clear I2C interrupt flag I2C0CON2 = 0x00; // Set hold and setup times to 0 IPH |= Set_BIT6; // Set I2C interrupt priority to high I2C0IE = 1; // Enable I2C interrupt EA = 1; // Enable global interrupts} void main(void) { I2C_init(); while(1) { if (i2c_busy == 0) // Check if I2C is idle { I2C0CON1 |= 0x02; // Send start condition while(i2c_busy == 0) // Wait for I2C communication to finish { // Do nothing } if (i2c_error == 0) // Check if I2C communication was successful { // Process received data here } } }}
|
|
|
|
|
只有小组成员才能发言,加入小组>>
1018 浏览 1 评论
1849 浏览 0 评论
1830 浏览 1 评论
3254 浏览 5 评论
3581 浏览 9 评论
1021浏览 1评论
1850浏览 1评论
如何知道嵌入式电子控制单元 (ECU) 中的RAM使用情况?
1360浏览 1评论
1852浏览 0评论
1178浏览 0评论
/9
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2025-12-2 01:17 , Processed in 0.751958 second(s), Total 76, Slave 56 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191

淘帖
549