SHARE
TWEET

595-blink

a guest Apr 21st, 2015 307 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #define __spi_clock 52          // SCK - hardware SPI
  2. #define __spi_data_in 50        // MISO - hardware SPI (unused)
  3. #define __spi_data 51           // MOSI - hardware SPI
  4. #define __spi_latch 53
  5. #define __LATCH_LOW PORTC &= ~_BV(PC0)
  6. #define __LATCH_HIGH PORTC |= _BV(PC0)
  7.  
  8. #define __display_enable 49
  9. #define __DISPLAY_ON PORTD &= ~_BV(PD6)
  10. #define __DISPLAY_OFF PORTD |= _BV(PD6)
  11.  
  12. #include <avr/io.h>
  13. #include <avr/interrupt.h>
  14. #include <stdint.h>
  15.  
  16. void setup(void)
  17. {
  18.         pinMode(__spi_clock, OUTPUT);
  19.         pinMode(__spi_latch, OUTPUT);
  20.         pinMode(__spi_data, OUTPUT);
  21.         pinMode(__spi_data_in, INPUT);
  22.         pinMode(__display_enable, OUTPUT);
  23.         digitalWrite(__spi_latch, LOW);
  24.         digitalWrite(__spi_data, LOW);
  25.         digitalWrite(__spi_clock, LOW);
  26.         setup_hardware_spi();
  27. }
  28.  
  29. void loop(void)
  30. {
  31.         __DISPLAY_OFF;
  32.         __LATCH_LOW;
  33.         spi_transfer(0xFF);
  34.         spi_transfer(0xFF);
  35.         spi_transfer(0xFF);
  36.         spi_transfer(0xFF);
  37.         __LATCH_HIGH;
  38.         __DISPLAY_ON;
  39.        
  40.         delay(1000);
  41.        
  42.         __DISPLAY_OFF;
  43.         __LATCH_LOW;
  44.         spi_transfer(0x00);
  45.         spi_transfer(0x00);
  46.         spi_transfer(0x00);
  47.         spi_transfer(0x00);
  48.         __LATCH_HIGH;
  49.         __DISPLAY_ON;
  50.        
  51.         delay(1000);
  52. }
  53.  
  54. void setup_hardware_spi(void)
  55. {
  56.         uint8_t clr;
  57.         // spi prescaler:
  58.         //
  59.         // SPCR: SPR1 SPR0
  60.         // SPSR: SPI2X
  61.         //
  62.         // SPI2X SPR1 SPR0
  63.         //   0     0     0    fosc/4
  64.         //   0     0     1    fosc/16
  65.         //   0     1     0    fosc/64
  66.         //   0     1     1    fosc/128
  67.         //   1     0     0    fosc/2
  68.         //   1     0     1    fosc/8
  69.         //   1     1     0    fosc/32
  70.         //   1     1     1    fosc/64
  71.  
  72.         /* enable SPI as master */
  73.         SPCR |= (_BV(SPE) | _BV(MSTR));
  74.         /* clear registers */
  75.         clr = SPSR;
  76.         clr = SPDR;
  77.         /* set prescaler to fosc/2 */
  78.         SPCR &= ~(_BV(SPR1) | _BV(SPR0));
  79.         SPSR |= _BV(SPI2X);
  80. }
  81.  
  82. uint8_t spi_transfer(uint8_t data)
  83. {
  84.         SPDR = data;            // Start the transmission
  85.         while (!(SPSR & _BV(SPIF)))     // Wait the end of the transmission
  86.         {
  87.         };
  88.         return SPDR;            // return the received byte. (we don't need that here)
  89. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top