Translate code to arduino code

Hello,

I found a code which is a code for alcohol sensor but i can’t understand it. I want help to translate to arduino code to use it.
Thank you for help.

 #include <pic.h>
#include "lcd.h"
#include <math.h>
__CONFIG(FOSC_INTRCCLK & WDTE_OFF & PWRTE_ON & MCLRE_OFF & CP_OFF &
CPD_OFF);
#define ADC_CH 11
long int g_alcohol;
int g_vol;
char g_dis_buf[10];
void gpio_init(void)
{
TRISB |= 1<<5;
ANSEL=0;
PORTC = 0x00;
PORTA = 0x00;
TRISC = 0x00;
TRISA = 0x00;
}
void ADC_init(void) //Set ADC and ADC collects data
{
ANSELH = 0x08;
ADCON0 = 0x00;
ADCON0 |= ADC_CH<<2;
ADCON1 = 0x50;
ADCON0 |= 1<<0;
ADRESH = 0x00;
ADRESL = 0x00;
}
void ADC_calc(void)
{
long l_tmp;
int w_m;
int w_adc=0;
char i;
for(i=0; i<8; i++){
ADCON0 |= 1<<1;/*set GO for 1*/

while(ADCON0 & (1<<1));
w_m = ADRESH;
w_m <<= 2;
w_m |= ADRESL>>6;
w_adc += w_m;
}
w_adc /= 8;/* divide 8*/
l_tmp = w_adc;
g_vol = (l_tmp*4550.)/1024;/*calc vol*/ //Translate ADC value to voltage
}
void CPU_wait(int n)
{
while(n--);
}
void LCD_str(char x, char y, const char *s)
{
if(y==0)
{x+=0x80;}
else
{x+=0xC0;}
print_instr(x);
while(*s!='\0')
{ print_data(*s);s++;}
}
void display(void)
{ float rs;
double g_logalc;
double abc;
rs = (5000./g_vol-1.)*10000.;/*Calc Rs*/
rs = (rs/5000.);/*0.1 * 10=1*/ //Calculate Rs/Ro
abc = rs;
g_logalc = log(abc);
g_alcohol = 169*(g_logalc*g_logalc*g_logalc*g_logalc)-
147*(g_logalc*g_logalc*g_logalc)+207*(g_logalc*g_logalc)-496*g_logalc+332; //Put
into the formula and obtain concentration
g_dis_buf[0] =g_alcohol/1000%10 + 0x30; // Display the result
g_dis_buf[1] =g_alcohol/100%10 + 0x30;

g_dis_buf[2] =g_alcohol/10%10 + 0x30;
g_dis_buf[3] =g_alcohol %10 + 0x30;
g_dis_buf[4] = '\0';
LCD_str(0,1,"Alcohol:");
LCD_str(8,1,g_dis_buf);
LCD_str(13,1,"PPM");
}
void main(void)
{
gpio_init();
ADC_init();
init_display();
while(1)
{
CPU_wait(50000);
ADC_calc();
display();
}
}

Replace the "main" function with:

void setup()
{
    gpio_init();
    ADC_init();
    init_display();
}

void loop()
{
    CPU_wait(50000); // Im not sure this is needed, but I'll leave it in
    ADC_calc();
    display();
}

Do you have the pic.h library? The Timers may need to be changed too, but Im not sure of that or even what to change them to.

TRISB |= 1<<5;

PIC?

Thanks.

i want to understand this code :

g_dis_buf[0] =g_alcohol/1000%10 + 0x30; // Display the result
g_dis_buf[1] =g_alcohol/100%10 + 0x30;

g_dis_buf[2] =g_alcohol/10%10 + 0x30;
g_dis_buf[3] =g_alcohol %10 + 0x30;
g_dis_buf[4] = '\0';
LCD_str(0,1,"Alcohol:");
LCD_str(8,1,g_dis_buf);
LCD_str(13,1,"PPM");

for example : g_alcohol/1000%10 + 0x30 what's is mean ????

thanks

g_alcohol/1000%10 + 0x30 what's is mean

Take the variable g_alcohol then divide it by 1000 and take the modules of 10. The % means divide but just give back the remainder. Then add to that the hexadecimal number 30 which is 40 in the base 10 ( decimal )

ok thanks

how This code can be used for arduino and LCD display ?

g_dis_buf[0] =g_alcohol/1000%10 + 0x30; // Display the result
g_dis_buf[1] =g_alcohol/100%10 + 0x30;

g_dis_buf[2] =g_alcohol/10%10 + 0x30;
g_dis_buf[3] =g_alcohol %10 + 0x30;
g_dis_buf[4] = '\0';
LCD_str(0,1,"Alcohol:");
LCD_str(8,1,g_dis_buf);
LCD_str(13,1,"PPM");

Then add to that the hexadecimal number 30 which is 40 in the base 10 ( decimal )

48, or ASCII zero.

Yes even my calculator said 48, I just put in 40. Doh!

how This code can be used for arduino and LCD display

No change?

if i want to check the result using the serie monitor of arduino ?????
What code can i use ????

void display(void)
{ float rs;
double g_logalc;
double abc;
rs = (5000./g_vol-1.)*10000.;/*Calc Rs*/
rs = (rs/5000.);/*0.1 * 10=1*/ //Calculate Rs/Ro
abc = rs;
g_logalc = log(abc);
g_alcohol = 169*(g_logalc*g_logalc*g_logalc*g_logalc)-
147*(g_logalc*g_logalc*g_logalc)+207*(g_logalc*g_logalc)-496*g_logalc+332; //Put
into the formula and obtain concentration
g_dis_buf[0] =g_alcohol/1000%10 + 0x30; // Display the result
g_dis_buf[1] =g_alcohol/100%10 + 0x30;

g_dis_buf[2] =g_alcohol/10%10 + 0x30;
g_dis_buf[3] =g_alcohol %10 + 0x30;
g_dis_buf[4] = '\0';
LCD_str(0,1,"Alcohol:");
LCD_str(8,1,g_dis_buf);
LCD_str(13,1,"PPM");
}

