#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;
}