// FBS
#pragma config BWRP = WRPROTECT_OFF // (Boot Segment may be written)
#pragma config BSS = NO_FLASH // Boot Segment Program Flash Code Protection (No Boot program Flash segment)
#pragma config RBS = NO_RAM // Boot Segment RAM Protection (No Boot RAM)
// FSS
#pragma config SWRP = WRPROTECT_OFF // Secure Segment Program Write Protect (Secure segment may be written)
#pragma config SSS = NO_FLASH // Secure Segment Program Flash Code Protection (No Secure Segment)
#pragma config RSS = NO_RAM // Secure Segment Data RAM Protection (No Secure RAM)
// FGS
#pragma config GWRP = OFF // General Code Segment Write Protect (User program memory is not write-protected)
#pragma config GSS = OFF // General Segment Code Protection (User program memory is not code-protected)
// FOSCSEL
#pragma config FNOSC = PRI // Oscillator Mode (Primary Oscillator (XT, HS, EC))
#pragma config IESO = OFF // Internal External Switch Over Mode (Start-up device with user-selected oscillator source)
// FOSC
#pragma config POSCMD = HS // Primary Oscillator Source (HS Oscillator Mode)
#pragma config OSCIOFNC = OFF // OSC2 Pin Function (OSC2 pin has clock out function)
#pragma config IOL1WAY = ON // Peripheral Pin Select Configuration (Allow Only One Re-configuration)
#pragma config FCKSM = CSECME // Clock Switching and Monitor (Both Clock Switching and Fail-Safe Clock Monitor are enabled)
// FWDT
#pragma config WDTPOST = PS32768 // Watchdog Timer Postscaler (1:32,768)
#pragma config WDTPRE = PR128 // WDT Prescaler (1:128)
#pragma config WINDIS = OFF // Watchdog Timer Window (Watchdog Timer in Non-Window mode)
#pragma config FWDTEN = OFF // Watchdog Timer Enable (Watchdog timer enabled/disabled by user software)
// FPOR
#pragma config FPWRT = PWR128 // POR Timer Value (128ms)
#pragma config ALTI2C = ON // Alternate I2C pins (I2C mapped to ASDA1/ASCL1 pins)
#pragma config LPOL = OFF // Motor Control PWM Low Side Polarity bit (PWM module low side output pins have active-low output polarity)
#pragma config HPOL = OFF // Motor Control PWM High Side Polarity bit (PWM module high side output pins have active-low output polarity)
#pragma config PWMPIN = OFF // Motor Control PWM Module Pin Mode bit (PWM module pins controlled by PWM module at device Reset)
// FICD
#pragma config ICS = PGD2 // Comm Channel Select (Communicate on PGC1/EMUC1 and PGD1/EMUD1)
#pragma config JTAGEN = OFF // JTAG Port Enable (JTAG is Disabled)
int main() {
//This function will initialize the I2C(1) peripheral.
//First set the I2C(1) BRG Baud Rate.
//Consult the dSPIC Data Sheet for information on how to calculate the
//Baud Rate.
I2C1BRG = 0x0007;//set to be 400khz
//Now we will initialise the I2C peripheral for Master Mode, No Slew Rate
//Control, and leave the peripheral switched off.
I2C1CON = 0x1200;
I2C1RCV = 0x0000;
I2C1TRN = 0x0000;
//Now we can enable the peripheral
I2C1CON = 0x9200;
I2C1CONbits.SEN = 1; //Generate Start COndition
while (I2C1CONbits.SEN); //Wait for Start COndition
while(1){
while (I2C1STATbits.TRSTAT); //Wait for bus to be idle
I2C1TRN = 0x7f; //Load byte to I2C1 Transmit buffer
while (I2C1STATbits.TBF); //wait for data transmission
}
}