完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
我对I2C很陌生,很难让它发挥作用。我在好奇心板上使用PIC16F1619。我尝试使用MCC没有运气,所以我尝试在配置寄存器自己。SDA和SCL线对于起始位来说很低,但它们永远不会回升。我已经把数据表整理了几十遍,但找不到任何其他可能漏掉的线索。有人能帮助我理解为什么时钟和数据线会在启动位后保持低电平吗?我只是想看看我是否能通过I2C传送第一个地址,但实际上我没有任何其他东西绑在公共汽车上。为了它的价值,我一直在使用3.3K上拉电阻器,因为我周围没有更大的电阻器。
以上来自于百度翻译 以下为原文 I'm new to I2C and having a hard time getting it working. I am using a PIC16F1619 on the curiosity board. I've tried using MCC with no luck so I took a stab at configuring the registers myself. The SDA and SCL lines go low for the start bit but they never come back up. I've combed through the data sheet dozens of times but can't find any additional clues which I may have missed. Can someone please help me understand why the clock and data lines would be held low after the start bit? I am just trying to see if I can transmit the first address over I2C but I don't actually have anything else tied to the bus. For what it's worth, I've been using 3.3k pull-up resistors because I don't have any larger ones lying around. main.c #include "mcc_generated_files/mcc.h" #include "I2C.h" uint8_t address = 0x70; uint8_t data = 0xAA; uint8_t incoming_data; void initmain(void){ // Setup pins for I2C ANSELB = 0x00; TRISB = 0b00000000; LATB = 0b00000000; ODCONB = 0b01010000; // unlock PPS sequence PPSLOCK = 0x55; PPSLOCK = 0xAA; PPSLOCKbits.PPSLOCKED = 0; // unlock PPS // Set up I2C for RB4 as SDA and RB6 as SCL RB4PPS = 0x0C; // RC4 -> MSSP:SDA SSPDATPPS = 0x11; // RC4 -> MSSP:SDA SSPCLKPPS = 0x10; // RC5 -> MSSP:SCL RB6PPS = 0x0E; // RC5 -> MSSP:SCL // Lock PPS sequence PPSLOCK = 0x55; PPSLOCK = 0xAA; PPSLOCKbits.PPSLOCKED = 1; // lock PPS } void main(void) { initmain(); I2C_Init(); while (1) { /////////////////////////// // WRITE ////////////////////////// Send_I2C_StartBit(); // send start bit Send_I2C_ControlByte(address,0); // send control byte with R/W bit set low Send_I2C_Data(data); // send data byte Send_I2C_StopBit();// send stop bit __delay_ms(200); } } I2C.c #include "I2C.h" #include #include "mcc_generated_files/mcc.h" void I2C_Init(void){ //************************************************************ // Setup MSSP as I2C Master mode, clock rate of 100Khz //************************************************************ SSP1CON1bits.SSPM=0b00001000;// I2C Master mode, clock = Fosc/(4 * (SSPADD+1)) SSP1CON1bits.SSPEN=1; // enable MSSP SSP1ADD = 39; // set Baud rate clock divider __delay_ms(10); // let everything settle. } void Send_I2C_Data(unsigned int databyte) { PIR1bits.SSP1IF = 0; // clear SSP interrupt bit SSP1BUF = databyte; // send databyte while(!PIR1bits.SSP1IF); // Wait for interrupt flag to go high indicating transmission is complete } unsigned int Read_I2C_Data(void) { PIR1bits.SSP1IF=0; SSP1CON2bits.RCEN=1; while(!PIR1bits.SSP1IF);// Wait for interrupt flag to go high indicating transmission is complete return (SSP1BUF); } void Send_I2C_ControlByte(unsigned int BlockAddress,unsigned int RW_bit) { PIR1bits.SSP1IF=0; SSP1BUF = (((0b1010 << 4) | (BlockAddress <<1)) + RW_bit); // send the control byte while(!PIR1bits.SSP1IF);// Wait for interrupt flag to go high indicating transmission is complete } //******************************************************************** // Send start bit to SEE //******************************************************************** void Send_I2C_StartBit(void) { PIR1bits.SSP1IF=0; // clear SSP interrupt bit SSP1CON2bits.SEN=1; // send start bit while(!PIR1bits.SSP1IF); // Wait for the SSPIF bit to go back high before we load the data buffer } void Send_I2C_StopBit(void) { PIR1bits.SSP1IF=0; // clear SSP interrupt bit SSP1CON2bits.PEN=1; // send stop bit while(!PIR1bits.SSP1IF);// Wait for interrupt flag to go high indicating transmission is complete } void Send_I2C_ACK(void) { PIR1bits.SSP1IF=0; // clear SSP interrupt bit SSP1CON2bits.ACKDT=0; // clear the Acknowledge Data Bit - this means we are sending an Acknowledge or 'ACK' SSP1CON2bits.ACKEN=1; // set the ACK enable bit to initiate transmission of the ACK bit to the serial EEPROM while(!PIR1bits.SSP1IF); // Wait for interrupt flag to go high indicating transmission is complete } void Send_I2C_NAK(void) { PIR1bits.SSP1IF=0; // clear SSP interrupt bit SSP1CON2bits.ACKDT=1; // set the Acknowledge Data Bit- this means we are sending a No-Ack or 'NAK' SSP1CON2bits.ACKEN=1; // set the ACK enable bit to initiate transmission of the ACK bit to the serial EEPROM while(!PIR1bits.SSP1IF); // Wait for interrupt flag to go high indicating transmission is complete } |
|
相关推荐
5个回答
|
|
|
|
|
|
以上来自于百度翻译 以下为原文 #define SDA_DIR TRISC4 // Data pin direction #define SCK_DIR TRISC3 #define I2CAddress SSP1ADDbits.SSPADD #define I2Cstatus SSP1STAT #define I2Ccontrol SSP1CON2 void InitI2C(void) { SDA_DIR = 1; // Make SDA and SCK_DIR = 1; // SCK pins input I2CAddress = ((_XTAL_FREQ/4000)/I2C_SPEED) - 1; I2Cstatus = 0x80; // Slew Rate control is disabled I2Ccontrol = 0x28; // Select and enable I2C in master mode } |
|
|
|
我把它们设置为输出,当我将它们设置为输入时,我根本没有得到任何输出。数据表表示MSSP将对I2C引脚进行适当的控制。
以上来自于百度翻译 以下为原文 I did leave them set as output, I wasn’t getting any output at all when I set them as input. The data sheet says that the MSSP will take appropriate control of the pins for i2c. |
|
|
|
我把它们设置为输出,当我将它们设置为输入时,我根本没有得到任何输出。数据表表示MSSP将对I2C引脚进行适当的控制。
以上来自于百度翻译 以下为原文 I did leave them set as output, I wasn’t getting any output at all when I set them as input. The data sheet says that the MSSP will take appropriate control of the pins for i2c. |
|
|
|
|
|
|
|
只有小组成员才能发言,加入小组>>
5184 浏览 9 评论
2005 浏览 8 评论
1932 浏览 10 评论
请问是否能把一个ADC值转换成两个字节用来设置PWM占空比?
3179 浏览 3 评论
请问电源和晶体值之间有什么关系吗?PIC在正常条件下运行4MHz需要多少电压?
2230 浏览 5 评论
739浏览 1评论
626浏览 1评论
有偿咨询,关于MPLAB X IPE烧录PIC32MX所遇到的问题
511浏览 1评论
PIC Kit3出现目标设备ID(00000000)与预期的设备ID(02c20000)不匹配。是什么原因
637浏览 0评论
535浏览 0评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-28 13:57 , Processed in 1.405233 second(s), Total 85, Slave 68 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号