• AVR Freaks

AnsweredHot!I2C not working

Author
Mike432
Starting Member
  • Total Posts : 42
  • Reward points : 0
  • Joined: 2016/08/15 06:26:37
  • Location: 0
  • Status: offline
2019/04/21 15:36:38 (permalink)
3 (2)

I2C not working

So I have a AM2320 Temp/Humidity sensor and I'm trying to get readings from it using I2C.  It seems that my I2C is never starting.
 
In my wake sensor code I have an output to a LED that should toggle but my code never reaches that point.
 
I pulled the sensor from the board then went into debug mode to see what's happening and found that the SEN bit does not get set even thought I am telling it SEN=1
 
Any help would be greatly appreciated, Thanks!
 
// PIC16F15313 Configuration Bit Settings

// 'C' source line config statements

// CONFIG1
#pragma config FEXTOSC = OFF // External Oscillator mode selection bits (Oscillator not enabled)
#pragma config RSTOSC = HFINT32 // Power-up default value for COSC bits (HFINTOSC with OSCFRQ= 32 MHz and CDIV = 1:1)
#pragma config CLKOUTEN = OFF // Clock Out Enable bit (CLKOUT function is disabled; i/o or oscillator function on OSC2)
#pragma config CSWEN = ON // Clock Switch Enable bit (Writing to NOSC and NDIV is allowed)
#pragma config FCMEN = ON // Fail-Safe Clock Monitor Enable bit (FSCM timer enabled)

// CONFIG2
#pragma config MCLRE = OFF // Master Clear Enable bit (MCLR pin function is port defined function)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config LPBOREN = OFF // Low-Power BOR enable bit (ULPBOR disabled)
#pragma config BOREN = ON // Brown-out reset enable bits (Brown-out Reset Enabled, SBOREN bit is ignored)
#pragma config BORV = LO // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (VBOR) set to 1.9V on LF, and 2.45V on F Devices)
#pragma config ZCD = OFF // Zero-cross detect disable (Zero-cross detect circuit is disabled at POR.)
#pragma config PPS1WAY = ON // Peripheral Pin Select one-way control (The PPSLOCK bit can be cleared and set only once in software)
#pragma config STVREN = ON // Stack Overflow/Underflow Reset Enable bit (Stack Overflow or Underflow will cause a reset)

// CONFIG3
#pragma config WDTCPS = WDTCPS_31// WDT Period Select bits (Divider ratio 1:65536; software control of WDTPS)
#pragma config WDTE = OFF // WDT operating mode (WDT Disabled, SWDTEN is ignored)
#pragma config WDTCWS = WDTCWS_7// WDT Window Select bits (window always open (100%); software control; keyed access not required)
#pragma config WDTCCS = SC // WDT input clock selector (Software Control)

// CONFIG4
#pragma config BBSIZE = BB512 // Boot Block Size Selection bits (512 words boot block size)
#pragma config BBEN = OFF // Boot Block Enable bit (Boot Block disabled)
#pragma config SAFEN = OFF // SAF Enable bit (SAF disabled)
#pragma config WRTAPP = OFF // Application Block Write Protection bit (Application Block not write protected)
#pragma config WRTB = OFF // Boot Block Write Protection bit (Boot Block not write protected)
#pragma config WRTC = OFF // Configuration Register Write Protection bit (Configuration Register not write protected)
#pragma config WRTSAF = OFF // Storage Area Flash Write Protection bit (SAF not write protected)
#pragma config LVP = ON // Low Voltage Programming Enable bit (Low Voltage programming enabled. MCLR/Vpp pin function is MCLR.)

// CONFIG5
#pragma config CP = OFF // UserNVM Program memory code protection bit (UserNVM code protection disabled)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

#include <xc.h>

 void WakeSensor(void){
     SSP1CON2bits.SEN = 1; //start signal
     while(!PIR3bits.SSP1IF){} //Wait for the start signal to finish
     PIR3bits.SSP1IF = 0; //clear the flag -------------------------------Never gets here-------------
     LATAbits.LATA4 ^= 1; //temp output to led for testing+++++++++
     SSP1BUFbits.SSPBUF = 0xB8; //sending address
     //SSP1STATbits.RW = 0; // should not need to set?
     _delay(6400); //delay 800uS
     LATAbits.LATA4 ^= 1; //temp output to led for testing+++++++++
     PIR3bits.SSP1IF = 0; //clear the flag
     SSP1CON2bits.PEN = 1; //Send the stop signal
     while(!PIR3bits.SSP1IF){} //Wait for the stop signal to finish
     PIR3bits.SSP1IF = 0; //clear the flag
     LATAbits.LATA4 ^= 1; //temp output to led for testing+++++++++
 }

