+ Post New Thread
Results 1 to 17 of 17
  1. #1
    Newbie level 6
    Points: 286, Level: 3

    Join Date
    Mar 2012
    Posts
    13
    Helped
    0 / 0
    Points
    286
    Level
    3

    Need Help For: Humidity Sensor HSM20G to PIC18F4520

    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.

    Click image for larger version. 

Name:	Circuit@PSM(Editted).jpg 
Views:	25 
Size:	64.8 KB 
ID:	70113

    Code:
    // LCD module connections
    #include  
    #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 by Ayie29; 3rd March 2012 at 16:02.

  2. #2
    Junior Member level 1
    Points: 1,468, Level: 8
    Achievements:
    7 years registered

    Join Date
    Sep 2009
    Location
    India
    Posts
    15
    Helped
    0 / 0
    Points
    1,468
    Level
    8

    Re: Need Help For: Humidity Sensor HSM20G to PIC18F4520

    "It doesn't work " will not give you much response . Be specific on what's the error that happens. Does the code compile ? Have you tested your circuit with some simple program ?



  3. #3
    Newbie level 6
    Points: 286, Level: 3

    Join Date
    Mar 2012
    Posts
    13
    Helped
    0 / 0
    Points
    286
    Level
    3

    Re: Need Help For: Humidity Sensor HSM20G to PIC18F4520

    Quote Originally Posted by sunish.net View Post
    "It doesn't work " will not give you much response . Be specific on what's the error that happens. Does the code compile ? Have you tested your circuit with some simple program ?
    What I understand now the coding for PIC and AVR is different. My problem now is modifying the coding from AVR to PIC for the HSM-20G. Facing syntax error in hsm coding part by using MPLAB IDE 8.8.



    •   AltAdvertisment

        
       

  4. #4
    Junior Member level 1
    Points: 1,468, Level: 8
    Achievements:
    7 years registered

    Join Date
    Sep 2009
    Location
    India
    Posts
    15
    Helped
    0 / 0
    Points
    1,468
    Level
    8

    Re: Need Help For: Humidity Sensor HSM20G to PIC18F4520

    PIC and AVR are entirely different micros and the compilers are different. You have not mentioned which compiler you are using in MPLAB. What is the syntax error that you get. Ideally get an LED to blink first before trying compile a bigger program By doing that you can ensure that your IDE is setup right and everything from compiling to programming is right.



  5. #5
    Newbie level 6
    Points: 286, Level: 3

    Join Date
    Mar 2012
    Posts
    13
    Helped
    0 / 0
    Points
    286
    Level
    3

    Re: Need Help For: Humidity Sensor HSM20G to PIC18F4520

    Using MPLAB C18 Compiler. I have tried LED blinking coding. It was succeeded. But now facing error in try show welcome for the LCD. Get syntax error. Copied the coding from others but get error.

    Code:
    #include 
    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));  //get syntax error for this line
               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++);
    }



  6. #6
    Junior Member level 1
    Points: 1,468, Level: 8
    Achievements:
    7 years registered

    Join Date
    Sep 2009
    Location
    India
    Posts
    15
    Helped
    0 / 0
    Points
    1,468
    Level
    8

    Re: Need Help For: Humidity Sensor HSM20G to PIC18F4520

    Which line ? Is C18 the only option ? Look for compilers with built in libraries for lcd.



  7. #7
    Newbie level 6
    Points: 286, Level: 3

    Join Date
    Mar 2012
    Posts
    13
    Helped
    0 / 0
    Points
    286
    Level
    3

    Re: Need Help For: Humidity Sensor HSM20G to PIC18F4520

    Quote Originally Posted by sunish.net View Post
    Which line ? Is C18 the only option ? Look for compilers with built in libraries for lcd.
    I have play with lots of compiler. i using PICKit2 as the programmer and only MPLAB is compatible with this. mikroC can run but cnt run program into my programmer.

    .



  8. #8
    Junior Member level 1
    Points: 1,468, Level: 8
    Achievements:
    7 years registered

    Join Date
    Sep 2009
    Location
    India
    Posts
    15
    Helped
    0 / 0
    Points
    1,468
    Level
    8

    Re: Need Help For: Humidity Sensor HSM20G to PIC18F4520

    Ok if that's the problem, don't worry. Any compiler will produce a hex file as output, just go to File, Import of MP LAB and select the hex file generated by the compiler and then click the prograam ensuring pickit2 is selected as the programmer. BTW have you tried jal ? Its an open source free compiler, with jallib you have libraries for almost everything.



    •   AltAdvertisment

        
       

  9. #9
    Newbie level 6
    Points: 286, Level: 3

    Join Date
    Mar 2012
    Posts
    13
    Helped
    0 / 0
    Points
    286
    Level
    3

    Re: Need Help For: Humidity Sensor HSM20G to PIC18F4520

    Jallib? I will download it and play with it later. But, does jallib also compatible with PICKIT2 or not?



  10. #10
    Administrator
    Points: 69,512, Level: 64
    Achievements:
    Created Album pictures Created Blog entry 7 years registered
    bigdogguru's Avatar
    Join Date
    Mar 2010
    Location
    Southwest, USA
    Posts
    9,832
    Helped
    2355 / 2355
    Points
    69,512
    Level
    64
    Blog Entries
    4

    Re: Need Help For: Humidity Sensor HSM20G to PIC18F4520

    The Microchip C18 does offer LCD Routines.

    The source is available in the following directory:

    C:\Program Files\Microchip\MPLAB C18\src\pmc_common
    I have attached a zip file with the source for your convenience.

    Reference: MPLAB® C18 C COMPILER LIBRARIES, Section:Chapter 3. Software Peripheral Library, Pg. 75

    3.2 EXTERNAL LCD FUNCTIONS

    These functions are designed to allow the control of a Hitachi HD44780 LCD controller
    using I/O pins from a PIC18 microcontroller. The following functions are provided:

    The precompiled versions of these functions use default pin assignments that can be
    changed by redefining the following macro assignments in the file xlcd.h, found in the
    h subdirectory of the compiler installation:
    I have used these libraries successfully numerous times, if you have any issues just post your questions in this thread.


    BigDog
    The Edge... there is no honest way to explain it, because the only people who really know where it is are the ones who have gone over.
    -- Hunter S. Thompson



  11. #11
    Junior Member level 1
    Points: 1,468, Level: 8
    Achievements:
    7 years registered

    Join Date
    Sep 2009
    Location
    India
    Posts
    15
    Helped
    0 / 0
    Points
    1,468
    Level
    8

    Re: Need Help For: Humidity Sensor HSM20G to PIC18F4520

    Any compiler that produces hex file is enough for pickit programmer. Hex file can be imported to mplab and can be used to program the microcontroller. JAL is no exception. There are lots of jal users who happen to be pickit users.



  12. #12
    Newbie level 6
    Points: 286, Level: 3

    Join Date
    Mar 2012
    Posts
    13
    Helped
    0 / 0
    Points
    286
    Level
    3

    Re: Need Help For: Humidity Sensor HSM20G to PIC18F4520

    Quote Originally Posted by bigdogguru View Post
    The Microchip C18 does offer LCD Routines.

    The source is available in the following directory:



    I have attached a zip file with the source for your convenience.

    Reference: MPLAB® C18 C COMPILER LIBRARIES, Section:Chapter 3. Software Peripheral Library, Pg. 75



    I have used these libraries successfully numerous times, if you have any issues just post your questions in this thread.


    BigDog
    I cnt see where is your zip file. How am i going to use that library. Just add into project like add in 18f4520?

    ---------- Post added at 10:43 ---------- Previous post was at 10:39 ----------

    Quote Originally Posted by sunish.net View Post
    Any compiler that produces hex file is enough for pickit programmer. Hex file can be imported to mplab and can be used to program the microcontroller. JAL is no exception. There are lots of jal users who happen to be pickit users.
    Any compiler can produces hex file. But when i want to program using PICKit2, it show succeeded but the program memory is unchanged. remain FFFF. my programmer doesnt program properly the hex file.



  13. #13
    Junior Member level 1
    Points: 1,468, Level: 8
    Achievements:
    7 years registered

    Join Date
    Sep 2009
    Location
    India
    Posts
    15
    Helped
    0 / 0
    Points
    1,468
    Level
    8

    Re: Need Help For: Humidity Sensor HSM20G to PIC18F4520

    Please give the exact steps that you followed in trying to program.
    1.Close all projects (Project menu close)and open files (Close,Close Workspace) in MPLAB.
    2.Go to configure menu and then select the microcontroller that you want to program and click OK.(18f4520 is shown as supported by PICkit2 in MPLAB)
    3.Go to File Menu and select import and select the hex file that you want to program and click open
    4.Go to Programmer Menu,Select Programmer and select PIckit 2.
    5.Click Reconnect or whatever menu option for enabling the programmer
    6.You should see programmer enabled message in mplab output windows
    7.Click Program from Programmer menu and if successfully programmed corresponding message will be indicated in output window.

    Hope this should help you with programming hex file with pickit. When in doubt, please give real details on what goes wrong when you ask.



  14. #14
    Newbie level 6
    Points: 286, Level: 3

    Join Date
    Mar 2012
    Posts
    13
    Helped
    0 / 0
    Points
    286
    Level
    3

    Re: Need Help For: Humidity Sensor HSM20G to PIC18F4520

    Quote Originally Posted by sunish.net View Post
    Please give the exact steps that you followed in trying to program.
    1.Close all projects (Project menu close)and open files (Close,Close Workspace) in MPLAB.
    2.Go to configure menu and then select the microcontroller that you want to program and click OK.(18f4520 is shown as supported by PICkit2 in MPLAB)
    3.Go to File Menu and select import and select the hex file that you want to program and click open
    4.Go to Programmer Menu,Select Programmer and select PIckit 2.
    5.Click Reconnect or whatever menu option for enabling the programmer
    6.You should see programmer enabled message in mplab output windows
    7.Click Program from Programmer menu and if successfully programmed corresponding message will be indicated in output window.

    Hope this should help you with programming hex file with pickit. When in doubt, please give real details on what goes wrong when you ask.
    What i hv done is just same like what u mention above. I had tried to use others succeeded project hex file. but cnt manage put in to my programmer. i mean it show succeeded program into my pic but the value of the data din change at all from the program memory. the program doesnt program exactly the hex file. only led blinking hex file which build success using mplab is able to program into my pic. in other words, it seem like program which fail build in mplab was fail to program into my pic.



  15. #15
    Junior Member level 1
    Points: 1,468, Level: 8
    Achievements:
    7 years registered

    Join Date
    Sep 2009
    Location
    India
    Posts
    15
    Helped
    0 / 0
    Points
    1,468
    Level
    8

    Re: Need Help For: Humidity Sensor HSM20G to PIC18F4520

    Quote Originally Posted by Ayie29 View Post
    What i hv done is just same like what u mention above. I had tried to use others succeeded project hex file. but cnt manage put in to my programmer. i mean it show succeeded program into my pic but the value of the data din change at all from the program memory. the program doesnt program exactly the hex file. only led blinking hex file which build success using mplab is able to program into my pic. in other words, it seem like program which fail build in mplab was fail to program into my pic.
    1) other succeeded project file means what ? which compiler, what project ?
    2)can't put in programmer or do you mean load into PIC ?
    3)when you say program memory, is it program memory of MPLAB or program memory of PIC ?
    4)when you load a hex file like I mentioned above, you don't do a build. You can't do a build because you are just loading the hex file generated outside mplab to just program your PIC using the programmer software of MPLAB.
    5) I can send you a hex file that blinks all port pins high and low. Just try loading it exactly by following the steps given above and tell me if it blinks.
    But your circuit doesn't show that you are using a crystal, are you using internal oscillator ? If so what is the frequency ?
    What was the configuration bits that you set when you imported the hex file ? was it set to Configuration Bits set in code ?
    Without setting the configuration bits right the program will never run.



    •   AltAdvertisment

        
       

  16. #16
    Newbie level 6
    Points: 286, Level: 3

    Join Date
    Mar 2012
    Posts
    13
    Helped
    0 / 0
    Points
    286
    Level
    3

    Re: Need Help For: Humidity Sensor HSM20G to PIC18F4520

    I had construct a power circuit for the pic. using 10MHz crystal. the configuration bit set in code.



  17. #17
    Junior Member level 1
    Points: 1,468, Level: 8
    Achievements:
    7 years registered

    Join Date
    Sep 2009
    Location
    India
    Posts
    15
    Helped
    0 / 0
    Points
    1,468
    Level
    8

    Re: Need Help For: Humidity Sensor HSM20G to PIC18F4520

    Unless you give answers to all questions I will not be able to help. Sorry.



--[[ ]]--