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 support@ccsinfo.com

dsPIC33 I2C Hardware Issue
Goto page Previous  1, 2, 3
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
temtronic



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

View user's profile Send private message

PostPosted: Tue Feb 19, 2019 9:47 am     Reply with quote

While I don't use DSPICs...
I was curious as 'tolerant' doesn't necessarily mean 'it will work', so I downloaded the datasheet as well as DS70330 (ref app note). I can't see anywhere that MicroChip says you can tie the bus to +5 while using a VDD of 3v3.
It'd be nice to have someone confirm this is allowed.
Baskar Veerappan



Joined: 17 Feb 2019
Posts: 9

View user's profile Send private message Send e-mail

PostPosted: Tue Feb 19, 2019 10:32 am     Reply with quote

Dear temtronic,

I am able to Write to slaves without any issues out only reading causes issues
creating a Bus Collision.
_________________
Regards
Baskar V
Baskar Veerappan



Joined: 17 Feb 2019
Posts: 9

View user's profile Send private message Send e-mail

Code
PostPosted: Tue Feb 19, 2019 10:35 am     Reply with quote

This is my code
Please Correct if anything is wrong

Code:


#define CLK_16MHZ
#define FCY      16010000 /*16.01 MHz*/

#ifndef CLK_16MHZ
#define FCY      69400000 /*69.4 MHz*/
#define CLK_138MHZ
#endif

#define MAXTASK (5u)
#define DS3231_RD         ((104u << 1u)|1u) /* RW Bit 1 */
#define DS3231_WR         (104u << 1u)  /* RW Bit 0 */

typedef enum { TASK0=0u,TASK1,TASK2,TASK3,TASK4} T_NO ;

typedef enum
{
   T_RUN,      /* Run the Task  */
   T_SUSPEND,   /* Wait For the event to Occur */
   T_STOP,      /* Stop the task. Enabled by user */
   T_WAIT      /* Wait For specific Period  */
}T_STATE ;

typedef struct
{
    T_NO TaskNo;
   T_STATE  TaskState;   
    Uint16 WaitTime ;
    void  (*Task)(void);
}SCHEDULER ;

typedef struct
{
   SCHEDULER *OsPtr  ;
   T_NO CurrentTask ;
   
}MAINTASK;

