User Tools

Site Tools


projects:rgb_strip_driver:home

Table of Contents

RGB LED strip driver

~~META:description abstract=An ATMega8 based driver for up to 8 RGB common annode LED strips, with an autonomous mode as well as USB interface.~~ led microcontroller lighting

An ATMega8 based driver for up to 8 RGB common annode LED strips, with an autonomous mode as well as USB interface.

main.c
volatile uint8_t pwm_cnt = 0;
 
typedef struct {
	uint8_t r = 0x00, g = 0x00, b = 0x00;
} color;
 
#define PWM_RES 32
 
#define CHANNELS_COUNT 8
volatile Tcolor channels[CHANNELS_COUNT];
 
void setOutputs(color c) {
	uint8_t m = 0x80;
	while(m) {
		SR_DR = c.r & m;
		SR_DG = c.g & m;
		SR_DB = c.b & m;
		SR_CK = 1;
		asm('nop');
		SR_CK = 0;
		m >>= 1;
	}
}
 
SIGNAL(PWM_TICK) {
	uint8_t i;
	color c;
	pwm_cnt++;
	if(pwm_cnt == PWM_RES) {
		c.r = 0xff;
		c.g = 0xff;
		c.b = 0xff;
		pwm_cnt = 0;
	}else for(i=0; i<CHANNELS_COUNT; i++) {
		if(pwm_cnt >= channels[i].r) c.r |= 0x01 << i;
		if(pwm_cnt >= channels[i].g) c.g |= 0x01 << i;
		if(pwm_cnt >= channels[i].b) c.b |= 0x01 << i;
	}
	setOutputs(c);
	TCNT2 = 0x00;
}
projects/rgb_strip_driver/home.txt · Last modified: 2021/02/03 14:21 (external edit)