adcin and HPWM


Closed Thread
Results 1 to 25 of 25

Thread: adcin and HPWM

Hybrid View

  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.

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