void DelayFiveSeconds(void){
    _delay(39999996); //delay 5 seconds, 40M = 5 seconds - 4 cycles for the call
}

void main(void) {
    //Settings for 32MHz clock and no pre-scale
    OSCFRQbits.HFFRQ = 6;
    OSCCON1bits.NDIV = 0b0000;
    OSCCON1bits.NOSC = 0b110;
    
    //Interrupts (if used)
    INTCONbits.GIE = 1;
    INTCONbits.PEIE = 1;
    //Port direction
    TRISAbits.TRISA0=1; //input
    TRISAbits.TRISA1=1;
    TRISAbits.TRISA2=1;
    TRISAbits.TRISA4=0;
    TRISAbits.TRISA5=0;
    //Port Analog/Digital
    ANSELAbits.ANSA0=1; //analog
    ANSELAbits.ANSA1=0;
    ANSELAbits.ANSA2=0;
    ANSELAbits.ANSA4=0;
    ANSELAbits.ANSA5=0;
    //setup for I2C
    SSP1CON1bits.SSPM = 0b1000; //Master mode
    SSP1CON1bits.SSPEN = 1; //enable I2C
    SSP1ADDbits.SSPADD = 0x4F; //Setting for 100KHz transfer speed with 32MHz PIC
    
    SSP1STATbits.SMP=1; /* Slew rate disabled */
    
    //never ending loop
    while(1){
        DelayFiveSeconds();
        WakeSensor();
        GetReadings();
        ConvertReadings();
        DisplayErrorBinary(); //for testing+++++++++++++++++++++++++++++++
        DoActions();
    }
    return;
}

#1
jtemples
عُضْوٌ جَدِيد
  • Total Posts : 11394
  • Reward points : 0
  • Joined: 2004/02/13 12:31:19
  • Location: Southern California
  • Status: offline
Re: I2C not working 2019/04/21 15:54:50 (permalink)
#2
Mysil
Super Member
  • Total Posts : 3403
  • Reward points : 0
  • Joined: 2012/07/01 04:19:50
  • Location: Norway
  • Status: offline
Re: I2C not working 2019/04/21 18:05:11 (permalink) ☄ Helpfulby Mike432 2019/06/16 13:46:19
+1 (1)
Hi,
PIC16F15313 is a device with PPS remapping of I/O signals, also for I2C signals,
but I cannot see any setup for PPS in the code.
There is a default mapping for SDA and SCL input signals,
but there is no default routing for the output.
Furthermore, in the 8 pin device, one of the default I2 signals, use the same pin as one of the Programmer/Debugger signals, so for development, you should select some other pin.
 
I2C is a 'Listen before you talk' protocol, hardware will check that SDA and SCL signal levels are both High logic level before making Start signal sequence. If that is not correct, the operation will be refused, SEN bit will return to 0, S bit in SSPSTAT register will not be Set, and interrupt flag will remain unchanged. BCL1IF bit in PIR3 may become Set instead.
 
    Mysil
#3
Mike432
Starting Member
  • Total Posts : 42
  • Reward points : 0
  • Joined: 2016/08/15 06:26:37
  • Location: 0
  • Status: offline
Re: I2C not working 2019/04/21 19:17:27 (permalink)
0
I think this is my first device with PPS, from what I have looked at just now it seems that if I leave it alone then MSSP1 is defaulted to enabled and the Pins RA1 and RA2 are my function pins for it.  The I2C section of the datasheet does not give any detail about needing to change the PPS, it only mentions PPS at the end with the Registers Associated with I2C.
 
I have the SDA and SCL pulled up with 10KOhm resistors. 
 
I thought that the debugging might not work or have problems because of the shared pin but it wasn't working in non-debug mode either.
 
It's getting late so maybe I will read into the PPS a little more tomorrow
 
Thanks,
#4
qhb
Superb Member
  • Total Posts : 9999
  • Reward points : 0
  • Joined: 2016/06/05 14:55:32
  • Location: One step ahead...
  • Status: offline
Re: I2C not working 2019/04/21 23:05:46 (permalink)
+1 (1)
Also note, you will never see the SEN bit when you are in a debugger, as it only stays high for less than a millisecond.
 

Nearly there...
#5
Mysil
Super Member
  • Total Posts : 3403
  • Reward points : 0
  • Joined: 2012/07/01 04:19:50
  • Location: Norway
  • Status: offline
Re: I2C not working 2019/04/22 02:09:47 (permalink)
0 (2)
Hi,
About PPS mapping for I2C pins, it is actually pointed out in the datasheet DS40001897B 
 
