Extracting 10 bits from word variable and writing them into two separate registers?


Closed Thread
Results 1 to 10 of 10
  1. #1
    Join Date
    Feb 2013
    Posts
    1,078

    Default Extracting 10 bits from word variable and writing them into two separate registers?

    Hello.
    I want more precise and fine control of built-in PWM module of 16F1939, Since HPWM statement allows only 8 bit setting, and on each launch it re-starts the HPWM module, which makes not so good sound, when HPWM is used as sound generator. So for this, I need to directly write to two registers, in my case, LSB bits 0 and 1 go into CCP2CON bit 4 and bit 5, and remaining 8 bits go to CCPR2L register.
    So as I understand, I need to do the following

    CCP2CON.4=X DIG 0
    CCP2CON.5=X DIG 1
    CCPR2L.0=X DIG 3
    CCPR2L.1=X DIG 4
    CCPR2L.2=X DIG 5

    and so on.
    But I guess, there should be some simpler way of doing this?

  2. #2
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,521


    1 out of 1 members found this post helpful. Did you find this post helpful? Yes | No

    Default Re: Extracting 10 bits from word variable and writing them into two separate register

    The usual way of doing it is:
    Code:
    CCP2CON.4 = X.0
    CCP2CON.5 = X.1
    CCPR2L = X>>2
    PS. The DIG operator does not work like you seem to think.

  3. #3
    Join Date
    Feb 2013
    Posts
    1,078


    Did you find this post helpful? Yes | No

    Default Re: Extracting 10 bits from word variable and writing them into two separate register

    Yes might be
    Anyways, your method works, and now I have quite primitive, but still usable control over ADSR envelope, so my PICs can have some iconic 80s synth sounds

    btw, if anyone wants simple way to play two musical notes together with PBP, there's a simple trick to do it, with built in PWM generator. All you need to do, is start hardware PWM with frequency/duty cycle of 1st note, and restart it with frequency of 2nd note. Something like this:

    Code:
    ASDF:
    HPWM 1,127,440 'frequency of 1st note
    pauseus 110 'period of 2nd note frequency
    goto ASDF
    The exact numbers had to be adjusted according to actual frequencies, but general idea is this, and it works. I'm making a "tetris" music playback this way, and will post video later.

  4. #4
    Join Date
    Feb 2013
    Posts
    1,078


    Did you find this post helpful? Yes | No

    Default Re: Extracting 10 bits from word variable and writing them into two separate register

    Strange issue - sound duration changes with register status update.
    These registers somehow interfere with PAUSEUS statement?

    Code:
    z=0
    takra:
    hpwm 2,127,880+Z
    for x=1 to 1000 step 2
    CCP2CON.4 = X.0
    CCP2CON.5 = X.1
    CCPR2L = X>>2
    pauseus 1800
    next
    pause 300
    Z=Z+60
    goto takra
    Some sounds here sound 2-3 times longer than others!

  5. #5
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,521


    Did you find this post helpful? Yes | No

    Default Re: Extracting 10 bits from word variable and writing them into two separate register

    Not sure what you're trying to do there. Why are you using HPWM and setting the dutycycle manually at the same time?

    The inner loop will change the dutycycle (not the frequency) of the PWM signal but depending on the specific configuration of the CCP module that the HPWM command produces for the specific frequency you're requesting you might not HAVE ~10bits of resolution which is what you need since you're trying to get to a dutycycle value of 1000 meaning you might write an invalid duty cycle value to the duty cycle register.

  6. #6
    Join Date
    Feb 2013
    Posts
    1,078


    Did you find this post helpful? Yes | No

    Default Re: Extracting 10 bits from word variable and writing them into two separate register

    HPWM statement is used once to set the frequency.
    Duty cycle is varied in attempt to recreate ADSR Envelope, for audio synthesis.
    https://en.wikipedia.org/wiki/ADSR

  7. #7
    Join Date
    Nov 2005
    Location
    Bombay, India
    Posts
    947


    Did you find this post helpful? Yes | No

    Default Re: Extracting 10 bits from word variable and writing them into two separate register

    I suggest you look at this article by Michael Melkonian for inspiration of what is possible on PIC. https://www.embedded.com/get-by-without-an-rtos/

  8. #8
    Join Date
    Nov 2005
    Location
    Bombay, India
    Posts
    947


    Did you find this post helpful? Yes | No

    Default Re: Extracting 10 bits from word variable and writing them into two separate register

    I'm sorry, that article does not carry the link to his work. I cannot find the bundle called PIC_OSA which showed his implementation of a polyphonic piano(I think) using a PIC processor with MP3s to hear the results achieved. If I find it again, I will post the link.

  9. #9
    Join Date
    Nov 2005
    Location
    Bombay, India
    Posts
    947


    1 out of 1 members found this post helpful. Did you find this post helpful? Yes | No

    Default Re: Extracting 10 bits from word variable and writing them into two separate register

    After a few searches, I found what I was trying to suggest here.

    http://www.pic24.ru/doku.php/en/osa/...oduction/intro
    http://www.pic24.ru/doku.php/en/osa/ref/download/intro

    and this quartet example might be the one that may help you

    http://www.pic24.ru/doku.php/en/osa/...pendix/quartet

  10. #10
    Join Date
    Feb 2013
    Posts
    1,078


    Did you find this post helpful? Yes | No

    Default Re: Extracting 10 bits from word variable and writing them into two separate register

    Yeah I have seen that 10+ years ago.
    But for obvious reasons, I'm going my way

Similar Threads

  1. PIC 18F - read TMR0 registers in 16 bits mode
    By flotulopex in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 9th May 2018, 22:56
  2. shiftout with 32 bits long (ADF4351 registers)
    By F1CHF in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 26th August 2016, 16:15
  3. How to use symbolic names for bits within registers?
    By DwayneR in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 17th December 2009, 04:19
  4. About writing word variables into the eeprom
    By Ted's in forum mel PIC BASIC Pro
    Replies: 11
    Last Post: - 30th August 2008, 21:20
  5. How to isolate 10 bits from word variable?
    By rcbandwidth123 in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 14th April 2008, 20:18

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