Baud rate issues

LK/ELK/VK/PK/OK/MX/GLK/EGLK/GVK/GLT Series

Moderators: Henry, Mods

Post Reply
suwandis
LCD?
Posts: 2
Joined: Tue Oct 10, 2006 4:47 pm
Location: East Lansing, Michigan

Baud rate issues

Post by suwandis »

We think we are having some Baudrate issues with our LCD0821. The manual says it is capable of of 400kbs in I2C mode. Does that mean it comes from the factory set at 400kbs for I2C? All I'm trying to do is contact the device and clear the screen. I'm using a dsPIC 30f series. I want to make sure I'm setting the baudrate generator using the correct values.

here's my code:

#include "p30fxxxx.h"
#include "i2c.h"



void main()
{
unsigned int Con = 0x8010, BaudRate = 5;
unsigned char SendChar = 'X', AddChar;
AddChar = 0x50;
OpenI2C(Con,BaudRate);
StartI2C();
MasterWriteI2C(AddChar);
IdleI2C();
MasterWriteI2C(SendChar);
StopI2C();
//MasterputsI2C((unsigned char *) &SendChar);


while(1);
}
Raquel
Matrix Orbital
Matrix Orbital
Posts: 834
Joined: Thu Aug 19, 2004 3:37 pm
Location: MO Office

Post by Raquel »

Hello suwandis,

Thank you for posting on the forum.

Is it possible for you to send me the data sheet for the dsPIC you are using? You can email me at rmalinis@matrixorbital.ca or you can just give me the link to it.

I would like to read on how I2C is initialized in dsPIC. Also, it will be good if you post the complete code, since I can only guess the functions you are using.

Thanks,
Raquel Malinis
Design and Development
Matrix Orbital
suwandis
LCD?
Posts: 2
Joined: Tue Oct 10, 2006 4:47 pm
Location: East Lansing, Michigan

Post by suwandis »

Sorry about the amount of code I'm posting, but this is what we are dealing with. We are using MPLAB's C30 compiler and a dsPIC30f6014 development board.

Thanks a ton for trying to help us out, we really appreciate it.

Main Program:
#include "p30fxxxx.h"
#include "i2c.h"



int main()
{
unsigned int Con = 0xD010, BaudRate = 12;
unsigned char SendChar = 'X', AddChar, SendString = 0x14;
AddChar = 0x50;
OpenI2C(Con,BaudRate);
StartI2C();
IdleI2C();
MasterWriteI2C(AddChar);
IdleI2C();
MasterWriteI2C(SendChar);
IdleI2C();
MasterputsI2C((unsigned char *) &SendString);
IdleI2C();
StopI2C();
//MasterputsI2C((unsigned char *) &SendChar);


while(1);
return 0;
}

OpenI2C:

#include <i2c.h>
#include <p30fxxxx.h>

/******************************************************************************
* Function Name: OpenI2C
* Description: This function configures the I2C module for enable bit,
* disable slew rate, SM bus input levels, SCL release,
* Intelligent Peripheral Management Interface enable,
* sleep mode, general call enable,acknowledge data bit,
* acknowledge sequence enable, receive enable, stop
* condition enable, restart condition enable and start
* condition enable. The Baud rate value is also configured
* Parameters: unsigned int : config1
* unsigned int : config2
* Return Value: void
*******************************************************************************/

void OpenI2C(unsigned int config1,unsigned int config2)
{
I2CBRG = config2;
I2CCON = config1;
}

StartI2C:

#include <i2c.h>
#include <p30fxxxx.h>

/*********************************************************************
* Function Name: StartI2C
* Description: This routine generates Start condition
* during master mode.
* Parameters: void
* Return Value: void
*********************************************************************/

void StartI2C(void)
{
I2CCONbits.SEN = 1; /* initiate Start on SDA and SCL pins */
}


IdleI2C:

#include <i2c.h>
#include <p30fxxxx.h>

/************************************************************************
* Function Name: IdleI2C
* Description: This routine generates wait condition intil I2C
* bus is Idle.
* Parameters: void
* Return Value: void
*************************************************************************/

void IdleI2C(void)
{
/* Wait until I2C Bus is Inactive */
while(I2CCONbits.SEN || I2CCONbits.PEN || I2CCONbits.RCEN || I2CCONbits.ACKEN || I2CSTATbits.TRSTAT);
}


