EmbeddedRelated.com
Forums
The 2024 Embedded Online Conference

RCM 3110

Started by lau_robert July 23, 2004
Hello!
I am a new embedded programmer with the rabbit semiconductor chip.
I just started programming with this processsor about a month ago.
I am currently having problems on one of the programs that consists
of toggling an I/O in order to create a I2C communication.
I looked up the i2c sample code and I am currently using its
structure; however, I think I am not using it probably because I am
succesful at toggleing the I/O, but for some reason I am not
sucessfull on reading this I/O. It seems that they are only working
as output and not as input and output.

The I/Os are a clock and Data. The Data has to be Bidirectional and
the clock only has to be one direction.

I would really appreciated if anyone could help. Thank you very
much and have a nice day. Regards,

Robert


Do you have defines that specify which pins on the Rabbit are clock
and data?

// I2C can use any two bits on Port D (only)
#define I2CSCLBit 4 // Clock
#define I2CSDABit 5 // Data
--- In rabbit-semi@rabb..., "lau_robert" <lau_robert@y...> wrote:
> Hello!
> I am a new embedded programmer with the rabbit semiconductor chip.
> I just started programming with this processsor about a month ago.
> I am currently having problems on one of the programs that consists
> of toggling an I/O in order to create a I2C communication.
> I looked up the i2c sample code and I am currently using its
> structure; however, I think I am not using it probably because I am
> succesful at toggleing the I/O, but for some reason I am not
> sucessfull on reading this I/O. It seems that they are only working
> as output and not as input and output.
>
> The I/Os are a clock and Data. The Data has to be Bidirectional and
> the clock only has to be one direction.
>
> I would really appreciated if anyone could help. Thank you very
> much and have a nice day. > Regards,
>
> Robert




steve,
this is what I have: tell me what you think. with this code i am
able to toggle the I/o but for some odd reason I am not reading the
Data line; therefore, there is an error on the acknowledge send
command.
I hope that makes sense.

rob *THIS WILL BE A TEST FOR THE HUMIDITY TEMP SENSOR, THE FIRST ATTEMPT
IS TO
TRY TO SEND A BIT STRING TO WAKE UP THE DEVICE
THIS DEVICE NEEDS A COMMAND STRING IN ORDER TO REPLY BACK WITH THE
DATA SIGNAL INFORMATION

USE SCL=PD6=CLOCK, SDA=PD7 A. USING THE I2C PORT D PINS */

enum {TEMP, HUMI};

//#class auto
//#use rcm3100.lib

//#define DATA 0 //BIT 6 FROM PORT G
//#define SCK 1 //BIT 7 FROM PORT G
//#class auto
#define NoACK 0
#define ACK 1
//#define DS1 6
#define I2CSCLBit 6 // this defines the clock io on port d
#define I2CSDABit 7 // this defines the data io on port d //this is define in the library. I have change the high and low
int value in the library in order to match this
#define i2c_SCL_H() BitWrPortI(PDDDR, &PDDDRShadow,
1,I2CSCLBit)//SETS CLOCK HIGH BY SENDING 1
#define i2c_SCL_L() BitWrPortI(PDDDR, &PDDDRShadow,
0,I2CSCLBit)//SETS CLOCK LOW BY SENDING 0
#define i2c_SDA_H() BitWrPortI(PDDDR, &PDDDRShadow,
1,I2CSDABit)//SETS DATA BIT HIGH BY SENDING 1
#define i2c_SDA_L() BitWrPortI(PDDDR, &PDDDRShadow,
0,I2CSDABit)//SETS DATA BIT LOW BY SENDING 0
#define i2c_SDA() BitRdPortI
(PDDR,I2CSDABit) //READ DATA BIT (RETURN 1 =HIGH =5V
OR 0)
#define i2c_SCL() BitRdPortI
(PDDR,I2CSCLBit) //READ CLOCK BIT(RETURN 1 =HIGH =5V
OR 0)
//THIS IS THE COMMAND SET FOR THE HUMIDITY SENSOR: //ADDR COMMAND R/W
#define STATUS_REG_W 0X06 // 000 0011 0
#define STATUS_REG_R 0X07 // 000 0011 1
#define MEASURE_TEMP 0X03 // 000 0001 1
#define MEASURE_HUMI 0X05 // 000 0010 0
#define RESET 0X1e // 000 1111 0 void i2c_init();
int i2c_clocks_per_us;

