• AVR Freaks

Helpful ReplyHot!16f1829 I2C stops working

Author
jjpicwest
New Member
  • Total Posts : 13
  • Reward points : 0
  • Joined: 2015/04/28 07:17:35
  • Location: 0
  • Status: offline
2020/03/25 13:27:43 (permalink)
0

16f1829 I2C stops working

Hi All,
 
I have been programming a 16f1829 with the 16mhz internal oscillator selected. 
 
Today I experience the following problem with the 16mhz osc selected:
 
program using pickit 3
program runs
turn power off
turn power on
program doesn't run
 
re-program and the above happens again.
 
If i change the osc to 2mhz it runs fine after a power cycle.
 
I don't understand how it can be working fine for several days and numerous power cycles at 16mhz but today it just stops working after a power cycle and why just 16mhz osc after a power cycle?
 
Any help would be appreciated.
post edited by jjpicwest - 2020/03/28 07:45:15
#1
jjpicwest
New Member
  • Total Posts : 13
  • Reward points : 0
  • Joined: 2015/04/28 07:17:35
  • Location: 0
  • Status: offline
Re: 16f1829 internal oscillator stops working 2020/03/25 13:35:12 (permalink)
0
this actually seems to be an i2c initialisation issue, the pic chip is actually running with 16mhz but not running the i2c bus.
#2
ric
Super Member
  • Total Posts : 27655
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: 16f1829 internal oscillator stops working 2020/03/25 14:44:31 (permalink) ☄ Helpfulby jjpicwest 2020/03/25 15:39:07
+4 (4)
More likely the problem is with whatever else is on the I2C bus.
When you power down the PIC, does the other device also get reset?
You may need to do a full bus clear before enabling the I2C.
That would involve floating the SDA pin, then toggling the SCL pin nine times at 100 kHz (or slower).
 

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!
#3
jjpicwest
New Member
  • Total Posts : 13
  • Reward points : 0
  • Joined: 2015/04/28 07:17:35
  • Location: 0
  • Status: offline
Re: 16f1829 internal oscillator stops working 2020/03/25 15:42:38 (permalink)
+3 (3)
you were absolutely spot on sir, it just shows you how you can get stuck searching for what you think the problem is rather than looking properly at all the options.
#4
jjpicwest
New Member
  • Total Posts : 13
  • Reward points : 0
  • Joined: 2015/04/28 07:17:35
  • Location: 0
  • Status: offline
Re: 16f1829 internal oscillator stops working 2020/03/26 07:53:39 (permalink)
0
ok so this has got even more weird.
 
Scoping the I2C bus I found that the PIC doesn't give the power rails enough time to stabilise across the whole board before initialising and trying to send the first packet, causing the bus to hang. I added a delay to allow the rails to stabilise and solved that.
 
however, what is interesting is that when running pic at 16mhz and i2c at 15kHz the bus hangs when waiting for a reply from a slave device. This does not happen if you run the pic at 2mhz and the i2c bus at 15khz. if i increase the i2c bus to 50khz when the pic is at 16mhz then all works well.
 
I am at a loss to think why the i2c bus speed is causing issues when the pic is a 16mhz and the bus is at 15khz.
 
 
#5
ric
Super Member
  • Total Posts : 27655
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: 16f1829 internal oscillator stops working 2020/03/26 12:24:47 (permalink)
+3 (3)
The continued use of "mhz" = millihertz is doing my head in ...
mhz != MHz

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!
#6
jjpicwest
New Member
  • Total Posts : 13
  • Reward points : 0
  • Joined: 2015/04/28 07:17:35
  • Location: 0
  • Status: offline
Re: 16f1829 internal oscillator stops working 2020/03/26 15:01:01 (permalink)
0
I believe mHz is millihertz in SI, not mhz. 
post edited by jjpicwest - 2020/03/26 15:02:35
#7
mlp
boots too small
  • Total Posts : 934
  • Reward points : 0
  • Joined: 2012/09/10 15:12:07
  • Location: previously Microchip XC8 team
  • Status: offline
