Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion Fancy RGB LEDs with built in control chip

Received: by 10.180.8.196 with SMTP id t4mr1116631wia.3.1353074143430;
        Fri, 16 Nov 2012 05:55:43 -0800 (PST)
X-BeenThere: hacman@googlegroups.com
Received: by 10.180.75.50 with SMTP id z18ls396781wiv.4.canary; Fri, 16 Nov
 2012 05:55:42 -0800 (PST)
Received: by 10.180.81.198 with SMTP id c6mr1134267wiy.2.1353074142617;
        Fri, 16 Nov 2012 05:55:42 -0800 (PST)
Received: by 10.180.81.198 with SMTP id c6mr1134265wiy.2.1353074142605;
        Fri, 16 Nov 2012 05:55:42 -0800 (PST)
Return-Path: <alan.burli...@gmail.com>
Received: from mail-we0-f173.google.com (mail-we0-f173.google.com [74.125.82.173])
        by gmr-mx.google.com with ESMTPS id v1si31017wiw.2.2012.11.16.05.55.42
        (version=TLSv1/SSLv3 cipher=OTHER);
        Fri, 16 Nov 2012 05:55:42 -0800 (PST)
Received-SPF: pass (google.com: domain of alan.burli...@gmail.com designates 74.125.82.173 as permitted sender) client-ip=74.125.82.173;
Authentication-Results: gmr-mx.google.com; spf=pass (google.com: domain of alan.burli...@gmail.com designates 74.125.82.173 as permitted sender) smtp.mail=alan.burli...@gmail.com; dkim=pass header...@gmail.com
Received: by mail-we0-f173.google.com with SMTP id r1so1004389wey.4
        for <hacman@googlegroups.com>; Fri, 16 Nov 2012 05:55:42 -0800 (PST)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20120113;
        h=message-id:date:from:user-agent:mime-version:to:cc:subject
         :references:in-reply-to:content-type;
        bh=fbtQ2vYYKMXEBK/zyjU84EPMq8dtp1PR+fZ/xNQp1o8=;
        b=TRtpLT7QFOeyIuiv9CXV32TjumeA0PdT2AyT3Gfoo6o1PxzZzuMiZo4ug3ccp5pRtO
         DGpstfb7NFxqEL0GIN2Z6eJPiCTzAZpUgXR5rQ55igbKx0IGMoU2hrBIxGmRuVnp4tQ0
         31SFPQRl8mh4o85y6bLHKtsJvJvIdXfCPxF5ZYeJIYv4YAoaCQsYy93acIRhFa55abA5
         KSjcnsr3njQnrk5UaNbMi2ZWqfA5G2v8XPpKdtY5qmnjVtzkpNwfBn3nHPpqDG8odVd9
         uBrazcLZGjIyo+xhpk+dblZGbdnX2q1uDtxLuoTIVFB4ndt4zECeXT/ZD6jxhgfnrfO5
         NcEw==
Received: by 10.180.87.39 with SMTP id u7mr5327537wiz.6.1353074142172;
        Fri, 16 Nov 2012 05:55:42 -0800 (PST)
Return-Path: <alan.burli...@gmail.com>
Received: from [192.168.2.3] (host81-149-45-14.in-addr.btopenworld.com. [81.149.45.14])
        by mx.google.com with ESMTPS id c7sm955883wix.6.2012.11.16.05.55.39
        (version=TLSv1/SSLv3 cipher=OTHER);
        Fri, 16 Nov 2012 05:55:40 -0800 (PST)
Message-ID: <50A645DA.8070...@gmail.com>
Date: Fri, 16 Nov 2012 13:55:38 +0000
From: Alan Burlison <alan.burli...@gmail.com>
User-Agent: Mozilla/5.0 (X11; SunOS i86pc; rv:10.0.7) Gecko/20120926 Thunderbird/10.0.7
MIME-Version: 1.0
To: hacman@googlegroups.com
CC: Kimball Johnson <kimb...@bowerham.net>
Subject: Re: [HACMan] Fancy RGB LEDs with built in control chip
References: <CAKErKRCJ9xi=PEOCPqDQ19ewRhsA=8NSNwzpv02qkb3CptX...@mail.gmail.com> <50A54D8A.5070...@gmail.com> <CABimWo736qS8=ddC-_iOpQKXE9tUqeTWQ4CeVnD5870EBxD...@mail.gmail.com> <50A55F42.1000...@gmail.com> <CAGXLw-gVoHghBKLby88AJvBD0Bbd6r7mWgUqu6HwPgMaN4=...@mail.gmail.com> <CABimWo6kW7Uk0biuGpxtL+Rh-Fgqz=Fq0QeUHzxA7PuGOAz...@mail.gmail.com> <CABimWo66y8pZoqgzQ79e_Y5DdUiYZ6J6_ue+mUxtj_zokaG...@mail.gmail.com>
In-Reply-To: <CABimWo66y8pZoqgzQ79e_Y5DdUiYZ6J6_ue+mUxtj_zokaG...@mail.gmail.com>
Content-Type: multipart/mixed;
 boundary="------------050507090108090402090001"

This is a multi-part message in MIME format.
--------------050507090108090402090001
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit

On 16/11/2012 11:18, Kimball Johnson wrote:

> Seems I have it working now, i'll put up the code later.
>
> I misunderstood your timeings - they are high speed.  No idea why red
> wasn't working on my original, but I think the bringing the line up
> high again after the reset was the issue - misinterperation of the
> datasheet by my Father and I.

Yes, I'm using high speed timings and yes, I seem to remember the issue 
with red was as you say, although I've hacked the code a lot since then 
so I don't have that version around any more.

