Skip to content

Instantly share code, notes, and snippets.

@benpeoples
Created July 29, 2023 14:54
Show Gist options
  • Save benpeoples/954bb29541eb6ef8b07f2da71aa23811 to your computer and use it in GitHub Desktop.
Save benpeoples/954bb29541eb6ef8b07f2da71aa23811 to your computer and use it in GitHub Desktop.
avr dmx implementation
#define F_CPU 8000000UL
#include <avr/io.h>
#include <inttypes.h>
#include <util/delay.h>
#include <avr/interrupt.h>
volatile uint8_t universe[513];
uint16_t i;
#ifndef _BITFIELD_
#define _BITFIELD_
typedef struct
{
uint8_t B0 : 1;
uint8_t B1 : 1;
uint8_t B2 : 1;
uint8_t B3 : 1;
uint8_t B4 : 1;
uint8_t B5 : 1;
uint8_t B6 : 1;
uint8_t B7 : 1;
} bitfield;
#ifdef PORTB
#define _PORTB (*((volatile bitfield*)&PORTB))
#define _PINB (*((volatile bitfield*)&PINB))
#define _DDRB (*((volatile bitfield*)&DDRB))
#endif // PORTB
#endif // _BITFIELD_
int main(void) {
cli();
UBRR0L = 1;
UBRR0H = 0;
UCSR0C = 0b00001110;
UCSR0B = 0b10011000; // enabled value, disabled value is UCSR0B = 0b10010000;
UCSR0A = 0x00;
sei();
for(int x = 1; x < 513; x++) {
universe[x] = 255;
}
while (1) {
_DDRD.B1 = 1;
_PORTD.B1 = 0;
UCSR0B = 0b10010000; // This disables the UART TX so we can use GPIO to hold it low for BREAK
_delay_us(92);
_PORTD.B1 = 1; // Pull high
_delay_us(10); // We get 2 us as the UART enables
UCSR0B = 0b10011000; // Re-enable UART TX
while (!(UCSR0A & 0b00100000)); // Wait for port to be ready
UDR0 = 0x00; // Null start code
for (i = 1; i < 513; i++) {
while (!(UCSR0A & 0b00100000));
UDR0 = universe[i];
}
// Wait for the last byte to be sent
while (!(UCSR0A & 0b00100000));
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment