Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Need Help For: Humidity Sensor HSM20G to PIC18F4520

Status
Not open for further replies.

Ayie29

New Member
My circuit is constructed as below. I'm a new beginner in programming. Desperately need help for the programming of this system. I have modified coding from similar project, but it keep on getting syntax error. Need help in modifying the AVR code for HSM20G into PIC code for HSM20G. Facing syntax error in rx part.

View attachment 61677

Code:
// LCD module connections
#include  <p18f4520.h>
#define LCD_RS RB2_bit;
#define LCD_EN RB3_bit;
#define LCD_D4 RB4_bit;
#define LCD_D5 RB5_bit;
#define LCD_D6 RB6_bit;
#define LCD_D7 RB7_bit;
#define LCD_RS_Direction TRISB2_bit;
#define LCD_EN_Direction TRISB3_bit;
#define LCD_D4_Direction TRISB4_bit;
#define LCD_D5_Direction TRISB5_bit;
#define LCD_D6_Direction TRISB6_bit;
#define LCD_D7_Direction TRISB7_bit;

void pic_init(void);
void setup_MCU();
void display_common();
float read_voltage(unsigned short channel);
void convert_for_segments(unsigned long raw_data);
float read_humidity();
float read_temperature();
void display_data(unsigned char x, unsigned char y, float value);


void main()
{
     pic_init();
     setup_MCU();
     display_common();
     while(1)
     {
         display_data(1,2,read_temperature());
         display_data(1,4,read_humidity());
     }
}

void pic_init()
{
      TRISA=0b00000011;
      TRISB=0b00000000;
      TRISC=0b00000000;
      TRISD=0b00000000;
      TRISE=0b00000000;
      PORTA=0b00010000;
      PORTB=0b00000000;
      PORTC=0b00000000;
      PORTD=0b00000000;
      PORTE=0b00000000;
}

void setup_MCU()
{
     LCD_Init();
     ADC_Init();
     Lcd_Cmd(_LCD_CLEAR);
     Lcd_Cmd(_LCD_CURSOR_OFF);
     delay_ms(500);
}


void display_common()
{
     unsigned char disp1[] = "Temperature:";
     unsigned char disp2[] = "Relative Humidity:";
     unsigned char disp3[] = "C";
     unsigned char disp4[] = "%";
     Lcd_Out(1,1,disp1);
     Lcd_Out(2,7,176);
     Lcd_Out(2,7,disp3);
     Lcd_Out(3,1,disp2);
     Lcd_Out(4,6,disp4);
}


float read_voltage(unsigned short channel)
{
      register unsigned long raw_v = 0;
      rx float vin = 0;
      unsigned short samples = 0;

      for(samples = 0; samples < 64; samples++)
      {
       raw_v += adc_read(channel);
      }
      raw_v >>= 6;
      vin = (raw_v * 0.0048875);
      return vin;
}


float read_humidity()
{
    register float HSM_20G = 0;
    register float v = 0;
    v = read_voltage(0);
    HSM_20G = ((3.71 * v * v * v) - (20.65 * v * v) + (64.81 * v) - 27.44);
    return HSM_20G;
}


float read_temperature()
{
    register float HSM_20G = 0;
    register float v = 0;
    v = read_voltage(1);
    HSM_20G = ((5.26 * v * v * v) - (27.34 * v * v) + (68.87 * v) - 17.81);
    return HSM_20G;
}


void display_data(unsigned char x, unsigned char y, float value)
  {
    unsigned char c = 0;
    register unsigned long conv = 0;
    conv = (value * 100.0);
    c = (conv * 0.001);
    Lcd_Chr(y, x, 48 + c);
    c = ((conv / 100) % 10);
    Lcd_Chr_CP(48 + c);
    Lcd_Chr_CP('.');
    c = ((conv / 10) % 10);
    Lcd_Chr_CP(48 + c);
    c = (conv % 10);
    Lcd_Chr_CP(48 + c);
    delay_ms(100);
  }
 
Last edited:
Need help for the coding. Using MPLAB 8.80 for the coding compiler. Get syntax error.
 
Last edited:
I suggest you start by trying to blink an LED. Once that is working try to get "Hello World!" on the LCD. In other words, take small steps.

Mike.
 
Code:
#include <p18f4520.h>
void Init_LCD(void);
void W_ctr_8bit(char);
void W_data_8bit(char);
void Delay_1kcyc(void);

#define LCD_DATA   PORTD
#define LCD_RS       PORTBbits.RB1
#define LCD_E         PORTBbits.RB2

unsigned char LCD_TEMP, i,j,k,m;
char MESS[8] = "WELCOME";

void main ()
{

           ADCON1=0x0F;
           TRISB = 0b11111001;
           TRISD = 0;

           Init_LCD();
           for (i=0; i<7; i++)
           W_data_8bit(MESS(i));
           while(1);
}

void Init_LCD ()
{
           W_ctr_8bit(0b00111000);
           W_ctr_8bit(0b00001100);
           W_ctr_8bit(0b00000110);
           W_ctr_8bit(0b00000001);
           W_ctr_8bit(0x02);
}

void W_ctr_8bit(char x)
{
           LCD_RS           = 0;
           LCD_E             = 1;
           LCD_DATA       = x;
           LCD_E             = 0;
           Delay_1kcyc();
}

void W_data_8bit(char x)
{
           LCD_RS           = 1;
           LCD_E             = 1;
           LCD_DATA       = x;
           LCD_E             = 0;
           Delay_1kcyc();
}

void Delay_1kcyc()
{
          unsigned int j;
          for(j=0, j<32; j++);
}

This is the source code that i found for the Welcome word. But i get problem in build the coding. keep on getting syntax error. Using MPLAB IDE 8.80.
 
Zip up and post all the project files or indicate which line the syntax error is on.

Mike.
 
Status
Not open for further replies.

Latest threads

Back
Top