• AVR Freaks

AnsweredHot!PIC16F15313 programed for I2C now PICKIT3 will not work

Author
Mike432
Starting Member
  • Total Posts : 37
  • Reward points : 0
  • Joined: 2016/08/15 06:26:37
  • Location: 0
  • Status: offline
2019/06/12 16:41:05 (permalink)
0

PIC16F15313 programed for I2C now PICKIT3 will not work

I think I need to add in an I2CAck to my code but when I try to re-program with my PicKit3 I get connection errors.  I think theses errors are happening because the device is in I2C mode and I2C shares a line with the PicKit3 (pin 5 = I2C SCK and ICSPCLK for PicKIt3).  Is there a way to recover my device?
 
// CONFIG1
#pragma config FEXTOSC = OFF // External Oscillator mode selection bits->Oscillator not enabled
#pragma config RSTOSC = HFINT1 // Power-up default value for COSC bits->HFINTOSC (1MHz)
#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 = OFF // Low Voltage Programming Enable bit->High Voltage on MCLR/Vpp must be used for programming


// CONFIG5
#pragma config CP = OFF // UserNVM Program memory code protection bit->UserNVM code protection disabled


#define _XTAL_FREQ 4000000


#include <xc.h>
#include <stdint.h> //unit16_T variable

uint16_t convertedValue;//ADC value
uint8_t address = 0x40; //sensor address
double fTemp; //temperature reading in F
double setTemp; //temperature set point
double setHumidity; //humidity set point
double pHumid; //percent humidity

union humidity {
    uint16_t humidity;
    uint8_t bytes[2];
}humidityReading;

union temperature{
    uint16_t temperature;
    uint8_t bytes[2];
}temperatureReading;

void ReadADC(void){
    //Channel select
    ADCON0bits.CHS = 0; //select channel RA0
    ADCON0bits.ADON = 1; // Turn on the ADC module
    //Delay
    __delay_us(5);
    //Start Conversion
    ADCON0bits.GOnDONE = 1; // Start the conversion
    //Wait for completion
    while (ADCON0bits.GOnDONE){} // Wait for the conversion to finish
    convertedValue = (ADRESH << 8)+ ADRESL; // Conversion finished
}

void I2CStart()
{
    SSP1CON2bits.SEN = 1; /* Start condition enabled */
    while(SSP1CON2bits.SEN) /* automatically cleared by hardware */
    { /* wait for start condition to finish */
    NOP();
    }
}

void I2CAck()
{
    SSP1CON2bits.ACKDT = 0; /* Acknowledge data bit, 0 = ACK */
    SSP1CON2bits.ACKEN = 1; /* Ack data enabled */
    while(SSP1CON2bits.ACKEN) /* wait for ack data to send on bus */
    {
    NOP();
    }
}

void I2CNak()
{
    SSP1CON2bits.ACKDT = 1; /* Acknowledge data bit, 1 = NAK */
    SSP1CON2bits.ACKEN = 1; /* Ack data enabled */
    while(SSP1CON2bits.ACKEN) /* wait for ack data to send on bus */
    {
    NOP();
    }
}

void I2CWait()
{
    while ((SSP1CON2 & 0x1F ) || ( SSP1STATbits.R_nW /* .R_NOT_W*/ ) ) /* wait for any pending transfer */
    {
    NOP();
    }
}

void I2CRestart()
{
    SSP1CON2bits.RSEN = 1; /* Repeated start enabled */
    while(SSP1CON2bits.RSEN) /* wait for condition to finish */
    {
    NOP();
    }
}

void I2CStop()
{
    SSP1CON2bits.PEN = 1; /* Stop condition enabled */
    while(SSP1CON2bits.PEN) /* Wait for stop condition to finish */
    { /* PEN automatically cleared by hardware */
    NOP();
    }
}

void I2CSend(uint8_t u8)
{
    SSP1BUF = u8; /* Move data to SSPBUF */
    while(SSP1STATbits.BF) /* wait till complete data is sent from buffer */
    {
    NOP();
    }
    I2CWait(); /* wait for any pending transfer */
}

uint8_t I2CRead(void)
{
    uint8_t u8;
   /* Reception works if transfer is initiated in read mode */
    SSP1CON2bits.RCEN = 1; /* Enable data reception */
    while(!SSP1STATbits.BF) /* wait for buffer full */
    {
    NOP();
    }
    u8 = SSP1BUF; /* Read serial buffer and store in temp register */
    I2CWait(); /* wait to check any pending transfer */
    return u8; /* Return the read data from bus */
}

