CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Connect PIC16F876A to AD7745

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Diogogp24



Joined: 25 Jul 2023
Posts: 3

View user's profile Send private message

Connect PIC16F876A to AD7745
PostPosted: Tue Jul 25, 2023 4:04 pm     Reply with quote

I guys i am inserted in a university project to measure capacitance values using a PIC16F876A and AD7745.
So i never programmed a PIC i really need some help to do the configurations between this 2 components. After that i have 5 variables that are connected to a multiplexer in order to reach different capacitance sensors.
If someone could provideme some help mostly in the configuration would be perfect.
To be serius i have asked help to chapgpt but i dont trust him, but this is the code that he gave me:

#include <xc.h>
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <pic16lf1847.h>

#define _XTAL_FREQ 8000000 // Define your oscillator frequency here

// I2C Configuration
#define I2C_BAUD_RATE 100000 // Desired I2C baud rate (100 kHz)

// AD7745 I2C Address
#define AD7745_ADDRESS 0x48 // The 7-bit I2C address of the AD7745

// Define your pin variables
#define A3_PIN 2
#define A2_PIN 1
#define A1_PIN 18
#define A0_PIN 17
#define EN_PIN 3

// Function to initialize I2C
void I2C_Init() {
SSP1CON1 = 0b00101000; // Configure I2C for Master mode, clock = FOSC / (4 * (SSP1ADD+1))
SSP1CON2 = 0;
SSP1ADD = (_XTAL_FREQ / (4 * I2C_BAUD_RATE)) - 1;
SSP1STAT = 0;
}

// Function to send a start condition on I2C bus
void I2C_Start() {
SSP1CON2bits.SEN = 1;
while (SSP1CON2bits.SEN);
}

// Function to send a stop condition on I2C bus
void I2C_Stop() {
SSP1CON2bits.PEN = 1;
while (SSP1CON2bits.PEN);
}

// Function to transmit a byte on I2C bus
void I2C_Write(uint8_t data) {
SSP1BUF = data;
while (SSP1STATbits.BF);
__delay_us(5); // Add a small delay for safety
}

// Function to receive a byte on I2C bus
uint8_t I2C_Read(bool ack) {
SSP1CON2bits.RCEN = 1;
while (!SSP1STATbits.BF);
uint8_t data = SSP1BUF;
SSP1CON2bits.ACKDT = (ack) ? 0 : 1; // Set the ACK/NACK bit
SSP1CON2bits.ACKEN = 1; // Send ACK/NACK bit
while (SSP1CON2bits.ACKEN);
return data;
}

// Function to configure the AD7745 for capacitance measurement
void AD7745_Configuration() {
// Add your AD7745 configuration code here
// For example, you can set the AD7745 registers for desired settings
}

// Function to measure capacitance with the AD7745
uint32_t AD7745_MeasureCapacitance() {
// Add your code to read capacitance data from the AD7745
// For example, you can read the capacitance registers and convert the data
// into a meaningful format (e.g., microfarads or picofarads)
}

// Function to set the inputs for the AD7745 using the given pin values
void setInputs(int a3, int a2, int a1, int a0, int en) {
// Add code to set the input pins (A3_PIN, A2_PIN, A1_PIN, A0_PIN, EN_PIN) here
// For example, you can use appropriate port manipulation instructions
// to set the pins to the desired values
}

// Function to measure capacitance for a specific electrode
void measureCapacitance(int electrode) {
// Set the output pins based on the selected case number
switch (electrode) {
case 1:
setInputs(0, 0, 0, 0, 1);
break;
case 2:
setInputs(0, 0, 0, 1, 1);
break;
case 3:
setInputs(0, 0, 1, 0, 1);
break;
case 4:
setInputs(0, 0, 1, 1, 1);
break;
case 5:
setInputs(0, 1, 0, 0, 1);
break;
case 6:
setInputs(0, 1, 0, 1, 1);
break;
case 7:
setInputs(0, 1, 1, 0, 1);
break;
case 8:
setInputs(0, 1, 1, 1, 1);
break;
case 9:
setInputs(1, 0, 0, 0, 1);
break;
case 10:
setInputs(1, 0, 0, 1, 1);
break;
case 11:
setInputs(1, 0, 1, 0, 1);
break;
case 12:
setInputs(1, 0, 1, 1, 1);
break;
case 13:
setInputs(1, 1, 0, 0, 1);
break;
case 14:
setInputs(1, 1, 0, 1, 1);
break;
default:
// Invalid electrode number
break;
}

int numIterations = 200;
int interval = 50; // Delay between measurements in milliseconds

for (int i = 0; i < numIterations; i++) {
// Perform capacitance measurement for the given electrode
uint32_t capacitance = AD7745_MeasureCapacitance();
double capacity = (((double) capacitance - 8388608.0) / 8388608.0) * 4.095999;

// Print the capacitance value
printf("Electrode: %d, Capacitance: %.4f pF\n", electrode, capacity);

// Delay between measurements
__delay_ms(interval);
}
}

void main(void) {
// Initialize the I2C communication
I2C_Init();

// Check communication with AD7745 by reading the device ID register
I2C_Start(); // Send Start condition
I2C_Write(AD7745_ADDRESS << 1); // Send AD7745 address with Write bit (0)
I2C_Write(0x0C); // Send the address of the Device ID register
I2C_Start(); // Send repeated start condition
I2C_Write((AD7745_ADDRESS << 1) | 0x01); // Send AD7745 address with Read bit (1)

uint8_t deviceId = I2C_Read(0); // Read the Device ID register, acknowledge with ACK

I2C_Stop(); // Send Stop condition

if (deviceId == 0x09) {
printf("AD7745 Communication Successful! Device ID: 0x%02X\n", deviceId);
} else {
printf("AD7745 Communication Failed. Device ID: 0x%02X\n", deviceId);
while (1); // End the program if communication fails
}

// Configure the AD7745 for capacitance measurement
AD7745_Configuration();

// Wait for stabilization before taking measurements
__delay_ms(100);

int choice;

// Prompt the user for electrode analysis choice
printf("Select an option:\n");
printf("1. Analyze all electrodes sequentially\n");
printf("2. Choose a specific electrode\n");
printf("Enter your choice: ");
scanf("%d", &choice);

if (choice == 1) {
// Analyze all electrodes sequentially
for (int electrode = 1; electrode <= 14; electrode++) {
measureCapacitance(electrode);
}
} else if (choice == 2) {
// Choose a specific electrode
int selectedElectrode;
printf("Enter the electrode number (1-14): ");
scanf("%d", &selectedElectrode);

// Check if the electrode number is within the valid range
if (selectedElectrode >= 1 && selectedElectrode <= 14) {
measureCapacitance(selectedElectrode);
} else {
printf("Invalid electrode number!\n");
}
} else {
printf("Invalid choice!\n");
}

while (1) {
// Main program loop
// Add your other code here
}
}
dyeatman



Joined: 06 Sep 2003
Posts: 1934
Location: Norman, OK

View user's profile Send private message

PostPosted: Tue Jul 25, 2023 5:13 pm     Reply with quote

Wrong compiler. This site is for CCS C

You need to go to the Microchip XC compiler forum here:
https://forum.microchip.com/s/?&page=1&offset=0&filters=false&selectedlist=section1&followingtopics=false&myforums=false&myactivity=false
_________________
Google and Forum Search are some of your best tools!!!!
Diogogp24



Joined: 25 Jul 2023
Posts: 3

View user's profile Send private message

PostPosted: Wed Jul 26, 2023 5:17 am     Reply with quote

dyeatman wrote:
Wrong compiler. This site is for CCS C

You need to go to the Microchip XC compiler forum here:
https://forum.microchip.com/s/?&page=1&offset=0&filters=false&selectedlist=section1&followingtopics=false&myforums=false&myactivity=false


sorry my bad, but thanks
temtronic



Joined: 01 Jul 2010
Posts: 9226
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed Jul 26, 2023 5:28 am     Reply with quote

it'd be interesting to tell chapgpt to USE the CCS compiler and see what it comes up with !
Diogogp24



Joined: 25 Jul 2023
Posts: 3

View user's profile Send private message

PostPosted: Wed Jul 26, 2023 5:51 am     Reply with quote

temtronic wrote:
it'd be interesting to tell chapgpt to USE the CCS compiler and see what it comes up with !


Well he gave me something, but i am using a microchip compiler for my pic but i let here the code in ccs:


Code:
#include <18F4550.h>
#device ADC=10
#fuses HS, PLL5, CPUDIV1, VREGEN, USBDIV, PUT, NOWDT, NOPROTECT, NOBROWNOUT, NOLVP, NOXINST, NODEBUG, NOSTVREN, NOPBADEN
#use delay(clock=48000000)

#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>

#define I2C_SLAVE
#define I2C_FASTMODE
#include <i2c.h>

// I2C Configuration
#define I2C_BAUD_RATE 100000 // Desired I2C baud rate (100 kHz)

// AD7745 I2C Address
#define AD7745_ADDRESS 0x48 // The 7-bit I2C address of the AD7745

// Define your pin variables
#define A3_PIN PIN_B2
#define A2_PIN PIN_B1
#define A1_PIN PIN_B0
#define A0_PIN PIN_C1
#define EN_PIN PIN_C0

// Function to initialize I2C
void I2C_Init() {
    i2c_init(TRUE);
    i2c_speed(I2C_BAUD_RATE);
}

// Function to configure the AD7745 for capacitance measurement
void AD7745_Configuration() {
    // ... (same as previous code)
}