/********************************************************************************
* FUNCTION      :   I2C MASTER INITILIZATION
* ARGUMENT1     :   NONE
* RETURN TYPE   :   NONE
* NOTE          :   MASTER MODE
*            :   I2C BAUD RATE FORMULA : I2C1BRG = {[((1/FScl)-Delay)*Fcy]-2}
*            :   DELAY VARIES FROM 110nS to 130nS
* REFERENCE    :   MICROCHIP REFERENCE MANUAL : DS70330
********************************************************************************/
static void dsPIC33EP512GP502_I2C2MasterInit(void)
{
   TRISB      =  (BIT5|BIT6);       /* Inputs for I2C2 */
   I2C2CON      =   CLEAR ;          /*I2C2 Disabled */
#ifndef CLK_16MHZ /*Device Enters Full speed 70MIPS Approx */
   I2C2BRG    =   I2C_CLK_400KHz_AT_138MHz;
#endif
#ifdef CLK_16MHZ  /*Device Enters Low speed 16MIPS Approx */
   I2C2BRG    =   I2C_CLK_100KHz_AT_16MHz;
#endif
   I2C1ADD      =   CLEAR ;
   I2C1MSK      =   CLEAR ;
   I2C2CON      =   (BIT15|BIT13|BIT12|BIT8);
}
/********************************************************************************
* FUNCTION      :   SET I2C2 TIME OUT TIME
* ARGUMENT1     :   NONE
* RETURN TYPE   :   NONE
* NOTE          :   -/-
********************************************************************************/
void inline SetTimeoutTime(void)
{
#ifndef CLK_16MHZ /*Default Clock is 138 MHz*/
   I2C2.TimeOut   =   80000;/*Approx 2mS 70MIPS 0.001/Instrction Cycle14.4nS*/
#endif
#ifdef CLK_16MHZ /* Debug Clock is 16 MHz*/
   I2C2.TimeOut   =   17010;/*Approx 2mS 16MIPS 0.001/Instrction Cycle62.4nS*/
#endif    
}
/********************************************************************************
* FUNCTION      :   BUS IDLE  I2C2
* ARGUMENT1     :   NONE
* RETURN TYPE   :   BUS IDLE [1]  NOT IDLE [0]
* NOTE          :   -/-
********************************************************************************/
Flag BusIdle_I2C2(void)
{
   Flag ReturN = 0 ;
   ReturN =  (((I2C2STAT & (BIT14|BIT10|BIT7|BIT6)) == 0u) &&
           (( I2C2CON  & (BIT2|BIT1|BIT0)) == 0u)) ;
   return ReturN ;
}
/********************************************************************************
* FUNCTION      :   GENERATE ACK ON I2C2
* ARGUMENT1     :   ACK TYPE [0-ACK, 1-NACK]
* RETURN TYPE   :   NONE
* NOTE          :   -/-
********************************************************************************/
void PutAcknowledgeI2C2(Flag AckBit)
{
   I2C2CONbits.ACKDT   =   AckBit ;   
   I2C2CONbits.ACKEN   =   TRUE ;
}
/********************************************************************************
* FUNCTION      :   READ ACK FROM SLAVE ON I2C2 BUS
* ARGUMENT1     :   NONE
* RETURN TYPE   :   ACK TYPE [0-ACK, 1-NACK,2 ACK TIMEOUT ERROR]
* NOTE          :   -/-
********************************************************************************/
Uint08 GetAcknowledgeI2C2(void)
{
   if(I2C2.Sts == I2C_OK) {
   SetTimeoutTime();   
   while(I2C2.TimeOut != 0u) {
   if((BusIdle_I2C2() == TRUE) && (!I2C2STATbits.TBF)) {
   I2C2.Sts   =   I2C_OK ;
   return (I2C1STATbits.ACKSTAT);
   } else { I2C2.TimeOut -- ; }
   }
   return(I2C2.Sts=ACK_ERR) ;   
   }
   else { return(I2C2.Sts=ACK_ERR);}   
}
/********************************************************************************
* FUNCTION      :   INITIATE START,RESTART,STOP CONDITION ON I2C2 BUS
* ARGUMENT1     :   START,RESTART,STOP
* RETURN TYPE   :   NONE
* NOTE          :   -/-
********************************************************************************/
void dsPIC33EP512GP502_IIC2(STATE_IIC Action)
{
   if(I2C2.Sts != I2C_OK) {
   
   dsPIC33EP512GP502_I2C2MasterInit();
      
   } else { /* No Operation */ }
      
   SetTimeoutTime();   
   
   while(I2C2.TimeOut != 0u) {
      
   I2C2.Sts =    I2C_OK ;
   
   ((BusIdle_I2C2() == TRUE) ? ((Action == START) ? (I2C2CONbits.SEN = TRUE) :
      
   ((Action == RESTART) ? (I2C2CONbits.RSEN = TRUE):(I2C2CONbits.PEN = TRUE))):
      
   (I2C2.Sts  = BUS_NOT_IDLE) ) ;
      
   (I2C2.Sts  != BUS_NOT_IDLE) ? (I2C2.TimeOut = CLEAR) : (I2C2.TimeOut--) ;
   
   }
}
/********************************************************************************
* FUNCTION      :   WRITE TO I2C2 BUS
* ARGUMENT1     :   NONE
* RETURN TYPE   :   NONE
* NOTE          :   -/-
********************************************************************************/
void IIC2_Transmit(Uint08 DaTa)
{
   if(I2C2.Sts == I2C_OK) {
   SetTimeoutTime();   
   while(I2C2.TimeOut != 0u) {
   if(BusIdle_I2C2() == TRUE) {
   I2C2TRN      =   DaTa ; /*Shift Out*/
   I2C2.Sts   =   I2C_OK ;
   return ;
   }
   else {   I2C2.TimeOut -- ; }
   }
   I2C2.Sts   =   I2C_ERR;
   return ;   
   }
   else { /* Exit Without Operation */}
}
/********************************************************************************
* FUNCTION      :   READ FROM I2C2 BUS
* ARGUMENT1     :   NONE
* RETURN TYPE   :   READ DATA
* NOTE          :   -/-
********************************************************************************/
Uint08 IIC2_Recieve(void)
{
   Uint08 RxData = NULL ;
   if(I2C2.Sts == I2C_OK) {
   SetTimeoutTime();   
   while(I2C2.TimeOut != 0u) {
   if(I2C2STATbits.RBF == TRUE) {
   RxData      =   I2C2RCV; /*Shift In */
   I2C2.Sts   =   I2C_OK ;
   return RxData ;
   }
   else {   I2C2.TimeOut -- ; }
   }
   I2C2.Sts   =   I2C_ERR;
   return NULL ;   
   }
   else { return NULL ; /* Exit Without Operation */}
}
/********************************************************************************
* FUNCTION      :   READ TIME FROM DS3231 RTC CHIP
* ARGUMENT1     :   ADDRESS OF INTERNAL REGISTER TO BE READ
* RETURN TYPE   :   RTC READ VALUE
* NOTE          :   -/-
********************************************************************************/
Uint08 Read_DS3231(Uint08 Adrs )
{
   Uint08 RxD =0; Flag AckNow=1;
   AckNow = I2C2STATbits.ACKSTAT ;
   I2C2CONbits.RCEN = TRUE ;
   dsPIC33EP512GP502_IIC2(START);   
   IIC2_Transmit(DS3231_WR);
   IIC2_Transmit(Adrs); /*Internal Register Address */
   AckNow = (GetAcknowledgeI2C2() == ACK);
   dsPIC33EP512GP502_IIC2(RESTART);   
   IIC2_Transmit(DS3231_RD);/* Bus Collision Occurs Here Making SDA 0 for Ever*/
   RxD = IIC2_Recieve();
   PutAcknowledgeI2C2(NACK);
   dsPIC33EP512GP502_IIC2(STOP);   
   return RxD ;   
}
/********************************************************************************
* FUNCTION      :   SERIAL COMMUNICATIONS INIT
* ARGUMENT1     :   NONE
* RETURN TYPE   :   NONE
* NOTE          :   ALL TIMER SETTINGS
********************************************************************************/
static inline void dsPIC33EP512GP502_SerialCoM(void)
{
   dsPIC33EP512GP502_I2C2MasterInit();
   dsPIC33EP512GP502_UARTMaster();
}
/********************************************************************************
* FUNCTION      :   HARDWARE INITILIZATION 
* ARGUMENT1     :   NONE
* RETURN TYPE   :   NONE
* NOTE          :   NONE
********************************************************************************/
void dsPIC33EP512GP502_Hw_Initialization(void)
{
   dsPIC33EP512GP502_OscConfig();    /* Oscillator Configurations */
   dsPIC33EP512GP502_PMD();       /* PMD Configurations        */
   dsPIC33EP512GP502_IO_Setting();    /* IO Pin Configurations     */
   dsPIC33EP512GP502_Interrupts();    /* Interrupt Settings        */
   dsPIC33EP512GP502_Timers();       /* Timer Settings            */
   dsPIC33EP512GP502_SerialCoM();    /* Serial Communications     */
   dsPIC33EP512GP502_RemapingIO();    /* PPS- Remaping IO          */
}
/********************************************************************************
* FUNCTION      :   TASK STATE CONTROL   
* ARGUMENT1     :   TASK NUMBER
* ARGUMENT2     :   WAIT TIME [IF TASK STATE IS SET TO WAIT ELSE PASS 0]
* RETURN TYPE   :   NONE
* CATEGORY      :   TASK MANAGEMENT      
********************************************************************************/
void SetTaskState(T_NO TaskNo ,T_STATE StatE,Uint16 Time)
{
   Main.OsPtr[TaskNo].TaskState  = StatE ;
   Main.OsPtr[TaskNo].WaitTime   = Time ;
}
/********************************************************************************
* FUNCTION      :   TASK_INITIALIZATION TASK 
* ARGUMENT1     :   NONE
* RETURN TYPE   :   NONE
********************************************************************************/
void Task_Initialization(void)
{
   dsPIC33EP512GP502_Hw_Initialization();
   dsPIC33EP512GP502_Sw_Initialization(); /*Variables Initialization */
   Init_LCD_4bit();
   Print_LCD("Welcome Boss...",STRING,Line2); // Test Purpose
   Display_Auto_clear(2000,Line2+3,8); // Test Purpose Clear after 2 Seconds
   SetTaskState(TASK0,T_STOP,NULL);    // No More Further Execution
}
/********************************************************************************
* FUNCTION      :   500mS TASK 
* ARGUMENT1     :   NONE
* RETURN TYPE   :   NONE
********************************************************************************/
void Task_500mS(void)
{
   Uint16 Sec=0;
   Sec = Read_DS3231(0); // Read Seconds Register
   Print_LCD(&Sec,INTEGER2,Line3);
   SetTaskState(TASK1,T_WAIT,250u); /*250*2mS = 500mS*/
}
/********************************************************************************
* FUNCTION      :   MAIN
* ARGUMENT1     :   NOT REQUIRED
* RETURN TYPE   :   NONE
********************************************************************************/
int main(void)
{
   Uint08 TskNow ;

   SCHEDULER   TaskHandler[MAXTASK] = {
   { TASK0   ,   T_RUN         ,      0u  ,    Task_Initialization   },
   { TASK1   ,   T_RUN         ,      0u  ,    Task_500mS        },
   { TASK2   ,   T_RUN         ,      0u  ,    Task_1000mS        },
   { TASK3   ,   T_WAIT        ,      500u,    Task_DisplayHandler }, /* 1S Wait*/
   { TASK4   ,   T_STOP        ,      0u  ,    Task_AlarmHandler   }
   } ;
/*  Enabling for Global Access */
   Main.OsPtr = TaskHandler ;
/*  Run Time Task Handling */
   for(TskNow = 0; TskNow < MAXTASK; TskNow = ((TskNow+1u)%MAXTASK))
   {   
   if(TaskHandler[TskNow].TaskState == T_RUN) {   
   Main.CurrentTask = TaskHandler[TskNow].TaskNo;
   TaskHandler[TskNow].Task() ;
   }
   }
   return 0 ;
}