Re: 16f1829 internal oscillator stops working 2020/03/27 14:13:56 (permalink)
+2 (2)
jjpicwest
I believe mHz is millihertz in SI, not mhz. 

And "mhz" is nothing at all, so don't use it, which was ric's point: Case Distinctions Matter.

Mark (this opinion available for hire)
#8
jjpicwest
New Member
  • Total Posts : 13
  • Reward points : 0
  • Joined: 2015/04/28 07:17:35
  • Location: 0
  • Status: offline
Re: 16f1829 internal oscillator stops working 2020/03/27 14:28:53 (permalink)
-1 (1)
 
mark.pappin
jjpicwest
I believe mHz is millihertz in SI, not mhz. 

And "mhz" is nothing at all, so don't use it, which was ric's point: Case Distinctions Matter.




In this case the context was clear, there are no settings for the PIC internal oscillator in relation to setting it to millihertz.....only megahertz. To correct someone with incorrect terminology is no better than the person's original mistake. Case Distinctions matters when context is not obvious.
 
If we can, can we discuss the issue I have noticed with the I2C bus or was I expecting too much from the official Microchip forums?
#9
ric
Super Member
  • Total Posts : 27655
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: 16f1829 internal oscillator stops working 2020/03/27 21:09:09 (permalink)
+1 (1)
The "Official Microchip forums" are staffed by volunteer peer users, so to get help you have to put up with their foibles! ;)
 
What do you mean the bus hangs? Is your code checking for ACK after sending the address byte?
 

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!
#10
jjpicwest
New Member
  • Total Posts : 13
  • Reward points : 0
  • Joined: 2015/04/28 07:17:35
  • Location: 0
  • Status: offline
Re: 16f1829 internal oscillator stops working 2020/03/28 07:33:29 (permalink)
0
Here is the code, I am using MCC to generate all the needed handshaking, so the code checks an input on a port expander, if it is high then it will flash an LED also on the port expander.
 
#include "mcc_generated_files/mcc.h"

#define PORT_EXPANDER 0x40 //Control Byte - (address:0100)+(A0-3:000)+(R/Wbit - 0)

#define SLAVE_I2C_GENERIC_RETRY_MAX 100
I2C1_MESSAGE_STATUS status = I2C1_MESSAGE_PENDING;

union {
  unsigned char all;
  
  struct {
    unsigned b0:1; //M3_HOME INPUT 
    unsigned b1:1; //M2_HOME INPUT 
    unsigned b2:1; //M1_HOME INPUT 
    unsigned b3:1; 
    unsigned b4:1;
    unsigned b5:1; //TRIGGER INPUT
    unsigned b6:1; //PWR_LED OUTPUT
    unsigned b7:1; //xBEE_SLEEP OUTPUT
  };
} Expander_outputs

char port_expander_check_input_pcf8574(void)
{
    uint8_t writeBuffer[1];
   uint8_t Ddata;
   
   writeBuffer[0] = 0x00;
    timeOut = 0;

            while(status != I2C1_MESSAGE_FAIL)
            {
                I2C1_MasterRead(&Ddata, 1,PORT_EXPANDER/2,&status);//grab data

                // wait for the message to be sent or status has changed.
                while(status == I2C1_MESSAGE_PENDING);

                if (status == I2C1_MESSAGE_COMPLETE)
                {
                    break;
                }
                
                if (timeOut == SLAVE_I2C_GENERIC_RETRY_MAX)
                {
                    break;
                }else{
                    timeOut++;
                }
            }
    
    return Ddata;
}

void port_expander_output_pcf8574(void)
{
    // send control word with address
    // data represents p0-7 ouputs
    uint8_t writeBuffer[1];
    timeOut = 0;
    writeBuffer[0] = Expander_outputs.all;
   
   while(status != I2C1_MESSAGE_FAIL)
   {
        I2C1_MasterWrite(writeBuffer, 1, PORT_EXPANDER/2, &status);
        
        //wait for device to respond
        while (status == I2C1_MESSAGE_PENDING);
        
        //if message finished successfully
        if (status == I2C1_MESSAGE_COMPLETE) 
        {
            break;
        }
        
        //no reply and timeout
        if (timeOut == SLAVE_I2C_GENERIC_RETRY_MAX)
        {
            break;
        }else{
            timeOut++;
        }
    }
}