Page 6, Table 3: 8-Pin allocation table, pay attention to the Notes!
RA1 pin 6, column MSSP: SCL1(1,4)  
RA2 pin 5, column MSSP: SDA1(1,4) 
further down in the table,
OUT(2), column MSSP: SCL1(3,4)
OUT(2), column MSSP: SDA1(3,4)
Note 1: This is a PPS remappable input signal.
            The input function may be moved from the default location shown to one of several other PORTx pins.
        2: All digital output signals shown in this row are PPS re-mappable.
            These signals may be mapped to output onto one of several PORTx pin options.
        3: This is a bidirectional signal. For normal module operation,
            the firmware should map this signal to the same pin in both the PPS input and PPS output registers.
        4: These pins are configured for I2C logic levels.
            PPS assignments to the other pins will operate,
            but input logic levels will be standard TTL/ST as selected by the INLVL register,
            instead of the I2C specific or SMBUS input buffer thresholds.
 
Page 15:   TABLE 1-2:
Signal  SCL1 (1,2)
Signal  SDA1 (1,2) Pay attention to the Notes!
 
Page 17: TABLE 1-2    PIC16(L)F15313 PINOUT DESCRIPTION (CONTINUED)
    Pay attention to the Notes!
 
Page 189:   15.3 Bidirectional Pins
    Read the big Note block.
 
Page 361:    FIGURE 32-2: MSSP BLOCK DIAGRAM (I2C MASTER MODE)
Note 1: SDA pin selections must be the same for input and output.
        1: SCL pin selections must be the same for input and output.
 
Page 362:    FIGURE 32-3: MSSP BLOCK DIAGRAM (I2C SLAVE MODE)
Read the Notes.
 
Page 371:    32.4.3 SDA AND SCL PINS
Read the paragraph and the big Note block.
 
Regards,
    Mysil
post edited by Mysil - 2019/04/22 06:19:40
#6
Mike432
Starting Member
  • Total Posts : 42
  • Reward points : 0
  • Joined: 2016/08/15 06:26:37
  • Location: 0
  • Status: offline
Re: I2C not working 2019/04/22 04:21:17 (permalink)
0
Page 6, Table 3: Is not very helpful.  It shows SCL1 and SDA1 being mapped to pins RA1 and RA2.  Note 1 shows that the input is remappable, and Note 4 is a bit confusing, it starts out saying that they are configured for I2C but later says that the logic levels are not I2C specific.  The OUT(2) information again states that the pins are remappable but nothing here is telling me that it needs to be remapped.
 
Page 15:   TABLE 1-2:  This is the same  information from page 6.  It tells me that the pins are remappable but not that they need to be.
 
Page 17: TABLE 1-2:  Note 3 "This is a bidirectional signal. For normal module operation, the firmware should map this signal to the same pin in both the PPS input and PPS output registers.".  This points to me having to map the output to match the input but being my first PPS device this did not make much sense the first time I read through it.  All the rest of the information in this table is just repeats from the previous information.  One way someone could read this (as I did the first time) is make sure you remap both the inputs and outputs to be the same, otherwise nothing needs to be done with it.
 
Page 189:   15.3: "The I2C SCLx and SDAx functions can be remapped through PPS. However, only the RB1, RB2, RC3 and RC4 pins have the I2C and SMBus specific input buffers implemented (I2C mode disables INLVL and sets thresholds that are specific for I 2C). If the SCLx or SDAx functions are mapped to some other pin (other than RB1, RB2, RC3 or RC4), the general purpose TTL or ST input buffers (as configured based on INLVL register setting) will be used instead. In most applications, it is therefore recommended only to map the SCLx and SDAx pin functions to the RB1, RB2, RC3 or RC4 pins."  Again this information says that I can remap the pin but shows nothing that I need to.
 
Page 361:  This only shows a note that the pin selection need to be the same for input and output.  Reading this note and all the previous data I would assume that they are defaulted to the same mapping.  So again this information is not useful.
 
Page 362:  This is the same information from Page 361 and not helpful to the resolution of this posting.
 
Page 371:    32.4.3:  Again, this information shows that the pin functions can be remapped but does not say that they need to be.
 
Something that I noticed in the datasheet that might be helpful is page 186 table 15-1: it shows that the input signals have default locations, then looking at page 190 table 15-4 I see that the output signals do not have default locations.  This leads me to believe that the Inputs are fine as they are and that I need to map the outputs.  But again the datasheet does not specifically call out that this needs to happen.
 
I will not be able to try this out until later today but I appreciate the first response that you posted.  It was helpful in getting pointed in the right direction.
post edited by Mike432 - 2019/04/22 04:22:50
#7
pcbbc
Super Member
  • Total Posts : 1373
  • Reward points : 0
  • Joined: 2014/03/27 07:04:41
  • Location: 0
  • Status: offline