/*void __interrupt() INTERRUPT_InterruptManager (void){
    if(IOCAFbits.IOCAF3 == 1){
        while(PORTAbits.RA3 == 1){
            NOP();
            IOCAFbits.IOCAF3 = 0;
        }
    }
}*/

void main(void) {
    //************************need to unlock PPS first?*******************
    //Pin setup
    LATA = 0x00;
    TRISA = 0x07; //inputs on RA0-2
    ANSELA = 0x01; //analog on RA0
    WPUA = 0x00; //(default)
    ODCONA = 0x00; //open drain (default)
    SLRCONA = 0x37; //slew rate control (default)
    SSP1CLKPPS = 0x01; //RA1->MSSP1:SCL1; (default)
    RA1PPS = 0x15; //RA1->MSSP1:SCL1;
    RA2PPS = 0x16; //RA2->MSSP1:SDA1;
    SSP1DATPPS = 0x02; //RA2->MSSP1:SDA1; (default)
    
    //Oscillator Setup
    OSCCON1 = 0x62; // NOSC HFINTOSC; NDIV 4;
    OSCCON3 = 0x00; // CSWHOLD may proceed; (default)
    OSCEN = 0x00; // MFOEN disabled; LFOEN disabled; ADOEN disabled; EXTOEN disabled; HFOEN disabled; (default)
    OSCFRQ = 0x05; // HFFRQ 16_MHz;
    OSCSTAT = 0x00; // MFOR not ready;
    OSCTUNE = 0x00; // HFTUN 0;

    //I2C Setup
    SSP1STAT = 0x80; // SMP Standard Speed; CKE disabled;
    SSP1CON1 = 0x28; // SSPEN enabled; CKP Idle:Low, Active:High; SSPM FOSC/4_SSPxADD_I2C;
    SSP1CON3 = 0x00; // SBCDE disabled; BOEN disabled; SCIE disabled; PCIE disabled; DHEN disabled; SDAHT 100ns; AHEN disabled;
    SSP1ADD = 0x09; // SSPADD 9;
    PIR3bits.SSP1IF = 0; // clear the interrupt flags
    PIR3bits.BCL1IF = 0; // clear the interrupt flags
    PIE3bits.SSP1IE = 1; // enable the interrupts **********May not need if not using interrupt routine**********
    PIE3bits.BCL1IE = 1; // enable the interrupts **********May not need if not using interrupt routine**********

    //ADC Setup
    ADCON0 = 0x01; // GOnDONE stop; ADON enabled; CHS ANA0;
    ADCON1 = 0x40; // ADFM left; ADPREF VDD; ADCS FOSC/4;
    ADRESL = 0x00; // ADRESL 0;
    ADRESH = 0x00; // ADRESH 0;
    
    //Interrupt on change
    //IOCAP = 0x08;
    
    //initial environmental settings
    setTemp = 70;
    setHumidity = 50;

    while(1){ //never ending loop
        I2CStart(); //
        I2CSend(address); //let sensor know we are talking to it
        I2CSend(0xF5); //start humidity reading
        __delay_ms(500); //give the sensor time to take a reading
        humidityReading.bytes[1] = I2CRead(); //get first reading
        humidityReading.bytes[0] = I2CRead(); //get second reading
        I2CSend(0xF3); //start Temperature reading
        __delay_ms(500); //give the sensor time to take a reading
        temperatureReading.bytes[1] = I2CRead(); //get first reading
        temperatureReading.bytes[0] = I2CRead(); //get second reading
        I2CStop(); //
        pHumid = ((humidityReading.humidity * 125)/65536)-6;
        fTemp = (175.72 * (temperatureReading.temperature/65536))-46.85;
        fTemp = (fTemp * 1.8) + 32; //convert to F
        if(fTemp >= setTemp){
            //turn on the fan
            LATAbits.LATA5 = 1;
        }else{
            //turn off the fan
            LATAbits.LATA5 = 0;
        }
        if(pHumid < setHumidity){
            //activete the spray nozzle
            LATAbits.LATA4 = 1;
            __delay_ms(1000);
            LATAbits.LATA4 = 0;
        }
        __delay_ms(5000); //delay between readings
        
    }
    return;
}

#1
ric
Super Member
  • Total Posts : 23555
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: PIC16F15313 programed for I2C now PICKIT3 will not work 2019/06/12 16:54:27 (permalink)
0
Mike432
... when I try to re-program with my PicKit3 I get connection errors.  I think theses errors are happening because the device is in I2C mode and I2C shares a line with the PicKit3 (pin 5 = I2C SCK and ICSPCLK for PicKIt3).  Is there a way to recover my device?
 ...

