Trying I2C interface with dac (max5215) in pic24f microcontriller

Status
Not open for further replies.

Ranjitharanju

Newbie level 4
Joined
Feb 12, 2014
Messages
5
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Visit site
Activity points
72
Hi I m trying to interface I2c with DAC(IC-MAx5215(slave device)) in pic24f(master device) but I am not getting the output. Even i am not getting what mistake i m doing please anyone can help me I am using system clock 32Mhz.

here is my code


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include<p24fj128ga010.h>
#define FCY 16000000
 
_CONFIG1( JTAGEN_OFF & GCP_OFF & GWRP_OFF & COE_OFF & FWDTEN_OFF & ICS_PGx2) 
_CONFIG2( FCKSM_CSDCMD & OSCIOFNC_OFF & POSCMOD_HS & FNOSC_PRIPLL)
#define     bUs__idLe           0x8000
#define     i2c_disable         0x0000 
#define     I2c_____conTroL     I2C1CON
 
void I2C_WRITE(char Mdata,char Ldata);
void start(void)
{
    I2C1CONbits.SEN     = 1;         //Start Condition Enable bit
    while(I2C1CONbits.SEN);          // I2C start bit verification
}
void stop(void)
{
    I2C1CONbits.PEN     = 1;         //Stop Condition Enable bit
    while(I2C1CONbits.PEN);          // I2C start bit verification
}
void rpt_start(void)
{
    I2C1CONbits.RSEN    = 1;         //Restart  Condition Enable bit
}
void write(char data)
{
    I2C1TRN = data;
}
 
int main(void)
{
    while(!OSCCONbits.LOCK );           //CLOCK FREQUENCY    32MHz
        
    I2C_INIT();
    while(1)
    {
        I2C_WRITE(0xFF,0xFF);
    
    }
    return 0;
}
 
void I2C_INIT(void)
{
    
  /*  I2C1CONbits.I2CSIDL = 1;           // STOP IN IDEL MODE
    I2C1CONbits.SCLREL  = 0;         //SCL RELESE CONTROL BIT
    I2C1CONbits.A10M    = 0;         //SLAVE ADDRESS BIT SELECT PIN
    I2C1CONbits.DISSLW  = 1;         //SLEW RATE DISABLE BIT
    I2C1CONbits.SMEN    = 0;         //SMBUS INPUT LEVEL BIT // BIT 8
    I2C1STATbits.I2COV  = 1;         //Receive Overflow Flag bit
*/
 
    I2c_____conTroL     = 0x8000; 
    I2C1STAT            = 0x0030;                //status bit register
    I2C1MSK             = 0X0000;                //MASK REGISTER
    I2C1BRG             = 0x4f;                  //BAUD RATE SETTING
     
    
}
void I2C_WRITE(char Mdata,char Ldata)
{
    I2c_____conTroL     =   i2c_disable;            
    __delay32(100);                             
    I2c_____conTroL     =   bUs__idLe ;
    __delay32(100);                             
    start();
    
 
    write(0X38));               //slave device address
    while(I2C1STATbits.TBF);        //waiting for complete transmission 
    while(!IFS1bits.MI2C1IF);
    while(I2C1STATbits.ACKSTAT);        //waiting for slave ack
 
 
    write(0X02);                //user command address                          
    while(I2C1STATbits.TBF);
    while(!IFS1bits.MI2C1IF);
    while(I2C1STATbits.ACKSTAT);
 
    write(Mdata);
    while(I2C1STATbits.TBF);
    while(!IFS1bits.MI2C1IF);
    while(I2C1STATbits.ACKSTAT);
 
    write(Ldata);
    while(I2C1STATbits.TBF);
    while(!IFS1bits.MI2C1IF);
    while(I2C1STATbits.ACKSTAT);
    stop();
    __delay32(100);                             
    I2c_____conTroL     =   bUs__idLe ;
    __delay32(100);
    I2c_____conTroL     =   i2c_disable;            
    __delay32(200);
 
}
 
