MiniMO Compatible 4-Pot ADSR

May 21, 2020 at 12:18 pm (computers, maker, music) (, , , )

I’ve been enjoying having a play with the MiniMO synth, now I can programme it easily, but although I seriously like the idea of a piece of common hardware with software-defined synth personalities, I just found the ADSR with a single potentiometer just too limiting and not responsive enough for trying things out.

So I’ve sacrificed the common hardware and created a MiniMO compatible (i.e. based on the same circuit and code) 4-potentiometer ADSR instead.  To continue to use an ATtiny85 though requires a few compromises:

  • There is no button or LED.
  • There is no CV input to control the ADSR parameters (although this could be added).
  • The pots are always “live” – changing them will instantly change the parameter they represent.
  • And I needed to reclaim the ATtiny85 RST pin (pin 1) as an I/O pin.

If you are familiar with the ATtiny85 you’ll know that there is a fuse setting that will disable its response to the RST signal on pin 1 allowing you to use it as an additional I/O pin.

But you will also therefore probably know that this means you can no longer program it using the normal methods!  Once that fuse is set the only way to get back into your device is using a High Voltage Serial Programmer (HVSP).  There are a number of designs around the Internet for these and I’ve build some of them myself so can confirm they work.  The one I tend to use is a variation on this one with both 8-pin and 14-pin sockets, driven by an ATtiny2313 and some tweaked code to act as a “fuse resitter”.  I’ll perhaps post about that at some point.

You can’t set the RSTDISBL fuse from within the Arduino environment, so the general procedure is as follows:

  • Set your parameters as required for your device.
  • In “preferences” enable verbose messages on upload.
  • “Burn bootloader” to set the existing fuses – this will give you the command used to set the fuses.
  • In “preferences” disable verbose messages on upload again (if you want).
  • Upload your sketch to the ATtiny85.

THE NEXT COMMAND WILL “BRICK” YOUR DEVICE IF YOU DON’T HAVE A HVSP.

  • Copy the command to set the fuses and update the hfuse value to set the RSTDISBL flag (i.e. put it to zero) and run it in a command window.

Running through the above, my “burn bootloader” command looks like this:

[PATH TO AVRDUDE]/avrdude -C[PATH TO AVRDUDE CONF]/avrdude.conf -v      -pattiny85 -cusbtiny -e -Uefuse:w:0xFF:m -Uhfuse:w:0b11010111:m         -Ulfuse:w:0xE2:m -Uflash:w:[PATH TO BOOTLOADER]empty_all.hex:i

I am using a USBtiny programmer. We only want the hfuse “write” command (and really don’t want the -e option which erases the sketch in memory). You can use a ATtiny85 fuse calculator to see what the value should be – for example, see this one.

First I read back the just set hfuse value.

[PATH TO AVRDUDE]/avrdude -C[PATH TO AVRDUDE CONF]/avrdude.conf -v      -pattiny85 -cusbtiny -Uhfuse:r:out.txt

Then write the new value as calculated by the fuse calculator – RSTDISBL is the top bit of the hfuse, so for me, it read back 0xD7, so I write out 0x57 – running this command WILL “BRICK” YOUR DEVICE if you don’t have a HVSP.

[PATH TO AVRDUDE]/avrdude -C[PATH TO AVRDUDE CONF]/avrdude.conf -v      -pattiny85 -cusbtiny -Uhfuse:w:0x57:m

So with all that out of the way, this is the circuit and code I used to get a MiniMO compatible 4-potentiometer ADSR.MinoMo-ADSR2-2_schem

As I said, I’ve sacrificed some of the hardware, but the pot input stage is the same (just repeated four times), and the output and gate stages are the same as the original MinoMO design, but the output is on a different pin to free up all four ADCs. It has moved from using OC1B as the PWM output to using OC1A instead.

The code is a lot simpler as it isn’t having to handle the button and shared parameter setting via the single pot.  Instead the potentiometer values are read “live” in my version.