typedef union
{
unsigned int i;
float f;
}
value;
//-------------------------------
------------------------------
nodebug
void i2c_init(void) //this could be an option for initialization
of the of the i/o
{
// Set the bits to tristate and the output to 0. Toggle using
//tristate reg.
// This function affects the port D transfer transfer clock
// Transfer clock on port D is PCLK/2
WrPortI(PDCR,&PDCRShadow,0);
BitWrPortI(PDFR ,&PDFRShadow ,0,I2CSCLBit);
// Set the bits to normal function
BitWrPortI(PDFR ,&PDFRShadow ,0,I2CSDABit);
BitWrPortI(PDDDR,&PDDDRShadow,0,I2CSCLBit);
// Set the bits to input (=0)
BitWrPortI(PDDDR,&PDDDRShadow,0,I2CSDABit);
BitWrPortI(PDDCR,&PDDCRShadow,1,I2CSCLBit);
// Set the bits to Open Drain
BitWrPortI(PDDCR,&PDDCRShadow,1,I2CSDABit);
BitWrPortI(PDDR ,&PDDRShadow ,0,I2CSCLBit);
// Set the outputs to 0, when outputs
BitWrPortI(PDDR ,&PDDRShadow ,0,I2CSDABit);
i2c_clocks_per_us = (int)(19200L*32*freq_divider/1000000L);
if(i2c_clocks_per_us < 3)
{
i2c_clocks_per_us = 3;
}
}

//-------------------------------
-------------------------------
char s_write_byte(unsigned char value)
//-------------------------------
--------------------------------
// write a byte on the Sensibus and checks the acknoledge

{
unsigned char i, error;
error =0;
//i2c_init();
for (i = 0x80; i>0; i/=2) //shift bit for masking

{
if (i & value)

i2c_SDA_H(); //masking value with i, write to sensi-bus
//DATA =1;
else
i2c_SDA_L();
//DATA = 0;

i2c_SCL_H();
//SCK =1;

costate

{
waitfor (DelayMs(50)); //pulswidth of 5 u seconds

}

i2c_SCL_L();
//SCK =0;

} i2c_SDA_H(); //release data-line DATA =1; i2c_SCL_H(); //clk # 9 for ack; clock high

error = i2c_SDA(); //error = BitRdPortI(PDDR, I2CSDABit),
error A
i2c_SCL_L(); //SCK =0
return error; // error =1 in case of no acknowledge

}

//----------------------------
-----------------------------------

char s_read_byte(unsigned char ack)

//----------------------------

//read a byte from the sensibus and gives an acknowledge in case
of "ack=1"

{
unsigned char i, val;
val=0;
//i2c_init();
i2c_SDA_H(); //release data line

for ( i=0x80; i>0; i/=2) //shift bit for masking

{
i2c_SCL_H(); //release data line

//if (DATA)
if (i2c_SDA())
val = ( val | i ); //read bit

i2c_SCL_L(); //release data line

}

BitWrPortI(PDDDR, &PDDDRShadow, !ack, I2CSDABit); //in case
of "ack ==1" pull down DATA-LINE

//DATA = !(ack);
i2c_SCL_H(); //CLK # 9 FOR ACK

costate

{
waitfor (DelayMs(50)); //pulswidth of 5 u seconds

} i2c_SCL_L();
i2c_SDA_H(); // RELEASE THE DATA-LINE

return val; } //-------------------------------
------------
void s_transstart(void)
//-------------------------------
------------

//generatea transmission start
//

// DATA: 1-0-1-1-1
// SCK: 0-1-0-1-0

{

//i2c_init(); i2c_SDA_H();
i2c_SCL_L(); //INITIATE STATE costate

{
waitfor (DelayMs(20)); //pulswidth of 2 u seconds

}

i2c_SCL_H();

costate

{
waitfor (DelayMs(20)); //pulswidth of 2 u seconds

} i2c_SDA_L();

{
waitfor (DelayMs(20)); //pulswidth of 2 u seconds

} i2c_SCL_L();

costate

{
waitfor (DelayMs(50)); //pulswidth of 5 u seconds

}

i2c_SCL_H();

costate

{
waitfor (DelayMs(20)); //pulswidth of 2 u seconds

}

i2c_SDA_H();

costate

{
waitfor (DelayMs(20)); //pulswidth of 2 u seconds

}

i2c_SCL_L();
} //-------------------------------
-----------
void s_connectionreset(void)
//-------------------------------
---
// communication reset: Data -line=1 and at least 9 sck cycles
followed by transstart

{
unsigned char i;
//i2c_init();

i2c_SDA_H(); //Initial state
i2c_SCL_L();
for (i=0; i<9; i++) //9 sck cycles {
i2c_SCL_H();
i2c_SCL_L();

}

s_transstart(); //transmision start }

//-------------------------------
-----------
char s_softreset(void)

//-------------------------------
---

//reset the sensor by a softreset {
unsigned char error;

error=0;
s_connectionreset(); //reset communication
error+=s_write_byte(RESET); //send RESET-COMMAND TO
SENSOR
return error; //error=1 in case of no
response from the sensor } //-------------------------------
-------
char s_read_statusreg(unsigned char *p_value, unsigned char
*p_checksum)
//-------------------------------
---------
//reads the status register with checksum (8 bit) {
unsigned char error;

error=0; s_transstart(); //transmission start
error=s_write_byte(STATUS_REG_R); //send command to sensor
*p_value = s_read_byte(ACK); //read status register(8 bit)
*p_checksum=s_read_byte(NoACK); //read checksum (8 bit)
return error; //error =1 in case of no
response from the sensor

}

//-------------------------------
----------
char s_write_statusreg(unsigned char*p_value)
//-------------------------------
----------------------------------
//write the status register with checksum (8 bit)