Remove the pullup resistor on SCK when programming
The PK3 documentation specifically tells you that you must not have pullups on ICSPCLK or ICSPDAT.

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#2
Mike432
Starting Member
  • Total Posts : 37
  • Reward points : 0
  • Joined: 2016/08/15 06:26:37
  • Location: 0
  • Status: offline
Re: PIC16F15313 programed for I2C now PICKIT3 will not work 2019/06/12 19:33:53 (permalink)
0
Thank you for the reply but that's not the problem.  I have been removing my sensor and the resistors between re-programming.  I have LED's connected to RA4 and RA5 but they shouldn't interfere with programming.
post edited by Mike432 - 2019/06/16 06:51:42
#3
ric
Super Member
  • Total Posts : 23555
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: PIC16F15313 programed for I2C now PICKIT3 will not work 2019/06/12 19:49:54 (permalink)
0
Mike432
I have been removing my sensor and and resistors between re-programming.

You never mentioned that. We can only work with what you reveal.
 
Edit: Agreed, the LEDs on RA4 and RA5 can't affect anything.
post edited by ric - 2019/06/12 20:32:51

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#4
NorthGuy
Super Member
  • Total Posts : 5578
  • Reward points : 0
  • Joined: 2014/02/23 14:23:23
  • Location: Northern Canada
  • Status: online
Re: PIC16F15313 programed for I2C now PICKIT3 will not work 2019/06/12 20:10:30 (permalink)
0
1. You have LVP set to OFF. Make sure your PICkit3 uses HVP (as opposed to LVP).
 
2. Post the schematics of everything connected to RA0, RA1, and RA3 while programming.
 
 
#5
Ian.M
Super Member
  • Total Posts : 13225
  • Reward points : 0
  • Joined: 2009/07/23 07:02:40
  • Location: UK
  • Status: offline
Re: PIC16F15313 programed for I2C now PICKIT3 will not work 2019/06/13 03:54:58 (permalink) ☼ Best Answerby Mike432 2019/06/16 08:33:04
+2 (2)
Try using the IPE utility, dropping Vdd to 2.3V (below the 'F' version BOR threshold you have set) and changing the CONFIG to enable external /MCLR.  Hint: you'll probably need to change the IPE settings to allow memory editing.
 
If that lets you get back in, to avoid further trouble with reprogramming during development from MPLAB, either leave /MCLR enabled, or add a one second delay after powerup before enabling the I2C peripheral (or anything else that can make the programming pins outputs). 

--
NEW USERS: Posting images, links and code - workaround for restrictions.
I also support http://picforum.ric323.com because this forum is sometimes too broken to use!
#6
Mike432
Starting Member
  • Total Posts : 37
  • Reward points : 0
  • Joined: 2016/08/15 06:26:37
  • Location: 0
  • Status: offline
Re: PIC16F15313 programed for I2C now PICKIT3 will not work 2019/06/16 08:31:11 (permalink)
0
I have nothing but the PicKit3 connected to RA0, RA1, and RA3.
I have the 2 LEDs connected to RA4 and RA5 as mentioned before and I do have resistors with them.
I have a cap of the recommended value per the datasheet between power and ground.
I was using 3.3V but I have changed to 5V as I attempt to recover my device.
 
The following is my error message when trying to program in IDE
 
The following memory area(s) will be programmed:
program memory: start address = 0x0, end address = 0x7ff
configuration memory
program memory
Address: 0 Expected Value: 2801 Received Value: 0
Failed to program device

 
I tried to use IPE (had never used it before) and was getting an error message that my PicKit3 was set for LVP.  I changed the power settings to switch the PicKit3 to HVP.  I tried setting Vpp to 2.375V (the minimum) and also 5V.  I get the error message quoted below.  I tried to erase the device and IPE tells me "Erase successful" but the program is still running on it after the erase.
 
Currently loaded firmware on PICkit 3
Firmware Suite Version.....01.55.01
Firmware type..............Enhanced Midrange
Target voltage detected
Target Device ID (0x0) is an Invalid Device ID. Please check your connections to the Target Device.

 
I was playing with IPE and IDE as I was typing this reply.  After playing with the power setting again (Vdd was grayed out,  Vpp set to 8.5V, Vdd Nom set to 2.375, and Vdd APP set to 2.375) I disconnected my PicKit3 from IPE.  After that I was able to re-program my device in IDE.
 
I now have a timeout routine set in my program so it will not get stuck in a loop waiting for I2C communication.  After the I2C times out I have a 1 minute delay before it tries again.  I hope this will help prevent this re-programming problem.
 
Thanks for the help!
post edited by Mike432 - 2019/06/16 09:29:21
#7
Jump to:
© 2019 APG vNext Commercial Version 4.5