C++ in Arduino

I bought a co2, tvoc, temp humidity all in one sensor LGAQS-HT01 by Logoele that has CCS811, they supplied a code in C but I am not used to C and am a sophomore in Arduino. Can anyone help in converting the code to get all 4 values? I have used the Sparkfun examples for CCS811 but they are designed to give co2, tvoc values but not temp and humidity.

参考程序-2.zip (2.28 KB)

Please post the code here, using code tags when you do.

uint8_t sta ;

u8 Tem_Low;
u8 Tem_High ;
u8 Hum_Low ;
u8 Hum_High;


u8 CO2_High;
u8 CO2_Low;
u8 TVOC_High;
u8 TVOC_Low ;


unsigned int CO2;
unsigned int TVOC;
unsigned int Hum;
unsigned int Tem;


unsigned int CO2_value; //´æ´¢×ª»¯ºóµÄÖµ¡£
unsigned int TVOC_value;
float Hum_value;
 float Tem_value;





void CCS811_Init(void)
{
IIC_Init();

}


u8 CCS_reg_readOneByte(u8 reg)
{  
u8 result=0; 
unsigned int N,ack;
N = 500;

IIC_Start();  
IIC_Send_Byte(0XB4);  
IIC_Wait_Ack(); 

IIC_Send_Byte(reg);                                   
IIC_Wait_Ack(); 


delay_us(6000); 

IIC_Start();     
IIC_Send_Byte(0XB5);        


ack=IIC_Wait_Ack(); 

while(ack && N) 
{
IIC_Start();     
IIC_Send_Byte(0XB5);                     
ack=IIC_Wait_Ack(); 
delay_us(60);
N--;
} 
delay_us(6000);
result=IIC_Read_Byte(0);   

return result;
}


u32 CCS_reg_readLenByte(u8 reg,u8 Len)
{   
u8 t;
u32 temp=0;
IIC_Start(); 
IIC_Send_Byte(0XB4);   
IIC_Wait_Ack();
IIC_Send_Byte(reg);                                   
IIC_Wait_Ack(); 

IIC_Start();     
IIC_Send_Byte(0XB5);             
IIC_Wait_Ack(); 

for(t=0;t<Len;t++)
{
temp<<=8;
temp+=IIC_Read_Byte(0);   
}


IIC_Stop(); 

return temp;    
}






void CCS_reg_write(u8 reg,u8 data ) 
{                                 

IIC_Start();               
IIC_Send_Byte(0XB4);  
IIC_Wait_Ack();
delay_us(6000);

IIC_Send_Byte(reg); 
IIC_Wait_Ack();   
delay_us(6000);     
IIC_Send_Byte(data);       
IIC_Wait_Ack();         

delay_ms(1); 
}



void CSS811_APP_START(void )                         
{ 
IIC_Start();               
IIC_Send_Byte(0XB4);
IIC_Wait_Ack(); 
delay_ms(4);
IIC_Send_Byte(C_APP_START);
IIC_Wait_Ack(); 
delay_ms(6); 

}




////////////////////////////////////////////////////////////////////
u8 Send_Two_Byte(u8 x,u8 y)
{  


IIC_Start();  
IIC_Send_Byte(x);    
IIC_Wait_Ack(); 
delay_ms(6);delay_ms(6);
IIC_Send_Byte(y);                                  
IIC_Wait_Ack(); 

}

void Get_CO2_TVOC_4_Byte()
{
unsigned int d ;
unsigned int N,ack;
N = 500;

IIC_Start();     
IIC_Send_Byte(0XB5);         
ack=IIC_Wait_Ack(); 


while(ack && N) 
{
IIC_Start();     
IIC_Send_Byte(0XB5);         

ack=IIC_Wait_Ack(); 
delay_us(60);
N--;
} 


delay_us(1000);
CO2_High =IIC_Read_Byte(1);delay_us(10);
CO2_Low = IIC_Read_Byte(1);delay_us(10);

TVOC_High = IIC_Read_Byte(1);delay_us(10);
TVOC_Low = IIC_Read_Byte(0);delay_us(10);


CO2=CO2_High;CO2=CO2<<8;
CO2=CO2|CO2_Low;

TVOC=TVOC_High;TVOC=TVOC<<8;
TVOC=TVOC|TVOC_Low;

IIC_Stop();             

}



