Weird ADC / Math Results with 9-Bit PWM


Closed Thread
Results 1 to 22 of 22

Hybrid View

  1. #1


    Did you find this post helpful? Yes | No

    Default Re: Weird ADC / Math Results with 9-Bit PWM

    Thanks again tumbleweed. I can't use PBPL without moving up to a 18F chip, and the minimum # of pins is then 18 (I only need two outputs so an 8-pin chip is ideal. Also, the PCB is pretty cramped now as it is).

    But a lookup table could be tricky given the 0-255 range of the 8-bit ADC input but a PWM duty cycle range of 512. Maybe I should switch to 10-bit ADC and just divide by 2?

  2. #2
    Join Date
    Mar 2003
    Location
    Commerce Michigan USA
    Posts
    1,166


    Did you find this post helpful? Yes | No

    Default Re: Weird ADC / Math Results with 9-Bit PWM

    Why not look at using the DIV32 command?
    Dave Purola,
    N8NTA
    EN82fn

  3. #3
    Join Date
    Aug 2011
    Posts
    453


    Did you find this post helpful? Yes | No

    Default Re: Weird ADC / Math Results with 9-Bit PWM

    That's an interesting operation. Splitting the equation up into two parts for DIV32 seems to work...
    Code:
    LEDBrVal = ((compVal - 0) * (MaxDuty - MinDuty))
    LEDBrVal = DIV32 (MaxADCVal - 0) + MinDuty
    Is it safe to do something like that or would you suggest making the DIV32 statement simpler so that you know it immediately follows the multiplication (I don't know what the OP's intent for the " - 0" part of the above is for)?

  4. #4
    Join Date
    Mar 2003
    Location
    Commerce Michigan USA
    Posts
    1,166


    Did you find this post helpful? Yes | No

    Default Re: Weird ADC / Math Results with 9-Bit PWM

    Yes, The way you have it is OK. Just remenber that the result of the first multiplication before the DIV32 is to a dumby variable and it won't be usefull after the DIV32 function. It should be sized as a word.
    Dave Purola,
    N8NTA
    EN82fn

  5. #5


    Did you find this post helpful? Yes | No

    Default Re: Weird ADC / Math Results with 9-Bit PWM

    Quote Originally Posted by Dave View Post
    Yes, The way you have it is OK. Just remenber that the result of the first multiplication before the DIV32 is to a dumby variable and it won't be usefull after the DIV32 function. It should be sized as a word.
    Thanks Dave and tumbleweed! I've never had to use DIV32 before so I appreciate the explicit examples.

  6. #6


    Did you find this post helpful? Yes | No

    Default Re: Weird ADC / Math Results with 9-Bit PWM

    Everything works just fine now. The last thing I need to do is introduce linearization correction to get a better, more linear fade in. I think the info in this thread should help.

    Question: if the time it takes to calculate LEDBrVal is too long with DIV32, and I want to move to either using LOOKUP or a data table, can I have 2 data tables in one program?

  7. #7


    Did you find this post helpful? Yes | No

    Default Re: Weird ADC / Math Results with 9-Bit PWM

    Tried this but it doesn't fade in the LED and I never get anything on the LCD screen after the counter to 100:

    Code:
    ' For production version of this code, comment out the line below re:
    ' LCD debugging and also update config fuses to have code protect on
    #DEFINE USE_LCD_FOR_DEBUG     ; comment out for non-debug use
    
    ' ***************************************************************
    ' Pin Connections
    ' ***************************************************************
    
    ' VDD    -> pin 1            -> +5V
    ' RA5/Rc -> pin 2            -> EUSART receive
    ' RA2    -> pin 5            -> 1kohm -> 2N2222A transistor -> LEDs
    ' RA1    -> pin 6            -> Trim pot input
    ' RA0/Tx -> pin 7            -> EUSART transmit (LCD)
    ' VSS    -> pin 8            -> GND
    
    DEFINE OSC 16               ; Set oscillator 16Mhz
    
    ' ***************************************************************
    ' EUSART 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 desired 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 160      ' 9600 Baud @ 16MHz, -0.08%
    
    ' ***************************************************************
    ' Device Fuses
    ' ***************************************************************
    ' PIC chip data sheets can be found here: C:\Program Files\Microchip\MPASM Suite
    
    #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                    
    
    APFCON.2  = 0                ; Tx on RA0 for LCD display
    APFCON.7  = 1                ; Rc on RA5
    APFCON.0  = 0                ; CCP1 on RA2
    
    ' Some LCD serial modules need inverted data, some do not
    ' Enable the line below if needed, but for SparkFun SerLCD it should be
    ' commented out
    
    'BAUDCON.4 = 1               ; Transmit inverted data to the Tx pin  
    
    ' From Mister E's Multi-Calc for EUSART:
    ' *****************************************************************
    SPBRGH    = 1
    BAUDCON.3 = 1                ' Enable 16 bit baudrate generator
    ' *****************************************************************
    
    TRISA     = %00100010	     ' Make all pins output except for RA1 (trim pot input)
                                 ' and RA5 (EUSART Rc)
    ANSELA    = %00000010        ; Analog on PORTA.1 (AN1) only
    
    FVRCON    = 0                ' Fixed Voltage Reference is disabled
    ADCON0    = %00000101        ' ADC (analog-to-digital) is enabled on AN1 (RA1) only
    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
        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
        LCD_SP_CMD CON 124       ' special command character (for adjusting backlight brightness or toggling splash screen)
        LCD_BR_LVL CON 140       ' 140==40%
        LCD_SPLASH CON 9         ' toggles splash screen display
    #ENDIF
    
    ADCInVal       VAR BYTE      ; stores ADCIN result read from trim pot
    compVal        VAR BYTE      ; stores last-changed ADC value
    
    CounterA       var BYTE		 ' Just a BYTE Temporary working variable
    DataW          var WORD		 ' Just a WORD Temporary working variable
    RawData        var BYTE [16] ' Array holding ADC Result
    
    LEDBrVal       VAR WORD
    
    i              VAR WORD
    
    ' Array size = 100
    ' Total PWM steps: 512
    ' Gamma correction: 1.35
    ' # of values per line: 25
    
    '-----[The data table]--------------------------------------------------------
    DataWord       VAR WORD
    DataTable      CON EXT
    GOTO OverData                ; Make sure data doesn't try to execute
    ASM
    DataTable
      DW   0,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  2,  2,  2,  2,  2,  2,  2,  2,  2
      DW   3,  3,  3,  3,  3,  3,  4,  4,  4,  4,  5,  5,  5,  6,  6,  6,  7,  7,  8,  8,  9,  9, 10, 11, 11
      DW  12, 13, 14, 15, 16, 17, 18, 20, 21, 23, 24, 26, 28, 30, 33, 35, 38, 41, 44, 47, 51, 55, 59, 64, 69
      DW  74, 80, 86, 93,101,109,118,128,138,150,162,176,190,206,224,243,263,286,310,337,366,398,433,471,511
    endasm
    OverData:
    '-----[Continue with code]----------------------------------------------------
    
    ' ***************************************************************
    ' Set up PWM on CCP1
    ' ***************************************************************
    
    CCP1CON = %00001100          ; Use CCP1 in PWM mode
    
    ' Set duty cycle registers initially to 0
    CCP1CON.4 = 0
    CCP1CON.5 = 0
    CCPR1L    = 0
    
    ' Use Mister E's PICMultiCalc_1.3.1.exe application (Windows only)   
    ' to determine prescaler and PR2 values for given OSC frequency (e.g. 16Mhz)
    ' and duty cycle (use 100% to see highest actual value)
    
    ' ************************
    ' CCP1 uses TMR2
    ' ************************
                           
    T2CON   = %00000110         ; Timer2 on with 1:16 prescaler
    PR2     = 127               ; For 16Mhz OSC the desired output freq of 1954Hz
                                ; is achieved with this PR2 value (9-bit resolution
                                ; with 1:16 prescaler)
                                ; MAX DUTY = 100
                                
    MaxDuty       VAR WORD      ; According to Darrel:
                                ;   MaxDuty = (PR2 + 1) * 4
    
    MaxDuty = (PR2 + 1) * 4     ; 100
    MinDuty       CON 75        ; Minimum brightness for this application
    PWMDuty       VAR WORD      ; Duty cycle variable for PWM
    
    ' Fade in LED to set brightness
    FOR i = 0 to 100             ; must match ARRAY SIZE of data table above
        ReadCODE  (DataTable + i), DataWord
        PWMDuty = DataWord
    
        GOSUB ChngLEDBrightness 
                
        pause 1
    NEXT i
    
    #IFDEF USE_LCD_FOR_DEBUG
        HSEROUT [LCD_INST, LCD_CLR]
        pause 5
        HSEROUT ["i=",DEC i,"  "]
    #ENDIF
    
    Main:
        PAUSE 100
    
    GOTO Main
    
    '*********** Set duty registers ****************************************
    ChngLEDBrightness:
        CCP1CON.4 = PWMDuty.0
        CCP1CON.5 = PWMDuty.1
        CCPR1L    = PWMDuty >> 2
        
    RETURN

  8. #8
    Join Date
    May 2013
    Location
    australia
    Posts
    2,632


    Did you find this post helpful? Yes | No

    Default Re: Weird ADC / Math Results with 9-Bit PWM

    the fade will take 100mS what do you expect to see ?

    ' Fade in LED to set brightness
    FOR i = 0 to 100 ; must match ARRAY SIZE of data table above
    ReadCODE (DataTable + i), DataWord
    PWMDuty = DataWord

    GOSUB ChngLEDBrightness

    pause 1
    NEXT i
    this is the only write to the lcd
    what else do you expect ?

    #IFDEF USE_LCD_FOR_DEBUG
    HSEROUT [LCD_INST, LCD_CLR]
    pause 5
    HSEROUT ["i=",DEC i," "]
    #ENDIF
    the code is doing exactly what it should do , not what you hoping for maybe

  9. #9


    Did you find this post helpful? Yes | No

    Default Re: Weird ADC / Math Results with 9-Bit PWM

    Quote Originally Posted by tumbleweed View Post
    That's an interesting operation. Splitting the equation up into two parts for DIV32 seems to work...
    Code:
    LEDBrVal = ((compVal - 0) * (MaxDuty - MinDuty))
    LEDBrVal = DIV32 (MaxADCVal - 0) + MinDuty
    Is it safe to do something like that or would you suggest making the DIV32 statement simpler so that you know it immediately follows the multiplication (I don't know what the OP's intent for the " - 0" part of the above is for)?
    I grabbed this scaling function from an Arduino library and was emulating that:

    Code:
    '   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;
    '   }
    In general, the minimum brightness level is 0. But in this case, I only want to adjust the LED brightness between 50-100% duty cycle, so maybe I need to tweak that part as well.

Similar Threads

  1. Weird PWM Behaviour on 16F1825
    By RossWaddell in forum mel PIC BASIC Pro
    Replies: 14
    Last Post: - 26th October 2012, 21:59
  2. Averaging 16 bit values without using 32 bit math
    By sirvo in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 5th October 2007, 22:18
  3. Strangw results when using PWM command
    By malc-c in forum mel PIC BASIC Pro
    Replies: 20
    Last Post: - 10th July 2006, 12:14
  4. Strange Results in Math
    By CocaColaKid in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 31st August 2005, 07:09
  5. PBP 16-bit ADC result math
    By sonic in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 13th March 2005, 14:21

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