I am using an ATmega328p to control this RGB LED module. The ATmega is on an Arduino Uno, but I am programming it in C, without using the Arduino libraries.
To power the RGB module I am also using a 12V power supply. To power the Arudino Uno I am using the USB power. I connected it the following way:
- RGB LED Module 12V -> 12V power supply 12V
- RGB LED Module GND -> 12V power supply GND
- RGB LED Module SDI -> ATmega PORTB0
- RGB LED Module SCK -> ATmega PORTB1
- RGB LED Module GND -> ATmega GND
Now if I send some data R=0xFF, G=0xFF, B=0xFF (Set SDI to 5V and make every second 24 SCK pulses) according to the datasheet nothing happens. The lights stay off. But randomly I found out, that if I disconnect the 12V power supply and connected it again randomly, sometimes the Lights turning on in a random color. So I don't have any clue whats going on.
What can cause this behavior, what can I do to find the error?
#include <avr/io.h>
// 16ms delay.
static void delShort()
{
TCCR0B = 0;
TCCR0A = 0;
TIMSK0 = 0;
TCNT0 = 0;
GTCCR = 0;
TIFR0 = TIFR0 | (1 << TOV0);
TCCR0B = (1 << CS02) | (1 << CS00);
while(!(TIFR0 & (1 << TOV0)))
{
}
TCCR0B = 0;
}
// 1000ms delay.
static void delLong()
{
for(unsigned char lI = 0; lI < 61; ++lI)
{
delShort();
}
}
static void ledSetup()
{
PORTB &= ~(1 << PORTB0);
DDRB |= (1 << DDB0);
PORTB &= ~(1 << PORTB1);
DDRB |= (1 << DDB1);
}
static void ledWrite(uint32_t pData)
{
uint32_t lValue = pData;
for(uint8_t lIndex = 0; lIndex < 24; ++lIndex)
{
if(lValue & 0x1)
{
PORTB |= (1 << PORTB0);
}
else
{
PORTB &= ~(1 << PORTB0);
}
delShort();
PORTB |= (1 << PORTB1);
delShort();
PORTB &= ~(1 << PORTB1);
delShort();
lValue = lValue >> 1;
}
}
void main(void)
{
ledSetup();
delLong();
while (1)
{
delLong();
ledWrite(0xFFFFFFFF);
}
}