PIC Development Board with PK3

hexreader

Joined Apr 16, 2011
581
Your battery connection appears to be floating.

I see old threads on the Microchip forum that seem to suggest that DS1307 will not work with pin 3 (Vbat) unconnected.
 

Thread Starter

Djsarakar

Joined Jul 26, 2020
489
Your battery connection appears to be floating.

I see old threads on the Microchip forum that seem to suggest that DS1307 will not work with pin 3 (Vbat) unconnected.
@hexreader I am going to shorting the battery terminals with a 100R. I have very poor understand to read layout.

I have identified Vbat pin on board but not sure see the black line. Can you confirm this ?

1620999786111.png
 

hexreader

Joined Apr 16, 2011
581
The first picture is wrong - you are connecting pin 6 to 200 Ohm resistor.
The second picture is correct.

A more reliable way is to connect your 200 Ohm resistor to the pins where the battery holder would go.

Why not do a proper job and buy a battery holder and battery? A clock with no battery is a useless device.
 

Thread Starter

Djsarakar

Joined Jul 26, 2020
489
Why not do a proper job and buy a battery holder and battery? A clock with no battery is a useless device.
I will purchase but for now its not available in the local market. if I order online it will take one or two moths because of covid[/QUOTE]
 
Last edited:

JohnInTX

Joined Jun 26, 2012
4,787
If your immediate goal is to get the I2C link up and debugged, you could use the 24C04 EEPROM. Except for the slave addresses, the EE and RTC have the same I2C format. Treat the first 8 bytes of the EE as the 8 registers of the RTC. The only other difference is you need to wait 10msec after writing the EE.
 

Thread Starter

Djsarakar

Joined Jul 26, 2020
489
If your immediate goal is to get the I2C link up and debugged, you could use the 24C04 EEPROM. Except for the slave addresses, the EE and RTC have the same I2C format. Treat the first 8 bytes of the EE as the 8 registers of the RTC. The only other difference is you need to wait 10msec after writing the EE.
Yes Now I'm thinking exactly same. I have tried everything as per my best knowledge but I could not solved problem. I was getting error code 0x02 that means no acknowledgement from IN1307.

For 24C04 EEPROM I have to desolder pull resistor that I was using for ds1307.

@JohnInTX if possible please review my code and let me know if you find any bug

1621006115854.png
 

hexreader

Joined Apr 16, 2011
581
Shopping list:

1) 10x 0.1 uF ceramic capacitor (for decoupling - needs mounting on track side of development board, as designer failed to provide space on PCB)
2) CR2032 batteries and 2032 horizontal through-hole battery holder
3) 10x 4K7 resistors - 10x 10K resistors
4) 2x 4-pin resistor pack (3x 4k7 with single common connection)
5) whatever else I suggested in the past, but I forgot about

EDIT: 6) cheap ($10 to $20 + delivery) Logic Analyser
 
Last edited:

JohnInTX

Joined Jun 26, 2012
4,787
For 24C04 EEPROM I have to desolder pull resistor that I was using for ds1307.
I would just add wires to connect both SDA together and both SCL together then use the same pullups for both chips to make an I2C bus. Connect the resulting I2C bus to the PIC in the normal way. Pull the RTC chip from the socket to keep it from affecting the I2C signals until you get the rest working. Use your multimeter to verify +5V at SCL and SDA when idle then turn power off and verify proper connections to the PIC.
 

JohnInTX

