Jump to content

scaling pot values


cimo
 Share

Recommended Posts

i ve been tinkering around with the following for a little while, just to get used to the new C enviroment but i couldn t really accomplish what i am trying to do.

I need to scale (as to expand) the cc sent by a pot which is atm ranging from 42 to 75.

Is this code ok also for expanding the range?

unsigned char Scale_7bit(unsigned char value, unsigned char min, unsigned char max)
{
  // scaled value is (<8-bit random> * ) >> 8
  PRODL = value << 1; // 8bit value
  PRODH = max-min+1;  // range
__asm
    movf _PRODL, W
    mulwf _PRODH, 0
__endasm;

  return min + PRODH;
}

there are still too many things i need to understand..  ::)

simone

Link to comment
Share on other sites

////////////////////////////////////////////////////////////////////////////
// This is an assembly optimized function which scales a 7bit value between
// a minimum and maximum value
/////////////////////////////////////////////////////////////////////////////
unsigned char Scale_7bit(unsigned char value, unsigned char min, unsigned char max)
{
  // scaled value is (<8-bit random> * ) >> 8
  PRODL = value << 1; // 8bit value
  PRODH = max-min+1;  // range
__asm
    movf _PRODL, W
    mulwf _PRODH, 0
__endasm;

  return min + PRODH;
}

/////////////////////////////////////////////////////////////////////////////
// This function is called by MIOS when a pot has been moved
/////////////////////////////////////////////////////////////////////////////
void AIN_NotifyChange(unsigned char pin, unsigned int pin_value) __wparam
{
  unsigned char value;

  // scale 7bit value between min and max boundary
  value = Scale_7bit(MIOS_AIN_Pin7bitGet(pin), // converted to 7bit
                     0x00,
                     0x7f);
					   
  // a pot has been moved, send CC# at channel 1
  MIOS_MIDI_TxBufferPut(0xb0); // CC at channel 1
  MIOS_MIDI_TxBufferPut(CimosCCvariable);  // pin number corresponds to CC number
  MIOS_MIDI_TxBufferPut(value);   // don't send 10bit pin_value,
                                                     // but 7bit value
}

this is the code atm, i ve been adding dividing etc etc numbers here and therebut i can t follow the logic path of the code yet, it s getting fun though.

Link to comment
Share on other sites

hi

nop those min and max are the min and max value for the outgoing CC, they scale but just the inverse i need to

i was wondering.. i should take the 10bit value insted of the 7bit and do the code scaling as i need and therefore make it in 7bit, ,to get a better resolution. ???

still trying (mostly randomly)

Link to comment
Share on other sites

a little improvement, now i got from 0 to 83 with this code:

////////////////////////////////////////////////////////////////////////////

// This is an assembly optimized function which scales a 7bit value between

// a minimum and maximum value

/////////////////////////////////////////////////////////////////////////////

unsigned char Scale_7bit(unsigned char value, unsigned char min, unsigned char max)

{

  // scaled value is (<8-bit random> * ) >> 8

  PRODL = value << 1; // 8bit value

  PRODH = 255;  // range

__asm

    movf _PRODL, W

    mulwf _PRODH, 0

__endasm;

  return    PRODH -81 ;

}

/////////////////////////////////////////////////////////////////////////////

// This function is called by MIOS when a pot has been moved

/////////////////////////////////////////////////////////////////////////////

void AIN_NotifyChange(unsigned char pin, unsigned int pin_value) __wparam

{

  unsigned char value;

  // scale 7bit value between min and max boundary

  value = Scale_7bit(MIOS_AIN_Pin7bitGet(pin), // converted to 7bit

                      0,

                    127);

 

  // a pot has been moved, send CC# at channel 1

  MIOS_MIDI_TxBufferPut(0xb0); // CC at channel 1

  MIOS_MIDI_TxBufferPut(CimosCCvariable);  // pin number corresponds to CC number

  MIOS_MIDI_TxBufferPut(value);  // don't send 10bit pin_value,

                                                    // but 7bit value

}

