Here’s a project from back in 2011 ..
Testing wordpress code syntax hilighting and youtube support.
Yes, there are NO resistors limiting the current to those LEDs, it was a proof of concept.
and the code for this project :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 | // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY // KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE // IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR // PURPOSE. // // This material may not be duplicated in whole or in part, except for // personal use, without the express written consent of the author. // // Email: thygate@gmail.com // // Copyright (C) 1999-2011 Bob Thiry. All Rights Reserved. // // // AVR 32-channel 8-bit soft-pwm led dimmer with envelope generator // controlled by midi note messages. (experiment for Tony Decap) // // Created: 28/01/11 // Revised: 16/02/11 // // Target: ATMega324P / ATMega162 @ 16MHz // // #include <avr/io.h> #include <avr/interrupt.h> #include <string.h> /* defines */ #define NUM_CHAN 32 #define F_PWM 100 // pwm frequency #define F_TIMER1 (F_PWM * 256UL) #define T_OCR1A (F_CPU / F_TIMER1) #define F_ENVFPS 40 // envelope framerate #define F_TIMER2 (F_ENVFPS * NUM_CHAN * 1024UL) #define T_OCR2A (F_CPU / F_TIMER2) #define MIDI_CHANMASK 0x0f // midi channel mask #define MIDI_BPS 31250 // midi baudrate #define UART_UBRR ((F_CPU / (16UL * MIDI_BPS)) -1) #define ADSR_ATTACK 4 // envelope settings #define ADSR_DECAY 3 #define ADSR_RELEASE 15 #define ADSR_SUSTAIN 235 #define ENV_ATTACK (255 / ADSR_ATTACK) #define ENV_DECAY ((255 - ADSR_SUSTAIN) / ADSR_DECAY) #define ENV_RELEASE (255 / ADSR_RELEASE) #define NS_OFF (0) #define NS_ATTACK (1) #define NS_DECAY (2) #define NS_SUSTAIN (3) #define NS_RELEASE (4) #define MS_STATUS (0) #define MS_NOTE (1) #define MS_VELO (2) #define LED_USEEXP 1 // use exp func #define LED_DRIVELOW 1 // leds are low active // macro for comparing phase with accumulator #if LED_DRIVELOW == 1 #define COMPAREPHASE(a, b) (a > b) #else #define COMPAREPHASE(a, b) (a < b) #endif // manually define signal names for ATMega162 #if defined(__AVR_ATmega162__) #define SIG_USART_RECV SIG_USART0_RECV #define SIG_OUTPUT_COMPARE2A SIG_OUTPUT_COMPARE2 #endif /* global variables */ volatile unsigned char phaseaccum; // phase accumulator volatile unsigned char duty[NUM_CHAN]; // active duty volatile unsigned char dutyshadow[NUM_CHAN]; // shadow duty volatile unsigned char note_value[NUM_CHAN]; // note envelope value volatile unsigned char note_states[NUM_CHAN]; // note states volatile unsigned char note_times[NUM_CHAN]; // note times volatile unsigned char env_chan; // envelope current channel volatile unsigned char midistate; // state of midi rx volatile unsigned char midistatusbyte; // last midi status byte volatile unsigned char midinote; // last midi note /* midi notenumber to pwm-channel look-up table */ const unsigned char lut_notetochan[] = { // C, C#, D, D#, E, F, F#, G, G#, A, A#, B 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // octave -1 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // octave 0 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // octave 1 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // octave 2 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // octave 3 0xff, 0xff, 16, 15, 17, 14, 18, 13, 19, 12, 20, 11, // octave 4 21, 10, 22, 9, 23, 8, 24, 7, 25, 6, 26, 5, // octave 5 27, 4, 28, 3, 29, 2, 30, 1, 31, 0, 0xff, 0xff, // octave 6 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // octave 7 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // octave 8 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff // octave 9 }; /* exponential function look-up table to linearise led brightness: f(x) = exp(x/(255/ln(255))) */ const unsigned char lut_exp[] = { 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x09, 0x09, 0x09, 0x09, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0b, 0x0b, 0x0b, 0x0b, 0x0c, 0x0c, 0x0c, 0x0c, 0x0d, 0x0d, 0x0d, 0x0e, 0x0e, 0x0e, 0x0f, 0x0f, 0x0f, 0x10, 0x10, 0x10, 0x11, 0x11, 0x11, 0x12, 0x12, 0x13, 0x13, 0x14, 0x14, 0x14, 0x15, 0x15, 0x16, 0x16, 0x17, 0x17, 0x18, 0x18, 0x19, 0x1a, 0x1a, 0x1b, 0x1b, 0x1c, 0x1d, 0x1d, 0x1e, 0x1e, 0x1f, 0x20, 0x21, 0x21, 0x22, 0x23, 0x24, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x3a, 0x3b, 0x3c, 0x3e, 0x3f, 0x40, 0x42, 0x43, 0x45, 0x46, 0x48, 0x49, 0x4b, 0x4d, 0x4e, 0x50, 0x52, 0x54, 0x56, 0x57, 0x59, 0x5b, 0x5d, 0x5f, 0x62, 0x64, 0x66, 0x68, 0x6a, 0x6d, 0x6f, 0x72, 0x74, 0x77, 0x79, 0x7c, 0x7f, 0x82, 0x84, 0x87, 0x8a, 0x8d, 0x90, 0x94, 0x97, 0x9a, 0x9e, 0xa1, 0xa5, 0xa8, 0xac, 0xb0, 0xb4, 0xb8, 0xbc, 0xc0, 0xc4, 0xc8, 0xcd, 0xd1, 0xd6, 0xdb, 0xdf, 0xe4, 0xe9, 0xee, 0xf4, 0xf9, 0xff }; /* midi noteOn handler */ inline void midi_noteOn(unsigned char notenumber) { unsigned char ch = lut_notetochan[notenumber]; if (ch >= NUM_CHAN) return; note_states[ch] = NS_ATTACK; note_times[ch] = 0; } /* midi noteOff handler */ inline void midi_noteOff(unsigned char notenumber) { unsigned char ch = lut_notetochan[notenumber]; if (ch >= NUM_CHAN) return; note_states[ch] = NS_RELEASE; note_times[ch] = 0; } /* uart rx complete ISR : midi byte received */ ISR(SIG_USART_RECV) { unsigned char data = UDR0; switch (midistate) { case MS_STATUS: if ((data & 0x80) && (data & MIDI_CHANMASK)) { midistatusbyte = data; midistate = MS_NOTE; } break; case MS_NOTE: midinote = data; midistate = MS_VELO; break; case MS_VELO: if (midistatusbyte & 0x10) { if (data > 0) midi_noteOn(midinote); else midi_noteOff(midinote); } else { midi_noteOff(midinote); } midistate = MS_STATUS; break; default: break; } } /* timer 2 output compare a ISR : envelope generator */ ISR(SIG_OUTPUT_COMPARE2A) { switch (note_states[env_chan]) { case NS_ATTACK: if (note_times[env_chan] == 0) note_value[env_chan] = 0; if (++note_times[env_chan] == ADSR_ATTACK) { note_states[env_chan] = NS_DECAY; note_times[env_chan] = 0; } note_value[env_chan] += ENV_ATTACK; break; case NS_DECAY: if (++note_times[env_chan] == ADSR_DECAY) note_states[env_chan] = NS_SUSTAIN; if (note_value[env_chan] > ADSR_SUSTAIN) note_value[env_chan] -= ENV_DECAY; break; case NS_RELEASE: if (++note_times[env_chan] == ADSR_RELEASE) { note_states[env_chan] = NS_OFF; note_value[env_chan] = 0; } if (note_value[env_chan] > ENV_RELEASE) note_value[env_chan] -= ENV_RELEASE; else { note_value[env_chan] = 0; note_states[env_chan] = NS_OFF; } break; case NS_OFF: case NS_SUSTAIN: default: break; } // assign duty cycle from envelope value #if LED_USEEXP == 0 dutyshadow[env_chan] = note_value[env_chan]; #else // use exp lut to compensate for non-linearity of led brightness dutyshadow[env_chan] = lut_exp[note_value[env_chan]]; #endif // loop through channels if (++env_chan == NUM_CHAN) env_chan = 0; } /* init, setup io and peripherals */ void init(void) { #if defined(__AVR_ATmega162__) // all ports are all outputs DDRA = 0xff; DDRB = 0xff; DDRC = 0xff; DDRD = 0xff; DDRE = 0xff; // pwm timer // setup timer 1 for CTC mode, prescaler = 1 TCCR1B = (1<<WGM12)|(1<<CS10); OCR1A = T_OCR1A; // envelope timer // setup timer 2 for CTC mode, prescaler = 1024 TCCR2 = (1<<WGM21)|(1<<CS22)|(1<<CS21)|(1<<CS20); OCR2 = T_OCR2A; TIMSK = (1<<OCIE1A)|(1<<OCIE2); // init uart 0 UCSR0B = (1<<RXCIE0)|(1<<RXEN0); UCSR0C = (1<<UCSZ00)|(1<<UCSZ01); UBRR0H = (UART_UBRR >> 8); UBRR0L = (UART_UBRR & 0xff); #elif defined(__AVR_ATmega324P__) // all ports are all outputs DDRA = 0xff; DDRB = 0xff; DDRC = 0xff; DDRD = 0xff; // pwm timer // setup timer 1 for CTC mode, prescaler = 1 TCCR1B = (1<<WGM12)|(1<<CS10); OCR1A = T_OCR1A; TIMSK1 = (1<<OCIE1A); // envelope timer // setup timer 2 for CTC mode, prescaler = 1024 TCCR2A = (1<<WGM21); TCCR2B = (1<<CS22)|(1<<CS21)|(1<<CS20); OCR2A = T_OCR2A; TIMSK2 = (1<<OCIE2A); // init uart 0 UCSR0B = (1<<RXCIE0)|(1<<RXEN0); UCSR0C = (1<<UCSZ00)|(1<<UCSZ01); UBRR0 = UART_UBRR; #else #error only ATMega324 and ATMega162 are supported. #endif // enable interrupts sei(); } /* main */ int main(void) { init(); while (1) { asm("nop;"); } } /* timer 1 output compare ISR : pwm generator */ ISR(SIG_OUTPUT_COMPARE1A) { // copy shadow values to actual values at beginning of pwm cycle if (phaseaccum == 0) memcpy(&duty, &dutyshadow, NUM_CHAN); // increment phase accumulator phaseaccum++; // pointer to first element unsigned char *pd = (unsigned char*)duty; // do the magic if COMPAREPHASE(*pd, phaseaccum) { PORTA &= ~(1<<0); } else { PORTA |= (1<<0); } pd++; // 00 if COMPAREPHASE(*pd, phaseaccum) { PORTA &= ~(1<<1); } else { PORTA |= (1<<1); } pd++; // 01 if COMPAREPHASE(*pd, phaseaccum) { PORTA &= ~(1<<2); } else { PORTA |= (1<<2); } pd++; // 02 if COMPAREPHASE(*pd, phaseaccum) { PORTA &= ~(1<<3); } else { PORTA |= (1<<3); } pd++; // 03 if COMPAREPHASE(*pd, phaseaccum) { PORTA &= ~(1<<4); } else { PORTA |= (1<<4); } pd++; // 04 if COMPAREPHASE(*pd, phaseaccum) { PORTA &= ~(1<<5); } else { PORTA |= (1<<5); } pd++; // 05 if COMPAREPHASE(*pd, phaseaccum) { PORTA &= ~(1<<6); } else { PORTA |= (1<<6); } pd++; // 06 if COMPAREPHASE(*pd, phaseaccum) { PORTA &= ~(1<<7); } else { PORTA |= (1<<7); } pd++; // 07 if COMPAREPHASE(*pd, phaseaccum) { PORTB &= ~(1<<0); } else { PORTB |= (1<<0); } pd++; // 08 if COMPAREPHASE(*pd, phaseaccum) { PORTB &= ~(1<<1); } else { PORTB |= (1<<1); } pd++; // 09 if COMPAREPHASE(*pd, phaseaccum) { PORTB &= ~(1<<2); } else { PORTB |= (1<<2); } pd++; // 10 if COMPAREPHASE(*pd, phaseaccum) { PORTB &= ~(1<<3); } else { PORTB |= (1<<3); } pd++; // 11 if COMPAREPHASE(*pd, phaseaccum) { PORTB &= ~(1<<4); } else { PORTB |= (1<<4); } pd++; // 12 if COMPAREPHASE(*pd, phaseaccum) { PORTB &= ~(1<<5); } else { PORTB |= (1<<5); } pd++; // 13 if COMPAREPHASE(*pd, phaseaccum) { PORTB &= ~(1<<6); } else { PORTB |= (1<<6); } pd++; // 14 if COMPAREPHASE(*pd, phaseaccum) { PORTB &= ~(1<<7); } else { PORTB |= (1<<7); } pd++; // 15 if COMPAREPHASE(*pd, phaseaccum) { PORTC &= ~(1<<0); } else { PORTC |= (1<<0); } pd++; // 16 if COMPAREPHASE(*pd, phaseaccum) { PORTC &= ~(1<<1); } else { PORTC |= (1<<1); } pd++; // 17 if COMPAREPHASE(*pd, phaseaccum) { PORTC &= ~(1<<2); } else { PORTC |= (1<<2); } pd++; // 18 if COMPAREPHASE(*pd, phaseaccum) { PORTC &= ~(1<<3); } else { PORTC |= (1<<3); } pd++; // 19 if COMPAREPHASE(*pd, phaseaccum) { PORTC &= ~(1<<4); } else { PORTC |= (1<<4); } pd++; // 20 if COMPAREPHASE(*pd, phaseaccum) { PORTC &= ~(1<<5); } else { PORTC |= (1<<5); } pd++; // 21 if COMPAREPHASE(*pd, phaseaccum) { PORTC &= ~(1<<6); } else { PORTC |= (1<<6); } pd++; // 22 if COMPAREPHASE(*pd, phaseaccum) { PORTC &= ~(1<<7); } else { PORTC |= (1<<7); } pd++; // 23 /*if COMPAREPHASE(*pd, phaseaccum) { PORTD &= ~(1<<0); } else { PORTD |= (1<<0); }*/ pd++; // 24 /*if COMPAREPHASE(*pd, phaseaccum) { PORTD &= ~(1<<1); } else { PORTD |= (1<<1); }*/ pd++; // 25 if COMPAREPHASE(*pd, phaseaccum) { PORTD &= ~(1<<2); } else { PORTD |= (1<<2); } pd++; // 26 if COMPAREPHASE(*pd, phaseaccum) { PORTD &= ~(1<<3); } else { PORTD |= (1<<3); } pd++; // 27 if COMPAREPHASE(*pd, phaseaccum) { PORTD &= ~(1<<4); } else { PORTD |= (1<<4); } pd++; // 28 if COMPAREPHASE(*pd, phaseaccum) { PORTD &= ~(1<<5); } else { PORTD |= (1<<5); } pd++; // 29 if COMPAREPHASE(*pd, phaseaccum) { PORTD &= ~(1<<6); } else { PORTD |= (1<<6); } pd++; // 30 if COMPAREPHASE(*pd, phaseaccum) { PORTD &= ~(1<<7); } else { PORTD |= (1<<7); } // 31 } |
there.
can’t help but feel like there’s a more modern looking plugin out there..
admin edit: me too
i’m replying to myself now. will there be indentation ?
yes, yes, not looking too great either ..
let’s test some html tags
will it work ? i suspect not ..
well i’ll be a monkey’s uncle
fugly indentation