MasterwritesI2C:

#include <i2c.h>
#include <p30fxxxx.h>

/************************************************************************
* Function Name: MasterWriteI2C
* Description: This routine is used to write a byte to the I2C bus.
* The input parameter data_out is written to the
* I2CTRN register. If IWCOL bit is set,write collision
* has occured and -1 is returned, else 0 is returned.
* Parameters: unsigned char : data_out
* Return Value: unsigned int
*************************************************************************/

char MasterWriteI2C(unsigned char data_out)
{
I2CTRN = data_out;

if(I2CSTATbits.IWCOL) /* If write collision occurs,return -1 */
return -1;
else
{
return 0;
}
}


MasterputsI2C:

#include <i2c.h>
#include <p30fxxxx.h>

/***********************************************************************
* Function Name: MasterputsI2C
* Description: This routine is used to write out a string to the
* I2C bus.If write collision occurs,-3 is sent.If
* Nack is received, -2 is sent.If string is written
* and null char reached, 0 is returned.
* Parameters: unsigned char * : wrptr
* Return Value: unsigned int
************************************************************************/

unsigned int MasterputsI2C(unsigned char * wrptr)
{
while(*wrptr) //transmit data until null char
{
if(MasterputcI2C(*wrptr) == -1) // write a byte
return -3; //return with write collison error

while(I2CSTATbits.TBF); //Wait till data is transmitted.

IdleI2C();
wrptr++;
}
return 0;
}


H file:

/*********************************************************************/
/* Header File for I2C module Library routines */
/*********************************************************************/

#ifndef I2C_H
#define I2C_H

/* List of SFRs for I2C module */
/* This list contains the SFRs with default (POR) values to be used for configuring I2C module */
/* The user can modify this based on the requirement */
#define I2CRCV_VALUE 0x0000
#define I2CTRN_VALUE 0x00FF
#define I2CBRG_VALUE 0x0000
#define I2CCON_VALUE 0x0000
#define I2CSTAT_VALUE 0x0000
#define I2CADD_VALUE 0x0000

/* I2CCON register Configuration bit definitions */

#define I2C_ON 0xFFFF /*I2C module enabled */
#define I2C_OFF 0x7FFF /*I2C module disabled */

#define I2C_IDLE_STOP 0xFFFF /*stop I2C module in Idle mode */
#define I2C_IDLE_CON 0xDFFF /*continue I2C module in Idle mode */

#define I2C_CLK_REL 0xFFFF /*release clock */
#define I2C_CLK_HLD 0xEFFF /*hold clock */

#define I2C_IPMI_EN 0xFFFF /*IPMI mode enabled */
#define I2C_IPMI_DIS 0xF7FF /*IPMI mode not enabled */

#define I2C_10BIT_ADD 0xFFFF /*I2CADD is 10-bit address */
#define I2C_7BIT_ADD 0xFBFF /*I2CADD is 7-bit address */

#define I2C_SLW_DIS 0xFFFF /*Disable Slew Rate Control for 100KHz */
#define I2C_SLW_EN 0xFDFF /*Enable Slew Rate Control for 400KHz */

#define I2C_SM_EN 0xFFFF /*Enable SM bus specification */
#define I2C_SM_DIS 0xFEFF /*Disable SM bus specification */

#define I2C_GCALL_EN 0xFFFF /*Enable Interrupt when General call address is received. */
#define I2C_GCALL_DIS 0xFF7F /*Disable General call address. */

#define I2C_STR_EN 0xFFFF /*Enable clock stretching */
#define I2C_STR_DIS 0xFFBF /*disable clock stretching */

#define I2C_ACK 0xFFDF /*Transmit 0 to send ACK as acknowledge */
#define I2C_NACK 0xFFFF /*Transmit 1 to send NACK as acknowledge*/

#define I2C_ACK_EN 0xFFFF /*Initiate Acknowledge sequence */
#define I2C_ACK_DIS 0xFFEF /*Acknowledge condition Idle */

#define I2C_RCV_EN 0xFFFF /*Enable receive mode */
#define I2C_RCV_DIS 0xFFF7 /*Receive sequence not in progress */

