I'm kinda new with MikroC (but not in the C programming) and i need help converting my previously (working) driver (from CCS C).
Please take the time to take a look :D
Original working driver :
Code: Select all
///////////////////////////////////////////////////////////////////////////////
// //
// Driver file for SHT75 Temperature & Humidity Sensor //
// //
// ***** To initialise SHT75 sensor upon power up ***** //
// //
// Function : sht_init() //
// Return : none //
// //
// //
// ***** To measure and caluculate SHT75 temp & real RH ***** //
// //
// Function : sht_rd (temp, truehumid) //
// Return : temperature & true humidity in float values //
// //
///////////////////////////////////////////////////////////////////////////////
#define sht_data_pin PIN_C2 //Jaune
#define sht_clk_pin PIN_C1 // Orange
//***** Function to alert SHT75 *****
void comstart (void)
{
output_float(sht_data_pin); //data high
output_low(sht_clk_pin); //clk low
delay_us(1);
output_high(sht_clk_pin); //clk high
delay_us(1);
output_low(sht_data_pin); //data low
delay_us(1);
output_low(sht_clk_pin); //clk low
delay_us(2);
output_high(sht_clk_pin); //clk high
delay_us(1);
output_float(sht_data_pin); //data high
delay_us(1);
output_low(sht_clk_pin); //clk low
}
//***** Function to write data to SHT75 *****
int1 comwrite (int8 iobyte)
{
int8 i, mask = 0x80;
int1 ack;
//Shift out command
delay_us(4);
for(i=0; i<8; i++)
{
output_low(sht_clk_pin); //clk low
if((iobyte & mask) > 0) output_float(sht_data_pin); //data high if MSB high
else output_low(sht_data_pin); //data low if MSB low
delay_us(1);
output_high(sht_clk_pin); //clk high
delay_us(1);
mask = mask >> 1; //shift to next bit
}
//Shift in ack
output_low(sht_clk_pin); //clk low
delay_us(1);
ack = input(sht_data_pin); //get ack bit
output_high(sht_clk_pin); //clk high
delay_us(1);
output_low(sht_clk_pin); //clk low
return(ack);
}
//***** Function to read data from SHT75 *****
int16 comread (void)
{
int8 i;
int16 iobyte = 0;
const int16 mask0 = 0x0000;
const int16 mask1 = 0x0001;
//shift in MSB data
for(i=0; i<8; i++)
{
iobyte = iobyte << 1;
output_high(sht_clk_pin); //clk high
delay_us(1);
if (input(sht_data_pin)) iobyte |= mask1; //shift in data bit
else iobyte |= mask0;
output_low(sht_clk_pin); //clk low
delay_us(1);
}
//send ack 0 bit
output_low(sht_data_pin); //data low
delay_us(1);
output_high(sht_clk_pin); //clk high
delay_us(2);
output_low(sht_clk_pin); //clk low
delay_us(1);
output_float(sht_data_pin); //data high
//shift in LSB data
for(i=0; i<8; i++)
{
iobyte = iobyte << 1;
output_high(sht_clk_pin); //clk high
delay_us(1);
if (input(sht_data_pin)) iobyte |= mask1; //shift in data bit
else iobyte |= mask0;
output_low(sht_clk_pin); //clk low
delay_us(1);
}
//send ack 1 bit
output_float(sht_data_pin); //data high
delay_us(1);
output_high(sht_clk_pin); //clk high
delay_us(2);
output_low(sht_clk_pin); //clk low
return(iobyte);
}
//***** Function to wait for SHT75 reading *****
void comwait (void)
{
int16 sht_delay;
output_float(sht_data_pin); //data high
output_low(sht_clk_pin); //clk low
delay_us(1);
for(sht_delay=0; sht_delay<30000; sht_delay++) // wait for max 300ms
{
if (!input(sht_data_pin)) break; //if sht_data_pin low, SHT75 ready
delay_us(10);
}
}
//***** Function to reset SHT75 communication *****
void comreset (void)
{
int8 i;
output_float(sht_data_pin); //data high
output_low(sht_clk_pin); //clk low
delay_us(2);
for(i=0; i<9; i++)
{
output_high(sht_clk_pin); //toggle clk 9 times
delay_us(2);
output_low(sht_clk_pin);
delay_us(2);
}
comstart();
}
//***** Function to soft reset SHT75 *****
void sht_soft_reset (void)
{
comreset(); //SHT75 communication reset
comwrite(0x1e); //send SHT75 reset command
delay_ms(15); //pause 15 ms
}
//***** Function to measure SHT75 temperature *****
int16 measuretemp (void)
{
int1 ack;
int16 iobyte;
comstart(); //alert SHT75
ack = comwrite(0x03); //send measure temp command and read ack status
if(ack == 1) return;
comwait(); //wait for SHT75 measurement to complete
iobyte = comread(); //read SHT75 temp data
return(iobyte);
}
//***** Function to measure SHT75 RH *****
int16 measurehumid (void)
{
int1 ack;
int16 iobyte;
comstart(); //alert SHT75
ack = comwrite(0x05); //send measure RH command and read ack status
if(ack == 1) return;
comwait(); //wait for SHT75 measurement to complete
iobyte = comread(); //read SHT75 temp data
return(iobyte);
}
//***** Function to calculate SHT75 temp & RH *****
/*
void calculate_data (int16 temp, int16 humid, float & tc, float & rhlin, float & rhtrue)
{
float truehumid1, rh;
//calculate temperature reading
tc = ((float) temp * 0.01) - 40.0;
//calculate Real RH reading
rh = (float) humid;
rhlin = (rh * 0.0405) - (rh * rh * 0.0000028) - 4.0;
//calculate True RH reading
rhtrue = ((tc - 25.0) * (0.01 + (0.00008 * rh))) + rhlin;
}
*/
//***** Function to measure & calculate SHT75 temp & RH *****
void sht_rd (int16 & temp, int16 & truehumid)
{
int16 restemp, reshumid;
float realhumid;
restemp = 0; truehumid = 0;
temp = measuretemp(); //measure temp
truehumid = measurehumid(); //measure RH
calculate_data (restemp, reshumid, temp, realhumid, truehumid); //calculate temp & RH*/
}
//***** Function to initialise SHT75 on power-up *****
void sht_init (void)
{
comreset(); //reset SHT75
delay_ms(20); //delay for power-up
}
My adapted version:
Code: Select all
///////////////////////////////////////////////////////////////////////////////
// //
// Driver file for SHT75 Temperature & Humidity Sensor //
// //
// ***** To initialise SHT75 sensor upon power up ***** //
// //
// Function : sht_init() //
// Return : none //
// //
// //
// ***** To measure and caluculate SHT75 temp & real RH ***** //
// //
// Function : sht_rd (temp, truehumid) //
// Return : temperature & true humidity in float values //
// //
///////////////////////////////////////////////////////////////////////////////
#define sht_data_pin PORTB.F6
#define sht_data_pin_io TRISB.F6
#define sht_clk_pin PORTB.F7
//***** Function to alert SHT75 *****
void comstart (void)
{
sht_data_pin_io = 1; //data high
sht_clk_pin = 0; //clk low
delay_us(1);
sht_clk_pin = 1; //clk high
delay_us(1);
sht_data_pin = 0; //data low
delay_us(1);
sht_clk_pin = 0; //clk low
delay_us(2);
sht_clk_pin = 1; //clk high
delay_us(1);
sht_data_pin_io = 1; //data high
delay_us(1);
sht_clk_pin = 0; //clk low
}
//***** Function to write data to SHT75 *****
char comwrite (char iobyte)
{
char ack,i, mask = 0x80;
//Shift out command
delay_us(4);
for(i=0; i<8; i++)
{
sht_clk_pin = 0; //clk low
if((iobyte & mask) > 0) sht_data_pin_io = 1); //data high if MSB high
else sht_data_pin =0; //data low if MSB low
delay_us(1);
sht_clk_pin =1; //clk high
delay_us(1);
mask = mask >> 1; //shift to next bit
}
//Shift in ack
sht_clk_pin =0; //clk low
delay_us(1);
ack = sht_data_pin; //get ack bit
sht_clk_pin = 1; //clk high
delay_us(1);
sht_clk_pin=0; //clk low
return ack;
}
//***** Function to read data from SHT75 *****
unsigned int comread (void)
{
char i;
char iobyte = 0;
const unsigned int mask0 = 0x0000;
const unsigned int mask1 = 0x0001;
//shift in MSB data
for(i=0; i<8; i++)
{
iobyte = iobyte << 1;
sht_clk_pin =1; //clk high
delay_us(1);
if (sht_data_pin) iobyte |= mask1; //shift in data bit
else iobyte |= mask0;
sht_clk_pin=0; //clk low
delay_us(1);
}
//send ack 0 bit
sht_data_pin=0; //data low
delay_us(1);
sht_clk_pin=1; //clk high
delay_us(2);
sht_clk_pin=0; //clk low
delay_us(1);
sht_data_pin_io = 1; //data high
//shift in LSB data
for(i=0; i<8; i++)
{
iobyte = iobyte << 1;
sht_clk_pin=1; //clk high
delay_us(1);
if (sht_data_pin) iobyte |= mask1; //shift in data bit
else iobyte |= mask0;
sht_clk_pin=0; //clk low
delay_us(1);
}
//send ack 1 bit
sht_data_pin_io=1; //data high
delay_us(1);
sht_clk_pin=1; //clk high
delay_us(2);
sht_clk_pin=0; //clk low
return iobyte;
}
//***** Function to wait for SHT75 reading *****
void comwait (void)
{
unsigned int sht_delay;
sht_data_pin_io=1; //data high
sht_clk_pin=0; //clk low
delay_us(1);
for(sht_delay=0; sht_delay<30000; sht_delay++) // wait for max 300ms
{
if (!sht_data_pin) break; //if sht_data_pin low, SHT75 ready
delay_us(10);
}
}
//***** Function to reset SHT75 communication *****
void comreset (void)
{
char i;
sht_data_pin_io=1; //data high
sht_clk_pin=0; //clk low
delay_us(2);
for(i=0; i<9; i++)
{
sht_clk_pin=1; //toggle clk 9 times
delay_us(2);
sht_clk_pin=0;
delay_us(2);
}
comstart();
}
//***** Function to soft reset SHT75 *****
void sht_soft_reset (void)
{
comreset(); //SHT75 communication reset
comwrite(0x1e); //send SHT75 reset command
delay_ms(15); //pause 15 ms
}
//***** Function to measure SHT75 temperature *****
int16 measuretemp (void)
{
char ack;
unsigned int iobyte;
comstart(); //alert SHT75
ack = comwrite(0x03); //send measure temp command and read ack status
if(ack == 1) return;
comwait(); //wait for SHT75 measurement to complete
iobyte = comread(); //read SHT75 temp data
return iobyte;
}
//***** Function to measure SHT75 RH *****
int16 measurehumid (void)
{
char ack;
unsigned int iobyte;
comstart(); //alert SHT75
ack = comwrite(0x05); //send measure RH command and read ack status
if(ack == 1) return;
comwait(); //wait for SHT75 measurement to complete
iobyte = comread(); //read SHT75 temp data
return iobyte;
}
//***** Function to calculate SHT75 temp & RH *****
void calculate_data (unsigned int temp, unsigned int humid, float & tc, float & rhlin, float & rhtrue)
{
float truehumid1, rh;
//calculate temperature reading
tc = ((float) temp * 0.01) - 40.0;
//calculate Real RH reading
rh = (float) humid;
rhlin = (rh * 0.0405) - (rh * rh * 0.0000028) - 4.0;
//calculate True RH reading
rhtrue = ((tc - 25.0) * (0.01 + (0.00008 * rh))) + rhlin;
}
*/
//***** Function to measure & calculate SHT75 temp & RH *****
void sht_rd (unsigned int & temp, unsigned int & truehumid)
{
unsigned int restemp, reshumid;
float realhumid;
restemp = 0; truehumid = 0;
temp = measuretemp(); //measure temp
truehumid = measurehumid(); //measure RH
calculate_data (restemp, reshumid, temp, realhumid, truehumid); //calculate temp & RH*/
}
//***** Function to initialise SHT75 on power-up *****
void sht_init (void)
{
comreset(); //reset SHT75
delay_ms(20); //delay for power-up
}
So far so good. But i get some internal errors when compiling (with MikroC for dspic but anyway similar to mikroc).
1:57 11 ';' expected but ) found sht11.h
1:58 12 Internal error sht11.h
1:58 12 Internal error sht11.h
1:58 4 result is not defined function: comwrite sht11.h
1:58 12 Internal error sht11.h
Some people may ask why i want to port this driver.
I've seen the driver from Puggs ( http://www.mikroe.com/forum/viewtopic.php?p=75323#75323 )
and it doesn't work.
I think his code work up to 40 mhz.
Mine is running at 117.92mhz (Dspic30f6015 PLL 16x).
Any clues ?
Many thanks!
Best Regards,
Laurent