Joined Jun 26, 2012
4,787
I ran up the code on an 18F4685 (don't have a 46K80) and it failed by missing every other byte in the write message. The WCOL (write collision) flag was set, indicating that SSPBUF was written while it was still sending the previous byte. It turned out, on my chip anyway, that it takes a little time between writing SSPBUF and the setting of SEN. The result is that in I2C_SendByte, it doesn't wait for the first byte to be transmitted before returning and the next byte is written to SSPBUF while the first is still being sent. That's what sets WCOL. A 'NOP' delay between writing SSPBUF and polling for SEN took care of the problem. I have seen similar issues in the MSSP on other flags and controls, notably CKE in slave mode so it's not a big surprise.
Delay added between writing SSPBUF and polling for SEN.:
//Send one byte. Return 0 if ACK received, or 1 if NAK received
__bit I2C_Sendbyte(unsigned char dat)
{
    SSPBUF = dat;

    asm("nop");     // <<<--- wait a little for R_W to be set

    while (R_W);    //wait until byte sent and ACK/NAK received
    return ACKSTAT;
}
I don't know for sure if that is @Djsarakar 's problem since it is a different PIC but after adding the NOP, the rest of the code runs without issue.
ALSO: in the code snippet posted in #149 you are not calling Port_Initialized() to initialize your IO.
Good luck!
 
Last edited:

Thread Starter

Djsarakar

Joined Jul 26, 2020
489
@Djsarakar 's problem since it is a different PIC but after adding the NOP, the rest of the code runs without issue.
ALSO: in the code snippet posted in #149 you are not calling Port_Initialized() to initialize your IO.
Good luck!
@JohnInTX Thank you for help. I tried the code with calling Port_Initialized()
But I am getting error code 0x02.

There is a problem with this board when I was writing the code for the LCD, Data was not showing to my display reason was jumper wires. because I connected the jumper wires. When I soldered the LCD on board, the LCD started working well.
 

Thread Starter

Djsarakar

Joined Jul 26, 2020
489
I don't know for sure if that is @Djsarakar 's problem since it is a different PIC but after adding the NOP, the rest of the code runs without issue.
@JohnInTX I was not sure yet that there is problem with program or in the program.

I have extra I2C ADS1115. I would test code perhaps this way we can find out whether program is faulty or hardware

Connection between ADS1115 and PIC18
IC VDD - 5V DC
IC GND - GND
IC SCL - RC3 pin of PIC18
IC SDA - RC4 pin of PIC18
ADDR - Ground
 

Attachments

Last edited:

Thread Starter

Djsarakar

Joined Jul 26, 2020
489
@JohnInTX

I have written program that only send one byte from master to slave and check ACK or NAK. I ran below code but again I get same error code 0x2 NAK

C:
#define _XTAL_FREQ 20000000     // crystal 20MHz
// PIC18F45K80 Configuration Bit Settings
// CONFIG1L
#pragma config RETEN = ON       // VREG Sleep Enable bit (Ultra low-power regulator is Enabled (Controlled by SRETEN bit))
#pragma config INTOSCSEL = LOW  // LF-INTOSC Low-power Enable bit (LF-INTOSC in Low-power mode during Sleep)
// SOSCSEL = No Setting
#pragma config XINST = OFF      // Extended Instruction Set (Disabled)
// CONFIG1H
#pragma config FOSC = HS2       // HS oscillator (high power, 16 MHz-25 MHz
#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 = ON      // Power Up Timer (Enabled)
#pragma config BOREN = OFF      // Brown Out Detect (Disabled in hardware, SBOREN disabled)
#pragma config BORV = 0         // Brown-out Reset Voltage bits (3.0V)
#pragma config BORPWR = LOW     // BORMV Power level (BORMV set to low power level)
// CONFIG2H
#pragma config WDTEN = OFF      // Watchdog Timer (WDT disabled in hardware; SWDTEN bit disabled)
#pragma config WDTPS = 1        // Watchdog Postscaler (1:1)
// CONFIG3H
#pragma config CANMX = PORTC    // ECAN Mux bit (ECAN TX and RX pins are located on RC6 and RC7, respectively)
#pragma config MSSPMSK = MSK5   // MSSP address masking (5 bit address masking mode)
#pragma config MCLRE = ON      // Master Clear Enable (MCLR Enabled, RE3 Disabled)
// CONFIG4L
#pragma config STVREN = OFF     // Stack Overflow Reset (Disabled)
#pragma config BBSIZ = BB1K     // Boot Block Size (1K word Boot Block size)
// CONFIG5L
#pragma config CP0 = ON         // Code Protect 00800-01FFF (Enabled)
#pragma config CP1 = ON         // Code Protect 02000-03FFF (Enabled)
#pragma config CP2 = ON         // Code Protect 04000-05FFF (Enabled)
#pragma config CP3 = ON         // Code Protect 06000-07FFF (Enabled)
// CONFIG5H
#pragma config CPB = ON         // Code Protect Boot (Enabled)
#pragma config CPD = ON         // Data EE Read Protect (Enabled)
// CONFIG6L
#pragma config WRT0 = ON        // Table Write Protect 00800-01FFF (Enabled)
#pragma config WRT1 = ON        // Table Write Protect 02000-03FFF (Enabled)
#pragma config WRT2 = ON        // Table Write Protect 04000-05FFF (Enabled)
#pragma config WRT3 = ON        // Table Write Protect 06000-07FFF (Enabled)
// CONFIG6H
#pragma config WRTC = ON        // Config. Write Protect (Enabled)
#pragma config WRTB = ON        // Table Write Protect Boot (Enabled)
#pragma config WRTD = ON        // Data EE Write Protect (Enabled)
// CONFIG7L
#pragma config EBTR0 = ON       // Table Read Protect 00800-01FFF (Enabled)
#pragma config EBTR1 = ON       // Table Read Protect 02000-03FFF (Enabled)
#pragma config EBTR2 = ON       // Table Read Protect 04000-05FFF (Enabled)
#pragma config EBTR3 = ON       // Table Read Protect 06000-07FFF (Enabled)
// CONFIG7H
#pragma config EBTRB = ON       // Table Read Protect Boot (Enabled)
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

#include <xc.h>

#define ADS1115_ADDRESS 0x90    //address of ADS1115 with write mode  (

unsigned char Result;


void Port_Initialized (void)
{

// LATx registers
    LATA =  0x00;
    LATB =  0x00;
    LATC =  0x00;
    LATD =  0x00;
    LATE =  0x00;

//  TRISx registers
    TRISA = 0x00;    // All are output, Unused
    TRISB = 0x00;   // all are output, Unused
    TRISD = 0x00;   // all are output, Unused
    TRISE = 0x00;   // All are output, Unused
   
    ANCON0 = 0x00;    // digital
    ANCON1 = 0x00;    // Set to digital port
    CM1CON = 0x00;      // Comparator off
    CM2CON = 0x00;      // Comparator off
    ADCON0 = 0x00;      // A/D conversion Disabled
    ADCON1 = 0x00;      // A/D conversion Disabled
    ADCON2 = 0x00;     // A/D conversion Disabled
}

//Initialize I2C in master mode
void I2C_Initialized(void)
{
     TRISCbits.TRISC3 = 1; // Clock Pin
     TRISCbits.TRISC4 = 1; // SDA Pin
   
    SSPSTAT=0x80; //Slew rate control is disabled for Standard Speed mode (100 kHz and 1 MHz)
    SSPCON1=0x28; // I2C Master mode, clock = FOSC/(4 * (SSPADD + 1))
    SSPCON2=0x00;
    SSPADD = 49;  //100kHz clock @ 20MHz Fosc SSPADD = ( (Fosc/4) / BiteRate )-1    
   // SSPADD = ( 20MHz / 100KHz ) - 1 = 49 //
}

// Send an I2C START
// Return 0 if all ok, 1 if bus collision
__bit I2C_Start(void)
{
    BCLIF = 0;  //Clear 'Bus collision" flag
    SEN = 1;    //initiate a START cycle
    while (SEN);    //wait until it has been sent
    return BCLIF;   //return value of BCLIF flag
}

// Send an I2C STOP
void I2C_Stop(void)
{
    PEN = 1;    //initiate a STOP cycle
    while (PEN);    //wait until it has been sent
}

// Send an I2C REPEATED START
void I2C_Restart(void)
{
    RSEN = 1;    //initiate a REPEATED START cycle
    while (RSEN);    //wait until it has been sent
}

//Receive one byte. ackflag=0 to send ACK, or 1 to send NAK in reply
//Send one byte. Return 0 if ACK received, or 1 if NAK received
__bit I2C_Sendbyte(unsigned char dat)
{
    SSPBUF = dat;

    asm("nop");     // <<<--- wait a little for R_W to be set

    while (R_W);    //wait until byte sent and ACK/NAK received
    return ACKSTAT;
}

unsigned char I2C_Recvbyte(unsigned char ackflag)
{
    RCEN = 1;   // initiate a RECEIVE cycle
    ACKDT = ackflag;    //specify if we should send ACK or NAK after receiving
    while (RCEN);   //wait until RECEIVE has completed
    ACKEN = 1;  //initiate an ACK cycle
    while (ACKEN);  //wait until it has completed
    return SSPBUF;
}

//Send address one byte address of I2C device.
//Return 0 if all OK, 1 if bus error, 2 if slave address NAK,
unsigned char ADS1115_Write(unsigned char slave_address)
{
    if (I2C_Start() )   //send a start, and check if it succeeded
        return 1;   //abort if bus collision
    //send the I2C slave address (force R/W bit low)
    if (I2C_Sendbyte(slave_address))
    {
        I2C_Stop(); //if address was NAKed, terminate the cycle
        return 2;   //and return error code
    }

    I2C_Stop();
    return 0;   //no error
}

void main(void)
{
     Port_Initialized ();
   
     I2C_Initialized();
   
     Result = ADS1115_Write(ADS1115_ADDRESS);
   
    while(1);
 
}
 
Last edited:

Thread Starter

Djsarakar

Joined Jul 26, 2020
489
This is screenshot of debug window. I think there is a problem with program and I have to find out what's that problem.

1621151618427.png
 
Last edited:

JohnInTX

Joined Jun 26, 2012
4,787
If multiple devices fail the same way, it's probably not the devices but the bus. I would do this:
Power off.
Remove the DS1307 and the ADS1115 from the circuit.
Verify that SCL and SDA are properly connected to the PIC using the continuity function on your meter.
Make sure you have a suitable capacitor on VDDCORE (pin 6). See section 2.4 in the datasheet.

Install the 24C04 EEPROM. It is the simplest to use.
Change the slave address to 0xA0 for the EEPROM.

Delete these lines
TRISCbits.TRISC3 = 1; // Clock Pin
TRISCbits.TRISC4 = 1; // SDA Pin

and replace with:
TRISC = 0x18; in the init IO routine.
The reason is that you never want to set TRIS bits individually, particularly when using the I2C peripheral.

Add this line just before #include <xc.h> to disable the 'function not called' message.
#pragma warning disable 520

Leave the rest of the code as is. You can't debug effectively if you continually change the code. All that does is introduce new errors.

Compile to NO ERRORS, NO WARNINGS. Fix any warnings. It is pointless to debug code that the compiler says has problems.

Set two breakpoints
1) line 169, just before calling the Start routine
2) line 153, just after calling the Start routine

Run to the first breakpoint. Get your meter and verify that SCL and SDA are both solid logic '1' i.e. > .7 * Vdd = >3.5V
Run to the next breakpoint and verify that SCL and SDA are both solid logic '0' i.e. < 0.8V

If all of that is OK, there is probably some other init stuff that we can take a look at but do all of this first.
Good luck!
 
Last edited:
Top