/*
//************************
//*      miniMO ADSR     *
//*   2016 by enveloop   *
//************************
//
   
Home
CC BY 4.0 Licensed under a Creative Commons Attribution 4.0 International license: http://creativecommons.org/licenses/by/4.0/ // --- Kevin --- Updated to build on recent Arduino IDE with ATTiny Core from: https://github.com/SpenceKonde/ATTinyCore ADSR Re-written to use four individual potentiometers and reduced inputs. Note: this sacrifices the LED output, button for input, control inputs on the pots and relies on repurposing RST as an I/O pin (see below). It also switches the output from digital I/O 4 (pin 3) to digital I/O 1 (pin 6) so that ADC4 can be used. This means switching the PWM output from OC1B on compare to OC1A. I/O 1 and 2: Outputs - control voltage (usually for amplitude) 3: Not connected - but could optionally double with the potentiometers as per the original design 4: Input - gate (note ON/OFF) Mapped to ATtiny85 I/O: --------- ADC0 - 5/A0 - PB5 - | 1 8 | - VCC ADC3 - 3/A3 - PB3 - | 2 7 | - PB2 - 2/A1 - ADC1 ADC2 - 4/A2 - PB4 - | 3 6 | - PB1 - 1 - OC1A - PWM (Output) GND - | 4 5 | - PB0 - 0 - dig I/P (Gate) --------- Map potentiometers to ADSR: ADC0 (5) - (A)ttack ADC1 (2) - (D)ecay ADC3 (3) - (S)ustain ADC2 (4) - (R)elease NOTE: To use ADC0 need to programme the fuses to repurpose RST as an I/O pin which means it is no longer possible to programme the ATtiny85 except with a high voltage serial programmer (HVSP). --- Kevin --- */ #include <avr/io.h> #include <util/delay.h> // Define Arduino pin numbers - NB: Digital and Analogue have different numbers! #define ADSR_A 5 // PB5 = ADC0 - digital I/O 5 (pin 1) is the repurposed RST pin #define ADSR_ALG_A A0 #define ADSR_D 2 // PB2 = ADC1 #define ADSR_ALG_D A1 #define ADSR_S 3 // PB3 = ADC3 #define ADSR_ALG_S A3 #define ADSR_R 4 // PB4 = ADC2 #define ADSR_ALG_R A2 #define ADSR_G 0 // Gate input #define ADSR_O 1 // OC1A output int ADSR_adc[] = { ADSR_ALG_A, ADSR_ALG_D, ADSR_ALG_S, ADSR_ALG_R }; volatile unsigned int globalTicks; //output int envelopeValue; //envelope stage control; bool readyToAttack = true; bool readyToRelease = false; const int attackLevel = 255; int currentStep = 0; int ADSR[] = { 0, //attackLength 1, //decayLength 255, //sustainLevel 0 //releaseLength }; void setup() { //disable USI to save power as we are not using it PRR = 1<<PRUSI; ADMUX = 0; //reset multiplexer settings pinMode(ADSR_G, INPUT); //digital input (gate) pinMode(ADSR_O, OUTPUT); //output (PWM on OC1A) pinMode(ADSR_A, INPUT); //analog- ADC0 - (A)ttack pinMode(ADSR_D, INPUT); //analog- ADC1 - (D)ecay pinMode(ADSR_S, INPUT); //analog- ADC2 - (S)ustain pinMode(ADSR_R, INPUT); //analog- ADC3 - (R)elease //set clock source for PWM -datasheet p94 PLLCSR |= (1 << PLLE); // Enable PLL (64 MHz) while (!(PLLCSR & (1 << PLOCK))); // Ensure PLL lock PLLCSR |= (1 << PCKE); // Enable PLL as clock source for timer 1 cli(); // Interrupts OFF (disable interrupts globally) //PWM Generation -timer 1 TCCR1 = (1 << PWM1A) | // PWM, output on PB1, compare with OCR1A (see interrupt below), reset on match with OCR1C (1 << COM1A1) | (1 << CS10); // no prescale OCR1C = 0xff; // 255 //Timer Interrupt Generation -timer 0 TCCR0A = (1 << WGM01); // Clear Timer on Compare (CTC) with OCR0A TCCR0B = (1 << CS01); // prescaled by 8 OCR0A = 0x64; // 0x64 = 100 //10000hz - 10000 ticks per second TIMSK = (1 << OCIE0A); // Enable Interrupt on compare with OCR0A sei(); // Interrupts ON (enable interrupts globally) } //Timer0 interrupt ISR(TIMER0_COMPA_vect) { //10000 ticks per second globalTicks++; } void loop() { setADSR(); triggerADSR(); } void setADSR(){ // Read one each scan if (currentStep == 2) setLevel(currentStep); // S is a level not a length else setLength (currentStep); // ADR are all lengths currentStep++; currentStep &= 0x03; } void setLength(int step) { int lengthRead = analogRead(ADSR_adc[step]) >> 6 ; //values between 0-15 ADSR[step] = lengthRead; } void setLevel(int step) { int levelRead = analogRead(ADSR_adc[step]) >> 2 ; //values between 0-255 ADSR[step] = levelRead; } int checkGATE() { return (PINB & (1<<ADSR_G)); } void triggerADSR() { // Check the Gate input if (checkGATE()) { if (readyToAttack){ readyToAttack = false; readyToRelease = true; readADS(); } } else{ if (readyToRelease){ readyToAttack = true; readyToRelease = false; readR(); } } } void readADS() { int attackLength = ADSR[0]; int decayLength = ADSR[1]; int sustainLevel = ADSR[2]; //ATTACK if (attackLength == 0) OCR1A = attackLevel; else { globalTicks = 0; for (envelopeValue = 0; envelopeValue <= 255; ){ OCR1A = envelopeValue; if (globalTicks >= attackLength) { envelopeValue++; globalTicks = 0; } } } //DECAY globalTicks = 0; if ((decayLength == 0) || (sustainLevel == attackLevel)) OCR1A = sustainLevel; else{ if (sustainLevel < attackLevel){ for (envelopeValue = attackLevel; envelopeValue >= sustainLevel; ){ OCR1A = envelopeValue; if (globalTicks >= decayLength) { envelopeValue--; globalTicks = 0; } } } } //SUSTAIN --nothing to do, we keep the last value until the "note off" trigger } void readR() { int sustainLevel = ADSR[2]; int releaseLength = ADSR[3]; //RELEASE if (releaseLength == 0) OCR1A = 0; else { OCR1A = sustainLevel; globalTicks = 0; for (envelopeValue = sustainLevel; envelopeValue >= 0;){ if (checkGATE()){ //if during R stage there's a trigger, silence and exit OCR1A = 0; return; } OCR1A = envelopeValue; if (globalTicks >= releaseLength) { envelopeValue--; globalTicks = 0; } } } }

