unkwntech

Untitled

Apr 1st, 2013
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.94 KB | None | 0 0
  1. #include "intrinsics.h"
  2. #include "iostm8s103f3.h"
  3. #include "types.h"
  4.  
  5. uint8 matrix[8];
  6.  
  7. uint8 dataBuffer[4];
  8.  
  9. //dataBuffer[0] Anodes MSB First
  10. //dataBuffer[1] Green Cathodes LSB
  11. //dataBuffer[2] Blue Cathodes LSB
  12. //dataBuffer[3] Red Cathodes LSB
  13.  
  14.  
  15. #define CS PE_ODR_ODR5
  16. #define CS_PIN 1 << 5
  17.  
  18. void sleep(uint32 count)
  19. {
  20.   while(count)
  21.   {
  22.     asm("nop");
  23.     count--;
  24.   }
  25. }
  26.  
  27.  
  28. void Blinker(uint8 x, uint8 y)
  29. {
  30.   matrix[x++] = (2 << y);
  31.   matrix[x++] = (2 << y);
  32.   matrix[x] = (2 << y);
  33. }
  34.  
  35. void SetupSPI()
  36. {
  37.   SPI_CR1_BR = 7; //Baude Rate to clock/2
  38.  
  39.   SPI_CR1_CPOL = 0; //Clock Idle LOW
  40.   SPI_CR1_CPHA = 0; //First Clock Edge is data
  41.  
  42.   SPI_CR1_LSBFIRST = 0;
  43.  
  44.   SPI_CR1_MSTR = 1; //SPI Master Enabled
  45.   SPI_CR1_SPE = 1; //Enable SPI
  46.  
  47.   SPI_CR2 = 0x01;
  48.  
  49.   //Setup CS Pin
  50.   CS = 1;
  51.   PE_DDR |= CS_PIN;
  52.   PE_CR1 |= CS_PIN;
  53.   PE_CR2 |= CS_PIN;
  54.  
  55.  
  56.  
  57. }
  58.  
  59. void TransmitSPI(uint8 data[], uint8 count)
  60. {
  61.   CS = 0;
  62.    
  63.   for(uint8 i = 0; i < count; i++)
  64.   {
  65.     SPI_DR = data[i];
  66.     //Wait until the buffer is clear
  67.     while(SPI_SR_TXE == 0);
  68.   }
  69.  
  70.   //Wait until transmition is complete
  71.   while(SPI_SR_BSY == 1);
  72.  
  73.   CS = 1;
  74. }
  75.  
  76. void SetColumn(uint8 column, uint8 state)
  77. {
  78.  
  79. }
  80.  
  81. void ClearMatrix()
  82. {
  83.   uint8 data[4];
  84.   data[0] = 0x00;
  85.   data[1] = 0x00;
  86.   data[2] = 0x00;
  87.   data[3] = 0x00;
  88.     TransmitSPI(data, 4);
  89.     sleep(1000);
  90. }
  91.  
  92.  
  93. void main( void )
  94. {
  95.   CLK_CKDIVR = 0x00;
  96.  
  97.   SetupSPI();
  98.  
  99.   ClearMatrix();
  100.  
  101.   sleep(1000);
  102.  
  103.   for(int i =0; i < 5; i++)
  104.   {
  105.     CS = 0;
  106.     SPI_DR = 0x00;
  107.     while(SPI_SR_TXE == 0);
  108.     SPI_DR = 0xFF;
  109.     while(SPI_SR_TXE == 0);
  110.     while(SPI_SR_BSY == 1);
  111.     asm("NOP");
  112.     CS = 1;
  113.     sleep(5000);
  114.   }
  115. //  uint8 data[2];
  116. //  data[0] = 0x00;
  117. //  data[1] = 0xFF;
  118. //  
  119. //  while(TRUE)
  120. //  {
  121. //    data[0] = 0x80;
  122. //    data[1] = (uint8)~(1 << 7);
  123. //    
  124. //    TransmitSPI(data, 2);
  125. //  }
  126.  
  127.  
  128.    
  129.   while(TRUE);
  130. }
Advertisement
Add Comment
Please, Sign In to add comment