用M8控制RGBLED测试了一下,效果不错。并做了一个上位机软件,通过USB控制,现将资料整理上传与君共享!
原理图
源代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
|
#include <avr/io.h> #include <avr/interrupt.h> #include <avr/pgmspace.h> #include <avr/wdt.h> #include "usbdrv.h"
typedef struct { unsigned char r; unsigned char g; unsigned char b; } RGB; #define Ledport PORTD #define DDR_Ledport DDRD #define R_PIN 5 #define G_PIN 6 #define B_PIN 7
void init(void); void hsv_to_rgb (unsigned char h, unsigned char s, unsigned char v, RGB * out);
RGB lamp = {0,0,0}; unsigned char type,H,S,V;
void init(void) { PORTD = 0; PORTB = 0; DDRD = ~(1 << 2); DDRB = 0; DDR_Ledport = (1<<R_PIN)|(1<<G_PIN)|(1<<B_PIN); TCCR0 = 1; TIMSK = (1<<TOIE0); } uchar usbFunctionSetup(uchar data[8]) { usbRequest_t \*rq = (void \*)data; if( rq->bRequest ) { type = 1; H = rq->wValue.bytes[0]; S = rq->wValue.bytes[1]; V = rq->wValue.bytes[2]; } else { type = 0; lamp.r = rq->wValue.bytes[0]; lamp.g = rq->wValue.bytes[1]; lamp.b = rq->wValue.bytes[2]; } return 0; }
int main(void) { cli(); init(); usbInit(); sei(); H = 180; S = 255; V = 355; type = 1; for(;;) { usbPoll(); if ( type ) { type = 0; hsv_to_rgb(H,S,V,&lamp); } } return 0; }
void hsv_to_rgb (unsigned char h, unsigned char s, unsigned char v, RGB * out) { unsigned char r=0,g=0,b=0, i, f; unsigned int p, q, t; if( s == 0 ) { r = g = b = v; } else { i=h/43; f=h%43; p = (v * (255 - s))/256; q = (v * ((10710 - (s * f))/42))/256; t = (v * ((10710 - (s * (42 - f)))/42))/256; switch( i ) { case 0: r = v; g = t; b = p; break; case 1: r = q; g = v; b = p; break; case 2: r = p; g = v; b = t; break; case 3: r = p; g = q; b = v; break; case 4: r = t; g = p; b = v; break; case 5: r = v; g = p; b = q; break; } } out->r=r; out->g=g; out->b=b; }
SIGNAL (SIG_OVERFLOW0) { static unsigned char PWM_cnt; static unsigned char prescale; if(prescale++) { prescale=0; return; } if (PWM_cnt <lamp.r) Ledport |= (1 << R_PIN); else Ledport &=~(1 << R_PIN); if (PWM_cnt <lamp.g) Ledport |= (1 << G_PIN); else Ledport &=~(1 << G_PIN); if (PWM_cnt <lamp.b) Ledport |= (1 << B_PIN); else Ledport &=~(1 << B_PIN); PWM_cnt++; }
|
PC软件界面
调光效果
调试一角
资料包
- iocomp.v3.04.SP2.Delphi.BCB(文件大小:336K)
- AAFont 平滑特效字体控件包 V2.63(文件大小:12.28M)
发表链接
amoBBS 阿莫电子论坛 http://www.amobbs.com/thread-883719-1-1.html