I made one up and am pretty pleased with the results!

2020-05-21 13.10.52

Kevin

 

Permalink Leave a Comment

ATtiny85 VGA Sync

May 3, 2020 at 8:52 pm (computers, maker, Uncategorized) (, )

I’ve been reading Ben Eater’s inspiring series on producing a VGA video output from breadboards containing basic logic gates implementing counters, so wanted to see for myself how to create the timing signals for a VGA output.  There are loads of tutorials for how to produce VGA using an Arduino, with the main source appearing to be from Nick Gammon.  So for the sake of variety, I decided to see how it might work on an ATtiny85 (although naturally, many have also been there before too).

I’ve only implemented the sync signals to see if a monitor would recognise my signal.  I might see if I can get some data out at some point at some relatively low-level resolution.

The key decision is all centred around what clock frequency the ATtiny can support and how that relates to VGA video timings.  After a bit of messing around with a calculator, I settled on the following:

VESA Signal 640×480 @ 75 Hz and used the 64 MHz PLL clock mode of the ATtiny85 using a /8 prescaler – so a 8MHz clock, with each “tick” being 0.125uS.

So taking the timing values from the above linked tinyvga site, and translating that to “ticks” gives:

Scanline Pixels Time (uS) ‘Ticks’ @ 8Mhz
Visible Area 640 20.31746031746 16.546
Front Porch 16 0.50793650793651 4.063
Sync Pulse 64 2.031746031746 16.254
Back Porch 120 3.8095238095238 30.478
Whole Line 840 26.666666666667 213.333

