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

    I switched to using the EUSART DEFINEs & register settings (commenting out the original ones) and it seems better (no initial wonky characters) but the 2nd line still isn't going to the 2nd line. Maybe if I split out the instruction for the 2nd line with a 5 ms pause?

    Here's the data sheet for my serial LCD.

  2. #2
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default Re: Scaling ADC Result to a Set Range

    From Data sheet
    Smart Linefeed (control-J, ASCII 10)
    Move cursor down one line. If immediately preceded by carriage return, linefeed is ignored

    Try it without the 13, see what happens. Or try it with only 13 (more likely)
    Failing that, if your message never changes, add spaces between " and MOTOR
    Last edited by Archangel; - 1st November 2015 at 00:50.
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  3. #3
    Join Date
    May 2013
    Location
    australia
    Posts
    2,645


    Did you find this post helpful? Yes | No

    Default Re: Scaling ADC Result to a Set Range

    none of these are correct
    #IFDEF USE_LCD_FOR_DEBUG
    LCD_INST CON 254 ' instruction
    LCD_CLR CON 1 ' Clear screen
    LCD_L1 CON 128 ' LCD line 1
    LCD_L2 CON 192 ' LCD line 2
    #ENDIF

    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
    Last edited by richard; - 1st November 2015 at 01:34. Reason: ps and home display

  4. #4


    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.

  5. #5
    Join Date
    May 2013
    Location
    australia
    Posts
    2,645


    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 ?

  6. #6


    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