CAN - Pic18f4520 - CNA (PCF8591 - I2C )

Status
Not open for further replies.

servntes

New Member
Hi,

I want to inject a signal between (0V to 5V) on PORTA, 0 ==> AN0, using a potentiometer, and convert this value by an ADC, analog digital convertision

and the result obtained, I want to get it from an analog output, by using a circuit 8-bit Digital to Analog Converter (PCF 8591) through the I2C bus.

I already made the program of DAC and ADC, and I have compiled without error, but its not working in simulation.

I tested the ADC and it works very well, but the DAC not work! I vary the potentiometer for 0V to 5V, but the output is always the result to 0V

Please if anyone can help me in this problem !!

Here is my code, I simulated (no error when compiling):


#include <p18f45k20.h>
#include <stdio.h>
#include <math.h>

#define SCL_PIN PORTCbits.RC3
#define SDA_PIN PORTCbits.RC4

#define SCL_DIR TRISCbits.TRISC3
#define SDA_DIR TRISCbits.TRISC4

void main (void);
void DelayMs(unsigned int);
void Pcf_8591_d2a(unsigned char);
void i2c_init(void);
void i2c_start(void);
void i2c_wr(unsigned char);
void i2c_ack(void);
void i2c_stop(void);
void i2c_high_scl(void);
void i2c_low_scl(void);
void i2c_high_sda(void);
void i2c_low_sda(void);

#pragma code

/*************************
* Variables Definition
*************************/
unsigned char Ys;
/*********************
* principal Fonction
********************/
void main()
{
TRISD=0;

ADCON1=0x00;
ADCON0=0x01;
ADCON2=0x00;

while(1)
{
ADCON0bits.GO = 1; // launch of analog to digital conversion

while (ADCON0bits.GO)
{
//pas de opcode; //wait closure conversion, where GO = 0
}

Ys = ADRESH; //result of the analog to digital conversion,

Pcf_8591_d2a(Ys); //launch of the digital to analog conversion

}
}
/*******************************************
* PCF 8591 //// digital to analog conversion
*******************************************/
void Pcf_8591_d2a(unsigned char dat)
{
int iConfig=0x40; // Configuration of DAC PCF8591; bit6=1(Enable analog output)
int cPCF8591_w=0x90; // Adresse of PCF85910
i2c_init(); // Acknowledge
i2c_start(); // Signal Start
i2c_wr(cPCF8591_w); // Adresse of slave (PCF8591)
i2c_ack(); // Acknowledge
i2c_wr(iConfig); // enable DAC
i2c_ack(); // Acknowledge
i2c_wr(dat); // Data Digital to analog
i2c_ack(); // Acknowledge
i2c_stop(); // Signal Stop
}
/***********************
* Initialisatin Bus I2C
***********************/
void i2c_init(void)
{
SSPSTAT=0x80;
SSPCON1=0x28;
SSPCON2=0x01;
}
/*******************
* Start Condition
******************/
void i2c_start(void)
{
i2c_low_scl();
i2c_high_sda();
i2c_high_scl();
i2c_low_sda();
i2c_low_scl();
}
/*******************
* Stop Condition
*****************/
void i2c_stop(void)
{
i2c_low_scl();
i2c_low_sda();
i2c_high_scl();
i2c_high_sda();
}
/*****************
* Write PCF8591
*****************/
void i2c_wr(unsigned char data)
{
unsigned char n;
for(n=0; n<8; n++)
{
if(data&0x80)
{
i2c_high_sda();
}
else
{
i2c_low_sda();
}
i2c_high_scl();
i2c_low_scl();
data <<=1;
}
i2c_high_sda();
}


/******************************
* Acknowledge
******************************/
void i2c_ack(void)
{
i2c_high_sda();
i2c_high_scl();
i2c_low_scl();
}

/******************************
* Low and High of SCL/SDA
******************************/
void i2c_high_sda(void)
{
SDA_DIR = 1;
DelayMs(1);
}

void i2c_low_sda(void)
{
SDA_PIN = 0;
SDA_DIR = 0;
DelayMs(1);
}

void i2c_high_scl(void)
{
SCL_DIR = 1;
DelayMs(1);
}

void i2c_low_scl(void)
{
SCL_PIN = 0;
SCL_DIR = 0;
DelayMs(1);
}
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…