Re: I2C not working 2019/04/22 04:57:59 (permalink)
+2 (2)
Is this all your code?
Where is your ISR?
NO need to turn on GIE and PEIE if you are not using interrupts.
Although I could t see you’d enabled any individual interrupt flags, so it may not matter.
#8
Mysil
Super Member
  • Total Posts : 3403
  • Reward points : 0
  • Joined: 2012/07/01 04:19:50
  • Location: Norway
  • Status: offline
Re: I2C not working 2019/04/22 06:13:04 (permalink) ☼ Best Answerby Mike432 2019/06/16 13:51:19
+2 (2)
You are quite right, the same hints are repeated in several places in the datasheet.
I have not tried to do your work for you.
 
You have nowhere clearly specified what device you are using,
it is only comments in configuration settings that indicate device type.
 
If you are actually using PIC16F15313, a device in 8 pin package,
then default SCL signal on pin RA1 will not work together with Debugger, since that pin is already in use by the ICSP clock signal.
 
To see how PPS mapping may actually be done for PIC16F15313, or whatever device you are using,
you may use MCC, Microchip Code Configurator,
to point and click on the pins you want to use for SCL1 and SDA1 signals. 
After generating code, you may look in: /mcc_generated_files/pin_manager.c
to see what settings are made for PPS registers.
 
If you do not want to confuse your current projects, you may make a new MPLAB project for MCC experiments, then eventually copy PPS register settings to your manually created project. 
 
    Mysil
 
#9
Mike432
Starting Member
  • Total Posts : 42
  • Reward points : 0
  • Joined: 2016/08/15 06:26:37
  • Location: 0
  • Status: offline
Re: I2C not working 2019/04/22 06:34:23 (permalink)
0
The I2C generates interrupts after successful completion of the transfer and other places so I think GIE and PEIE need to be on for that.  This question brought up a possible problem, according to the datasheet SSP1IE is defaulted to dissabled.  The area of my code that I was having problems with is related to SSP1IF.
 
If this is what my problem is, it would be nice if the datasheet mentioned enabling the interrupt in the example that is given.  Page 394, 32.6.6.4
 
Thanks,
#10
qhb
Superb Member
  • Total Posts : 9999
  • Reward points : 0
  • Joined: 2016/06/05 14:55:32
  • Location: One step ahead...
  • Status: offline
Re: I2C not working 2019/04/22 06:36:56 (permalink)
+1 (1)
Mike432
The I2C generates interrupts after successful completion of the transfer and other places so I think GIE and PEIE need to be on for that.

Wrong!
You can test the flags without GIE or PEIE set.
You should NOT set GIE if you do not have an interrupt service routine.
 
 

Nearly there...
#11
Mike432
Starting Member
  • Total Posts : 42
  • Reward points : 0
  • Joined: 2016/08/15 06:26:37
  • Location: 0
  • Status: offline
Re: I2C not working 2019/04/22 06:42:41 (permalink)
0
I still would need to set SSP1IE, right?
#12
qhb
Superb Member
  • Total Posts : 9999
  • Reward points : 0
  • Joined: 2016/06/05 14:55:32
  • Location: One step ahead...
  • Status: offline
Re: I2C not working 2019/04/22 06:45:40 (permalink)
+1 (1)
Wrong.
SSPIE does NOT stop SSPIF from being set, its ONLY function is to enable it to jump to the interrupt service routine (if GIE and PEIE are also set).
 

Nearly there...
#13
Mike432
Starting Member
  • Total Posts : 42
  • Reward points : 0
  • Joined: 2016/08/15 06:26:37
  • Location: 0
  • Status: offline
Re: I2C not working 2019/04/22 09:45:30 (permalink)
+1 (1)
The duplicate has been i.p.o.d
#14
Mike432
Starting Member
  • Total Posts : 42
  • Reward points : 0
  • Joined: 2016/08/15 06:26:37
  • Location: 0
  • Status: offline
Re: I2C not working 2019/06/16 13:57:06 (permalink)
0
I have been away from this project for a while because I had more important things to do and because my sensor burned out from not having the I2C configured properly.
 
I used MCC as you suggested and everything was set the way that I had except for the mapping of the pins for I2C outputs.  I have a new sensor and I'm getting proper readings from it.
 
RA1PPS = 0x15; //RA1->MSSP1:SCL1;
RA2PPS = 0x16; //RA2->MSSP1:SDA1;
#15
Jump to:
© 2019 APG vNext Commercial Version 4.5