Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <wiringPi.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdint.h>
- // define our pins :
- #define Set_DIN(high) (high?(digitalWrite(7, HIGH)):(digitalWrite(7, LOW)))
- #define Set_CS(high) (high?(digitalWrite(8, HIGH)):(digitalWrite(8, LOW)))
- #define Set_CLK(high) (high?(digitalWrite(10, HIGH)):(digitalWrite(10, LOW)))
- const uint8_t smile[8] = {
- 0b00000000,
- 0b01100110,
- 0b01100110,
- 0b00011000,
- 0b00011000,
- 0b10000001,
- 0b01000010,
- 0b00111100};
- const uint8_t sad[8] = {
- 0b00000000,
- 0b01100110,
- 0b01100110,
- 0b00011000,
- 0b00011000,
- 0b00000000,
- 0b00111100,
- 0b01000010
- };
- void spi_send(uint8_t data)
- {
- uint8_t i;
- for (i = 0; i < 8; i++, data <<= 1)
- {
- Set_CLK(0);//CLK_HIGH();
- if (data & 0x80)
- Set_DIN(1);//DATA_HIGH();
- else
- Set_DIN(0);//DATA_LOW();
- Set_CLK(1);//CLK_HIGH();
- }
- }
- void max7219_writec(uint8_t high_byte, uint8_t low_byte)
- {
- Set_CS(0);//CS_LOW();
- spi_send(high_byte);
- spi_send(low_byte);
- Set_CS(1);//CS_HIGH();
- }
- void max7219_clear(void)
- {
- uint8_t i;
- for (i = 0; i < 8; i++)
- {
- max7219_writec(i+1, 0);
- }
- }
- void max7219_init(void)
- {
- // Decode mode: none
- max7219_writec(0x09, 0);
- // Intensity: 3 (0-15)
- max7219_writec(0x0A, 1);
- // Scan limit: All "digits" (rows) on
- max7219_writec(0x0B, 7);
- // Shutdown register: Display on
- max7219_writec(0x0C, 1);
- // Display test: off
- max7219_writec(0x0F, 0);
- max7219_clear();
- }
- uint8_t display[8];
- void update_display(void)
- {
- uint8_t i;
- for (i = 0; i < 8; i++)
- {
- max7219_writec(i+1, display[i]);
- }
- }
- void image(const uint8_t im[8])
- {
- uint8_t i;
- for (i = 0; i < 8; i++)
- display[i] = im[i];
- }
- void set_pixel(uint8_t r, uint8_t c, uint8_t value)
- {
- switch (value)
- {
- case 0: // Clear bit
- display[r] &= (uint8_t) ~(0x80 >> c);
- break;
- case 1: // Set bit
- display[r] |= (0x80 >> c);
- break;
- default: // XOR bit
- display[r] ^= (0x80 >> c);
- break;
- }
- }
- int main (void)
- {
- wiringPiSetup();
- pinMode(7, OUTPUT);
- pinMode(8, OUTPUT);
- pinMode(10, OUTPUT);
- uint8_t i;
- max7219_init();
- while(1)
- {
- image(sad);
- update_display();
- usleep(5000);
- image(smile);
- update_display();
- usleep(5000);
- // Invert display
- for (i = 0 ; i < 8*8; i++)
- {
- set_pixel(i / 8, i % 8, 2);
- update_display();
- usleep(5000);
- }
- usleep(5000);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement