Hot!I2C PROBLEMS WITH PIC16F15356

Author
Piero65
Starting Member
  • Total Posts : 54
  • Reward points : 0
  • Joined: 2017/09/08 00:28:32
  • Location: 0
  • Status: offline
2018/04/16 05:09:36 (permalink)
0

I2C PROBLEMS WITH PIC16F15356

Hello everyone,
 
a strange problem,
with MMC configure the RC3 ports clk and RC4 given to manage a pressure sensor, everything works properly.
On the same I2C bus is also connected a speech synthesis chip that has an I2C but non-standard communication for example does not manage the ack.
 
If I use RC3 and RC4 to manage the speech synthesis chip in a simulated way, the chip receives the commands and executes them, when I configure RC3 and RC4 in standard I2C mode the routine that simulates I2C for the voice chip stops, no more clocks or data will appear on the pins RC3 and RC4, instead the pressure sensor works.
 
The hardware power supply is 3.3 volts so I am obliged for I2C to use RB1 / RB2 or RC3 / RC4.
 
I thought about using RB1 and RB2 to simulate the I2C communication of the speech synthesis chip and leave RC3 and RC4 to manage the sensor, but this involves hardware modifications.
 
Some advice?
The hardware is done and intervening would be problematic.
The sample code of the speech synthesis chip manufacturer is the attached one, clearly adapted for PIC16F15356 
 
#include "config_io.h"

//=================================================================//
//pic18f65j50 config set
//
#pragma config XINST = OFF // Extended instruction set
#pragma config STVREN = ON // Stack overflow reset
#pragma config PLLDIV = 3 // (12 MHz crystal used on this board)
#pragma config WDTEN = OFF // Watch Dog Timer (WDT)
#pragma config CP0 = OFF // Code protect
#pragma config CPUDIV = OSC1 // OSC1 = divide by 1 mode
#pragma config IESO = OFF // Internal External (clock) Switchover
#pragma config FCMEN = OFF // Fail Safe Clock Monitor
#pragma config FOSC = HSPLL // Firmware must also set OSCTUNE<PLLEN> to start PLL!
#pragma config WDTPS = 32768
#pragma config MSSPMSK = MSK5
#pragma config CCP2MX = DEFAULT

//=================================================================//
//main
//

void main()
{
 
 unsigned char CheckBtn = 0;

 InitPortB();

 while(1)
 {
  PushBtn(&CheckBtn);

  if(CheckBtn == 1)//button is pushed
  {
   SoundChip_Active(0xa800);

   SoundChip_Play(0);
   while(BUSY);

   SoundChip_Play(1);
   while(BUSY);

   SoundChip_Play(2);
   while(BUSY);

   SoundChip_Play(3);
   while(BUSY);
   
   SoundChip_DeActive(0xb800);
   
   Nop();
   Nop();
   Nop();
   Nop();
   Nop();

   CheckBtn = 0;


  }//if(Btn)
     
 }//while(1)

}

 
#ifndef _CONFIG_IO_H
#define _CONFIG_IO_H

//=================================================================//
//include file
//
#include"p18f65j50.h"

//=================================================================//
//define I/O
//
#define SCLK LATBbits.LATB1 //47 in
#define SI LATBbits.LATB2 //46 in
#define BUSY PORTBbits.RB3 //45 out
 
#define BTN PORTBbits.RB4 //44 out //work as btn use

#define H 1
#define L 0

//=================================================================//
//method declare
//
void Delay20us(void);
void Delay2ms(void);
void PushBtn(unsigned char *btn);
void I2Cdata(unsigned int cmd);
void SoundChip_Active(unsigned int cmd);
void SoundChip_DeActive(unsigned int cmd);
void SoundChip_Play(unsigned int addr);
                 
//=================================================================//
//initialize PORTB
//
void InitPortB(void)
{
 PORTB = 0x00;
 TRISB = 0x18;
 LATB = 0x00;
}
//=================================================================//
//delay 20uS
//
void Delay20us(void)
{
 unsigned char count = 7;
    while(count--);
}
//=================================================================//
//delay 2mS
//
void Delay2ms(void)
{
 int count = 470;
    while(count--);
}
//=================================================================//
//push button
//
void PushBtn(unsigned char *btn)
{
 while(BTN == H);
   *btn = 1;
 while(BTN == L);
 Nop();
}

//=================================================================//
//send I2C data 16 bits
//
void I2Cdata(unsigned int cmd)
{
 unsigned int mask = 0;
   
 for(mask = 0x8000;mask > 0;mask >>=1)
 {
  SCLK = L;
  SI = (cmd & mask) ? H:L;
  SCLK = H;
 }

 SCLK = L;
}

//=================================================================//
//power up chip
//
void SoundChip_Active(unsigned int cmd)
{
 SCLK = H; //start condition
 SI = H; //
 SI = L; //
 //--------------//

 Delay20us(); //wait sleep to wake up state
 I2Cdata(cmd); //power up chip with ramp-up or without ramp-up
 Delay2ms(); //wait state exist wake up state

 //--------------//stop condition
 SI = L; //
 SCLK = H; //
 SI = H; //
 
 while(BUSY); //wait end of ramp
}

