Menu

Cannot get scaling function work: RESOLVED

iDeeW
2019-03-17
2021-02-14
  • iDeeW

    iDeeW - 2019-03-17

    Hi everyboy,

    I just started GCBasic. I wrote some code to read a Microchip MCP3428 ADC chip over i2c.
    Basically my library returns ADC value as an integer value. i2c code works great.
    But when I try to scale, it returns a byte value (0 to 255).
    But I expect a number 0 to 32767. What am I doing wrong here?

        read = MCP342x_convert_oneshot(2, , , err)
        HSerPrintStringCRLF str(read)' here 0 to 32767 value is returned as expected
        read = scale( read, 0, 32767, 0, 3000)
        HSerPrintStringCRLF str(read)' here I see a 0-255 value, not what I expects
    

    Thank you

     
  • Anobium

    Anobium - 2019-03-17

    My guess, a total guess as I do not the code segment about.

    Try adding #option explicit to your USER program not the .h

    Then, I am guessing, but read is a byte.... should be a word?
    read = MCP342x_convert_oneshot(2, , , err) this returns an?
    and,
    read = scale( read, 0, 32767, 0, 3000) this is an Integer or Word? and, you want the returnd range to be between 0 and 3000?

    The .h
    Lovely clean code. Very nice.
    And, check... line 81... missing brace!
    And, check .. line 81... can you return an out_err? Hard code an out_err and make sure this is working.

    So, a few things to check.

     
  • iDeeW

    iDeeW - 2019-03-17

    Thank you so much for the quick reply.
    The function MCP342x_convert_oneshot(2, , , err) returns an integer
    and read is defined as integer.
    Acording to the documantation scale function returns an integer.
    You are correct, I'm expecting a number from 0 to 3000 after scaling.

    But even with #option explicit my serial turminal shows a 0 to 255 number after scaling.

    I'm really confused...

     
  • Anobium

    Anobium - 2019-03-17

    You will have to post the complete project. New .h as well.

    But, try this comment out the MCP call. Hard code the read value, first as 0 then as 32767, what happens? Is scale returning 0 and 3000?

     
  • Anobium

    Anobium - 2019-03-17

    You will have to post the complete project. New .h as well.

    But, try this comment out the MCP call. Hard code the read value, first as 0 then as 32767, what happens? Is scale returning 0 and 3000?

     
  • iDeeW

    iDeeW - 2019-03-17
    #chip pic16f877a, 16
    #option explicit
    
    'include files
    #include <maths.h>              ;required maths.h
    #include <MCP342x.h>
    #include <MCP4725.h>
    
    'defines LCD Settings
    #define LCD_NO_RW 'Needed when RW is grounded
    #define LCD_IO 4  'Mode
    #define LCD_RS PORTB.0    '
    #define LCD_Enable PORTB.1
    #define LCD_DB4 PORTB.4
    #define LCD_DB5 PORTB.5
    #define LCD_DB6 PORTB.6
    #define LCD_DB7 PORTB.7
    'dir Portb.2 in   ' Backlight on
    
    'define I2C settings
    hi2cmode Master 'I2C MASTER MODE
    #define HI2C_BAUD_RATE 400
    #define HI2C_DATA PORTC.4
    #define HI2C_CLOCK PORTC.3
    dir HI2C_DATA in
    Dir HI2C_CLOCK in
    
    'Serial Port Settings
    #define USART_BAUD_RATE 19200
    #define USART_TX_BLOCKING
    #define USART_DELAY 0 ms
    dir PORTC.6 Out
    
    'Variables
    dim err as Integer
    dim read as Integer
    dim adc_val as String
    
    start: 'Main Loop Start here
     'if MCP342x_available then
          locate 0,0: print "Inp Volt:"
          locate 0,10: printVol(): print "V"
          locate 1,0: print "Out Curr:"
          locate 1,10: printCur(): print "A"
     'end if
     'if MCP4725_available then
          MCP4725_set_voltage(250)'0 to 4095
     'end if
      wait 200 ms
    goto start
    
    sub printVol()
      read = MCP342x_convert_oneshot(2, , , err)
      HSerPrintStringCRLF str(read)
      read = scale( read, 0, 32767, 0, 3000)
      HSerPrintStringCRLF str(read)
      print str(read)
    end sub
    
    sub PrintCur()
      read = MCP342x_convert_oneshot(1, ,8 , err)
      if read > 32767 then read = 0
      adc_val = leftpad(str(scale([Integer]read, 0,6403 , 0, 5000)), 4, "0")
      print left(adc_val, 2)+ "." + right(adc_val, 2)
    end sub
    
     
  • Anobium

    Anobium - 2019-03-18

    Will not compile. Missing .h = MCP4725.h

    #include <MCP342x.h>
    #include <MCP4725.h>
    
     
  • Anobium

    Anobium - 2019-03-18

    I have worked around.

    What I know:
    1. The scale function is working as expected, as are the string methods.
    2. Returning 0 and 32767 from MCP342x_convert_oneshot provides the correct results of 0 and 32767 and the PrintVol and PrintCur handle these tests correctly.

    Do this.

    Line 137: MCP342x_convert_oneshot = FnLSL([word]Value1, 8)+ Value2. Hard code values here that are meaningful values and test. I cannot test as I dont have this sensor. I think the value being returned from MCP342x_convert_oneshot is not what you expect. So, we need to look carefully in this method.

    Test and let me know what happens.

    Also, ONLY DO THIS ONCE WE HAVE THE VALUE RETURNED CORRECTLY. Consider this. Remember, I do not know this sensor.. so, check my thinking.

    'add - the may work better
    dim Returned_Value as integer 
    dim sersor_state as byte
    
    HI2CSend  MCP342x_AddrRead
      HI2CReceive Value1,  MCP342x_convert_oneshot_h
      HI2CReceive Value2,  MCP342x_convert_oneshot
      HI2CReceive Value3,  sersor_state
      HI2CStop
      set config.7 off
      if sersor_state = config then
        'err = -100 'One-shot Config Error
      end if
    

    Anobium

     

    Last edit: Anobium 2019-03-18
  • iDeeW

    iDeeW - 2019-03-20

    I tested the library function just like you asked. It returns expected values.
    Below is what I am seeing right now.

    sub printVol()
          Dim readv as integer
          readv = MCP342x_convert_oneshot(2, , , err)'....here 30V input to ADC
          print str(readv)'...............................LCD shows value 32767
          print " "
          readv = scale(readv, 0, 32767, 0, 30000)'......expected value is 30000
          print str(readv)'...............................but LCD shows value 48
    end sub
    
     

    Last edit: iDeeW 2019-03-21
  • Anobium

    Anobium - 2019-03-21

    Post the complete project. As I get 32767 30000 when I use the following code. I just hard coded the 32767 in your method.

    Try this code. What value do you get?

    Puzzled

    Anobium

      #chip 16f877a, 20
    
      #define LCD_NO_RW 'Needed when RW is grounded
      #define LCD_IO 4  'Mode
      #define LCD_RS PORTB.0    '
      #define LCD_Enable PORTB.1
      #define LCD_DB4 PORTB.4
      #define LCD_DB5 PORTB.5
      #define LCD_DB6 PORTB.6
      #define LCD_DB7 PORTB.7
    
      printVol
    
       sub printVol()
          Dim readv as integer
          readv = 32767   'MCP342x_convert_oneshot(2, , , err)'....here 30V input to ADC
          print str(readv)'...............................LCD shows value 32767
          print " "
          readv = scale(readv, 0, 32767, 0, 30000)'......expected value is 30000
          print str(readv)'...............................but LCD shows value 48
    end sub
    
     

    Last edit: Anobium 2019-03-21
  • Anobium

    Anobium - 2019-03-21

    Is this the cause?

    StrInteger() not Str()

     
  • iDeeW

    iDeeW - 2019-03-21

    I agree, this is very puzzling...!
    I tried the below code with StrInteger& it behaves exactly the same.

    sub printVol()
      dim readv as Integer
      readv = MCP342x_convert_oneshot(2, , , err)'....here 30V input to ADC
      'if readv < 0 then readv = 0
      print StrInteger(readv)'..LCD shows value 32767. which is what I expects
      print " "
      readv = readv * 30000 / 32767'......expected value is 30000
      'readv = scale(readv, 0, 32767, 0, 30000)'...this prints value 48 on LCD
      'adc_val =  leftpad(StrInteger(readv), 5, "")+ "mV"
      print StrInteger(readv)'.........................This prints value 0 on LCD
    end sub
    

    Its not the scaling function!
    What am I doing wrong?
    Thanks,

     

    Last edit: iDeeW 2019-03-21
  • Anobium

    Anobium - 2019-03-21

    Do this.


    Line 137 of MCP342x.h. Hard code a value of 16383
    MCP342x_convert_oneshot = 16383
    Test - what is the result? 15000?


    If the above test returns 15000 , then, line 133 is the same file is not returning what you think. So Value3 is not return a config - to proove this add this to line 139. So, to test is value is not equal to config. Do this.

    MCP342x_convert_oneshot =32767
    Test - what is the result?


    Then, I would get the protocol analyser out.

     
  • iDeeW

    iDeeW - 2019-03-21

    I hardcoded 16383......function returns 16383 but LCD shows 151


    I change the else side to return MCP342x_convert_oneshot =32767, but the function still returns 16383 & LCD shows 151.
    Edited library file is attached...


    I have my Logic Analyser hooked up.... all return values looks good and match with the datasheet.

     

    Last edit: iDeeW 2019-03-22
  • iDeeW

    iDeeW - 2019-03-22
     

    Last edit: iDeeW 2019-03-22
  • Anobium

    Anobium - 2019-03-22

    Got an update?

     
  • iDeeW

    iDeeW - 2019-03-22

    I created a new file and rewrote the whole code (used the same library files) & it started working...yay!

    Is it possible the old project file was somehow currupted??

    Thanks

     

    Last edit: iDeeW 2019-03-22
  • Anobium

    Anobium - 2019-03-23

    Post the code - may be we can spot the original condition but there is no cache of code. Each compile is unique and all new ASM is generated... it would be good to figure out the root cause.

     
  • iDeeW

    iDeeW - 2019-03-23

    Attached.

    Thanks

     

    Last edit: iDeeW 2019-03-23
  • Anobium

    Anobium - 2019-03-23

    Cheers.

    1. So, what is the MCP3428 ? and, the MCP4725? What do they do?
    2. Should we take you code and create a demo and two libraries?
    3. Why two libraries? This may be obvious when #1 is ansered. :)
     
  • iDeeW

    iDeeW - 2019-03-23

    MCP342x is a ADC and the MCP4725 is a DAC from Microchip.
    Once I add all the functionalities, I will definitely share the libraries.
    Thanks

     
  • Anobium

    Anobium - 2019-03-24

    I see. So, hence the two libraries. Make a lot more sense now.

    Be good to have 'similar' ADC commands like READAD16_MCP32xx. An idea.

     
  • iDeeW

    iDeeW - 2019-03-25

    Great idea...will do.

    Thanks

     
  • Pic Fan 1975

    Pic Fan 1975 - 2021-01-27

    any progress on making a library for MCP4725?

     

    Last edit: Pic Fan 1975 2021-01-27

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.