• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Understanding Small part of AVR code

Not open for further replies.


New Member
Hello fellow Electro-Techs, thank you for your time!! I am currently in the process of combining my love for music with my fascination with ucontrollers and am trying to design a midi to cv converter by myself. I am analysing a code and am currently stuck with the following. It is for a ATtiny2313, the idea is that incoming MIDI data in converted via the Tiny, and then sent to a DAC. BITS 5,6, and 7 of PORT B are the outputs to the DAC. BIT 0 of PORT D is the MIDI input. the rest of PORT D is unused (no idea why they are high).

void make_cv() {
static uint8_t dac_value = 0;
dac_value = (last_active_key()-36)<<1;
PORTD &= ~(0b00111100); // Put Port D bit 2,3,4,5 LOW
PORTD |= (dac_value & 0x0F)<<2;
PORTB &= ~(0b11100000); // Put Port B bit 5,6,7 LOW
PORTB |= (dac_value & 0xF0)<<1;

So last_active_key is a function that returns (as expected) the last key played on a monosynth. Why is it subtracted from 36? what is happening there?

and what does << do?

I have attached a Schematic if it helps! :)


Last edited:


Active Member
dac_value = (last_active_key()-36)<<1;
I guess the subtracting of 36 is a "Midi Thing".
Look at MiDi documentation to find out what happens with this - I guess that is the lowest transmitted note.
That's a command to shift binary left.
The nuber behind tells You the number of shifts.
The MSB got be lost at this action.
Original Byte = 0b00000011
Shift by 1 0b00000011<<1 = 0b00000110
Shift by 2 0b00000011<<2 = 0b00001100

The shift also works with 16, 32, 64, ... Bit variables.

I guess You read something wrong in the schematic.
Pins that are signed with the same label are connected.
PD2..5 from ADC are connected with PD2...5 of the Controller.
Last edited:
Not open for further replies.

Latest threads

EE World Online Articles