_________________
Regards
Baskar V
gaugeguy



Joined: 05 Apr 2011
Posts: 129

View user's profile Send private message

PostPosted: Tue Feb 19, 2019 10:44 am     Reply with quote

5V tolerant pins on PIC24 & PIC33 have the specs in the specific data sheet for that processor under electrical characteristics. This is typical of many:

Voltage on any general purpose digital or analog pin (not 5.5V tolerant) with respect to VSS: -0.3V to (VDD + 0.3V)

Voltage on any general purpose digital or analog pin (5.5V tolerant, including MCLR) with respect to VSS:
When VDD = 0V: -0.3V to +4.0V
When VDD > = 2.0V:. -0.3V to +6.0V

So I2C can be operated this way with 5V tolerant pins.
Baskar Veerappan



Joined: 17 Feb 2019
Posts: 9

View user's profile Send private message Send e-mail

PostPosted: Tue Feb 19, 2019 10:51 am     Reply with quote

Anybody Having Working I2C Code for dsPIC33EP512GP502

It will be very helpful if any one provide.
_________________
Regards
Baskar V
PCM programmer



Joined: 06 Sep 2003
Posts: 20775

View user's profile Send private message

PostPosted: Tue Feb 19, 2019 10:54 am     Reply with quote

Your posted code is for a Microchip compiler, not CCS.
This is the CCS forum.
Baskar Veerappan



Joined: 17 Feb 2019
Posts: 9

View user's profile Send private message Send e-mail

PostPosted: Tue Feb 19, 2019 10:59 am     Reply with quote

Dear PCM Programmer,

Yup you are Right

But I have not used any inbuilt library code from XC16 compiler.
Smile
_________________
Regards
Baskar V
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2, 3
Page 3 of 3

 
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