void main(void) {
    // initialize the device
    SYSTEM_Initialize();
    
    //wait for power rails to stabilise
    __delay_ms(100);
    
    I2C1_Initialize();

    // Enable the Global Interrupts
    INTERRUPT_GlobalInterruptEnable();

    // Enable the Peripheral Interrupts
    INTERRUPT_PeripheralInterruptEnable();
    
    char test;
    test = port_expander_check_input_pcf8574();
    
    while(1){
        if (test & 0x01 == 0x01)
 
 
 
 
 
 
 
        {
            Expander_outputs.b6 = 0;
            port_expander_output_pcf8574();
            __delay_ms(500);
            Expander_outputs.b6 = 1;
            port_expander_output_pcf8574(); 
            __delay_ms(500); 
        }
    }
}

 
If I run the PIC INTOSC at 16MHz_HF and the I2C bus at 16.129kHz it just seems to send one read message and there is no reply and nothing else happens on the I2C bus (clearly because it fails to collect the data the program needs):
 
EDIT: please see first two images
 
If I change the bus speed to 50.632kHz, the read message gets sent and a reply shows. The bus also gets updates to flash the LED continuously:
 
EDIT: please see second two images
 
What I can't figure out is why changing the I2C bus speed seems to make any difference. The port expander (pcf8574) has a maximum I2C bus speed of 100kHz but does not list a lower speed limit. I have also found that if I lower the INTOSC to 2MHz_HF the bus will work fine at 16.129kHz. 
 
 
EDIT: sorry but the forum says I have access denied when I try to insert the images inline.
post edited by jjpicwest - 2020/03/28 07:47:00

Attached Image(s)

#11
davea
Super Member
  • Total Posts : 243
  • Reward points : 0
  • Joined: 2016/01/28 13:12:13
  • Location: Tampa Bay FL USA
  • Status: offline
Re: 16f1829 internal oscillator stops working 2020/03/28 08:40:00 (permalink)
+1 (1)
if (test & 0x01 == 0x01) check disassemble of this statment 
if ((test & 0x01) == 0x01)
#12
mlp
boots too small
  • Total Posts : 934
  • Reward points : 0
  • Joined: 2012/09/10 15:12:07
  • Location: previously Microchip XC8 team
  • Status: offline
Re: 16f1829 internal oscillator stops working 2020/03/28 16:28:19 (permalink)
+5 (5)
jjpicwest
Case Distinctions matters when context is not obvious.

If you are careless about terminology in your text intended for humans, you might also be careless in your source code, and context is never obvious to a compiler. So, I'll poke carelessness with a stick whenever I see it.
 
Don't be careless.
 
If we can, can we discuss the issue I have noticed with the I2C bus or was I expecting too much from the official Microchip forums?

As noted by ric, you were expecting too much Official-ness.
There is no Official-ness about these forums.
No Microchip staff are paid to follow the forums - any that you might see responding here (Jeff, George, Jason, ...) are doing this in their down-time.
Everybody else is an outsider, volunteering their time, and if they choose to go off on a tangent, well, you can immediately receive double your money back.
 
When I was employed by Microchip I was not permitted to fully express my opinions here and also keep my job. Now that my day job is elsewhere I can say what I like here.
 

Mark (this opinion available for hire)
#13
jjpicwest
New Member
  • Total Posts : 13
  • Reward points : 0
  • Joined: 2015/04/28 07:17:35
  • Location: 0
  • Status: offline
Re: 16f1829 internal oscillator stops working 2020/04/01 11:10:18 (permalink)
0
I have tried this and it makes no difference, seeing as the I2C bus never sees a reply then this logic will never get processed properly.
#14
Jump to:
© 2020 APG vNext Commercial Version 4.5