{

unsigned int error;
error=0;

s_transstart(); //transmission start
error+=s_write_byte(STATUS_REG_W); //send command to sensor
error+=s_write_byte(*p_value); //send value of status
register
return error; // error >=1 in case of no
response from the sensor }

//-------------------------------
--------------
char s_measure (unsigned char *p_value, unsigned char *p_checksum,
unsigned char mode)
//-------------------------------
--------------------
// make a measurement (humidity/temperature)with checksum {
unsigned char error;
unsigned int i;

error =0;

s_transstart (); //transmission start
//i2c_init();
switch (mode) //send command to sensor {
case TEMP : error+=s_write_byte(MEASURE_TEMP);
break;

case HUMI: error+=s_write_byte(MEASURE_HUMI);
break;

default : break;

}

for (i =0; i <65535; i++)
//if (DATA ==0)

if (i2c_SDA()==0)
break; //wait until sensor has finished the
measurements
//if (DATA)
if (i2c_SDA()) //BitRdPortI
(PDDR,I2CSDABit))
error+=1; //or timeout (~2usec) is reached

*(p_value) =s_read_byte(ACK); //READ THE FIRST BYTE
(MSB)
*(p_value+1) =s_read_byte(ACK); //read the second byte
(LSB)
*p_checksum =s_read_byte(NoACK); //read checksum
return error;

}

//------------------------------
------
//skip the UART for now
//------------------------------
-----

//------------------------------
---
void calc_sth11(float *p_humidity, float *p_temperature)
//-------------------------------
---

//calculates temperature [c] and humidity [%RH]
//input : humi [ticks] (12 bit)
// temp [ticks] (14 bit)

//output: humi [%RH]
// temp [c]

{
float C1;//= -4.0;
float C2;//= 0.0405;
float C3;//=-0.0000028;
float T1;//=0.01;
float T2;//=0.00008;
float rh;//=*p_humidity;
float t;//=*p_temperature;
float rh_lin;
float rh_true;
float t_C;

rh=*p_humidity;
t=*p_temperature;

C1= -4.0;
C2= 0.0405;
C3=-0.0000028;
T1=0.01;
T2=0.00008; t_C=t*0.01-40;
rh_linrh*rh+C2*rh+C1;
rh_true=(t_C-25)*(T1+T2*rh)+rh_lin;
if(rh_true>100)rh_true0;
if(rh_true<0.1)rh_true=0.1;

*p_temperature=t_C;
*p_humidity=rh_true;

}

//----------------------------
------

float calc_dewpoint(float h, float t)
//---------------------------
----
//calculate dew point
//input humidity [%rh], temp [c]
//output: dew point [C]

{
float logEx, dew_point;
logEx = 0.66077+7.5*t/(237.3+t)+(log10(h)-2);
dew_point = (logEx -0.66077)*237.3/(0.66077+7.5-logEx);
return dew_point; } //---------------------------
---------------

void main ()
/*sample program that shows how to use sht11 functions
1. connection reset
2. measure humidity 12 bits and temp 14 bits
3. calculates dew point
4. print temp, humidity , dew point note: SE SCL=PD6=CLOCK, SDA=PD7 A. USING THE I2C PORT D PINS */

{
//clock for the sensi bus
value humi_val, temp_val;
float dew_point;
unsigned char error, checksum;
unsigned int i;

//brdInit();

s_connectionreset();
//i2c_init();
//printf("error is:%d\n", error); i2c_SDA_L(); //masking value with i, write to sensi-bus
i2c_SCL_L(); //clock for the sensi bus
//printf("finished\r\n");
while(1)

{
//BitWrPortI(PGDR, &PGDRShadow, 0, DS1);
//printf("while1\n");
error =0;
//printf("while2\n");
error+=s_measure((unsigned char*)
&humi_val.i,&checksum,HUMI);
//printf("measure humidity=%d\n",humi_val.i);
error+=s_measure((unsigned char*)
&temp_val.i,&checksum,TEMP);
printf("error= %d,TEMP= %d\n\n\n\n",error,TEMP);

if (error!=0)

{
s_connectionreset();
//printf("reset4");
//BitWrPortI(PGDR, &PGDRShadow, 0, DS1);
// printf("reset3\n\n\n\n");

}

else

{ //printf("while7\n\n");
humi_val.f=(float) humi_val.i;
//printf("humi");
temp_val.f=(float) temp_val.i;
//printf("temp");
calc_sth11(&humi_val.f, &temp_val.f);
//printf("calc");
dew_pointc_dewpoint
(humi_val.f, temp_val.f);
//printf("dew");
printf("temp:%5.1fC\n humi:%5.1f%%\n dew point: %
5.1fC\n\n\n\n", temp_val.f, humi_val.f, dew_point);
//printf("%d\n",error);

}
//BitWrPortI(PGDR, &PGDRShadow, 1, DS1);
//------------ wait for .8s to avoid heating up shtxx------
--

for (i=0; i<40000;i++);

} }




The 2024 Embedded Online Conference