Newbie HPWM / ADC question


Closed Thread
Results 1 to 9 of 9

Hybrid View

  1. #1
    Join Date
    Jun 2007
    Posts
    56

    Default Newbie HPWM / ADC question

    Hello,

    I am new to PICBASIC and I want to read from a pot and output to HPWM with PIC 16 F 88

    OSCCON = $60 ' Internal oscillator frequency 4 Mhz

    TRISA = %11111111 ' Set PORTA to all input
    TRISB = %00000000 ' Set PORTB to all output
    ANSEL = %01111011 ' Analog channel 0,1,3-7 Digital channel 2,8
    ADCON1= %10000000 ' VDD & VSS as VREF

    DEFINE ADC_BITS 10 ' ADC resolution 10 bit
    DEFINE ADC_CLOCK 3 ' Set clock source (rc = 3)
    DEFINE ADC_SAMPLEUS 50 ' Sampling time 50ms
    define OSC 4 ' Clock 4 Mhz
    analogport VAR PORTA.0 ' Analog port pin RA.0
    analogvalue VAR word ' Value digital 0 - 1024
    pwmduty VAR word

    main:
    ADCIN analogport,analogvalue
    pwmduty = analogvalue / 2
    hpwm 1,pwmduty,7800
    goto main

    is the code correct, since I am not satisfied with the pwm output: the pwm turn off at 2.5v and portb.0 & portb.7 always on

    btw fuse is set on mikroe programmer

    Thanks
    Last edited by Johan; - 23rd June 2007 at 08:45.

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


    Did you find this post helpful? Yes | No

    Post

    Hi,

    First ...

    your ADC gives 1024 steps ...

    HPWM has 256 steps

    your PWMDuty has 512 possible values ...

    sooooo .... pwmduty = analogvalue / 4 ...

    OR .... DEFINE ADC_BITS 8 ' ADC resolution 8 bit

    which is ... much better !!! ( try and see why ...)



    Now, for PORTB.0 and B.7 ...

    did you define their initial state somewhere ????

    nooooo, you didn't !!! ... and you should had !!!

    Have a Good day

    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 " !!!
    *****************************************

  3. #3
    Join Date
    Jun 2007
    Posts
    56


    Did you find this post helpful? Yes | No

    Default

    Yes it works now, thank you Alain
    Changed the ADC resolution to match with HPWM and
    I forgot to put PORTB = 0 to clear the PORTB

    As for the ADC & HPWM , why is 8 bit better than 10 bit ?

    Johan

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


    Did you find this post helpful? Yes | No

    Wink

    Hi, Johan

    For what you do at this time ... no great difference !!!

    Note HPWM Always use 8 bits...

    but why use 10 bits resolution to divide the result afterwards ... ( note here it's a division by 4 or two times right shifting !!! ).

    Here, with 8 bits, ADC result is faster given, no need to add complementary formatting and no need to use an intermediate variable ( "pwmduty" can be replaced by "analogvalue" )
    add to this your variables can be only BYTES ... so you spare lots of room !!!

    Note also ADCON1.7 must be set to LEFT Justification ( "0" for BYTE result )

    One habit I have is NOT to change HPWM if pwmduty doesn't change ... that saves also time for other stuff ... and response to pot change is faster.

    Also always give initial values to variables ( CLEAR, if all = 0 )

    so, gives at last ...


    DEFINE OSC 4 ' Clock 4 Mhz
    DEFINE ADC_BITS 8 ' ADC resolution 10 bit
    DEFINE ADC_CLOCK 3 ' Set clock source (rc = 3)
    DEFINE ADC_SAMPLEUS 50 ' Sampling time 50ms

    OSCCON = $60 ' Internal oscillator frequency 4 Mhz
    ANSEL = %01111011 ' Analog channel 0,1,3-7 Digital channel 2,8
    ADCON1 = %00000000 ' VDD & VSS as VREF

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



    analogport VAR PORTA.0 ' Analog port pin RA.0

    analogvalue VAR BYTE ' Value digital 0 - 255
    oldanalogvalue VAR BYTE

    CLEAR


    main:
    ADCIN analogport,analogvalue

    IF analogvalue = oldanalogvalue THEN Jump

    hpwm 1,analogvalue,7800

    oldanalogvalue = analogvalue

    Jump:
    goto main



    Hope I didn't forget anything ... LOL

    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 " !!!
    *****************************************

  5. #5
    Join Date
    Jun 2007
    Posts
    56


    Did you find this post helpful? Yes | No

    Default PORTA.1 not working ?

    Hi Alain,

    I got another problem :
    If I try to read from PORTA.1 and change the code accordingly ( analogport var porta.1 )
    HPWM does not function, it just send output at 50% regardless the position of the pot

    Anything I set wrong?

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


    Did you find this post helpful? Yes | No

    Wink the debug sequence ...

    Hi, Johan

    When happening ... the first reflex is to open the datasheet and look to the functions connected to the "faulty" pins ...

    have a look to pict $ 5.1 ...

    Here we also find an ANALOG Comparator ...

    so, let's disable it !!!

    CMCON = 7 ( from the table $13.1 )

    By the way, we don't need the voltage ref nor ...

    CVRCON = 0


    So, I did forget some littles things ??? ...

    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 " !!!
    *****************************************

Similar Threads

  1. Multiple HPWM question
    By Johan in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 17th October 2007, 13:00
  2. ADC question
    By Srigopal007 in forum mel PIC BASIC Pro
    Replies: 11
    Last Post: - 8th June 2007, 09:52
  3. sound command & music
    By trying in forum mel PIC BASIC Pro
    Replies: 14
    Last Post: - 26th May 2006, 14:14
  4. HPWM and A to D interaction question 18F252
    By tcbcats in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 10th May 2006, 03:50
  5. 3 HPWM channels
    By docwisdom in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 4th April 2006, 02:43

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