// Function to send a single byte to the AD7745
void AD7745_SendByte(uint8_t data) {
    i2c_start();
    i2c_write(AD7745_ADDRESS);
    i2c_write(data);
    i2c_stop();
}

// Function to read a single byte from the AD7745
uint8_t AD7745_ReadByte() {
    uint8_t data;
    i2c_start();
    i2c_write(AD7745_ADDRESS | 0x01);
    data = i2c_read(0);
    i2c_stop();
    return data;
}

// Function to measure capacitance with the AD7745
uint32_t AD7745_MeasureCapacitance() {
    // ... (same as previous code)
}

// Function to set the inputs for the AD7745 using the given pin values
void setInputs(int a3, int a2, int a1, int a0, int en) {
    output_bit(A3_PIN, a3);
    output_bit(A2_PIN, a2);
    output_bit(A1_PIN, a1);
    output_bit(A0_PIN, a0);
    output_bit(EN_PIN, en);
}

// Function to measure capacitance for a specific electrode
void measureCapacitance(int electrode) {
    // Set the output pins based on the selected case number
    switch (electrode) {
        case 1:
            setInputs(0, 0, 0, 0, 1);
            break;
        case 2:
            setInputs(0, 0, 0, 1, 1);
            break;
        case 3:
            setInputs(0, 0, 1, 0, 1);
            break;
        case 4:
            setInputs(0, 0, 1, 1, 1);
            break;
        case 5:
            setInputs(0, 1, 0, 0, 1);
            break;
        case 6:
            setInputs(0, 1, 0, 1, 1);
            break;
        case 7:
            setInputs(0, 1, 1, 0, 1);
            break;
        case 8:
            setInputs(0, 1, 1, 1, 1);
            break;
        case 9:
            setInputs(1, 0, 0, 0, 1);
            break;
        case 10:
            setInputs(1, 0, 0, 1, 1);
            break;
        case 11:
            setInputs(1, 0, 1, 0, 1);
            break;
        case 12:
            setInputs(1, 0, 1, 1, 1);
            break;
        case 13:
            setInputs(1, 1, 0, 0, 1);
            break;
        case 14:
            setInputs(1, 1, 0, 1, 1);
            break;
        default:
            // Invalid electrode number
            break;
    }

    int numIterations = 200;
    int interval = 50; // Delay between measurements in milliseconds

    for (int i = 0; i < numIterations; i++) {
        // Perform capacitance measurement for the given electrode
        uint32_t capacitance = AD7745_MeasureCapacitance();
        double capacity = (((double) capacitance - 8388608.0) / 8388608.0) * 4.095999;

        // Print the capacitance value
        printf("Electrode: %d, Capacitance: %.4f pF\n", electrode, capacity);

        // Delay between measurements
        delay_ms(interval);
    }
}

void main() {
    setup_adc_ports(NO_ANALOGS);
    setup_adc(ADC_OFF);
    setup_spi(FALSE);
    setup_wdt(WDT_OFF);
    setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
    setup_timer_1(T1_DISABLED);
    setup_timer_2(T2_DISABLED,0,1);

    // Initialize the I2C communication
    I2C_Init();

    // Check communication with AD7745 by reading the device ID register
    i2c_start();
    if (i2c_write(AD7745_ADDRESS)) {
        printf("AD7745 Communication Successful!\n");
    } else {
        printf("AD7745 Communication Failed!\n");
        while (1); // End the program if communication fails
    }
    i2c_stop();

    // Configure the AD7745 for capacitance measurement
    AD7745_Configuration();

    // Wait for stabilization before taking measurements
    delay_ms(100);

    int choice;

    // Prompt the user for electrode analysis choice
    printf("Select an option:\n");
    printf("1. Analyze all electrodes sequentially\n");
    printf("2. Choose a specific electrode\n");
    printf("Enter your choice: ");
    scanf("%d", &choice);

    if (choice == 1) {
        // Analyze all electrodes sequentially
        for (int electrode = 1; electrode <= 14; electrode++) {
            measureCapacitance(electrode);
        }
    } else if (choice == 2) {
        // Choose a specific electrode
        int selectedElectrode;
        printf("Enter the electrode number (1-14): ");
        scanf("%d", &selectedElectrode);

        // Check if the electrode number is within the valid range
        if (selectedElectrode >= 1 && selectedElectrode <= 14) {
            measureCapacitance(selectedElectrode);
        } else {
           
dyeatman



Joined: 06 Sep 2003
Posts: 1934
Location: Norman, OK

View user's profile Send private message

PostPosted: Wed Jul 26, 2023 7:05 am     Reply with quote

Interesting. As expected it doesn't compile, a number of errors, but looks half way promising.
It uses, for example <include i2C.c> rather than #use I2C
It doesn't know #use rs232...
It throws in some (Same as previous code) rather than the actual code which is unusual.
I made few tweaks and got it to compile...still a bit more needed though.
_________________
Google and Forum Search are some of your best tools!!!!
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group