void Get_TVOC_2_Byte()
{
unsigned int N,ack;
N = 500;

IIC_Start();     

IIC_Send_Byte(0x81);   
ack=IIC_Wait_Ack(); 

while(ack && N) 
{
IIC_Start();     

IIC_Send_Byte(0x81);   
ack=IIC_Wait_Ack(); 
delay_us(60);
N--;
} 

TVOC_High = IIC_Read_Byte(0);
TVOC_Low = IIC_Read_Byte(1);

CO2=CO2_High;CO2=CO2<<8;
CO2=CO2|CO2_Low;

TVOC=TVOC_High;TVOC=TVOC<<8;
TVOC=TVOC|TVOC_Low;

IIC_Stop();           

}




void Get_Hum_Value()

{
unsigned int d ;
unsigned int N,ack;
N = 500;

IIC_Start();     
IIC_Send_Byte(0x81);                     
ack=IIC_Wait_Ack(); 

while(ack && N) 
{
IIC_Start();     
IIC_Send_Byte(0x81);                       
ack=IIC_Wait_Ack(); 
delay_us(60);
N--;
}

Hum_High =IIC_Read_Byte(1);
Hum_Low = IIC_Read_Byte(0);



IIC_Stop();            

Hum=Hum_High;
Hum=(Hum<<8)&0xff00;
Hum=Hum|Hum_Low;


}


void Get_Tem_Value()

{
unsigned int r ;
unsigned int N,ack;
N = 500;

IIC_Start();     
IIC_Send_Byte(0x81);                         
ack=IIC_Wait_Ack(); 

while(ack && N) 
{
IIC_Start();     
IIC_Send_Byte(0x81);                   
ack=IIC_Wait_Ack(); 
delay_us(60);
N--;
}

Tem_High =IIC_Read_Byte(1);

Tem_Low = IIC_Read_Byte(0);

IIC_Stop();            

Tem=Tem_High;
Tem=(Tem<<8)&0xff00;
Tem=Tem|Tem_Low;


  }


void CCS_811_rest()
{

CCS811_Init();delay_ms(30);

GPIO_ResetBits(GPIOB,GPIO_Pin_9 );////// HW REST//////À­µÍ>20us,then set high////////////////////////
delay_us(300);
GPIO_SetBits(GPIOB,GPIO_Pin_9 );

delay_ms(1000);

CSS811_APP_START( ); //0xf4
IIC_Stop();  
delay_ms(300); 

CCS_reg_readOneByte(0x20); 
IIC_Stop(); 
delay_ms(200);

CCS_reg_write(C_MEAS_MODE,0x10); 
IIC_Stop();
delay_ms(1000);

sta=CCS_reg_readOneByte(0x00);delay_ms(1); 
IIC_Stop(); 
delay_ms(200);

delay_ms(2);


}

Please modify your post and use the code button </> so your code looks like this and is easy to copy to a text editor. See How to use the Forum Your code is too long for me to study quickly without copying to a text editor.