//=================================================================//
//power down chip and into sleep state
//
void SoundChip_DeActive(unsigned int cmd)
{
 while(BUSY); //wait the end of last voice group

 SCLK = H; //start condition
 SI = H; //
 SI = L; //
 //--------------//

 I2Cdata(cmd); //power down chip with ramp-down or without ramp-down

 //--------------//stop condition
 SI = L; //
 SCLK = H; //
 SI = H; //

 while(BUSY); //wait end of ramp
}

//=================================================================//
//Play to assign Voice Group
//
void SoundChip_Play(unsigned int addr)
{
 unsigned int cmd = 0;
 
 cmd = 0x9800 + addr;

 SCLK = H; //start condition
 SI = H; //
 SI = L; //
 //--------------//

 I2Cdata(cmd);
 
 //--------------//stop condition
 SI = L; //
 SCLK = H; //
 SI = H; //

 Delay20us(); //for max output delay of BUSY/FULL signal
}

#endif

 
 
Thanks
post edited by infopsp - 2018/04/16 05:47:23
#1

4 Replies Related Threads

    Mysil
    Super Member
    • Total Posts : 2881
    • Reward points : 0
    • Joined: 2012/07/01 04:19:50
    • Location: Norway
    • Status: offline
    Re: I2C PROBLEMS WITH PIC16F15356 2018/04/16 06:49:15 (permalink)
    0
    Hi,
    There have been another thread about interfacing a sound synthesis chip,
    presenting wery much the same kind of communication.
     
    My observation is that having to use RB1 / RB2 or RC3 / RC4 for communication with the sound chip,
    is a misunderstanding, and is wrong.
    I think you may use any two I/O pins for connecting to the sound chip,
    and should keep that away from other I2C devices.
     
    Then, in the second snippet, this is wrong for PIC16F15356:
    #include"p18f65j50.h"
    Replace with: 
    #include <xc.h>  

    all places where it occur.
     
       Mysil
    #2
    Gort2015
    Klaatu Barada Nikto
    • Total Posts : 2645
    • Reward points : 0
    • Joined: 2015/04/30 10:49:57
    • Location: 0
    • Status: offline
    Re: I2C PROBLEMS WITH PIC16F15356 2018/04/16 07:34:21 (permalink)
    0
    What speech synthesis chip are you using?

    MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
    https://www.youtube.com/watch?v=Iu1qa8N2ID0
    + ST:Continues, "What Ships are Made for", Q's back.
    #3
    PStechPaul
    Super Member
    • Total Posts : 2023
    • Reward points : 0
    • Joined: 2006/06/27 16:11:32
    • Location: Cockeysville, MD, USA
    • Status: online
    Re: I2C PROBLEMS WITH PIC16F15356 2018/04/16 13:08:14 (permalink)
    +2 (2)
    Some things in the code do not seem right:
    //=================================================================//
    //delay 20uS
    //
    void Delay20us(void)
    {
     unsigned char count = 7;
        while(count--);
    }
    //=================================================================//
    //delay 2mS
    //
    void Delay2ms(void)
    {
     int count = 470;
        while(count--);
    }
    //=================================================================//
    //push button
    //
    void PushBtn(unsigned char *btn)
    {
     while(BTN == H);
       *btn = 1;
     while(BTN == L);
     Nop();
    }

    The delay functions would require instruction times of about 4 uSec but the board uses a 12 MHz crystal with PLLDIV=3 and CPUDIV=OSC1. It would be better to define the _XTAL_FREQ and use the __delay_us functions.
     
    It appears you are using bit-banging for I2C, so the instruction clock rate is important.
     
    The void PushBtn() function looks like it will block until the button is pressed and then released. That seems to be as desired, but the implementation seems a bit odd, using a global variable with a void function. That might just be personal preference.

     
    #4
    Mysil
    Super Member
    • Total Posts : 2881
    • Reward points : 0
    • Joined: 2012/07/01 04:19:50
    • Location: Norway
    • Status: offline
    Re: I2C PROBLEMS WITH PIC16F15356 2018/04/16 14:14:07 (permalink)
    0
    Hi,
    Seem like the code in message #1 might be an attempt to translate assembler code from this thread,
    http://www.microchip.com/forums/FindPost/1047706
    into C.
     
    Code in message #1 do not have any delay or other control of timing for each bit in communication with the Audio chip, it just toggle bits in instruction frequency, so instruction frequency must not be too high.
    This should make device run with 4 MHz oscillator frequency and CDIV 1:4, giving 1 MHz system clock, and 250 kHz instruction frequency:
      #pragma config RSTOSC = HFINT1   

     
    Code in message #1 have a number of #pragma config ... 
    settings that will never work for PIC16F15356,  they are for a PIC18 device with USB.
    Remove all those #pragma lines that cause error messages.
     
       Mysil
    post edited by Mysil - 2018/04/16 14:15:14
    #5
    Jump to:
    © 2018 APG vNext Commercial Version 4.5