0
\$\begingroup\$

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
        }
    }
}
\$\endgroup\$
7
  • 2
    \$\begingroup\$ If you're asking to troubleshoot your existing code, that is much more on-topic than asking to be supplied with example code. \$\endgroup\$
    – JYelton
    Commented Aug 10, 2023 at 15:42
  • \$\begingroup\$ Any help would be appreciated. I understand that I have to also work on it and figure it out. Thanks for the response. \$\endgroup\$
    – Bottle
    Commented Aug 10, 2023 at 16:05
  • \$\begingroup\$ Please edit your question with the following: 1) Remove the request to be supplied with example code. 2) Explain what is not working in your current implementation (what it's doing versus what is expected). \$\endgroup\$
    – JYelton
    Commented Aug 10, 2023 at 16:07
  • 1
    \$\begingroup\$ What is your issue? \$\endgroup\$
    – Julien
    Commented Aug 10, 2023 at 16:17
  • 1
    \$\begingroup\$ @Bottle I've removed my vote for closure with your edits. Also note that on a Q&A such as this, show thanks by voting on and accepting answers rather than adding it to the question where it is just noise. \$\endgroup\$
    – JYelton
    Commented Aug 10, 2023 at 16:54

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.