11 hours ago, lichtuberstromt said:
It gets weirder though. The original AINSER64 code addresses the ADC channels in reverse order.
u16 pin = muxed ? (mux_pin_map[mux_ctr] + 8*chn) : chn; // the mux/chn -> pin mapping is layout dependent
//u16 pin = muxed ? (mux_pin_map[mux_ctr] + 8*(7-chn)) : (7-chn); // the mux/chn -> pin mapping is layout dependend
The first line is modified as suggested by Antichambre to work around the fact that I wired it backwards from the original midibox wiring. The commented out line is the original code. Until now, I thought this was a bit of a kludge and fairly odd that MUX 1 was supposed to be wired to Chan 7 of the ADC. I’m assuming the problem I’m having is maybe the reason (or one of the reasons) why its wired as such. If I use the original code, I have no weird issues. The pin numbers are completely wrong, but thats to be expected.
I’m surprised cause I did it for the HAARP and use a 4 channel ADC to reduce the cost. But I use it on the regular J19. Channels are reversed like you.
I don’t use the AINSER64 module and add/rewrite a simplified/dedicated copy in the app folder.
I made some changes to reduce the scanning and make it faster. The CS uses only 16 inputs, the 16 others are for a future extension like CV in.
And it works for me.
s32 ARP\_AIN\_POT\_Handler(void (\*\_callback)(u32 pot, u32 value)) { // the mux\_ctr -\> pin mappin is layout dependend const u8 mux\_pin\_map[8] = {0, 1, 2, 3, 4, 5, 6, 7}; static u8 mux\_ctr = 0; // will be incremented on each update to select the next AIN pin static u8 first\_scan\_done = 0; static u16 link\_status\_ctr = 0; s32 status = 0; // init SPI port for fast frequency access // we will do this here, so that other handlers (e.g. AOUT) could use SPI in different modes // Maxmimum allowed SCLK is 2 MHz according to datasheet // We select prescaler 64 @120 MHz (-\> ca. 500 nS period) status |= MIOS32\_SPI\_TransferModeInit(ARP\_AIN\_SPI, MIOS32\_SPI\_MODE\_CLK0\_PHASE0, MIOS32\_SPI\_PRESCALER\_64); // determine next MUX selection int next\_mux\_ctr = (mux\_ctr + 1) % 8; // loop over channels int chn; for(chn=0; chn\<2; ++chn) { // CS=0 status |= MIOS32\_SPI\_RC\_PinSet(ARP\_AIN\_SPI, ARP\_AIN\_SPI\_RC\_PIN, 0); // retrieve conversion values // shift in start bit + SGL + MSB of channel selection, shift out dummy byte MIOS32\_SPI\_TransferByte(ARP\_AIN\_SPI, 0x06 | (chn\>\>2)); // shift in remaining 2 bits of channel selection, shift out MSBs of conversion value u8 b1 = MIOS32\_SPI\_TransferByte(ARP\_AIN\_SPI, chn \<\< 6); // shift in mux\_ctr + "Link LED" status to 74HC595, shift out LSBs of conversion value //u8 b2 = MIOS32\_SPI\_TransferByte(ARP\_AIN\_SPI, ((chn == (ARP\_AIN\_NUM\_CHANNELS-1) ? next\_mux\_ctr : mux\_ctr) \<\< 5) | link\_status); u8 b2 = MIOS32\_SPI\_TransferByte(ARP\_AIN\_SPI, (chn == 1 ? next\_mux\_ctr : mux\_ctr) \<\< 5); // CS=1 (the rising edge will update the 74HC595) MIOS32\_SPI\_RC\_PinSet(ARP\_AIN\_SPI, ARP\_AIN\_SPI\_RC\_PIN, 1); // store conversion value if difference to old value is outside the deadband u16 pot = mux\_pin\_map[mux\_ctr] + 8\*chn; // the mux/chn -\> pin mapping is layout dependend u16 value = (b2 | (b1 \<\< 8)) & 0xfff; previous\_arp\_ain\_pot\_values = arp\_ain\_pot\_values[pot]; int diff = value - previous\_arp\_ain\_pot\_values; int abs\_diff = (diff \> 0 ) ? diff : -diff; if( !first\_scan\_done || abs\_diff \> ARP\_AIN\_POT\_DEADBAND ) { arp\_ain\_pot\_values[pot] = value; // notify callback function // check pin number as well... just to ensure if( first\_scan\_done && \_callback && pot \< 16 ) \_callback(pot, value); } } // select MUX input mux\_ctr = next\_mux\_ctr; // one complete scan done? if( next\_mux\_ctr == 0 ) first\_scan\_done = 1; return 0; // no error }
For your layout:
You can change the MUXes mapping here:
// the mux\_ctr -\> pin mappin is layout dependend //const u8 mux\_pin\_map[8] = {0, 1, 2, 3, 4, 5, 6, 7 }; //const u8 mux\_pin\_map[8] = {1, 4, 3, 5, 2, 7, 0, 6 }; // reversed pins const u8 mux\_pin\_map[8] = {6, 3, 4, 2, 5, 0, 7, 1 }; // order of MUX channels
You can reduce the channels:
for(chn=0; chn\<3; ++chn) {
u8 b2 = MIOS32\_SPI\_TransferByte(AINSER\_SPI, ((chn == 2 ? next\_mux\_ctr : mux\_ctr) \<\< 5) | link\_status);
And limits the number of inputs by security:
if( first\_scan\_done && \_callback && pin \< 24 )