Naturally there will have to be some rounding – I won’t be able to get fractions of a “tick” out of the ATtiny85 Timers.

This all rounds up to using the ATtiny85 Timer1 in PWM mode providing an output on OC1A to provide the H_sync pulse, then manually counting lines in the overflow interrupt routine to manually drive the V_sync pulse.

I originally wanted to use OC1B which made the pin assignments on the ATtiny85 much simpler, but ended losing an afternoon attempting to work out why I was getting no signal on OC1B. Turns out there is a bug in the ATtiny85 Compare on B for Timer1 – the compare mode bits in GTCCR are ignored unless the same bits are set for Compare on A in TCCR1.  Having found reference to this on the Internet, this kind of sounds familiar so I’m wondering if I’ve fallen foul of this before! Anyway, OC1A it is.

Pin assignments are as follows:

  • H_sync = PB1 (pin 6) OC1A
  • V_sync = PB4 (pin 3)

And the Timer1 settings are as follows:

  // Run a timer interrupt off a 64 MHz clock
  PLLCSR |= (1<<PLLE);            // Enable 64 MHz PLL clock
  while (!(PLLCSR & (1<<PLOCK))); // Wait for the PLL lock
  PLLCSR |= (1<<PCKE);            // Enable PLL as source for Timer 1

  // Timer 1 for CTC mode; set on compare with OCR1A; overflow interrupt
  TCCR1 = (1<<CTC1) |                // Reset on match with OCR1C
          (1<<CS12) |                // Prescale /8
          (1<<PWM1A) |               // PWM mode
          (1<<COM1A1) | (1<<COM1A0); // Set OC1A on compare; clear on rest
  GTCCR = 0;
  OCR1A = 15;
  OCR1C = 218;

There was some experimentation required to adjust the timings from the theoretical 213 ticks – 218 seems to give the most accurate horizontal sync frequency for some reason.  I haven’t done the maths to see why.  I was assuming I’d use 213 with a possible “leap tick” required every now and again to account for the lost 0.333 ticks to keep things in check, but turns out this worked ok for me instead.

Note that VESA 640×480@75Hz requires negative pulses, hence using “Set on Compare” rather than “Clear on Compare”.  Some modes require positive pulses so this would have to change.

The rest is just counting lines for the vertical refresh (using the line timings from tinyvga: 480-1-3-16=500), which is all done using the Timer 1 overflow interrupt, which signals the end (or in my case, start) of each horizontal line.

The final code is shown below, and sure enough, hooking this up to a monitor does indeed bring up the required mode to be recognised.

Next up, I’ll have a think about sending some data over.  Although if I want RGB outputs, that basically uses up the rest of the ATtiny85 I/O pins. That is an experiment for another day.

Kevin

// Use a ATtiny85 to output a VGA signal using
// its 5 output pins
//
//    (RST) ---------- | 1   8 | --------- (Vcc)
//    V_sync -- PB3 -- | 2   7 | -- PB2 -- R (0-0.7v)
//(0-0.7v) G -- PB4 -- | 3   6 | -- PB1 -- H_sync
//    (GND) ---------- | 4   5 | -- PB0 -- B (0-0.7v)
//
// NOTE: Wanted to use OC1B but there seems to be a bug in many ATtiny85 that
//       means that OC1B doesn't work properly unless OC1A has the same mode.
//       Consequently, using OC1A on its own to serve the purpose required.
//
// Note: Use ATtiny85 PORTB numbers
#define H_SYNC 1  // OC1A
#define V_SYNC 3
#define VGA_R  2
#define VGA_G  4
#define VGA_B  0

