adcin and HPWM


Closed Thread
Results 1 to 25 of 25

Thread: adcin and HPWM

  1. #1
    Join Date
    Sep 2006
    Posts
    747

    Default adcin and HPWM

    HI

    I have a program here, I guess it is somewhat working. I get a very erratic output, I need a steady frequency with a varying duty cycle. What I see on the scope is varying duty cycle frequency going in all direction, without me touching the circuit. I have set a pot on analog pin 1 . I can see the voltage varying from 0 to 5 with the multimeter , so this is working, I have a 47k on the MCLR pin and 22pF cap on the 20Mhz oscillator. Can somebody see the problem,
    I may have gotten the initialization wrong or something like it
    Thanks

    INCLUDE "modedefs.bas" 'Includes supoprt for PicBasic language
    @ DEVICE pic16F88, HS_OSC , CCPMX_ON
    DEFINE OSC 20 'use external 20mhz crystal
    PAUSE 100 ' start-up delay

    '/////////////////////////
    '// PIN configuration //
    '/////////////////////////

    DEFINE CCP1_REG PORTB 'Define output port of pulses out
    DEFINE CCP1_BIT 3 'Define output port of pulses out
    Define ADC_BITS 8 ' Set number of bits in result
    Define ADC_CLOCK 3 ' Set clock source (3=rc)
    Define ADC_SAMPLEUS 50 ' Set sampling time in uSec

    CMCON = 7 ' Disable analog comparator
    ANSEL = %00000010 ' set AN6 (RA1) as analog, others to digital
    ADCON1.7 = 1 ' Right justified results... duh... really needed in 8 bits???
    ADCON0 = %11000001 ' Configure and turn on A/D Module

    TRISB = %00000000 ' Set PORTB to all output
    TRISA = %11111111 ' Set PORTA to all input


    '///////////////////////////////////////////////
    '// Variable Declaration and initialization //
    '///////////////////////////////////////////////

    DutyCycle var byte 'Create adval to store result

    '//////////////////////////
    '// Program starts here //
    '//////////////////////////

    Mainloop:
    ADCON0.2 = 1 'Start Conversion

    ADCIN 1, DutyCycle 'analog pin 1 get the 8 bit result

    HPWM 1,DutyCycle,10500

    GOTO Mainloop
    end

  2. #2
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by lerameur View Post
    I have a program here, I guess it is somewhat working.
    It's a start eh?

    I get a very erratic output, I need a steady frequency with a varying duty cycle. What I see on the scope is varying duty cycle frequency going in all direction,
    Are you sure it's actually the frequency varying and not the 'scope triggering at different points?

    Do you have an LCD (or anything similar) that you can output the variable DUTYCYCLE to, so you can monitor it?
    How about a small cap across the ADC input pin to smooth that out just a bit? Most pot's aren't exactly clean...
    And try left justify...16F88 has a 10 bit ADC. PBP manual says 8 bit ADC results should be LEFT justified. Maybe a high sampling time.

  3. #3
    Join Date
    Sep 2006
    Posts
    747


    Did you find this post helpful? Yes | No

    Default

    Ya thats it, ADCON1.7 = 0

    works fine

    thanks skimask

    K

  4. #4
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by lerameur View Post
    Ya thats it, ADCON1.7 = 0
    works fine
    thanks skimask
    K
    I'd still put a small cap across the A/D input you're using.
    Might kill off any stray spikes you get from a dirty pot...

  5. #5
    Join Date
    Sep 2006
    Posts
    747


    Did you find this post helpful? Yes | No

    Default

    Hi

    works better with cap.
    Although I still have problem with the HPWM. I decided to put a value in for the duty cycle, and I am not getting the right duty cycle out. I mean it is going from 10 to 80% to 50%, its all over the place:


    INCLUDE "modedefs.bas" 'Includes supoprt for PicBasic language
    @ DEVICE pic16F88, HS_OSC , CCPMX_ON
    DEFINE OSC 20 'use external 20mhz crystal
    PAUSE 100 ' start-up delay

    '/////////////////////////
    '// PIN configuration //
    '/////////////////////////

    DEFINE CCP1_REG PORTB 'Define output port of pulses out
    DEFINE CCP1_BIT 3 'Define output port of pulses out
    Define ADC_BITS 8 ' Set number of bits in result
    Define ADC_CLOCK 3 ' Set clock source (3=rc)
    Define ADC_SAMPLEUS 50 ' Set sampling time in uSec

    CMCON = 7 ' Disable analog comparator
    ANSEL = %00000010 ' set AN1 (RA1) as analog, others to digital
    ADCON1.7 = 0 ' Right justified results... duh... really needed in 8 bits???
    ADCON0 = %11000001 ' Configure and turn on A/D Module

    TRISB = %00000000 ' Set PORTB to all output
    TRISA = %11111111 ' Set PORTA to all input


    '///////////////////////////////////////////////
    '// Variable Declaration and initialization //
    '///////////////////////////////////////////////

    DutyCycle var byte 'Create adval to store result

    '//////////////////////////
    '// Program starts here //
    '//////////////////////////

    Mainloop:
    ADCON0.2 = 1 'Start Conversion

    ADCIN 1, DutyCycle 'analog pin 1 get the 8 bit result

    HPWM 1,166,10800

    GOTO Mainloop
    end

  6. #6
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Don't keep overwriting the duty cycle value. Just write it once and let it run unless it changes.
    If you look at the datasheet, you'll see that a few things tend to reset themselves whenever the duty cycle register is written, thereby screwing up your duty cycle.

  7. #7
    Join Date
    Sep 2006
    Posts
    747


    Did you find this post helpful? Yes | No

    Default

    I just tested the program above to test the HPWM command, in reality I want to use the following:

    Mainloop:
    ADCON0.2 = 1 'Start Conversion
    ADCIN 1, DutyCycle 'analog pin 1 get the 8 bit result
    HPWM 1,DutyCycle,10800
    GOTO Mainloop
    end

    but even with
    HPWM 1,166,10800
    I am not getting the right duty cycle.

    Do I need to define HPWM1_TIMER?

    k

  8. #8
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    What are you getting for an output?
    Skip the ADC for now, and just run a HPWM command, nothing else, with a duty cycle of 127. Should output a nice clean 50% on, 50% off square wave.

  9. #9
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,648


    Did you find this post helpful? Yes | No

    Wink Once is enough !

    Hi, Ken

    Just change the dutycycle IF there's a change in ADC result ...

    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  10. #10
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Acetronics View Post
    Just change the dutycycle IF there's a change in ADC result ...
    Isn't that what I said earlier?

  11. #11
    Join Date
    Sep 2006
    Posts
    747


    Did you find this post helpful? Yes | No

    Default

    oK I will try this when I get home tonight.

    I did some change in my program, mostly in the Pin Configuration section.
    If this do not work, I will remove all the ADC stuff and just try the HPWM


    INCLUDE "modedefs.bas" 'Includes supoprt for PicBasic language
    @ DEVICE pic16F88, HS_OSC , CCPMX_ON
    DEFINE OSC 20 'use external 20mhz crystal
    PAUSE 100 ' start-up delay

    '/////////////////////////
    '// PIN configuration //
    '/////////////////////////

    DEFINE CCP1_REG PORTB 'Define output port of pulses out
    DEFINE CCP1_BIT 3 'Define output port of pulses out

    Define ADC_BITS 8 ' Set number of bits in result
    Define ADC_CLOCK 3 ' Set clock source (3=rc)
    Define ADC_SAMPLEUS 50 ' Set sampling time in uSec

    CMCON = 7 ' Disable analog comparator
    ANSEL = %00000010 ' set AN1 (RA1) as analog, others to digital
    ADCON1 = %00000000 ' Left justified results in 8 bits
    ADCON0 = %10000001 ' Configure and turn on A/D Module

    TRISB = %00000000 ' Set PORTB to all output
    TRISA = %11111111 ' Set PORTA to all input

    '///////////////////////////////////////////////
    '// Variable Declaration and initialization //
    '///////////////////////////////////////////////

    DutyCycle var byte 'Create adval to store result

    '//////////////////////////
    '// Program starts here //
    '//////////////////////////

    Mainloop:
    ADCON0.2 = 1 'Start Conversion

    ADCIN 1, DutyCycle 'analog pin 1 (RA1) get the 8 bit result
    pause 50

    HPWM 1,DutyCycle,10800
    pause 100

    GOTO Mainloop
    end

  12. #12
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Maybe try this instead:
    Code:
    INCLUDE "modedefs.bas" 'Includes supoprt for PicBasic language
    @ DEVICE pic16F88, HS_OSC , CCPMX_ON
    DEFINE OSC 20 'use external 20mhz crystal
    PAUSE 100 ' start-up delay
    
    '/////////////////////////
    '// PIN configuration //
    '/////////////////////////
    
    DEFINE CCP1_REG PORTB 'Define output port of pulses out
    DEFINE CCP1_BIT 3 'Define output port of pulses out
    
    Define ADC_BITS 8 ' Set number of bits in result
    Define ADC_CLOCK 3 ' Set clock source (3=rc)
    Define ADC_SAMPLEUS 100 ' Set sampling time in uSec
    
    CMCON = 7 ' Disable analog comparator
    ANSEL = %00000010 ' set AN1 (RA1) as analog, others to digital
    ADCON1 = %00000000 ' Left justified results in 8 bits
    ADCON0 = %10000001 ' Configure and turn on A/D Module
    
    TRISB = %00000000 ' Set PORTB to all output
    TRISA = %11111111 ' Set PORTA to all input
    
    '///////////////////////////////////////////////
    '// Variable Declaration and initialization //
    '///////////////////////////////////////////////
    
    DutyCycle var byte 'Create adval to store result
    LastDutyCycle var byte 'hold last time around
    
    '//////////////////////////
    '// Program starts here //
    '//////////////////////////
    
    Mainloop:
    ADCON0.2 = 1 'Start Conversion
    
    ADCIN 1, DutyCycle 'analog pin 1 (RA1) get the 8 bit result
    pause 50
    
    If dutycycle <> lastdutycycle then
         lastdutycycle = dutycycle
         HPWM 1,DutyCycle,10800
    Endif
    
    GOTO Mainloop
    end

  13. #13
    Join Date
    May 2007
    Posts
    604


    Did you find this post helpful? Yes | No

    Default

    I'm guessing that whenever this is executed:
    Code:
    HPWM 1,DutyCycle,10800
    PBP is stopping, setting up and restarting the HPWM.

    If you do this in the loop,
    Code:
    CCPR1L = DutyCycle
    then your problem should go away. I don't know if PBP allows you to do this. If not, you will have to set up the hardware manually.
    Code:
    PR2 = 462  '10,800Hz frequency
    CCPR1L = 0  'Initial duty cycle
    CCP1CON = %1100  'PWM mode
    T2CON.2 = 1  'Start TIMER2

  14. #14
    Join Date
    Sep 2006
    Posts
    747


    Did you find this post helpful? Yes | No

    Default

    I tried the following program, very simple, still not getting anything out..
    using internal osc, trying to cut down on the hardware...



    'Test HPWM command
    '
    INCLUDE "modedefs.bas" 'Includes supoprt for PicBasic language
    @ DEVICE pic16F88, CCPMX_ON, INTRC_OSC_NOCLKOUT

    OSCCON = %110 ' Set OSC TO 4MHZ 'select the clock


    'DEFINE OSC 20 'use external 20mhz crystal
    PAUSE 100 ' start-up delay

    '/////////////////////////
    '// PIN configuration //
    '/////////////////////////

    DEFINE CCP1_REG PORTB 'Define output port of pulses out
    DEFINE CCP1_BIT 3 'Define output port of pulses out

    CMCON = 7 ' Disable analog comparator

    TRISB = %00000000 ' Set PORTB to all output
    TRISA = %11111111 ' Set PORTA to all input

    '///////////////////////////////////////////////
    '// Variable Declaration and initialization //
    '///////////////////////////////////////////////

    '//////////////////////////
    '// Program starts here //
    '//////////////////////////
    HPWM 1,126,3000
    PAUSE 200 ' delay

    Mainloop:

    GOTO Mainloop
    end

  15. #15
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by lerameur View Post
    I tried the following program, very simple, still not getting anything out..
    using internal osc, trying to cut down on the hardware...
    Which pin on the PIC are you looking at? And how are you looking at it?

  16. #16
    Join Date
    Sep 2006
    Posts
    747


    Did you find this post helpful? Yes | No

    Default

    I am looking at B.3 through my scope.
    I am using the same hardware as my digital thermometer now, which was using B.3 to get the temperature and have a LCDout. (and that circuit is working fine, but no a/d or HPWM)
    So I removed that wire on B.3 (of course) , placed the new chip in place and I am not seeing anything to the scope

    k

  17. #17
    Join Date
    Sep 2006
    Posts
    747


    Did you find this post helpful? Yes | No

    Default

    CCPMX_ON .... that should be off....

  18. #18
    Join Date
    Sep 2006
    Posts
    747


    Did you find this post helpful? Yes | No

    Default

    I found a working program from steve in another thread:
    @ DEVICE MCLR_ON, INTRC_OSC, WDT_ON, LVP_OFF, BOD_OFF, PWRT_ON, PROTECT_OFF, CCPMX_ON
    OSCCON=$60 ' use internal 4MHZ osc
    PAUSE 100 ' start-up delay
    TRISB=0
    DEFINE CCP1_REG PORTB
    DEFINE CCP1_BIT 3
    HPWM 1,128,1000
    HERE: GOTO HERE

    I just change the oscillation to 20Mhz, and it si not working: I think this is the problem..
    I change the program to:

    @ DEVICE MCLR_ON, HS_OSC, WDT_ON, LVP_OFF, BOD_OFF, PWRT_ON, PROTECT_OFF, CCPMX_ON
    DEFINE OSC 20 'use external 20mhz crystal 'OSCCON=$60 ' use internal 4MHZ osc
    PAUSE 100 ' start-up delay
    TRISB=0
    DEFINE CCP1_REG PORTB
    DEFINE CCP1_BIT 3
    HPWM 1,128,1000
    HERE: GOTO HERE

  19. #19
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by lerameur View Post
    CCPMX_ON .... that should be off....
    That's one problem I'm thinking of. Not reading the right pin...

    Quote Originally Posted by lerameur View Post
    I found a working program from steve in another thread:
    Look at your manual under HPWM. Can't do 1000 Hz at 20Mhz...

  20. #20
    Join Date
    Sep 2006
    Posts
    747


    Did you find this post helpful? Yes | No

    Default

    19531Hz for a 20Mhz

    I remember using 5Khz for one of my project for motor driver PWM.

    How can I change steve's program to use a 20Mhz crystal?

  21. #21
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by lerameur View Post
    19531Hz for a 20Mhz

    I remember using 5Khz for one of my project for motor driver PWM.

    How can I change steve's program to use a 20Mhz crystal?
    The MINIMUM is 1221 hz, the max is 19531 hz. Can't do a lot about is, built into the hardware.
    Steve has a program called MultiCalc. Check it out...

  22. #22
    Join Date
    Sep 2006
    Posts
    747


    Did you find this post helpful? Yes | No

    Default

    well 10800hz fall well within those range , so no problem there.

    k

  23. #23
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by lerameur View Post
    well 10800hz fall well within those range , so no problem there.
    Ok... In the last couple of posts, the freq wasn't 10.8khz.

  24. #24
    Join Date
    Sep 2006
    Posts
    747


    Did you find this post helpful? Yes | No

    Default

    I changed the header to
    INCLUDE "modedefs.bas" 'Includes supoprt for PicBasic language
    @ DEVICE pic16F88,CCPMX_ON

    ' Setup Crystal oscillator Frequency to be used by PIC chip in MHz
    DEFINE OSC 20 'use external 20mhz crystal


    and it is now working.

    Ok... In the last couple of posts, the freq wasn't 10.8khz.
    ....really? I only see 10800hz in my post, where is the typo

    k

  25. #25
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by lerameur View Post
    I changed the header to
    INCLUDE "modedefs.bas" 'Includes supoprt for PicBasic language
    @ DEVICE pic16F88,CCPMX_ON
    ' Setup Crystal oscillator Frequency to be used by PIC chip in MHz
    DEFINE OSC 20 'use external 20mhz crystal
    and it is now working.
    Ok... In the last couple of posts, the freq wasn't 10.8khz.
    ....really? I only see 10800hz in my post, where is the typo
    k
    In post #18, you've got 1000hz...

    So, changing the header fixed it and all is good now?
    Nuts ain't it?

Similar Threads

  1. ADCIN > HPWM but with min/max limits - i failed maths
    By jamie_s in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 29th November 2009, 02:02
  2. 16F684 adcin and hpwm
    By astouffer in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 21st November 2008, 17:29
  3. Portc.3 latching?
    By Bronurstomp in forum mel PIC BASIC
    Replies: 4
    Last Post: - 10th November 2008, 17:47
  4. PIC16F819 HPWM CCP1 clariffication
    By earltyso in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 20th March 2008, 18:43
  5. Newbie HPWM / ADC question
    By Johan in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 25th June 2007, 12:52

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