microrobotics

Max7219 Sample Code - Raspberry Pi

Jul 31st, 2017
1,120
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // blink.c
  2. //
  3. // Example program for bcm2835 library
  4. // Blinks a pin on an off every 0.5 secs
  5. //
  6. // After installing bcm2835, you can build this
  7. // with something like:
  8. // make or gcc -o led led.c -lbcm2835
  9. // sudo ./led
  10. /*
  11.              define from bcm2835.h                       define from Board DVK511
  12.                  3.3V | | 5V               ->                 3.3V | | 5V
  13.     RPI_V2_GPIO_P1_03 | | 5V               ->                  SDA | | 5V
  14.     RPI_V2_GPIO_P1_05 | | GND              ->                  SCL | | GND
  15.        RPI_GPIO_P1_07 | | RPI_GPIO_P1_08   ->                  IO7 | | TX
  16.                   GND | | RPI_GPIO_P1_10   ->                  GND | | RX
  17.        RPI_GPIO_P1_11 | | RPI_GPIO_P1_12   ->                  IO0 | | IO1
  18.     RPI_V2_GPIO_P1_13 | | GND              ->                  IO2 | | GND
  19.        RPI_GPIO_P1_15 | | RPI_GPIO_P1_16   ->                  IO3 | | IO4
  20.                   VCC | | RPI_GPIO_P1_18   ->                  VCC | | IO5
  21.        RPI_GPIO_P1_19 | | GND              ->                 MOSI | | GND
  22.        RPI_GPIO_P1_21 | | RPI_GPIO_P1_22   ->                 MISO | | IO6
  23.        RPI_GPIO_P1_23 | | RPI_GPIO_P1_24   ->                  SCK | | CE0
  24.                   GND | | RPI_GPIO_P1_26   ->                  GND | | CE1
  25.  
  26. ::if your raspberry Pi is version 1 or rev 1 or rev A
  27. RPI_V2_GPIO_P1_03->RPI_GPIO_P1_03
  28. RPI_V2_GPIO_P1_05->RPI_GPIO_P1_05
  29. RPI_V2_GPIO_P1_13->RPI_GPIO_P1_13:
  30.  
  31. */
  32. #include <bcm2835.h>
  33. #include <stdio.h>
  34. #define uchar unsigned char
  35. #define uint unsigned int
  36.  
  37.  
  38. //#define Max7219_pinCLK  RPI_GPIO_P1_11
  39. #define Max7219_pinCS  RPI_GPIO_P1_24
  40. //#define Max7219_pinDIN  RPI_V2_GPIO_P1_13
  41.  
  42. uchar disp1[38][8] = {
  43. {0x3C,0x42,0x42,0x42,0x42,0x42,0x42,0x3C},//0
  44. {0x10,0x30,0x50,0x10,0x10,0x10,0x10,0x7C},//1
  45. {0x3E,0x02,0x02,0x3E,0x20,0x20,0x3E,0x00},//2
  46. {0x00,0x7C,0x04,0x04,0x7C,0x04,0x04,0x7C},//3
  47. {0x08,0x18,0x28,0x48,0xFE,0x08,0x08,0x08},//4
  48. {0x3C,0x20,0x20,0x3C,0x04,0x04,0x3C,0x00},//5
  49. {0x3C,0x20,0x20,0x3C,0x24,0x24,0x3C,0x00},//6
  50. {0x3E,0x22,0x04,0x08,0x08,0x08,0x08,0x08},//7
  51. {0x00,0x3E,0x22,0x22,0x3E,0x22,0x22,0x3E},//8
  52. {0x3E,0x22,0x22,0x3E,0x02,0x02,0x02,0x3E},//9
  53. {0x08,0x14,0x22,0x3E,0x22,0x22,0x22,0x22},//A
  54. {0x3C,0x22,0x22,0x3E,0x22,0x22,0x3C,0x00},//B
  55. {0x3C,0x40,0x40,0x40,0x40,0x40,0x3C,0x00},//C
  56. {0x7C,0x42,0x42,0x42,0x42,0x42,0x7C,0x00},//D
  57. {0x7C,0x40,0x40,0x7C,0x40,0x40,0x40,0x7C},//E
  58. {0x7C,0x40,0x40,0x7C,0x40,0x40,0x40,0x40},//F
  59. {0x3C,0x40,0x40,0x40,0x40,0x44,0x44,0x3C},//G
  60. {0x44,0x44,0x44,0x7C,0x44,0x44,0x44,0x44},//H
  61. {0x7C,0x10,0x10,0x10,0x10,0x10,0x10,0x7C},//I
  62. {0x3C,0x08,0x08,0x08,0x08,0x08,0x48,0x30},//J
  63. {0x00,0x24,0x28,0x30,0x20,0x30,0x28,0x24},//K
  64. {0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x7C},//L
  65. {0x81,0xC3,0xA5,0x99,0x81,0x81,0x81,0x81},//M
  66. {0x00,0x42,0x62,0x52,0x4A,0x46,0x42,0x00},//N
  67. {0x3C,0x42,0x42,0x42,0x42,0x42,0x42,0x3C},//O
  68. {0x3C,0x22,0x22,0x22,0x3C,0x20,0x20,0x20},//P
  69. {0x1C,0x22,0x22,0x22,0x22,0x26,0x22,0x1D},//Q
  70. {0x3C,0x22,0x22,0x22,0x3C,0x24,0x22,0x21},//R
  71. {0x00,0x1E,0x20,0x20,0x3E,0x02,0x02,0x3C},//S
  72. {0x00,0x3E,0x08,0x08,0x08,0x08,0x08,0x08},//T
  73. {0x42,0x42,0x42,0x42,0x42,0x42,0x22,0x1C},//U
  74. {0x42,0x42,0x42,0x42,0x42,0x42,0x24,0x18},//V
  75. {0x00,0x49,0x49,0x49,0x49,0x2A,0x1C,0x00},//W
  76. {0x00,0x41,0x22,0x14,0x08,0x14,0x22,0x41},//X
  77. {0x41,0x22,0x14,0x08,0x08,0x08,0x08,0x08},//Y
  78. {0x00,0x7F,0x02,0x04,0x08,0x10,0x20,0x7F},//Z
  79. {0x08,0x7F,0x49,0x49,0x7F,0x08,0x08,0x08},//中
  80. {0xFE,0xBA,0x92,0xBA,0x92,0x9A,0xBA,0xFE},//国
  81. };
  82. void Delay_xms(uint x)
  83. {
  84.  
  85.  bcm2835_delay(x);
  86.  
  87. }
  88. //------------------------
  89. void Write_Max7219_byte(uchar DATA)
  90. {
  91.          uchar i ;
  92.          bcm2835_gpio_write(Max7219_pinCS,LOW);
  93.        /* for(i = 8; i >= 1; i--)
  94.             {
  95.              bcm2835_gpio_write(Max7219_pinCLK,LOW);
  96.              bcm2835_gpio_write( Max7219_pinDIN, (DATA & 0x80));
  97.              DATA = DATA << 1;
  98.              bcm2835_gpio_write(Max7219_pinCLK,HIGH);
  99.             }
  100.        */
  101.       bcm2835_spi_transfer(DATA);
  102.  
  103. }
  104. void Write_Max7219(uchar address1,uchar dat1,uchar address2,uchar dat2)
  105. //void Write_Max7219(uchar address,uchar dat)
  106. {
  107.   bcm2835_gpio_write(Max7219_pinCS,LOW);
  108.  
  109.   Write_Max7219_byte(address1);
  110.   Write_Max7219_byte(dat1);
  111.   Write_Max7219_byte(address2);
  112.   Write_Max7219_byte(dat2);
  113.    //_nop_();
  114.   //Write_Max7219_byte(address);
  115.   //Write_Max7219_byte(dat);
  116.   bcm2835_gpio_write(Max7219_pinCS,HIGH);
  117. }
  118.  
  119. void Init_MAX7219(void)
  120. {
  121.  Write_Max7219(0x09,0x00,0x09,0x00);
  122.  Write_Max7219(0x0a,0x03,0x0a,0x03);
  123.  Write_Max7219(0x0b,0x07,0x0b,0x07);
  124.  Write_Max7219(0x0c,0x01,0x0c,0x01);
  125.  Write_Max7219(0x0f,0x00,0x0f,0x00);
  126. }
  127.  
  128. /*void Init_MAX7219(void)
  129. {
  130.  Write_Max7219(0x09,0x00);
  131.  Write_Max7219(0x0a,0x03);
  132.  Write_Max7219(0x0b,0x07);
  133.  Write_Max7219(0x0c,0x01);
  134.  Write_Max7219(0x0f,0x00);
  135. }
  136. */
  137.  
  138. void main(void)
  139. {
  140.  uchar i , j;
  141.  if (!bcm2835_init())
  142.         return 1;
  143.  bcm2835_spi_begin();
  144.  bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST);      // The default
  145.  bcm2835_spi_setDataMode(BCM2835_SPI_MODE0);                   // The default
  146.  bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_256); // The default
  147.  
  148.  
  149.   bcm2835_gpio_fsel(Max7219_pinCS, BCM2835_GPIO_FSEL_OUTP);
  150.  
  151.   bcm2835_gpio_write(disp1[j][i],HIGH);
  152.  
  153.  Delay_xms(50);
  154.  Init_MAX7219();
  155.  while(1)
  156.  {
  157.   for(j = 0;j <37; j++)
  158.   {
  159.    for(i = 1;i < 9;i++)
  160.      Write_Max7219(i , disp1[j+1][i-1],i,disp1[j][i-1]);
  161.      Delay_xms(1000);
  162.   }
  163.  }
  164.   bcm2835_spi_end();
  165.   bcm2835_close();
  166.   return 0;
  167. }
RAW Paste Data