Here's a version that steps through the 7 primary/secondary colours and 
then does a slow fade across them all.  Note I'm using pin 0 on portb 
rather than pin1 so you should change the PIN #define appropriately.

I can see glitching on some of the fades on the attached code.  The 
timings on the datasheet are +-150nsec and on the AVR at 16MHz each 
instruction takes 62.5nsec, so we are talking about less than 3 
instructions. The unavoidable loop overhead is going to skew the 
timings, probably enough to cause the glitching I'm seeing.  To get 
reliable operation, it's probably going to be necessary to drop into 
assembly.

-- 
Alan Burlison
--

--------------050507090108090402090001
Content-Type: text/plain;
 name="main.c"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
 filename="main.c"

#include <avr/io.h>
#include <avr/wdt.h>
#include <avr/power.h>
#include "delay_x.h"

#define BIT(B)           (0x01 << (uint8_t)(B))
#define SET_BIT_HI(V, B) (V) |= (uint8_t)BIT(B)
#define SET_BIT_LO(V, B) (V) &= (uint8_t)~BIT(B)

#define PORT  PORTB
#define DDR    DDRB
#define PIN       0

#define RESET    50	// usec
#define ON_HI   600	// nsec
#define ON_LO   650	// nsec
#define OFF_HI  250	// nsec
#define OFF_LO 1000	// nsec

#define PAUSE  1000     // msec
#define DELAY    25	// msec
 
static void rgb(uint8_t r, uint8_t g, uint8_t b)
{
	// Reset.
	SET_BIT_LO(PORTB, PIN);
	_delay_us(RESET);

	// Green
	for (uint8_t bit = 0; bit < 8; bit++) {
		if (g & 0x80) {
			SET_BIT_HI(PORT, PIN);
			_delay_ns(ON_HI);
			SET_BIT_LO(PORT, PIN);
			_delay_ns(ON_LO);
		} else {
			SET_BIT_HI(PORT, PIN);
			_delay_ns(OFF_HI);
			SET_BIT_LO(PORT, PIN);
			_delay_ns(OFF_LO);
		}
		g <<= 1;
	}

	// Red
	for (uint8_t bit = 0; bit < 8; bit++) {
		if (r & 0x80) {
			SET_BIT_HI(PORT, PIN);
			_delay_ns(ON_HI);
			SET_BIT_LO(PORT, PIN);
			_delay_ns(ON_LO);
		} else {
			SET_BIT_HI(PORT, PIN);
			_delay_ns(OFF_HI);
			SET_BIT_LO(PORT, PIN);
			_delay_ns(OFF_LO);
		}
		r <<= 1;
	}

	// Blue
	for (uint8_t bit = 0; bit < 8; bit++) {
		if (b & 0x80) {
			SET_BIT_HI(PORT, PIN);
			_delay_ns(ON_HI);
			SET_BIT_LO(PORT, PIN);
			_delay_ns(ON_LO);
		} else {
			SET_BIT_HI(PORT, PIN);
			_delay_ns(OFF_HI);
			SET_BIT_LO(PORT, PIN);
			_delay_ns(OFF_LO);
		}
		b <<= 1;
	}

}

int main(int argc, const char *argv[])
{

	// Init system clock, disable WDT...
	clock_prescale_set(clock_div_1);
	MCUSR &= ~(1 << WDRF);
	wdt_disable();
	
	// Configure pin
	SET_BIT_HI(DDR, PIN);
	// Off
	rgb(0, 0, 0);

	for (;;) {
		// Red
		rgb(255, 0, 0);
		_delay_ms(PAUSE);
		// Yellow
		rgb(255, 255, 0);
		_delay_ms(PAUSE);
		// Green
		rgb(0, 255, 0);
		_delay_ms(PAUSE);
		// Cyan
		rgb(0, 255, 255);
		_delay_ms(PAUSE);
		// Blue
		rgb(0, 0, 255);
		_delay_ms(PAUSE);
		// Magenta
		rgb(255, 0, 255);
		_delay_ms(PAUSE);
		// White
		rgb(255, 255, 255);
		_delay_ms(PAUSE);

		// Off
		rgb(0, 0, 0);
		_delay_ms(PAUSE);

		// Red up
		for (int i = 0; i <= 255; i++) {
			rgb(i, 0, 0);
			_delay_ms(DELAY);
		}

		// Red -> yellow	
		for (int i = 0; i <= 255; i++) {
			rgb(255, i, 0);
			_delay_ms(DELAY);
		}
		// Yellow -> green
		for (int i = 0; i <= 255; i++) {
			rgb(255 - i, 255, 0);
			_delay_ms(DELAY);
		}
		// Green -> cyan
		for (int i = 0; i <= 255; i++) {
			rgb(0, 255, i);
			_delay_ms(DELAY);
		}
		// Cyan -> blue
		for (int i = 0; i <= 255; i++) {
			rgb(0, 255 - i, 255);
			_delay_ms(DELAY);
		}
		// Blue -> magenta
		for (int i = 0; i <= 255; i++) {
			rgb(i, 0, 255);
			_delay_ms(DELAY);
		}
		// Magenta -> white
		for (int i = 0; i <= 255; i++) {
			rgb(255, i, 255);
			_delay_ms(DELAY);
		}

		// White -> Off
		for (int i = 0; i <= 255; i++) {
			rgb(255 - i, 255 - i, 255 - i);
			_delay_ms(DELAY);
		}

		_delay_ms(PAUSE);
	}
}

--------------050507090108090402090001--