完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
电子发烧友论坛|
您好,我用I2C编写了一个从MPU6000传感器读取数据的代码。目标:PIC18F8K90MPLAB V3.55用XC8 V3.41i从PLIB/I2C.H中检索出一些功能来实现I2C功能,但是,我的代码暂时卡住(SSP2CON2BITS。SEM)AMD永远不会到达主环中的“传感器唤醒”。(P.S.我受启发了)由McMcCd(谢谢马克!)在与MPU6050相关的另一个线程中导入一些函数定义。
以上来自于百度翻译 以下为原文 Hello, I am writing a code to read data from MPU6000 sensor using I2C. Target: PIC18F87k90 MPLAB v3.55 with XC8 v3.41 I retrieved some functions from plib/i2c.h to implement i2c functions but , my code stuck at while(SSP2CON2bits.SEN) amd never reaches to "sensor wake up" in main loop. (P.S. I am inspired by Markmchip(thanks Mark !) to import some of the function definition in another thread related to the MPU6050.) Thank you in Advance ! #define _XTAL_FREQ 16000000 #include #include #include #include #include #include #include #include #define I2C_V3 unsigned char sync_mode=0, slew=0,addr,w,length,status; unsigned char garbage; unsigned char buffer=0x80; unsigned int raw_data[7]; int gyro_x_temp, gyro_y_temp, gyro_z_temp, accel_x_temp, accel_y_temp, accel_z_temp, temp_raw; char jj, buff, gyro_x_out[15], gyro_y_out[15], gyro_z_out[15], accel_x_out[15], accel_y_out[15], accel_z_out[15], temp_out[15]; float temp=0, gyro_x=0, gyro_y=0, gyro_z=0, accel_x=0, accel_y=0, accel_z=0; // CONFIG1L #pragma config RETEN = ON // VREG Sleep Enable bit->Enabled #pragma config INTOSCSEL = HIGH // LF-INTOSC Low-power Enable bit->LF-INTOSC in High-power mode during Sleep #pragma config SOSCSEL = HIGH // SOSC Power Selection and mode Configuration bits->High Power SOSC circuit selected #pragma config XINST = OFF // Extended Instruction Set->Disabled // CONFIG1H #pragma config FOSC = INTIO2 // Oscillator->Internal RC oscillator #pragma config PLLCFG = OFF // PLL x4 Enable bit->Disabled #pragma config FCMEN = OFF // Fail-Safe Clock Monitor->Disabled #pragma config IESO = OFF // Internal External Oscillator Switch Over Mode->Disabled // CONFIG2L #pragma config PWRTEN = OFF // Power Up Timer->Disabled #pragma config BOREN = SBORDIS // Brown Out Detect->Enabled in hardware, SBOREN disabled #pragma config BORV = 3 // Brown-out Reset Voltage bits->1.8V #pragma config BORPWR = ZPBORMV // BORMV Power level->ZPBORMV instead of BORMV is selected // CONFIG2H #pragma config WDTEN = SWDTDIS // Watchdog Timer->WDT enabled in hardware; SWDTEN bit disabled #pragma config WDTPS = 1048576 // Watchdog Postscaler->1:1048576 // CONFIG3L #pragma config RTCOSC = SOSCREF // RTCC Clock Select->RTCC uses SOSC // CONFIG3H #pragma config CCP2MX = PORTC // CCP2 Mux->RC1 #pragma config ECCPMX = PORTH // ECCP Mux->Enhanced CCP1/3 [P1B/P1C/P3B/P3C] muxed with RH7/RH6/RH5/RH4 #pragma config MSSPMSK = MSK7 // MSSP address masking->7 Bit address masking mode #pragma config MCLRE = ON // Master Clear Enable->MCLR Enabled, RG5 Disabled // CONFIG4L #pragma config STVREN = ON // Stack Overflow Reset->Enabled #pragma config BBSIZ = BB2K // Boot Block Size->2K word Boot Block size #pragma config DEBUG = OFF // Background Debug->Disabled // CONFIG5L #pragma config CP0 = OFF // Code Protect 00800-03FFF->Disabled #pragma config CP1 = OFF // Code Protect 04000-07FFF->Disabled #pragma config CP2 = OFF // Code Protect 08000-0BFFF->Disabled #pragma config CP3 = OFF // Code Protect 0C000-0FFFF->Disabled #pragma config CP4 = OFF // Code Protect 10000-13FFF->Disabled #pragma config CP5 = OFF // Code Protect 14000-17FFF->Disabled #pragma config CP6 = OFF // Code Protect 18000-1BFFF->Disabled #pragma config CP7 = OFF // Code Protect 1C000-1FFFF->Disabled // CONFIG5H #pragma config CPB = OFF // Code Protect Boot->Disabled #pragma config CPD = OFF // Data EE Read Protect->Disabled // CONFIG6L #pragma config WRT0 = OFF // Table Write Protect 00800-03FFF->Disabled #pragma config WRT1 = OFF // Table Write Protect 04000-07FFF->Disabled #pragma config WRT2 = OFF // Table Write Protect 08000-0BFFF->Disabled #pragma config WRT3 = OFF // Table Write Protect 0C000-0FFFF->Disabled #pragma config WRT4 = OFF // Table Write Protect 10000-13FFF->Disabled #pragma config WRT5 = OFF // Table Write Protect 14000-17FFF->Disabled #pragma config WRT6 = OFF // Table Write Protect 18000-1BFFF->Disabled #pragma config WRT7 = OFF // Table Write Protect 1C000-1FFFF->Disabled // CONFIG6H #pragma config WRTC = OFF // Config. Write Protect->Disabled #pragma config WRTB = OFF // Table Write Protect Boot->Disabled #pragma config WRTD = OFF // Data EE Write Protect->Disabled // CONFIG7L #pragma config EBRT0 = OFF // Table Read Protect 00800-03FFF->Disabled #pragma config EBRT1 = OFF // Table Read Protect 04000-07FFF->Disabled #pragma config EBRT2 = OFF // Table Read Protect 08000-0BFFF->Disabled #pragma config EBRT3 = OFF // Table Read Protect 0C000-0FFFF->Disabled #pragma config EBRT4 = OFF // Table Read Protect 10000-13FFF->Disabled #pragma config EBRT5 = OFF // Table Read Protect 14000-17FFF->Disabled #pragma config EBRT6 = OFF // Table Read Protect 18000-1BFFF->Disabled #pragma config EBRT7 = OFF // Table Read Protect 1C000-1FFFF->Disabled // CONFIG7H #pragma config EBRTB = OFF // Table Read Protect Boot->Disabled void PIN_MANAGER_Initialize(void) { /** LATx registers */ LATE = 0x40; LATJ = 0x10; LATD = 0x01; LATA = 0x00; LATF = 0x80; LATB = 0x00; LATG = 0x00; LATC = 0x00; LATH = 0x00; /** TRISx registers */ TRISE = 0xB0; TRISF = 0x7E; TRISA = 0xFF; TRISG = 0x1D; TRISB = 0xFF; TRISH = 0x93; TRISC = 0x93; TRISD = 0xFE; TRISJ = 0xE3; /** ANSELx registers */ ANCON0 = 0xDF; ANCON1 = 0xDF; ANCON2 = 0xC0; } void OSCILLATOR_Initialize(void) { // SCS INTOSC; IDLEN disabled; IRCF 16MHz_HF; OSCCON = 0x72; // SOSCGO disabled; MFIOSEL disabled; OSCCON2 = 0x00; // INTSRC INTRC; PLLEN disabled; TUN 0; OSCTUNE = 0x00; // ROSEL System Clock(FOSC); ROON disabled; ROSSLP Disabled in Sleep mode; RODIV Fosc; REFOCON = 0x00; // Set the secondary oscillator } void EUSART1_Initialize(void) { // disable interrupts before changing states PIE1bits.RC1IE = 0; PIE1bits.TX1IE = 0; // Set the EUSART1 module to the options selected in the user interface. // ABDOVF no_overflow; TXCKP async_noninverted_sync_fallingedge; RCIDL idle; BRG16 16bit_generator; WUE disabled; ABDEN disabled; RXDTP not_inverted; BAUDCON1 = 0x48; // SPEN enabled; RX9 8-bit; RX9D 0; CREN enabled; ADDEN disabled; SREN disabled; RCSTA1 = 0x90; // TX9 8-bit; TX9D 0; SENDB sync_break_complete; TXEN enabled; SYNC asynchronous; BRGH hi_speed; CSRC slave_mode; TXSTA1 = 0x24; // Baud Rate = 115200; SPBRG1 34; SPBRG1 = 0x22; // Baud Rate = 115200; SPBRGH1 0; SPBRGH1 = 0x00; // enable receive interrupt PIE1bits.RC1IE = 1; } void C_IdleI2C2( void ) { while ((SSP2CON2 & 0x1F) | (SSP2STAT & 0x04)); } void C_StartI2C2( void ) { //C_IdleI2C2(); SSP2CON2bits.SEN = 1; // initiate bus start condition } void WaitForACK(void) { while(SSP2CON2bits.ACKSTAT); // wait for ack receive from slave } void WaitFlag(void) { while(!PIE2bits.SSP2IE); // wait for flag to be high PIE2bits.SSP2IE=0; } /*last function*/ void WriteByte_I2C2(unsigned char WriteAddressIC, unsigned char WriteAddressRegister, unsigned char Byte) { // Reset I2C variables and set start condition to I2C bus // ResetVariables_I2C(); C_StartI2C2(); while(SSP2CON2bits.SEN); WaitFlag(); // while(!IFS1bits.MI2C1IF); IFS1bits.MI2C1IF=0; // Write Slave address and set master for transmission MasterWriteI2C2((WriteAddressIC << 1)); // Wait till address is transmitted WaitFlag(); WaitForACK(); //Write register address and set master for transmission MasterWriteI2C2(WriteAddressRegister); WaitFlag(); WaitForACK(); //Write 1 Byte data to register and set master for transmission MasterWriteI2C2(Byte); WaitFlag(); WaitForACK(); // ResetVariables_I2C(); //Set stop condition and give I2C bus free for other slaves StopI2C2(); WaitFlag(); WaitForACK(); C_IdleI2C2(); } void C_OpenI2C2() { SSP2ADD=9; // SMP Standard Speed; CKE disabled; SSP2STAT = 0x80; // SSPEN enabled; WCOL no_collision; CKP Idle:Low, Active:High; SSPM FOSC/4_SSPxADD_I2C; SSPOV no_overflow; SSP2CON1 = 0x28; // clear the master interrupt flag PIR2bits.SSP2IF = 0; // enable the master interrupt PIE2bits.SSP2IE = 1; SSP2CON1bits.SSPEN=1; // Enable MSSP port C_IdleI2C2(); //~ } unsigned char buffer7[]=""; void main(void) { LATDbits.LATD0 = 1; PIN_MANAGER_Initialize(); OSCILLATOR_Initialize(); EUSART1_Initialize(); RCONbits.IPEN = 0; puts1USART("hello world"); Write1USART(13); unsigned char i2c_rx[15]; TRISDbits.TRISD5 = 1; TRISDbits.TRISD6 = 1; C_OpenI2C2(); WriteByte_I2C2(0x69,0x6B, 0x00); // WriteByte_I2C2(mpu_I2C_ADDR, mpu_rm_PWR_MGMT_1, 0x00); puts1USART("sensor wake up"); Write1USART(13); while(1) { } } |
|
相关推荐
8个回答
|
|
|
在尝试通信之前,我会等待几百毫秒,此外,我还会重复循环中的事务,并有一些延迟。
以上来自于百度翻译 以下为原文 I would wait some hundreds of milliseconds before attempting to communicate, and moreover I'd also repeat the transaction inside the loop, with some delay. |
|
|
|
|
|
你好,CinziaG,谢谢你的回复。我在CyOpenI2C~()之后加了200毫秒延迟;和CyStistI2c();但是仍然SEN位保持高。
以上来自于百度翻译 以下为原文 Hi CinziaG, Thank you for your reply. I put 200ms delay after C_OpenI2C(); and C_StartI2C(); but still SEN bit stays high. |
|
|
|
|
|
SDA和SCL上有上拉电阻吗?那些针在你开始之前高吗?设置SEN时它们会变低吗?
以上来自于百度翻译 以下为原文 Are there pullup resistors on SDA and SCL? Are those pins high before you start? Do they go low when you set SEN? |
|
|
|
|
|
Hi-QHB,他们在SDA和SCLIf上有4.7K的拔出,我在调试模拟器模式中查看PORTD SFR值。不,RD5和RD6引脚保持低。他们任何时候都不高。谢谢。
以上来自于百度翻译 以下为原文 Hi qhb, They have 4.7k pull up on SDA and SCL If i look at the PORTD SFR value in debug simulator mode. No, RD5 and RD6 pins are staying LOW. They are not HIGH any time. Thanks |
|
|
|
|
|
我的意思是用一个表看引脚。如果引脚很高,但是PoDD读起来很低,那么这是一个配置问题。如果引脚低,这是硬件问题。I2C设备是通电的吗?
以上来自于百度翻译 以下为原文 I meant to look at the pins with a meter. If the pins are high, but PORTD reads as low, then it's a configuration problem. If the pins are low, it's a hardware problem. Is the I2C device powered up? |
|
|
|
|
|
Hi-QHB,传感器用3.3V供电(在引脚18和13之间:数据表页22),我在SDA(PIN NO:23)上检查PIN是3.3V,SCL(PIN NO:24)引脚在表上是0.019V。而当我编程时,两个引脚都被拉高。代码初始化后SDA变低,SCL保持在3.3V。谢谢。
以上来自于百度翻译 以下为原文 Hi qhb, Sensor is powered up with 3.3V(between pin 18 and 13 : datasheet page No.22), i checked on SDA(pin no:23) pin on meter is 3.3V and SCL(pin no:24) pin on meter is 0.019V. While i do programming, both pins are pulled high. After code initialized SDA goes low and SCL stays high at 3.3V. Thank you. |
|
|
|
|
|
你是指目标模块上的SDA和SCL PIN号吗?你检查过PIC本身了吗?(引脚65和64)您是否确保了LCD外围设备没有启用?(这些引脚与SEG5和SEG6共享)
以上来自于百度翻译 以下为原文 Are you referring to SDA & SCL pin numbers on the target module? Have you checked on the PIC itself? (Pins 65 and 64) Have you made sure the LCD peripheral is not enabled? (Those pins are shared with SEG5 and SEG6) |
|
|
|
|
|
嗨,QHB,是的,我指的是目标模块PIOUT。在我的定制板上,64和65通过PCB迹线连接到相应的引脚到目标模块。(自定义板与MikroE代码一起工作),我这样做:是否有其他要求禁用SEG5/SEG6谢谢?
以上来自于百度翻译 以下为原文 Hi qhb, Yes, i am referring to target module pinouts. On my custom board, 64 and 65 are connected through pcb traces to the corresponding pins to target module. (Custom board works with MikroE code.) I did this: TRISDbits.TRISD5 = 1; TRISDbits.TRISD6 = 1; Is there any other requirements to disable SEG5/SEG6. Thanks |
|
|
|
|
只有小组成员才能发言,加入小组>>
MPLAB X IDE V6.25版本怎么对bootloader和应用程序进行烧录
494 浏览 0 评论
5807 浏览 9 评论
2347 浏览 8 评论
2235 浏览 10 评论
请问是否能把一个ADC值转换成两个字节用来设置PWM占空比?
3542 浏览 3 评论
1150浏览 1评论
有偿咨询,关于MPLAB X IPE烧录PIC32MX所遇到的问题
1117浏览 1评论
我是Microchip 的代理商,有PIC16F1829T-I/SS 技术问题可以咨询我,微信:A-chip-Ti
884浏览 1评论
MPLAB X IDE V6.25版本怎么对bootloader和应用程序进行烧录
495浏览 0评论
/9
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2025-12-10 23:20 , Processed in 0.943823 second(s), Total 88, Slave 71 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191

淘帖
3857