My code:
- Code: Select all
#include <xc.h>
#define _XTAL_FREQ 16000000
#define XC_HEADER_TEMPLATE_H
// CONFIG1
#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 = OFF // MCLR Pin Function Select (MCLR/VPP pin function is digital input)
#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 = ON // CLKOUT enabled *16MHz clk?
#pragma config IESO = OFF // Internal/External Switchover Mode (Internal/External Switchover Mode is disabled)
#pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is disabled)
// CONFIG2
#pragma config WRT = OFF // Flash Memory Self-Write Protection (Write protection off)
#pragma config STVREN = OFF // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will not 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)
int main()
{
SSP1CON1 = 0x28;
SSP1CON2 = 0x00;
SSP1CON3 = 0x00;
SSP1ADD = 255;
TRISBbits.TRISB4 = 1;
TRISBbits.TRISB6 = 1; //SDA ans SCL
TRISC = 0;
TRISAbits.TRISA2 = 0;
ANSELB = 0;
OSCCON = 0b01111010;
LATAbits.LATA2 = 0; //LED connected to A2
SSP1CON2bits.SEN = 1;
while(SSP1CON2bits.SEN);
SSP1BUF = (0x0D << 1) + 1;
while(SSP1CON2bits.ACKSTAT);
LATAbits.LATA2 = 1;
while(1);
return 1;
}
LED turns on but SDA and SCL signals are exactly same.
1 is SDA, 2 for SCL and red is 1-2.
SDA and SCL are both connected to VDD with pull up register 10k since slave requires 10k in datasheet.