Forum: Projekte & Code ATtiny102/104 WS2812 Led Driver


von Stefan A. (ripper121)


Angehängte Dateien:

Lesenswert?

Guten Morgen,
ich habe hier ein kleinen Led Driver für bis zu 10 WS2812b Led's 
geschrieben. Das war für uns billiger als 6 RGB Leds über ein 18 Kanal 
PWM controller zu steuern.
1
/*  ATTINY104_LED_DRIVER
2
*  Created: 07/07/2016 08:20:00 PM
3
*  Author : Ripper121
4
*  Drive up to 10 WS2812b Led's over Uart
5
*  Protocol: 1 Start Byte (0x01), (Led count * 3) Color Bytes, 1 CRC Byte
6
*  ACK = 0xFF, NAK = 0x00
7
*  FOSC=1000000 :"After powering up the device or after a reset the system clock is automatically
8
*  set to calibrated internal 8MHz oscillator, divided by 8" (Datasheet pag.32)
9
*   But we need at least 8MHz so we set the divider to 1
10
          __________
11
      VCC--|1       14|--GND (TPI CLK)
12
      PA0--|2       13|--PB3 (CDC TX) -->USART_Transmit()
13
(TPI DATA)  PA1--|3       12|--PB2 (CDC RX) <--USART_Receive()
14
(RESET)    PA2--|4       11|--PB1 (BUTTON)
15
      PA3--|5       10|--PB0
16
      PA4--|6        9|--PA7
17
(LED)    PA5--|7        8|--PA6
18
         \__________/
19
Atmel ATtiny104/102 Xplained Nano
20
*/
21
#include <util/delay.h>
22
#include <avr/io.h>
23
#include <avr/interrupt.h>
24
#include <avr/interrupt.h>
25
#include "ws2812.h"
26
#define LED_PIN    (1 << PA5)
27
#define BUTTON_PIN (1 << PB1)
28
29
unsigned char packCRC(unsigned char *s, unsigned char length)
30
{
31
  unsigned char c = 0;
32
  for (unsigned char i = 0; i < length; i++) {
33
    c ^= s[i];
34
  }
35
  return c;
36
}
37
38
void USART_Transmit( unsigned char data )
39
{
40
  /* Wait for empty transmit buffer */
41
  while ( !( UCSRA & (1<<UDRE)) );
42
  /* Put data into buffer, sends the data */
43
  UDR = data;
44
}
45
46
unsigned char USART_Receive( void )
47
{
48
  /* Wait for data to be received */
49
  while ( !(UCSRA & (1<<RXC)) );
50
  /* Get and return received data from buffer */
51
  return UDR;
52
}
53
54
void USART_Flush( void )
55
{
56
  unsigned char dummy;
57
  while ( UCSRA & (1<<RXC) ) dummy = UDR;
58
}
59
60
/*----------------------------------------------------------------------------------------------------------------------------------------------------------*/
61
#define LEDPIX 6 //<- Max is 10
62
#define MAXPIX LEDPIX*3
63
64
int main(void){
65
  CCP = 0xD8; //Set Unsecure
66
  CLKPSR = 0; //Clockdiv 0 -> 8Mhz
67
  PUEB |= BUTTON_PIN;         // Enable Pull-Up function in PB1.
68
  PORTB |= BUTTON_PIN;        // Set Pull-Up for the Button.
69
  DDRA |= LED_PIN;            // Configure LED pin as Output.
70
  /*Set baud rate */
71
  UBRRH = (unsigned char)(51>>8);//9600 Baud @ 8Mhz
72
  UBRRL = (unsigned char)51;//9600 Baud @ 8Mhz
73
  /*Enable receiver and transmitter */
74
  UCSRB = (1<<RXEN)|(1<<TXEN);
75
  /* Set frame format: 8data, 2stop bit */
76
  UCSRC = (1<<USBS)|(3<<UCSZ0);
77
  _delay_ms(250);
78
  PORTA |= LED_PIN;         // Switch off the LED.
79
  
80
  while(1){
81
    unsigned char rxLedPack[MAXPIX],crcRGB = 0x00,rxcrcRGB = 0x00;
82
    if(UCSRA & (1<<RXC)){
83
      if(USART_Receive()==0x01){
84
        for(unsigned char i=0;i<MAXPIX;i++){
85
          rxLedPack[i]=USART_Receive();
86
        }
87
        crcRGB = packCRC(rxLedPack,MAXPIX);
88
        rxcrcRGB = USART_Receive();
89
        USART_Flush();
90
        if(rxcrcRGB==crcRGB){
91
          ws2812_sendarray((unsigned char *)rxLedPack,MAXPIX);
92
          USART_Transmit(0xFF);
93
        }
94
        else
95
        {
96
          for(unsigned char i=0;i<MAXPIX;i++){
97
            rxLedPack[i]=0x00;
98
          }
99
          ws2812_sendarray((unsigned char *)rxLedPack,MAXPIX);
100
          USART_Transmit(0x00);
101
        }
102
      }
103
    }
104
  }
105
}

: Verschoben durch Moderator
Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.