if i want to check the result using the serie monitor of arduino

I'd go with Serial.print

Is there something sticking on your '?' key?

Yes with serial.print

Hello,

i have a code but i can’t use it in arduino and i don’t understand the display function. I want to use the code in my arduino and make trye using serial.print.

Thanks.

void LCD_str(char x, char y, const char *s)
{
if(y==0)
{x+=0x80;}
else
{x+=0xC0;}
print_instr(x);
while(*s!='\0')
{ print_data(*s);
s++;}
}

while(ADCON0 & (1<<1));
w_m = ADRESH;
w_m <<= 2;
w_m |= ADRESL>>6;
w_adc += w_m;
}
w_adc /= 8;/* divide 8*/
l_tmp = w_adc;
g_vol = (l_tmp*4550.)/1024;/*calc vol*/ //Translate ADC value to voltage
}
void CPU_wait(int n)
{
while(n--);
}
void LCD_str(char x, char y, const char *s)
{
if(y==0)
{x+=0x80;}
else
{x+=0xC0;}
print_instr(x);
while(*s!='\0')
{ print_data(*s);
s++;}
}
void display(void)
{ float rs;
double g_logalc;
double abc;
rs = (5000./g_vol-1.)*10000.;/*Calc Rs*/
rs = (rs/5000.);/*0.1 * 10=1*/ //Calculate Rs/Ro
abc = rs;
g_logalc = log(abc);
g_alcohol = 169*(g_logalc*g_logalc*g_logalc*g_logalc)-
147*(g_logalc*g_logalc*g_logalc)+207*(g_logalc*g_logalc)-496*g_logalc+332; //Put
into the formula and obtain concentration
g_dis_buf[0] =g_alcohol/1000%10 + 0x30; // Display the result
g_dis_buf[1] =g_alcohol/100%10 + 0x30;
g_dis_buf[2] =g_alcohol/10%10 + 0x30;
g_dis_buf[3] =g_alcohol %10 + 0x30;
g_dis_buf[4] = '\0';
LCD_str(0,1,"Alcohol:");
LCD_str(8,1,g_dis_buf);
LCD_str(13,1,"PPM");
}

It's a bit of a mess, isn't it.

Maybe it would be easier to ignore it and just write your own code?

As you have given no indication of what any of it is supposed to do it is hard to help you.

If you do want to persist with that code at least format it better so it is easier to follow. And better still, post a complete sketch that will compile properly.

...R

Topics merged.

CROSS-POSTING IS A FAST TRACK TO THE SIN BIN.

hello,

i want to understand the following code and how i can use it to my arduino

void display(void)
{ float rs;
double g_logalc;
double abc;
rs = (5000./g_vol-1.)*10000.;/*Calc Rs*/
rs = (rs/5000.);/*0.1 * 10=1*/ //Calculate Rs/Ro
abc = rs;
g_logalc = log(abc);
g_alcohol = 169*(g_logalc*g_logalc*g_logalc*g_logalc)-
147*(g_logalc*g_logalc*g_logalc)+207*(g_logalc*g_logalc)-496*g_logalc+332; //Put
into the formula and obtain concentration
g_dis_buf[0] =g_alcohol/1000%10 + 0x30; // Display the result
g_dis_buf[1] =g_alcohol/100%10 + 0x30;

g_dis_buf[2] =g_alcohol/10%10 + 0x30;
g_dis_buf[3] =g_alcohol %10 + 0x30;
g_dis_buf[4] = '\0';
LCD_str(0,1,"Alcohol:");
LCD_str(8,1,g_dis_buf);
LCD_str(13,1,"PPM");
}

thanks for help.

Have you tried reading some of the answers instead of posting new copies of the same question?

...R

Yes ....R, but i don't understand

" Take the variable g_alcohol then divide it by 1000 and take the modules of 10. The % means divide but just give back the remainder. Then add to that the hexadecimal number 30 which is 40 in the base 10 ( decimal )"

what's mean by take a module of 10 ?
divide but just give back the remainder ?

the code have 4 tabs buf [0] ... buf [4] how the result must be written ?

Thanks.

Then add to that the hexadecimal number 30 which is 40 in the base 10 ( decimal )"

"which is 48 in base 10, or ASCII zero"

I can understand that you may not understand some of the answers but it would help if you ask your subsequent question clearly so we could understand what you don't understand.

The word is modulus and not modules. It is a form of division in which you get the remainder rather than the other part (forget the technical name for it). For example 14 / 4 gives 3 with a remainder of 2. In integer maths on a computer 14 mod 4 gives an answer of 2. It is coded as 14 % 4 in C.

This is often used to get values to go back to 0 - for example if you want to count 0,1,2,3,0,1,2,3 etc because you just want 4 things on each line even though the total count might go up to 24 or 32.

I don't understand the rest of your question.

...R

g_dis_buf[0] =g_alcohol/1000%10 + 0x30; // Display the result
g_dis_buf[1] =g_alcohol/100%10 + 0x30;

g_dis_buf[2] =g_alcohol/10%10 + 0x30;
g_dis_buf[3] =g_alcohol %10 + 0x30;
g_dis_buf[4] = '\0';

This code is taking the variable g_alcohol and converting it into a number of ASCII bytes ready to display.
It is rather a stupid way of doing this but there you are.
A simple:-

Serial.print(g_alcohol);

will do the same thing.