// Basic operating principle:
//  Timer configured to automatically create the H_sync pulses by using Timer 1
//  compare values.  H_sync needs to drop negative for a short pulse at the end
//  of each line.
//
//    <----- H pixel display ------><HFP><H_sync><HBP>
//
//  Or alternatively:
//    <H_sync><HBP><----- H pixel display -----><HFP>
//
//  Use Timer 1 to signal frequency of H_sync based on OCR1A.
//  Use Timer 1 OCR1A to specify duration of H_sync.
//  So attach H_sync signal to OC1A output.
//  Configure OC1A output as Clear or Set on Match as required
//
//    V_sync pulses are created by counting horizontal lines.
//    V scanning will be in one of the following modes:
//         In Display Area - hence H-scan is drawing pixels
//         In V front porch or back porch areas
//         In V_sync area
//
// VGA Timing Modes are taken from: http://www.tinyvga.com/vga-timing
//
// Based on the option of a 64 Mhz timing signal, choosing VESA 640x480 @ 75Hz
// gives us the following timing parameters
//
//     Screen refresh rate = 75 Hz
//     Verticle refresh    = 37.5 kHz
//     Pixel frequency     = 31.5 MHz
//
//     Timings are as follows
//      H visible area        640     20.31746031746 uS
//      H front porch          16      0.50793650793651 uS
//      H sync pulse           64      2.031746031746 uS
//      H back porch          120      3.8095238095238 uS
//      Total H line          840     26.666666666667 uS
//
//      H pulse is negative.
//
//      V visible area        480     12.8 mS
//      V front porch           1     0.026666666666667 mS
//      V sync pulse            3     0.08 mS
//      V back porch           16     0.426666666666667 mS
//      Total V frame         500     13.333333333333 mS
//
//      V pulse is negative
//
//  Using 64MHz PLL Clock as source for Timer 1 and setting Prescale to /8 gives
//  a frequency of 8 MHz and a single "tick" of 0.125 uS.
//
//  Total horizontal line timing is thus 26.666666666667/0.125 = 213.333 "ticks"
//  use 212 for OCR1C to define the line duration (same as the Timer 1 period).
//
//  At this resolution, H_sync is 2.031746031746/0.125 = 16.254 "ticks"
//  so use 15 for OCR1B to define the H_sync pulse.
//
//  As the required pulses are negative, need to use Set on Compare Match.

int V_Sync_Line;

void setup() {
  // Configure Output I/O pins
  PORTB = 0;
  DDRB = (1<<H_SYNC) | (1<<V_SYNC);

  // Run a timer interrupt off a 64 MHz clock
  PLLCSR |= (1<<PLLE);            // Enable 64 MHz PLL clock
  while (!(PLLCSR & (1<<PLOCK))); // Wait for the PLL lock
  PLLCSR |= (1<<PCKE);            // Enable PLL as source for Timer 1

  // Timer 1 for CTC mode and set on compare with OCR1A and overflow interrupt
  TCCR1 = (1<<CTC1) |                // Reset on match with OCR1C
          (1<<CS12) |                // Prescale /8
          (1<<PWM1A) |               // PWM mode
          (1<<COM1A1) | (1<<COM1A0); // Set OC1A on compare; clear on rest
  GTCCR = 0;
  OCR1A = 15;
  OCR1C = 218;  // Experimentation shows this is the best value

  // Enable Overflow Interrupt
  TIMSK = (1<<TOIE1);

  V_Sync_Line = 0;
}

void loop() {
  // put your main code here, to run repeatedly:
}

// Timer 1 Match with OCR1A interrupt
ISR (TIMER1_OVF_vect) {
   // Triggered every H_Sync line
   //
   // It automatically outputs the H_Sync pulse, so need to complete the line:
   // <---- H line Pixel output ---->
   //
   // From timings,  is approx 5.8 uS i.e. approx 23 ticks
   // So read the timer register and ensure it is > 23
   //
   while (TCNT1 < 24) {};

   if (V_Sync_Line <= 479) {
      // Visible line output
      // todo next!
    } else if (V_Sync_Line == 480) {
      // V Front Porch
    } else if ((V_Sync_Line >= 481) && (V_Sync_Line <= 483)) {
      // V Sync Pulse
     PORTB &= ~(1<<V_SYNC);
   } else { // 484 onwards
      // V Back Porch
     PORTB |= (1<<V_SYNC);
   }

   V_Sync_Line++;
   if (V_Sync_Line >= 500) {
     V_Sync_Line = 0;
   }
}

Permalink Leave a Comment