#define I2C_STOP_EN 0xFFFF /*Initiate Stop sequence */
#define I2C_STOP_DIS 0xFFFB /*Stop condition Idle */

#define I2C_RESTART_EN 0xFFFF /*Initiate Restart sequence */
#define I2C_RESTART_DIS 0xFFFD /*Start condition Idle */

#define I2C_START_EN 0xFFFF /*Initiate Start sequence */
#define I2C_START_DIS 0xFFFE /*Start condition Idle */


/* Priority for Slave I2C Interrupt */

#define SI2C_INT_PRI_7 0xFFFF
#define SI2C_INT_PRI_6 0xFFFE
#define SI2C_INT_PRI_5 0xFFFD
#define SI2C_INT_PRI_4 0xFFFC
#define SI2C_INT_PRI_3 0xFFFB
#define SI2C_INT_PRI_2 0xFFFA
#define SI2C_INT_PRI_1 0xFFF9
#define SI2C_INT_PRI_0 0xFFF8

/* Slave I2C Interrupt Enable/Disable */

#define SI2C_INT_ON 0xFFFF
#define SI2C_INT_OFF 0xFFF7

/* Priority for Master I2C Interrupt */

#define MI2C_INT_PRI_7 0xFFFF
#define MI2C_INT_PRI_6 0xFFEF
#define MI2C_INT_PRI_5 0xFFDF
#define MI2C_INT_PRI_4 0xFFCF
#define MI2C_INT_PRI_3 0xFFBF
#define MI2C_INT_PRI_2 0xFFAF
#define MI2C_INT_PRI_1 0xFF9F
#define MI2C_INT_PRI_0 0xFF8F

/* Master I2C Interrupt Enable/Disable */

#define MI2C_INT_ON 0xFFFF
#define MI2C_INT_OFF 0xFF7F

/* Macros to Enable/Disable interrupts and set Interrupt priority of SI2C module*/
#define EnableIntSI2C asm("BSET IEC0,#13")
#define DisableIntSI2C asm("BCLR IEC0,#13")
#define SetPriorityIntSI2C(priority) (IPC3bits.SI2CIP = priority)

/* Macros to Enable/Disable interrupts and set Interrupt priority of MI2C module*/
#define EnableIntMI2C asm("BSET IEC0,#14")
#define DisableIntMI2C asm("BCLR IEC0,#14")
#define SetPriorityIntMI2C(priority) (IPC3bits.MI2CIP = priority)


/* I2C module Converter Function Prototypes */

void AckI2C(void) __attribute__ ((section (".libperi")));

void CloseI2C(void) __attribute__ ((section (".libperi")));

void ConfigIntI2C(unsigned int) __attribute__ ((section (".libperi")));

char DataRdyI2C(void) __attribute__ ((section (".libperi")));

#define SlavegetcI2C SlaveReadI2C

#define MastergetcI2C MasterReadI2C

unsigned int SlavegetsI2C(unsigned char *, unsigned int) __attribute__ ((section (".libperi")));

unsigned int MastergetsI2C(unsigned int length, unsigned char * rdptr, unsigned int i2c_data_wait) __attribute__ ((section (".libperi")));

void IdleI2C(void) __attribute__ ((section (".libperi")));

void NotAckI2C(void) __attribute__ ((section (".libperi")));

void OpenI2C(unsigned int,unsigned int) __attribute__ ((section (".libperi")));

unsigned int MasterputsI2C(unsigned char *) __attribute__ ((section (".libperi")));

unsigned int SlaveputsI2C(unsigned char *) __attribute__ ((section (".libperi")));

#define SlaveputcI2C SlaveWriteI2C

#define MasterputcI2C MasterWriteI2C

unsigned char SlaveReadI2C(void) __attribute__ ((section (".libperi")));

unsigned char MasterReadI2C(void) __attribute__ ((section (".libperi")));

void SlaveWriteI2C(unsigned char) __attribute__ ((section (".libperi")));

char MasterWriteI2C(unsigned char) __attribute__ ((section (".libperi")));

void RestartI2C(void) __attribute__ ((section (".libperi")));

void StartI2C(void) __attribute__ ((section (".libperi")));

void StopI2C(void) __attribute__ ((section (".libperi")));

#endif /*I2C_H */
Post Reply