…R

 int main(void)
 {	

			delay_init();	 //ÑÓʱº¯Êý³õʼ»¯

			///////////CCS811///////////
			CCS811_Init();delay_ms(30);

			GPIO_ResetBits(GPIOB,GPIO_Pin_9 );//////	HW REST//////À­µÍ>20us,then set high////////////////////////
			delay_us(300);
			GPIO_SetBits(GPIOB,GPIO_Pin_9 );

			delay_ms(1000);

			CSS811_APP_START( );	//0xf4
			IIC_Stop();  
			delay_ms(300);	

			CCS_reg_readOneByte(0x20);//delay_ms(1); //Ó¦´ð //read atatus register is valid  
			IIC_Stop();//²úÉúÒ»¸öÍ£Ö¹Ìõ¼þ
			delay_ms(200);

			CCS_reg_write(C_MEAS_MODE,0x10); 
			IIC_Stop();
			delay_ms(1000);
		
			sta=CCS_reg_readOneByte(0x00);delay_ms(1); //Ó¦´ð //read atatus register is valid  
			IIC_Stop();//²úÉúÒ»¸öÍ£Ö¹Ìõ¼þ
			delay_ms(200);

	while(1)
	{
		
 	
				if(sta!= 0x98)
				{  


					CCS_811_rest();delay_ms(1000);	

					sta=0;delay_us(1);
					sta=CCS_reg_readOneByte(0x00);//Ó¦´ð //read atatus register is valid 
          delay_ms(1); 					
					IIC_Stop();
					delay_ms(200);

				}
 
							
		  if(sta==0x98))  //CCS811״̬Õý³£
					{	
								
					sta=0;delay_us(1);
					sta=CCS_reg_readOneByte(0x00);delay_ms(1); //Ó¦´ð //read atatus register is valid
					IIC_Stop();//²úÉúÒ»¸öÍ£Ö¹Ìõ¼þ
					delay_ms(200);	

					Send_Two_Byte(0xB4,0x02);//Get tvoc and co2 value
					delay_ms(1);
					IIC_Stop();//²úÉúÒ»¸öÍ£Ö¹Ìõ¼þ
					delay_ms(1);
					Get_CO2_TVOC_4_Byte();//¸Ä
					delay_ms(80);

					Send_Two_Byte(0x80,0xF5);//Get humidity  value
					IIC_Stop();//²úÉúÒ»¸öÍ£Ö¹Ìõ¼þ
					Get_Hum_Value();
					delay_ms(20);

					Send_Two_Byte(0x80,0xF3);//   Get temperature  value
					IIC_Stop();//²úÉúÒ»¸öÍ£Ö¹Ìõ¼þ
					Get_Tem_Value();
					delay_ms(5);

					Hum_value = (float)Hum*0.0190735 - 60;
							 
					Tem_value = (float)Tem*0.0268127 - 468.5;

					CO2_value=CO2;
					TVOC_value=TVOC;

				}
					delay_ms(1000);
			
 }

}
uint8_t sta ;

 u8 Tem_Low;
 u8 Tem_High ;
 u8 Hum_Low ;
 u8 Hum_High;
 
 
 u8 CO2_High;
 u8 CO2_Low;
 u8 TVOC_High;
 u8 TVOC_Low ;
 
 
 unsigned int CO2;
 unsigned int TVOC;
 unsigned int Hum;
 unsigned int Tem;
 
 
 unsigned int CO2_value; //´æ´¢×ª»¯ºóµÄÖµ¡£
 unsigned int TVOC_value;
 float Hum_value;
  float Tem_value;




 
 void CCS811_Init(void)
 {
 IIC_Init();
 
 }

 
 u8 CCS_reg_readOneByte(u8 reg)
 {  
 u8 result=0; 
 unsigned int N,ack;
 N = 500;

 IIC_Start();  
 IIC_Send_Byte(0XB4);  
 IIC_Wait_Ack(); 

 IIC_Send_Byte(reg);                                   
 IIC_Wait_Ack(); 


 delay_us(6000); 

 IIC_Start();     
 IIC_Send_Byte(0XB5);        

 
 ack=IIC_Wait_Ack(); 

 while(ack && N) 
 {
 IIC_Start();     
 IIC_Send_Byte(0XB5);                     
 ack=IIC_Wait_Ack(); 
 delay_us(60);
 N--;
 } 
 delay_us(6000);
 result=IIC_Read_Byte(0);   

 return result;
 }


 u32 CCS_reg_readLenByte(u8 reg,u8 Len)
 {   
 u8 t;
 u32 temp=0;
 IIC_Start(); 
 IIC_Send_Byte(0XB4);   
 IIC_Wait_Ack();
 IIC_Send_Byte(reg);                                   
 IIC_Wait_Ack(); 
 
 IIC_Start();     
 IIC_Send_Byte(0XB5);             
 IIC_Wait_Ack(); 
 
 for(t=0;t<Len;t++)
 {
 temp<<=8;
 temp+=IIC_Read_Byte(0);   
 }
 
 
 IIC_Stop(); 
 
 return temp;    
 }




 

 void CCS_reg_write(u8 reg,u8 data ) 
 {                                 
 
 IIC_Start();               
 IIC_Send_Byte(0XB4);  
 IIC_Wait_Ack();
 delay_us(6000);
 
 IIC_Send_Byte(reg); 
 IIC_Wait_Ack();   
 delay_us(6000);     
 IIC_Send_Byte(data);       
 IIC_Wait_Ack();         
 
 delay_ms(1); 
 }



 void CSS811_APP_START(void )                         
 { 
 IIC_Start();               
 IIC_Send_Byte(0XB4);
 IIC_Wait_Ack(); 
 delay_ms(4);
 IIC_Send_Byte(C_APP_START);
 IIC_Wait_Ack(); 
 delay_ms(6); 

 }




 ////////////////////////////////////////////////////////////////////
 u8 Send_Two_Byte(u8 x,u8 y)
 {  
 

 IIC_Start();  
 IIC_Send_Byte(x);    
 IIC_Wait_Ack(); 
 delay_ms(6);delay_ms(6);
 IIC_Send_Byte(y);                                  
 IIC_Wait_Ack(); 

 }

 void Get_CO2_TVOC_4_Byte()
 {
 unsigned int d ;
 unsigned int N,ack;
 N = 500;

 IIC_Start();     
 IIC_Send_Byte(0XB5);         
 ack=IIC_Wait_Ack(); 
 

 while(ack && N) 
 {
 IIC_Start();     
 IIC_Send_Byte(0XB5);         

 ack=IIC_Wait_Ack(); 
 delay_us(60);
 N--;
 } 

 
 delay_us(1000);
 CO2_High =IIC_Read_Byte(1);delay_us(10);
 CO2_Low = IIC_Read_Byte(1);delay_us(10);

 TVOC_High = IIC_Read_Byte(1);delay_us(10);
 TVOC_Low = IIC_Read_Byte(0);delay_us(10);


 CO2=CO2_High;CO2=CO2<<8;
 CO2=CO2|CO2_Low;

 TVOC=TVOC_High;TVOC=TVOC<<8;
 TVOC=TVOC|TVOC_Low;

 IIC_Stop();             

 }
 


 void Get_TVOC_2_Byte()
 {
 unsigned int N,ack;
 N = 500;

 IIC_Start();     

 IIC_Send_Byte(0x81);   
 ack=IIC_Wait_Ack(); 

 while(ack && N) 
 {
 IIC_Start();     

 IIC_Send_Byte(0x81);   
 ack=IIC_Wait_Ack(); 
 delay_us(60);
 N--;
 } 

 TVOC_High = IIC_Read_Byte(0);
 TVOC_Low = IIC_Read_Byte(1);

 CO2=CO2_High;CO2=CO2<<8;
 CO2=CO2|CO2_Low;

 TVOC=TVOC_High;TVOC=TVOC<<8;
 TVOC=TVOC|TVOC_Low;

 IIC_Stop();           

 }
 



 void Get_Hum_Value()
 
 {
 unsigned int d ;
 unsigned int N,ack;
 N = 500;

 IIC_Start();     
 IIC_Send_Byte(0x81);                     
 ack=IIC_Wait_Ack(); 

 while(ack && N) 
 {
 IIC_Start();     
 IIC_Send_Byte(0x81);                       
 ack=IIC_Wait_Ack(); 
 delay_us(60);
 N--;
 }

 Hum_High =IIC_Read_Byte(1);
 Hum_Low = IIC_Read_Byte(0);



 IIC_Stop();            

 Hum=Hum_High;
 Hum=(Hum<<8)&0xff00;
 Hum=Hum|Hum_Low;


 }


 void Get_Tem_Value()
 
 {
 unsigned int r ;
 unsigned int N,ack;
 N = 500;

 IIC_Start();     
 IIC_Send_Byte(0x81);                         
 ack=IIC_Wait_Ack(); 

 while(ack && N) 
 {
 IIC_Start();     
 IIC_Send_Byte(0x81);                   
 ack=IIC_Wait_Ack(); 
 delay_us(60);
 N--;
 }

 Tem_High =IIC_Read_Byte(1);

 Tem_Low = IIC_Read_Byte(0);

 IIC_Stop();            

 Tem=Tem_High;
 Tem=(Tem<<8)&0xff00;
 Tem=Tem|Tem_Low;


   }


