Menu

format problem trying to alter example code

Help
2019-03-16
2019-03-17
  • HackInBlack

    HackInBlack - 2019-03-16

    trying to alter the example code for A2D scaling for the 16F690 on the low
    pin count demo board

    code

     #chip 16f690,8
     #config FOSC_INTOSC , WDTE_OFF , PWRTE_OFF , MCLRE_OFF , CP_OFF , CPD_OFF
    , BOREN_ON , IESO_OFF, FCMEN_OFF
    
    #script
            'reference voltage                        5v
            'actual voltage at port when 100% Pot     4.15
            'adc max                                  1023
            'number of graduations requried           15
            reference_voltage = 4.15
            vcc = 5
            numberofgrads = 15
            maxADC = 1023
            ADC_adjustment = int((reference_voltage*100)/(vcc*10))
            ADC_adjustment = int(ADC_adjustment * maxADC / numberofgrads / 10 )
    
     #endscript
    
        dir portc out
        dir porta.0 in
    
          Do
          If ( ReadAD10 ( AN0 )  / ADC_adjustment ) > 500 Then
            Set portc.3 On
          Else
            Set portc.3 Off
          End If
        Loop
    

    end code

    it will only work if i remove the ADC adjustment part;i must be formatting
    it incorrectly?
    the example compiles and works; the altered version compiles OK then sits
    doing nothing..

     

    Last edit: Anobium 2019-03-16
  • Anobium

    Anobium - 2019-03-16

    Let us remove the script and use Scale()

    I have assumed 1019 is the max ADC for that board - you code calibrate and then change this value.

    #chip 16f690,8
     #config MCLRE_OFF
    
        dir portc out
        dir porta.0 in
    
          Do
          If ( Scale(ReadAD10(AN0), 0, 1019, 0, 103) > 500 ) Then
            Set portc.3 On
          Else
            Set portc.3 Off
          End If
        Loop
    

    For an example of self calibation see, C:\GCB@Syn\GreatCowBasic\demos\Vendor_Boards\Great_Cow_Basic_Demo_Board\18F25Q10_ChipRange_Demonstrations\050_Show_A2D_value_on_LEDs.gcb You can adapt the method to a scale of 0 to 1023.

     
  • HackInBlack

    HackInBlack - 2019-03-17

    nope; the new version does nothing either! it compiles and flashes the chip but sits unresponsive.

     
  • Anobium

    Anobium - 2019-03-17

    I will get out a 690 and LPC board.

    15 mins.....

     
  • Anobium

    Anobium - 2019-03-17

    Operational code.

    Silly error in my previous post.. Scale(ReadAD10(AN0), 0, 1019, 0, 103)..103 should read 1023.

    This code has serial output to terminal also - so, you can understand the raw value at the ADC and the Scaled value/

    Enjoy

    Anobium

        #chip 16f690,8
        #option Explicit
    
        'USART settings  - connection 0V and TX on Port = RB7
        #define USART_BAUD_RATE 9600
        #define USART_TX_BLOCKING
    
    
    
        dir portc out
    
        dir porta.0 in
    
        'Dimension a variable
        dim ADCValue as word
        dim MaxADCVal as word
        MaxADCVal = 0
    
        Do
    
          ADCValue = ReadAD10(AN0)
    
          'Print Raw value
          HSerPrint ADCValue
          HSerSend 9
    
    
          'Ensure the ADC value supports the widest range.
          'Use the MaxADCVal as the top range measure then scale from 0 to the MaxADCVal
          'You will have to swing the potentiometer through the complete range, but, do this once and you have a full range!
          if ( ADCValue > MaxADCVal ) Then
            MaxADCVal = ADCValue
          end if
    
          ADCValue = Scale ( ADCValue, 0, MaxADCVal, 0, 1023 )
    
          If ( ADCValue > 500 ) Then
            Set portc.3 On
          Else
            Set portc.3 Off
          End If
    
          'Print Scaled value
          HSerPrint ADCValue
          HSerPrintCRLF
          wait 25 ms
        Loop
    
     
  • HackInBlack

    HackInBlack - 2019-03-17

    many thanks. i added the missing 2 and it runs OK! and shows how noisy the pot on my LPCDB really is...:/ i did a fresh install and now have 2 Vendor board folders,odd.including the one referenced which wasn't in the version i was using. that'll teach me to mess about with things...8)

     
    • Anobium

      Anobium - 2019-03-17

      Yes.. the demo folder is now ALL lowercase to support Linux installation. So, delete the demo folder (save all your own stuff!).... and reinstall to tidy up.

       
  • Anobium

    Anobium - 2019-03-17

    See https://learn.sparkfun.com/tutorials/analog-to-digital-conversion/relating-adc-value-to-voltage for the explain of the calcultation on the LPC board.

    My VDD is 4.90v
    My Analog Voltage measured, the maximum is 4.08v
    therefore: 4.90v / 1023 * 4.08 = 851. And, 851 is the ADC raw value... it works!

     
  • HackInBlack

    HackInBlack - 2019-03-17

    my VDD measures a measly 4.65V on the demo header; my mileage definately varies! this explains why nothing made much sense...

     
  • Anobium

    Anobium - 2019-03-17

    I have replaced the pots on my boards here. They do play up. The serial terminal should show the raw values and if that if leaping about.....replace the pot.

     

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.