8 Replies Latest reply on Oct 5, 2018 5:55 PM by user_13463998

    Having issue displaying Current(mA) and Voltage(V) on LCD

    scott_2272156

      I am using a ADC_SAR to measure current across a shunt and voltage from a voltage divider and display them on a LCD.  I believe I understand the math and have attached my spreed sheet for review just in case.  I have also attached my project, which is basically a PICKIT-043 plugged into a custom PCB I produced.  I have verified I have all the correct voltages on the ADC pins and getting proper values.  I am also able to get reasonable decimal counts and display them from the two channels.  Yet when i try to display the conversion from decimal counts to volts or mA I basically get zero or nothing on my display.  I am using sprintf() for sending data to the display.  In my code lines 161 to 178 do not seem to work for some reason.  Could i get some help on doing this properly.

      Thanks

        • 1. Re: Having issue displaying Current(mA) and Voltage(V) on LCD
          vsrs

          Hello Scott,

           

          In Creator could you goto  ,CYDWR-> System tab and set the heap size to 0x0200. Could you let me know the result.

           

          Best Regards,

          VRS

          • 2. Re: Having issue displaying Current(mA) and Voltage(V) on LCD
            sclifford1_3419596

            I set heap as directed, but i still can not get any output to LCD when converting decimal counts to volts or mAmps.

            • 3. Re: Having issue displaying Current(mA) and Voltage(V) on LCD
              scott_2272156

              I had to do 2 things to get it to work ....

               

              1. under Projects-> Build Settings -> Linker: Set Use newlib-nano float formatting to true

                

              2.  set Heap size to 0x0200

              1 of 1 people found this helpful
              • 4. Re: Having issue displaying Current(mA) and Voltage(V) on LCD
                user_13463998

                Hi,

                 

                I did not know about

                > 1. under Projects-> Build Settings -> Linker: Set Use newlib-nano float formatting to true

                Thanks!

                 

                Meantime, I would do a couple of things

                 

                (1) to avoid 0 rounding

                > adcmAmps = (adcmAmps/(R_Shunt*GAIN)) * 1000 ;

                < adcmAmps = (1000 * adcmAmps)/(R_Shunt*GAIN) ;

                 

                (2) to avoid linking float format or when float format is not supported

                > sprintf(tmpStr, "%04.1f V",adcVolts);

                < sprintf(tmpStr, "%04d.%01dV", (int)adcVolts, ((int)(10*adcVolts))%10) ;

                 

                moto

                • 5. Re: Having issue displaying Current(mA) and Voltage(V) on LCD
                  scott_2272156

                  Moto ..... thanks for your looking at my issue.

                  I first must be honest, I am very green with C and PSoC, but  enjoy learning and working with it.  That being said would you please explain what you mean in more detail  as to why you  are making these recommendations.  I would like to understand and be educated for the future.

                  Thanks again

                  Scott

                   

                  (1) to avoid 0 rounding

                  > adcmAmps = (adcmAmps/(R_Shunt*GAIN)) * 1000 ;

                  < adcmAmps = (1000 * adcmAmps)/(R_Shunt*GAIN) ;

                   

                  (2) to avoid linking float format or when float format is not supported

                  > sprintf(tmpStr, "%04.1f V",adcVolts);

                  < sprintf(tmpStr, "%04d.%01dV", (int)adcVolts, ((int)(10*adcVolts))%10) ;

                  • 6. Re: Having issue displaying Current(mA) and Voltage(V) on LCD
                    user_13463998

                    Hi, Scott-san,

                     

                    (1) to avoid 0 rounding

                    > adcmAmps = (adcmAmps/(R_Shunt*GAIN)) * 1000 ;

                    < adcmAmps = (1000 * adcmAmps)/(R_Shunt*GAIN) ;

                     

                    Your adcmAmps is "int16", so in case adcAmps < (R_Shunt*GAIN)

                    (adcmAmps/(R_Shunt*GAIN)) results 0, even if it is bigger than 0.001

                    then multiplying by 1000 does not help.

                     

                    So at first I'd like to multiply int by 1000

                    so that 0.001 <= (adcmAmps)/(R_Shunt*GAIN) < 1.0

                    won't result 0.

                     

                    (2) to avoid linking float format or when float format is not supported

                    > sprintf(tmpStr, "%04.1f V",adcVolts);

                    < sprintf(tmpStr, "%04d.%01dV", (int)adcVolts, ((int)(10*adcVolts))%10) ;

                     

                    Working with MCUs, often "float" is too expensive for the memory,

                    in such cases and if we know the range of expected value,

                    we could survive handling the (should be float) value as an integer

                    by multiplying 10^n. (It does not have to be 10, though)

                     

                    In your case as you need only 1 digit under the decimal place,

                    unless your voltage range exceeds around 3000v (32767V as exact), 

                    you can use an int16 version of adcVolts, such as iadcVolts

                    multiply the real value by 10 and print something like

                    < sprintf(tmpStr, "%04d.%01dV", iadcVolts/10, iadcVolts%10) ;

                     

                    In case you'd like to round the value, and if the range is less than 300V,

                    you could multiply the value and add 5 then

                    < sprintf(tmpStr, '%04d.%01dV", iadcVolts/100, (iadcVolts/10)%10) ;

                    (In this case, may be we had better using int32 for iadcVolts...)

                     

                    Last but not least, as I'm very good at making mistakes,

                    please correct me if I'm wrong.

                     

                    moto

                    • 7. Re: Having issue displaying Current(mA) and Voltage(V) on LCD
                      scott_2272156

                      Moto,

                      Thank you for explaining in detail for me ... for a slow guy like me this really helps!

                       

                      I do have one more question for you.  Could you please explain  by example how the Gain API in the ADC_SAR is utilized.  An example would be very helpful.  I understand Gain, but this API is based on some multiple 10 of what the decimal count or volts ... confused by this?

                      Thanks

                      Scott

                      • 8. Re: Having issue displaying Current(mA) and Voltage(V) on LCD
                        user_13463998

                        Dear Scott-san,

                         

                        I think that the GAIN API (void ADC_SetGain(uint32 chan, int32 adcGain)) is for those who uses external ADC reference. So I think that unless you are planning to use an external analog reference, you can ignore it.

                         

                        As you may already noticed, from the config dialog of ADC, you can call up datasheet.

                        001-ADC-config2.JPG

                         

                        And it that datasheet it is written

                        ==================

                        void ADC_SetGain(uint32 chan, int32 adcGain)

                         

                        Description:

                        Sets the ADC gain in counts per 10 volt for the voltage conversion functions below.

                        This value is set by default by the reference and input range settings.

                        It should only be used to further calibrate the ADC with a known input or if an external reference is used. Affects the ADC_CountsTo_uVolts, ADC_CountsTo_mVolts and ADC_CountsTo_Volts functions by supplying the correct conversion between ADC counts and voltage.

                         

                        Parameters:

                        chan: ADC channel number.

                        adcGain: ADC gain in counts per 10 volt.

                        ==================

                         

                        Best Regards,

                        6-Oct-2018

                        Motoo Tanaka

                        P.S. If any of my message provide you useful information,

                        marking it as "helpful" would be greatly appreciated.