diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..4823d5e --- /dev/null +++ b/Makefile @@ -0,0 +1,23 @@ +PROGRAM_NAME = mqtt_ledpanel +SRC_DIR = src +BUILD_DIR = build + +all: $(PROGRAM_NAME) + +# Create the build directory +$(BUILD_DIR): + mkdir -pv $(BUILD_DIR) + +# Compile and build the program for Atmega328P +$(PROGRAM_NAME): $(BUILD_DIR) + avr-gcc -mmcu=atmega328p -Wall -Os -o $(BUILD_DIR)/$(PROGRAM_NAME).elf $(SRC_DIR)/$(PROGRAM_NAME).c + + avr-objcopy -j .text -j .data -O ihex $(BUILD_DIR)/$(PROGRAM_NAME).elf $(BUILD_DIR)/$(PROGRAM_NAME).hex + +# Upload the built program (hex file) to Atmega328P using USBasp +upload: $(PROGRAM_NAME) + avrdude -c arduino -p m328p -P /dev/ttyUSB0 -b 57600 -U flash:w:$(BUILD_DIR)/$(PROGRAM_NAME).hex -D + +# Remove build directory with all built files +clean: + rm -rf $(BUILD_DIR) diff --git a/src/.led_panel_example.c.swp b/src/.led_panel_example.c.swp new file mode 100644 index 0000000..18d2115 Binary files /dev/null and b/src/.led_panel_example.c.swp differ diff --git a/src/.mqtt_ledpanel.c.swp b/src/.mqtt_ledpanel.c.swp new file mode 100644 index 0000000..09be949 Binary files /dev/null and b/src/.mqtt_ledpanel.c.swp differ diff --git a/src/.si5351_rfgen.c.swp b/src/.si5351_rfgen.c.swp new file mode 100644 index 0000000..0dcf27c Binary files /dev/null and b/src/.si5351_rfgen.c.swp differ diff --git a/src/led_panel_example.c b/src/led_panel_example.c new file mode 100644 index 0000000..ac4ea78 --- /dev/null +++ b/src/led_panel_example.c @@ -0,0 +1,206 @@ +#include +#include +#include + +/// ATMEGA 328p + +#define ENABLE PD7 +#define LATCH PB0 +#define CLOCK PB1 + +#define ROT PD6 +#define GRÜN PD5 +#define BLAU PD4 + + + + + + + +void sendSPI (uint16_t SPIdata); + + uint16_t count = 0; + uint16_t value ; + + uint16_t SPIstring [16]; + uint16_t SPIbuffer = 0; + uint16_t SPIstring_count; + + uint8_t bcd [5]; + + + +//////////////////////////////////////// M A I N ///////////////////////////////////////////// +int main(void) +{ + + DDRD = 0xF0; // PD0-3 als Eingang, PD4-7 als Ausgang setzen + DDRB = 0x0F; // PB0-3 als Ausgang, PD4-7 als Ausgang setzen + PORTD = 0x0F; // Pullup für PD0-3 aktivieren. + // Initialisierung des LED + PORTB |= (1<> 1 ; // Byte um 1 nach links verschieben + PORTB |= (1 < +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include "uart.h" + +#define BAUDRATE 9600 +#define TRUE 1 +#define FALSE 0 + +#define CHAR_NEWLINE '\n' +#define CHAR_RETURN '\r' +#define RETURN_NEWLINE "\r\n" +#define CR "\r\n" +volatile unsigned char data_in[32]; +unsigned char command_in[32]; + +volatile unsigned int data_count; +volatile unsigned char command_ready; +volatile unsigned char remote; + +//volatile char cmd[32], value[32], unit[32]; +unsigned long t1, freq; + +#define ENABLE PD7 +#define LATCH PB0 +#define CLOCK PB1 + +#define ROT PD6 +#define GRÜN PD5 +#define BLAU PD4 + + + + +void sendSPI (uint16_t SPIdata); + + uint16_t count = 0; + uint16_t value ; + + uint16_t SPIstring [16]; + uint16_t SPIbuffer = 0; + uint16_t SPIstring_count; + + uint8_t bcd [5]; + + + +//////////////////////////////// BCD to bitmap converting /////////////////////////////////// + +void LED_putC (uint8_t putChar ){ +switch (putChar){ + case 0: SPIstring[SPIstring_count] = 0x5552; SPIstring[SPIstring_count+4] = 0x0255; break; + case 1: SPIstring[SPIstring_count] = 0x2232; SPIstring[SPIstring_count+4] = 0x0722; break; + case 2: SPIstring[SPIstring_count] = 0x2452; SPIstring[SPIstring_count+4] = 0x0712; break; + case 3: SPIstring[SPIstring_count] = 0x2452; SPIstring[SPIstring_count+4] = 0x0254; break; + case 4: SPIstring[SPIstring_count] = 0x7554; SPIstring[SPIstring_count+4] = 0x0444; break; + case 5: SPIstring[SPIstring_count] = 0x3117; SPIstring[SPIstring_count+4] = 0x0344; break; + case 6: SPIstring[SPIstring_count] = 0x3116; SPIstring[SPIstring_count+4] = 0x0255; break; + case 7: SPIstring[SPIstring_count] = 0x2447; SPIstring[SPIstring_count+4] = 0x0112; break; + case 8: SPIstring[SPIstring_count] = 0x2552; SPIstring[SPIstring_count+4] = 0x0255; break; + case 9: SPIstring[SPIstring_count] = 0x5552; SPIstring[SPIstring_count+4] = 0x0346; break; + } + SPIstring_count++; + if (SPIstring_count &0x04) SPIstring_count = SPIstring_count + 4; + } +///////////////////////// END of Data to bitmap converting /////////////////////////////////// + + + +/////////////////////////////////SPI-STRING//////////////////////////////////////////// +// Hier wird 16bit-Daten & Takt & /CS an SPI-PORT rausschicken & gesteuert. +void sendSPI (uint16_t SPIdata){ + SPIbuffer = SPIdata ; // Daten an MAX504 anpassen, 2 bit nach links verschieben + + int ii=1; + while(ii <= 16) { // 16mal wiederholen. + ii++; + PORTB &= ~(1 << PB1); // Takt fallende Flanke + _delay_us (1); // Verzögerung von 1µS + PORTD &= ~(1 << PD4); // MOSI auf LOW + PORTD &= ~(1 << PD5); // MOSI auf LOW + PORTD &= ~(1 << PD6); // MOSI auf LOW + if (SPIbuffer &0x0001) { // wenn 1, dann MOSI = H + PORTD |= (1 <> 1 ; // Byte um 1 nach links verschieben + PORTB |= (1 <>8); + UBRR0L = (uint8_t) (ubrr); + // Interrupts kurz deaktivieren + cli(); + // UART Receiver und Transmitter anschalten, Receive-Interrupt aktivieren + // Data mode 8N1, asynchron + UCSR0B = (1 << RXEN0) | (1 << TXEN0) | (1 << RXCIE0); + UCSR0C = (0 << 4) | (0 << 3) | (3 << 1); + // Flush Receive-Buffer (entfernen evtl. vorhandener ungültiger Werte) + do + { + // UDR auslesen (Wert wird nicht verwendet) + UDR0; + } + while (UCSR0A & (1 << RXC0)); + + // Rücksetzen von Receive und Transmit Complete-Flags + UCSR0A = (1 << RXC0) | (1 << TXC0); + +} + + +int uart_putc (const uint8_t c) +{ + // Warten, bis UDR bereit ist für einen neuen Wert + while (!(UCSR0A & (1 << UDRE0))) + ; + + // UDR Schreiben startet die Übertragung + UDR0 = c; + + return 1; +} + +void uart_puts (const char *s) +{ + do + { + uart_putc (*s); + } + while (*s++); +} + + +void usart_ok() +{ + uart_puts("OK\r\n"); +} + +void copy_command () +{ + // The USART might interrupt this - don't let that happen! + ATOMIC_BLOCK(ATOMIC_FORCEON) { + // Copy the contents of data_in into command_in + memcpy(command_in, data_in, 32); + + // Now clear data_in, the USART can reuse it now + memset(data_in, 0, 32); + } +} + +//void split_command(char str[]){ +// str[strlen(str)-1] = '\0'; +// strcpy(cmd, strtok(str , " ")); +// strcpy(value, strtok(NULL, " ")); +// strcpy(unit , strtok(NULL, " ")); +//} + +//////////////////////////////////////// M A I N ///////////////////////////////////////////// +int main(void) +{ + + DDRD = 0xF0; // PD0-3 als Eingang, PD4-7 als Ausgang setzen + DDRB = 0x0F; // PB0-3 als Ausgang, PD4-7 als Ausgang setzen + PORTD = 0x0F; // Pullup für PD0-3 aktivieren. + // Initialisierung des LED + PORTB |= (1< + +extern void uart_init (void); +extern int uart_putc (const uint8_t); +extern char uart_getc (void); + +static inline void uart_flush (void) +{ + while (UCSR0B & (1 << UDRIE0)); +} + +#endif /* UART_H */