Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[PIC] PIC16f1716 - I2C Initialization problem

Status
Not open for further replies.

nikhilsigma

Full Member level 2
Joined
Jun 19, 2010
Messages
142
Helped
17
Reputation
34
Reaction score
16
Trophy points
1,298
Location
Delhi, India
Activity points
2,584
Hello Everyone,

I am trying to implement I2C on PIC16f1716. I have written basic code for I2C initialization which is shown below.

PROBLEM: Even after setting START bit "SEN" nothing happens on SDA & SCL lines (SDA show go low ideally). Datasheet says that "SEN" will get cleared by hardware after generating start condition. But it never happens and SEN always remains high. This is checked by LED assigned in main loop "LATC0=SSP1CON2bits.SEN;" and also by debugging with PICKIT3.

Please help, I am stuck on this from last 2 days. :sad:

Important points:
1: 4.7Kohm pull up resistors aree connected to SCL n SDA lines.
2. I am able to change output of SCL and SDA line by setting these pins as output low. (this is just to check that pins and external hardware is working.)
3. Switch on B5 is also working fine. I checked it by controlling LED with it.
4. I am monitoring SCL n SDA line with an oscilloscope.

Code:
#pragma config FOSC = INTOSC    // Oscillator Selection Bits (INTOSC oscillator: I/O function on CLKIN pin)#pragma config WDTE = OFF       // Watchdog Timer Enable (WDT disabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable (PWRT disabled)
#pragma config MCLRE = ON       // MCLR Pin Function Select (MCLR/VPP pin function is MCLR)
#pragma config CP = OFF         // Flash Program Memory Code Protection (Program memory code protection is disabled)
#pragma config BOREN = ON       // Brown-out Reset Enable (Brown-out Reset enabled)
#pragma config CLKOUTEN = OFF   // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)
#pragma config IESO = ON        // Internal/External Switchover Mode (Internal/External Switchover Mode is enabled)
#pragma config FCMEN = ON       // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is enabled)


// CONFIG2
#pragma config WRT = OFF        // Flash Memory Self-Write Protection (Write protection off)
#pragma config PPS1WAY = ON     // Peripheral Pin Select one-way control (The PPSLOCK bit cannot be cleared once it is set by software)
#pragma config ZCDDIS = ON      // Zero-cross detect disable (Zero-cross detect circuit is disabled at POR and can be enabled with ZCDSEN bit.)
#pragma config PLLEN = ON       // Phase Lock Loop enable (4x PLL is always enabled)
#pragma config STVREN = ON      // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset)
#pragma config BORV = LO        // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)
#pragma config LPBOR = OFF      // Low-Power Brown Out Reset (Low-Power BOR is disabled)
#pragma config LVP = OFF        // Low-Voltage Programming Enable (High-voltage on MCLR/VPP must be used for programming)


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


#include <stdio.h>
#include <stdlib.h>
#include <xc.h>
#include "I2C_header.h"
//#include <xc8.h>
#define _XTAL_FREQ 16000000


void led_blink(void);


void main(void) {
    
    OSCCON=0x78;    // Setting oscillator frequency to 16MHz
    TRISC=0b11111100; // 0-LED 1-Buzzer 2-Temp_ADC_in 3,4-I2C 
    TRISB=0b11100000; // 0-4:Seven_segment_cathode 5:PullUp_switch 
    ANSELA=0;   // Turning off the GPIO sharing for ADC
    ANSELB=0;   // Turning off the GPIO sharing for ADC
    ANSELC=0;   // Turning off the GPIO sharing for ADC
    RC0 = 0; //Makes 0th bit of PORTB at Logic Low
    RC1=0;


    
    I2C_Master_Initialize();
    
      while(1)
      {
          if(PORTBbits.RB5==0)  // Switch is connected to B5
          {
            //led_blink();             
            SSP1CON2bits.SEN = 1;  // should generate I2C Start condition
          }
          LATC0=SSP1CON2bits.SEN;  // LED is connected to C0
      }
   return;
}

void I2C_Master_Initialize( void)
{
    TRISCbits.TRISC3=1;
    TRISCbits.TRISC4=1;
    ANSELCbits.ANSC3=0;
    ANSELCbits.ANSC4=0;
    
    //Interrupt Enable
    INTCONbits.PEIE=1;
    PIR1bits.SSP1IF=0;
    PIE1bits.SSP1IE=1;
    
    SSP1CON1bits.SSPEN=1;
    SSP1CON1bits.SSPM=0b1000;
    SSP1STATbits.CKE=0;
    SSP1STATbits.SMP=1;
    SSP1CON2=0;
    SSP1ADD=0x27;
}
 

Hi nikhilsigma,

You should probably turn off the watchdog timer (WDTE) in your config section. It's enabled by default, so unless you need it and know how to use it it's best to turn it off.

Do you have a hardware debounce circuit on RB5? Switches need to be debounced. It can be done with hardware, as a simple RC network. But the common practice is to do it in software by waiting until the switch contacts settle (a few dozen milliseconds, I think). Without a debounce mechanism the code for the switch will execute over and over many times, which is usually undesirable.

It's also common to set unused pins to outputs and drive them low. Some strange things can happen to pins that are allowed to float.
 

The initialization seems correct at first sight, except for enabling interrupts without declaring an interrupt handler. Did you trace code execution in debugger and checked that it's not caught in default interrupt handler?

Using a switch to generate the start condition will typically set SEN continuously for some time which doesn't look right.

I didn't ever use a modern PIC16 device like 16F1716, the PPS programming with default pin assignments looks unusual when you are familiar with PIC24, but according to datasheet it should work without explicit PPS initialization.

I wonder if there are Microchip programming examples for I2C usage with these processors. Presume you are using XC8?
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top