Link to comment
Share on other sites

bingo!

i had to shift of 2 bit instead of one

////////////////////////////////////////////////////////////////////////////
// This is an assembly optimized function which scales a 7bit value between
// a minimum and maximum value
/////////////////////////////////////////////////////////////////////////////
unsigned char Scale_7bit(unsigned char value, unsigned char min, unsigned char max)
{
  // scaled value is (<8-bit random> * ) >> 8
  PRODL = value << 2; // 8bit value
  PRODH = 255;  // range
__asm
    movf _PRODL, W
    mulwf _PRODH, 0
__endasm;

  return    PRODH -163 ;
}

/////////////////////////////////////////////////////////////////////////////
// This function is called by MIOS when a pot has been moved
/////////////////////////////////////////////////////////////////////////////
void AIN_NotifyChange(unsigned char pin, unsigned int pin_value) __wparam
{
  unsigned char value;

  // scale 7bit value between min and max boundary
  value = Scale_7bit(MIOS_AIN_Pin7bitGet(pin), // converted to 7bit
                       0,
                     127);
					   
  // a pot has been moved, send CC# at channel 1
  MIOS_MIDI_TxBufferPut(0xb0); // CC at channel 1
  MIOS_MIDI_TxBufferPut(CimosCCvariable);  // pin number corresponds to CC number
  MIOS_MIDI_TxBufferPut(value);   // don't send 10bit pin_value,
                                                     // but 7bit value
}

now i ll mount the pedal for fine tweaking if needed

still i think i could get a better resolution if i use the 10bit value

Link to comment
Share on other sites

ok the latest news is that i swapped the 50K pot for a 10K pot as it should be,removed the software offsetting and did an hardware offsetting (removed the little dent that prevents the pot to move around its ax and turned it so that it stays at 0R when the pedal is in its 0 position) and use 3 bit shifting, now the resolution is quite bad but it is ok for a pedal.I l try to use the 10bit value.

simone

Link to comment
Share on other sites