void __attribute__ ((__interrupt__,no_auto_psv)) _MI2C1Interrupt(void)
{
  //    IFS1bits.MI2C1IF  = 0;          
}






Thank you.
 
Last edited by a moderator:

Hai,
thanks for your reply please send the code which u have i'l compare with my code.

Thank you.
 

part of code concerning DAC 16 bits MAX541 ( C18 Mplab)

Code:
#define Clock 0.25    // µS   at 16Mhz
#define Ualim 5.00
#define ADC_Coeff 4.096/1024.0

// DAC 16 bits SPI ali=+5V  output max==Vref=2.5V  en DIP 8
#define MAX541     
#ifdef MAX541
                                    // VOut pin 1
                                    // VRef pin 3 ... +2,5V
                                    // VDD pin 8
                                    // VSS pin 7
#define MAX541_CS  LATCbits.LATC4    // CS out to pin 4
#define MAX541_SI  LATCbits.LATC1    // SI out to pin 6
#define MAX541_SCK LATCbits.LATC3    // SCK out to pin 5

#endif

#ifdef MAX541
void Out_MAX541(unsigned int V);



[COLOR="#0000CD"][I]void Out_MAX541(unsigned int V)
{
unsigned int i,j;

MAX541_CS=0; // CS pin 4
MAX541_SCK=0;// SCK pin 5
MAX541_SI=0; // SI pin 6
Nop();Nop();Nop();Nop();
j=32768;
// instruction byte     
for (i=0;i<16;i++)    
{

if ((V & j)==j)
MAX541_SI=1; 
  else
   MAX541_SI=0;  
Nop();Nop();
MAX541_SCK=0;
Nop();Nop();Nop();Nop();
MAX541_SCK=1;
Nop();Nop();Nop();Nop();
j=j>>1;
 MAX541_SCK=0;
}
Nop();
 MAX541_CS=1;
}    
#endif[/I][/COLOR]


void Raz_Receipt()
{ptecr=buffer;
      ptlec= ptecr;
      CptCars=0;
      buffer[0]=0;
      c=0;
      receive=0;
 }

void main(void)
{
 
#ifdef OSCILLATEUR_INTERNE  
 OSCCON=0x60;         // Select8 MHz internal clock 
 OSCTUNE=0;
  #ifdef WithPLL 
    OSCTUNEbits.PLLEN=1;  // 1= avec PLL => 64Mhz ou 40Mhz
    #else
    OSCTUNEbits.PLLEN=0;  // 0 =sans PLL => 16Mhz  ou 10Mhz
   #endif  
#endif   
 ................   
 Init_Hardware() ;
 ............
  Init_UART1();
 OUT_RS232
 ................ 
 // init sortie DAC à 0V
 Sd=0;
 [B]Out_MAX541(Sd);[/B]
 LATC=0;
 Volt=Ualim;
 k=fprintf(_H_USART,"Alim du Montage =5.0 -> %s%c",fltToa(Volt,CRam1,4),9);
 CRLF(); 
 ....................
 
  // test si saisie consigne DAC MAX  
   if ((CptCars>6) && (c==13)&& (buffer[0]=='M'))
   {
     SaisieOk=0;
     for (i=1;i<6;i++)
     {
       SaisieOk=SaisieOk+isdigit(buffer[i]);
     }
     if(SaisieOk!=5)
     { 
        strcpypgm2ram(txt,RS_Str[5]); k=PutStr_RS(txt);
        Delay_mS(1000);
        Raz_Receipt();
     }
      else
      { 
        buffer[6]=0; //terminateur de string
        Sd=atoi(buffer+1);       // numerique
        [B]Out_MAX541(Sd);[/B]
        Volt= (float)(Sd) *2.50/65536.0;
         k=fprintf(_H_USART,"\r\nBuffer= %s Out Max541 %05u soit %s V \r\n ",buffer,Sd,fltToa(Volt,CRam1,3));
        Raz_Receipt();   
     }
	 ..............
   }
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…