极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 15133|回复: 4

8*8 RGB 74ls595 点阵

[复制链接]
发表于 2013-10-10 08:40:12 | 显示全部楼层 |阅读模式
    在舍友提议下,做了个。8*8RGB点阵。功能有单色、彩色、各种图形显示,循环显示数字0至9、左右移动;整体刷屏。目前屏大小只有8*8,还不支持显示汉字,不过驱动板留出了级联接口,慢慢完善。

参考:http://blog.spitzenpfeil.org/wordpress/wp-content/uploads/2008/10/matrix_code.pde

附上原理图:

附上代码:
只需要连接三根线,74595的SCK、RCK、SER(需要级联,前一个595的QH连接到下一个的SER)

  1. #define __spi_clock 13   // SCK
  2. #define __spi_latch 10   //RCK
  3. #define __spi_data 11    //SER+

  4. #define __display_enable 9
  5. #define __rows 8
  6. #define __max_row __rows-1
  7. #define __leds_per_row 8
  8. #define __max_led __leds_per_row-1
  9. #define __brightness_levels 32 // 0...15 above 28 is bad for ISR ( move to timer1, lower irq freq ! )
  10. #define __max_brightness __brightness_levels-1
  11. #define __fade_delay 4

  12. #define __TIMER1_MAX 0xFFFF // 16 bit CTR
  13. #define __TIMER1_CNT 0x0130 // 32 levels --> 0x0130; 38 --> 0x0157 (flicker)
  14. #include <avr/interrupt.h>   
  15. #include <avr/io.h>
  16. #include <stdint.h>

  17. byte brightness_red[__leds_per_row][__rows];
  18. byte brightness_green[__leds_per_row][__rows];
  19. byte brightness_blue[__leds_per_row][__rows];

  20. ISR(TIMER1_OVF_vect) {
  21.   //TCNT2 = __TIMER2_MAX - __TIMER2_CNT; // precharge TIMER2 to maximize ISR time --> max led brightness
  22.   TCNT1 = __TIMER1_MAX - __TIMER1_CNT;
  23.   byte cycle;
  24.   
  25.   digitalWrite(__display_enable,LOW); // enable display inside ISR
  26.   
  27.   for(cycle = 0; cycle < __max_brightness; cycle++) {
  28.     byte led;
  29.     byte row = B00000000;    // row: current source. on when (1)
  30.     byte red;    // current sinker when on (0)
  31.     byte green;  // current sinker when on (0)
  32.     byte blue;   // current sinker when on (0)

  33.     for(row = 0; row <= __max_row; row++) {
  34.       
  35.       red = B11111111;    // off
  36.       green = B11111111;  // off
  37.       blue = B11111111;   // off
  38.       
  39.    for(led = 0; led <= __max_led; led++) {
  40.         if(cycle < brightness_red[row][led]) {
  41.           red &= ~(1<<led);
  42.         }
  43.         if(cycle < brightness_green[row][led]) {
  44.           green &= ~(1<<led);
  45.         }
  46.         if(cycle < brightness_blue[row][led]) {
  47.           blue &= ~(1<<led);
  48.         }
  49.       }
  50.       digitalWrite(__spi_latch,LOW);
  51.       spi_transfer(blue);
  52.       spi_transfer(green);
  53.       spi_transfer(red);
  54.       spi_transfer(B00000001<<row);
  55.       digitalWrite(__spi_latch,HIGH);
  56.       digitalWrite(__spi_latch,LOW);
  57.     }
  58.   }

  59.   digitalWrite(__display_enable,HIGH);    // disable display outside ISR
  60. }

  61. int data[]={0x00,0x36,0x7f,0x7f,0x3e,0x1c,0x08,0x00,//xin
  62.            0x00,0x06,0x69,0x90,0x00,0x42,0x24,0x18,/*"xiaonian",0*/
  63. 0x00,0x10,0x30,0x10,0x10,0x10,0x10,0x38,/*"1",0*/
  64. 0x00,0x18,0x24,0x04,0x08,0x10,0x20,0x3C,/*"2",1*/
  65. 0x00,0x18,0x24,0x04,0x18,0x04,0x24,0x18,/*"3",2*/
  66. 0x00,0x08,0x18,0x18,0x28,0x28,0x3C,0x08,/*"4",3*/
  67. 0x00,0x3C,0x20,0x20,0x38,0x04,0x04,0x38,/*"5",4*/
  68. 0x00,0x18,0x24,0x20,0x38,0x24,0x24,0x18,/*"6",5*/
  69. 0x00,0x3C,0x24,0x04,0x08,0x10,0x10,0x10,/*"7",6*/
  70. 0x00,0x18,0x24,0x24,0x18,0x24,0x24,0x18,/*"8",7*/
  71. 0x00,0x18,0x24,0x24,0x1C,0x04,0x24,0x18,/*"9",8*/
  72. 0x00,0x18,0x24,0x24,0x24,0x24,0x24,0x18,/*"0",9*/
  73.            };
  74. void setup(void) {
  75.   //Serial.begin(9600);
  76.   randomSeed(555);
  77.   byte ctr1;
  78.   byte ctr2;
  79.   
  80.   pinMode(__spi_clock,OUTPUT);
  81.   pinMode(__spi_latch,OUTPUT);
  82.   pinMode(__spi_data,OUTPUT);
  83.   pinMode(__display_enable,OUTPUT);
  84.   digitalWrite(__spi_latch,LOW);
  85.   digitalWrite(__spi_data,LOW);
  86.   digitalWrite(__spi_clock,LOW);
  87.    
  88.   setup_hardware_spi();
  89.   delay(10);
  90.   set_matrix_rgb(0,0,0);
  91.   setup_timer1_ovf();
  92. }

  93. void loop(void) {
  94.    int ctr;
  95.   
  96.   for(ctr=0; ctr < 2; ctr++) {
  97.     fader_hue();//整体刷屏,颜色随设定值改变
  98.   }
  99.   for(ctr=0; ctr < 1; ctr++) {
  100.     matrix_test();//从上往下刷屏,颜色随设定值改变
  101.   }

  102.   for(ctr=0; ctr < 4; ctr++) {
  103.     matrix_heart_2();    //彩色
  104.    // matrix_heart(100); //单色
  105.   }
  106. }

  107. byte spi_transfer(byte data)
  108. {
  109.   SPDR = data;                    // Start the transmission
  110.   while (!(SPSR & (1<<SPIF)))     // Wait the end of the transmission
  111.   {
  112.   };
  113.   return SPDR;                    // return the received byte, we don't need that
  114. }


  115. void set_led_red(byte row, byte led, byte red) {
  116.   brightness_red[row][led] = red;
  117. }

  118. void set_led_green(byte row, byte led, byte green) {
  119.   brightness_green[row][led] = green;
  120. }

  121. void set_led_blue(byte row, byte led, byte blue) {
  122.   brightness_blue[row][led] = blue;
  123. }

  124. void set_led_rgb(byte row, byte led, byte red, byte green, byte blue) {
  125.   set_led_red(row,led,red);
  126.   set_led_green(row,led,green);
  127.   set_led_blue(row,led,blue);
  128. }


  129. void set_matrix_rgb(byte red, byte green, byte blue) {
  130.   byte ctr1;
  131.   byte ctr2;
  132.   for(ctr2 = 0; ctr2 <= __max_row; ctr2++) {
  133.     for(ctr1 = 0; ctr1 <= __max_led; ctr1++) {
  134.       set_led_rgb(ctr2,ctr1,red,green,blue);
  135.     }
  136.   }
  137. }


  138. void set_led_hue(byte row, byte led, int hue) {

  139.   // see wikipeda: HSV
  140.   float S=100.0,V=100.0,s=S/100.0,v=V/100.0,h_i,f,p,q,t,R,G,B;
  141.    
  142.     hue = hue%360;
  143.     h_i = hue/60;            
  144.     f = (float)(hue)/60.0 - h_i;
  145.     p = v*(1-s);
  146.     q = v*(1-s*f);
  147.     t = v*(1-s*(1-f));
  148.    
  149.     if      ( h_i == 0 ) {
  150.       R = v;
  151.       G = t;
  152.       B = p;
  153.     }
  154.     else if ( h_i == 1 ) {
  155.       R = q;
  156.       G = v;
  157.       B = p;
  158.     }
  159.     else if ( h_i == 2 ) {
  160.       R = p;
  161.       G = v;
  162.       B = t;
  163.     }
  164.     else if ( h_i == 3 ) {
  165.       R = p;
  166.       G = q;
  167.       B = v;
  168.     }
  169.     else if ( h_i == 4 ) {
  170.       R = t;
  171.       G = p;
  172.       B = v;
  173.     }
  174.     else                   {
  175.       R = v;
  176.       G = p;
  177.       B = q;
  178.     }

  179.     set_led_rgb(row,led,byte(R*(float)(__max_brightness)),byte(G*(float)(__max_brightness)),byte(B*(float)(__max_brightness)));
  180.    
  181.     /*
  182.     Serial.println(byte(R*(float)(__max_brightness)),DEC);
  183.     Serial.println(byte(G*(float)(__max_brightness)),DEC);
  184.     Serial.println(byte(B*(float)(__max_brightness)),DEC);
  185.     Serial.println("---");
  186.     */   
  187. }

  188. void set_row_byte_hue(byte row, byte data_byte, int hue) {
  189.   byte led;
  190.   for(led = 0; led <= __max_led; led++) {
  191.     if( (data_byte>>led)&(B00000001) ) {
  192.       set_led_hue(row,led,hue);
  193.     }
  194.     else {
  195.       set_led_rgb(row,led,0,0,0);
  196.     }
  197.   }
  198. }

  199. void set_matrix_hue(int hue) {
  200.   byte ctr1;
  201.   byte ctr2;
  202.   for(ctr2 = 0; ctr2 <= __max_row; ctr2++) {
  203.     for(ctr1 = 0; ctr1 <= __max_led; ctr1++) {
  204.       set_led_hue(ctr2,ctr1,hue);
  205.     }
  206.   }
  207. }

  208. void set_column_hue(byte column, int hue) {
  209.   byte ctr1;
  210.   for(ctr1 = 0; ctr1 <= __max_row; ctr1++) {
  211.       set_led_hue(ctr1,column,hue);
  212.   }
  213. }


  214. void setup_hardware_spi(void) {
  215.   byte clr;

  216.   SPCR |= ( (1<<SPE) | (1<<MSTR) ); // enable SPI as master
  217.   //SPCR |= ( (1<<SPR1) ); // set prescaler bits
  218.   SPCR &= ~ ( (1<<SPR1) | (1<<SPR0) ); // clear prescaler bits
  219.   clr=SPSR; // clear SPI status reg
  220.   clr=SPDR; // clear SPI data reg
  221.   SPSR |= (1<<SPI2X); // set prescaler bits
  222.   //SPSR &= ~(1<<SPI2X); // clear prescaler bits
  223. }


  224. void setup_timer1_ovf(void) {

  225.   TCCR1B &= ~ ( (1<<CS11) );
  226.   TCCR1B |= ( (1<<CS12) | (1<<CS10) );      
  227.   //normal mode
  228.   TCCR1B &= ~ ( (1<<WGM13) | (1<<WGM12) );
  229.   TCCR1A &= ~ ( (1<<WGM11) | (1<<WGM10) );
  230.   //Timer1 Overflow Interrupt Enable  
  231.   TIMSK1 |= (1<<TOIE1);
  232.   TCNT1 = __TIMER1_MAX - __TIMER1_CNT;
  233.   // enable all interrupts
  234.   sei();
  235. }
  236. /*---------------------------单色图形--------------------------------------------*/
  237. //通过改变 hue的值,来改变颜色
  238. void matrix_heart(int hue) {
  239. int k,i;
  240. for(k = 0; k < 600; k=k+48) {
  241. for(int j=0;j<8;j++)
  242.       {
  243.       set_row_byte_hue(j,data[j+i],hue);
  244.       delay(20*__fade_delay);
  245.       if(j>=7)
  246.       i+=8;
  247.       else if(i>=96)
  248.       i=0;
  249.       }
  250. }
  251. }
  252. /*---------------------------彩色图形--------------------------------------------*/
  253. //通过改变 hue=hue++*里面*的值,来改变颜色变换速率
  254. void matrix_heart_2(void) {
  255.   int hue,i=0;
  256.   for(hue = 0; hue < 600; hue=hue+48) {
  257.       for(int j=0;j<8;j++)
  258.       {
  259.       set_row_byte_hue(j,data[j+i],hue);
  260.       delay(20*__fade_delay);
  261.       if(j>=7)
  262.       i+=8;
  263.       else if(i>=96)
  264.       i=0;
  265.       }
  266.   }
  267. }

  268. /*---------------------------满屏切换不同颜色--------------------------------------------*/
  269. //通过改变 ctr1=ctr1+*里面*的值,来改变颜色变换速率
  270. void fader_hue(void) {
  271.   int ctr1;
  272.   byte row;
  273.   byte led;

  274.   for(ctr1 = 0; ctr1 < 360; ctr1=ctr1+3) {
  275.     set_matrix_hue((float)(ctr1));
  276.     delay(__fade_delay);
  277.   }
  278. }
  279. /*---------------------------从上往下单行切换不同颜色--------------------------------------------*/
  280. //通过改变 hue=hue++*里面*的值,来改变颜色变换速率
  281. void matrix_test(void) {
  282.   byte ctr1;
  283.   byte ctr2;
  284.   int hue;
  285.   
  286.   for(hue = 0; hue < 360; hue=hue+32) {
  287.     for(ctr2 = 0; ctr2 <= __max_row; ctr2++) {
  288.       for(ctr1 = 0; ctr1 <= __max_led; ctr1++) {
  289.         set_led_hue(ctr2,ctr1,hue);
  290.         delay(5);
  291.       }
  292.     }
  293.   }  
  294. }
复制代码


视频:
回复

使用道具 举报

发表于 2013-10-10 11:42:58 | 显示全部楼层
不错不错!我也正在做这个!不过,我是照抄老外的
回复 支持 反对

使用道具 举报

发表于 2013-10-10 20:51:50 来自手机 | 显示全部楼层
我的是淘宝现成的 55全彩的模块
回复 支持 反对

使用道具 举报

 楼主| 发表于 2013-10-11 20:10:56 | 显示全部楼层
heiketiguo 发表于 2013-10-10 11:42
不错不错!我也正在做这个!不过,我是照抄老外的

老外这个给力,我也在改进中。。。
回复 支持 反对

使用道具 举报

发表于 2015-10-29 13:23:35 | 显示全部楼层
#define __display_enable 9  这个脚连到哪里?
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-15 23:00 , Processed in 0.040773 second(s), 22 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表