Meanwhile I understand what you are trying to do. I must admit, that I was focused on the code you posted, and not on your intention. The scale_7bit() function only scales down, but you want to scale up... here the "mulwf" trick (which is extremely performant, but hard to understand if you don't know how the assembler instruction is working) won't work.

Audiocommander has an nice example for scaling up in his http://www.midibox.org/dokuwiki/acsensorizer_04 application (see also the comment at the end of this page)

On the other hand: 75-43 means a value range of a little bit more than 32 (in 7bit range; in 10bit range: 256)

You might think "cool, 0..255 is 8bit value range, so I can scale down again" - but the other side of the story is, that you will have to reduce the AIN deadband as well, and therefore notice additional jitter if the cables are not short enough, and if you are not using a high-quality pot.

It's like a reduced dynamic range in audio world: you will notice a lot of noise, therefore try to make it "louder". Maybe a small transistor based amplifier after the pot could help

Best Regards, Thorsten.

Link to comment
Share on other sites

hi

thanks for clarifying, so there are 2 main issue (or solutions, thinking positive)

1 the software implementation is harder than i supposed, i ve laready read AC code, but, as you may understand it s far too complicated for my experience (1 week, but i am stubborn enough).I see the point, the "audio world" explication makes it quite clear.

2 on the hardware side either i could use a dual opamp circuit to tweak the range or i could use a charge pump to feed the pot with an higher voltage or i could use a transistor at the output to amplify the signal, as you suggested.Now i am using a 9volts supply for the pedal, but i d like not to use those 9 volts cause it s possible if not probable that i will be using other supplies in the future

I have a circuit i ve designed some time ago with 2 opamps, it was intended for the sharp sensors, then i never actually tested it cause i decided to go the easy way and use MidiPipe to tweak the range.Maybe it is time to etch that board.Afair It was designed to be used with 5VDC.It had a trim for expansion of contraction of the range and a trimpot for offsetting.

---------

now, i feel like i am requesting a lot of help and i see i actually receive a lot of help.. so i ask some more!  ;D

it s not entirely clear to me the difference between

void MIOS_MIDI_MergerSet(MIOS_MIDI_MERGER_ENABLED)

and

void MIOS_MPROC_MergerEnable(void)

should i add both of them to the code?

it seems to me that one will start the merger driver while the other containing a varialbe can be used to turn it on and off and as star/end link point

--------

i decided to put a midi in socket in the pedal so i can load new software with midi feedback but also cause with the midi merger i can connect another junky yamaha mfc05 that offer 5 buttons on 2 layers.The only problem it is that this pedal board sends program change so (again) i use MidiPipe for the translation.

Is it possible and if so is there any code example to make a en event translation?i think this is my starting point:

/////////////////////////////////////////////////////////////////////////////
//  This function is called by MIOS when a complete MIDI event has been received
/////////////////////////////////////////////////////////////////////////////
void MPROC_NotifyReceivedEvnt(
  unsigned char evnt0, unsigned char evnt1, unsigned char evnt2) __wparam
{
  // check for CC at any Channel, and forward event on CC#07 (volume)
  // for understanding the MIDI coding, please refer to the MIDI spec 
  // (-> http://www.borg.com/~jglatt/tech/midispec.htm)

  if( (evnt0 & 0xf0) == 0xb0 && evnt1 == 0x07 ) {
    // both values are matching, forward complete MIDI event to MIDI Out
    MIOS_MIDI_TxBufferPut(evnt0);
    MIOS_MIDI_TxBufferPut(evnt1);
    MIOS_MIDI_TxBufferPut(evnt2);
  }
}

-----

thanks

simone

Link to comment
Share on other sites

MIOS_MIDI_MergerSet(MIOS_MIDI_MERGER_ENABLED);

Will enable the midi merger

MIOS_MPROC_MergerDisable(); and MIOS_MPROC_MergerEnable();

are used to temporarily suspend the merger. You won't need to use these.

Translation is not difficult, you just need to alter the status byte.

You could try something like this.

/////////////////////////////////////////////////////////////////////////////
//  This function is called by MIOS when a complete MIDI event has been received
/////////////////////////////////////////////////////////////////////////////
unsigned char channel

void MPROC_NotifyReceivedEvnt(
  unsigned char evnt0, unsigned char evnt1, unsigned char evnt2) __wparam
{

// check if message is a program change. change it to a CC                    //
// Status = CC on recieved channel.   event1 = event1.   event2 = 127    //


  channel = evnt0 & 0x0f; 
  if( (evnt0 & 0xf0) == 0xc0) { 
    MIOS_MIDI_TxBufferPut(0xb0 | channel);
    MIOS_MIDI_TxBufferPut(evnt1);
    MIOS_MIDI_TxBufferPut(0x7f);
  }
}

Link to comment
Share on other sites

hi durisian

(and thank you)

how is the correct code?

void MIOS_MIDI_MergerSet(MIOS_MIDI_MERGER_ENABLED) __wparam
{
}

just anywhere in main.c ?

i get an error with this code

simone

you are redeclaring the function with that.

to call the function:

      MIOS_MIDI_MergerSet(MIOS_MIDI_MERGER_ENABLED);

Link to comment
Share on other sites

it s all good now i only had to change

 channel = evnt0 & 0x0f; 
  if( (evnt0 & 0xf0) == 0xc0) { 
    MIOS_MIDI_TxBufferPut(0xb0 | channel);
    MIOS_MIDI_TxBufferPut(evnt1);
    MIOS_MIDI_TxBufferPut(0x7f);
to
 channel = evnt0 & 0x0f; 
  if( (evnt0 & 0xf0) == 0xc0) { 
    MIOS_MIDI_TxBufferPut(0x90 | channel);
    MIOS_MIDI_TxBufferPut(evnt1);
    MIOS_MIDI_TxBufferPut(0x7f);

cause i wante note on/off on output

tx

simone

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...