Scaling ADC Result to a Set Range


Closed Thread
Results 1 to 40 of 45

Hybrid View

  1. #1


    Did you find this post helpful? Yes | No

    Default Re: Scaling ADC Result to a Set Range

    Quote Originally Posted by richard View Post
    none of these are correct



    LCD_CLR CON 12 ' Clear screen
    LCD_HOME CON 1 ' HOME
    LCD_NEXT_LINE CON 13 ' CR does this go back to line 1 when current line is line2 ? seems it will go to first pos of line

    LINE 2
    WOULD BE HSEROUT [16,80] to be sure


    TRY
    HSEROUT [1,"ADCInVal=", DEC ADCInVal, " ", 13, "MotorDuty=", DEC MotorDuty, " "]
    or
    HSEROUT [1,"ADCInVal=", DEC ADCInVal, " ", 16,80, "MotorDuty=", DEC MotorDuty, " "]


    ps the display needs 1000ms delay to power on/boot before you try to write to it
    What makes you think they're not correct? The clear screen command has worked just fine all along. Nonetheless, I will try what you suggest.

  2. #2
    Join Date
    May 2013
    Location
    australia
    Posts
    2,642


    Did you find this post helpful? Yes | No

    Default Re: Scaling ADC Result to a Set Range

    from your data sheet link

    Clear Screen (control-L, ASCII 12)
    Clear the screen.
    you did read it ?

  3. #3


    Did you find this post helpful? Yes | No

    Default Re: Scaling ADC Result to a Set Range

    Thanks to all your help, the screen is finally working. In case anyone is interested, here's the working code:

    Code:
    #DEFINE USE_LCD_FOR_DEBUG   ; comment out for non-debug use
       
    ' ***************************************************************
    ' Pin Connections
    ' ***************************************************************
    
    ' Vdd      -> pin 1         -> +5V   
    ' RC5/Rx   -> pin 5         -> EUSART receive
    ' RC4/Tx   -> pin 6         -> EUSART transmit (LCD)
    ' RC2      -> pin 8         -> Port motor direction signal (motor 2)
    ' RC1/CCP4 -> pin 9         -> Port motor PWM output (motor 2)
    ' RC0      -> pin 10        -> Stbd motor direction signal (motor 1)
    ' RA2/CCP3 -> pin 11        -> Stbd motor PWM output (motor 1)
    ' RA1      -> pin 12        -> trim pot input
    ' Vss      -> pin 14        -> GND
       
    DEFINE OSC 16               ; Set oscillator 16Mhz
    
    ' USART Settings for Tx/Rc (e.g. LCD)
    ' > use Mister E PIC Multi-Calc application to get register/DEFINE settings
    ' > as the values are dependent on the OSC and baud rate
    
    DEFINE HSER_RCSTA 90h ' Enable serial port & continuous receive
    DEFINE HSER_TXSTA 24h ' Enable transmit, BRGH = 1
    DEFINE HSER_CLROERR 1 ' Clear overflow automatically
    DEFINE HSER_SPBRG 130 ' 2400 Baud @ 16MHz, 0.0%
                                   
    ' ***************************************************************
    ' Device Fuses
    ' ***************************************************************
    
    #CONFIG
       __config _CONFIG1, _FOSC_INTOSC & _WDTE_ON & _PWRTE_ON & _MCLRE_OFF & _CP_OFF & _CPD_OFF
       __config _CONFIG2, _PLLEN_OFF & _STVREN_ON & _BORV_LO & _LVP_OFF
    #ENDCONFIG
    
    ' ***************************************************************
    ' Initialization
    ' ***************************************************************
    
    OSCCON    = %01111000       ; 16MHz internal osc
    
    pause 100
    
    APFCON0.2 = 0               ; Tx on RC4 for LCD display
    APFCON0.7 = 0               ; Rx on RC5
    
    BAUDCON.4 = 1               ; Transmit inverted data to the Tx pin
    
    ' From Mister E's Multi-Calc for EUSART:
    SPBRGH    = 6
    BAUDCON.3 = 1               ' Enable 16 bit baudrate generator
    
    ANSELC    = 0               ; Digital only for all PortC pins
    TRISC     = 0               ; Make all PORTC pins output
    
    TRISA     = %00000010	    ; Make all pins output except for RA1
                                ; (trim pot input)
    ANSELA    = %00000010       ; Analog on PORTA.1 (AN1) only
    
    FVRCON    = 0               ; Fixed Voltage Reference is disabled
    ADCON0    = %00000101       ; ADC enabled on AN1 (RA1) only; ADC conversion
                                ; enabled
    PAUSEUS 20                  ; wait for the analog switch 'glitch' to die down
    ADCON1    = %00110000       ; Left-justified results in 8-bits; Frc as timer
     
    #IFDEF USE_LCD_FOR_DEBUG
    '   Display control codes for PC1602LRU-XWA serial LCD
    '   (see 'Dropbox\PBP Projects\PIC Datasheets\PC1602LRU-XWA LCD Datasheet.pdf' 
    '   for list of control codes)
        LCD_HOME      CON 1     ' Cursor home
        LCD_CLR       CON 12    ' Clear screen
        LCD_HIDE_CRSR CON 4     ' Hide cursor
        LCD_NEXT_LINE CON 13    ' Move cursor to the first position of the next line 
    #ENDIF
    
    MotorDuty   VAR BYTE        ; Actual duty cycle for motor
    ADCInVal    VAR BYTE        ; stores ADCIN result read from trim pot
    compVal     VAR BYTE        ; stores last-changed ADC value
    MinDuty     CON 100         ; Minimum speed to rotate motor for this application
    MaxDuty     CON 252        
    MaxADCVal   CON 255         ; 255 for 8-bit; 1023 for 10-bit
    
    #IFDEF USE_LCD_FOR_DEBUG
        pause 1000
        HSEROUT [LCD_CLR]
        pause 5
        HSEROUT ["LCD Init",LCD_NEXT_LINE]
        pause 5
        HSEROUT ["PC1602LRU-XWA"]
        pause 1500
    #ENDIF
    
    gosub Do_ADC
    compVal = ADCInVal           ; set initial compare value
    GOSUB Map_ADC_Val_to_PWM_Duty
                                                                  
    Main:
        gosub Do_ADC
        pause 100
    
        If ADCInVal <> compVal Then
            compVal = ADCInVal
            GOSUB Map_ADC_Val_to_PWM_Duty
        endif
     
    GOTO Main
    
    Do_ADC:
        PAUSEUS 50              ' Wait for A/D channel acquisition time
        ADCON0.1 = 1            ' Start conversion
        
        WHILE ADCON0.1 = 1      ' Wait for it to complete
        WEND
    
        ADCInVal = ADRESH
    
        return
    
    
    Map_ADC_Val_to_PWM_Duty:
    '   Arduino Map function to emulate:
    '   ===============================
    '   map(value, fromLow, fromHigh, toLow, toHigh)
    
    '   long map(long x, long in_min, long in_max, long out_min, long out_max)
    '   {
    '     return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
    '   }
    
        MotorDuty = (compVal - 0) * (MaxDuty - MinDuty)/(MaxADCVal - 0) + MinDuty
        
        #IFDEF USE_LCD_FOR_DEBUG
            HSEROUT [LCD_CLR]
            pause 5
            HSEROUT ["compVal=",DEC compVal,"  ",LCD_NEXT_LINE]
            pause 5
            HSEROUT ["MotorDuty=",DEC MotorDuty,"  "]
        #ENDIF
    
        RETURN

Similar Threads

  1. Replies: 4
    Last Post: - 27th January 2015, 15:34
  2. Replies: 4
    Last Post: - 30th May 2012, 08:28
  3. Converting 10bit ADC result to 8 bit
    By jmgelba in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 5th March 2012, 20:38
  4. strange A2D 5V scaling result - 16F819
    By Max Power in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 8th April 2010, 02:28
  5. Scaling ADC values
    By purkolator in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 29th November 2007, 05:14

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts