This is asking for a lot and I've posted on a previous forum my problem. Help would be greatly appreciated! Please refer to my current attempt below: The problem is that I don't detect the PIC controller over I2C at all...
#include <xc.h>
#include <stdio.h>
#include <stdlib.h>
#include "pic16f18857.h"
#define _XTAL_FREQ 32000000 // Define the frequency for __delay_ms()
void main(void) {
// Initialize I2C
TRISCbits.TRISC3 = 1; // SDA pin as input
TRISCbits.TRISC4 = 1; // SCL pin as input
SSP1ADD = 0x50 << 1; // Set slave address (0x50)
SSP1STAT = 0x00; // Slew rate control disabled
SSP1CON1 = 0x36; // Enable I2C, Enable 7-bit slave address, Enable SCL and SDA pins
SSP1IE = 1; // Enable I2C interrupt
PEIE = 1; // Enable peripheral interrupts
GIE = 1; // Enable global interrupts
// Initialize serial communication
TX1STAbits.TXEN = 1; // Enable transmitter
RC1STAbits.SPEN = 1; // Enable serial port
TX1STAbits.SYNC = 0; // Asynchronous mode
TX1STAbits.BRGH = 1; // High-speed baud rate
BAUD1CONbits.BRG16 = 0; // 8-bit baud rate generator
SP1BRGL = 207; // Set baud rate to 9600
while (1) {
// Your main loop code here
}
}
void __interrupt() ISR(void) {
if (SSP1IF) {
if (!SSP1STATbits.R_nW) { // Check if this is a write operation
unsigned char receivedData;
if (!SSP1STATbits.D_nA) { // Check if this is an address byte
SSP1CON1bits.CKP = 0; // Release clock
receivedData = SSP1BUF; // Read and clear buffer
} else {
receivedData = SSP1BUF; // Read and clear buffer
// Process the received data
printf("Received message: %c\r\n", receivedData);
}
SSP1IF = 0; // Clear interrupt flag
SSP1CON1bits.CKP = 1; // Release clock
}
}
}