void CCS_811_rest()
 {

 CCS811_Init();delay_ms(30);

 GPIO_ResetBits(GPIOB,GPIO_Pin_9 );////// HW REST//////À­µÍ>20us,then set high////////////////////////
 delay_us(300);
 GPIO_SetBits(GPIOB,GPIO_Pin_9 );

 delay_ms(1000);

 CSS811_APP_START( ); //0xf4
 IIC_Stop();  
 delay_ms(300); 

 CCS_reg_readOneByte(0x20); 
 IIC_Stop(); 
 delay_ms(200);

 CCS_reg_write(C_MEAS_MODE,0x10); 
 IIC_Stop();
 delay_ms(1000);

 sta=CCS_reg_readOneByte(0x00);delay_ms(1); 
 IIC_Stop(); 
 delay_ms(200);

 delay_ms(2);

 
 }

What are Replies #4 and #5 about? You have not included any explanation.

And you have not put code tags on the code in your Original Post either!

...R

Looks like they're I2C (sometimes written IIC) devices. Should be able to talk to them with the Wire library.

DrAzzy:
Looks like they're I2C (sometimes written IIC) devices. Should be able to talk to them with the Wire library.

They are IIC one at 0x5A for co2 and tvoc and the other at 0x40 for temp and humidity. I used Wire library for data transmission from the registers 0xE3 and 0xE5 for temp and humidity respectively and get unrealistic values of 154 and 118 for temp both. What I wanted originally was a way to get the two programs iic.c and main.c to be used in Arduino , do you know a way?