Guest User

Untitled

a guest
Dec 11th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.91 KB | None | 0 0
  1. #ifndef F_CPU
  2. #define F_CPU 8000000UL // 8 MHz
  3. #endif
  4.  
  5. #define C3G 0x80; // PORTB
  6. #define C3R 0x40;
  7. #define C3B 0x20;
  8. #define C2G 0x10;
  9. #define C2R 0x08;
  10. #define C2B 0x04;
  11. #define C1G 0x02;
  12. #define C1R 0x01;
  13.  
  14. #define C1B 0x40; // PORTD
  15. #define C5R 0x20;
  16. #define C5G 0x10;
  17. #define C5B 0x08;
  18. #define C4R 0x04;
  19.  
  20. #define C4G 0x01; // PORTA
  21. #define C4B 0x02;
  22.  
  23.  
  24.  
  25. #include <avr/io.h>
  26. #include <avr/interrupt.h>
  27. #include <util/delay.h>
  28.  
  29. volatile unsigned char counter;
  30. volatile unsigned char portStateB;
  31. volatile unsigned char portStateD;
  32. volatile unsigned char portStateA;
  33.  
  34. volatile unsigned char led[31];
  35.  
  36. volatile unsigned char fadecount;
  37. volatile unsigned char fade;
  38.  
  39. volatile unsigned char rxSync;
  40. volatile unsigned char r;
  41.  
  42. void USART_Init(void);
  43. void USARTWriteChar(char);
  44.  
  45. int main(void) {
  46.  
  47. rxSync = 0x00;
  48. USART_Init();
  49.  
  50. counter=0x00;
  51.  
  52. led[0]=0xFF; // prefix
  53.  
  54. led[1]=0xFF;
  55. led[2]=0xFF;
  56. led[3]=0xFF;
  57.  
  58. led[4]=0xFF;
  59. led[5]=0xFF;
  60. led[6]=0xFF;
  61.  
  62. led[7]=0xFF;
  63. led[8]=0xFF;
  64. led[9]=0xFF;
  65.  
  66. led[10]=0xFF;
  67. led[11]=0xFF;
  68. led[12]=0xFF;
  69.  
  70. led[13]=0xFF;
  71. led[14]=0xFF;
  72. led[15]=0xFF;
  73.  
  74. led[16]=0xFF;
  75. led[17]=0xFF;
  76. led[18]=0xFF;
  77.  
  78. led[19]=0xFF;
  79. led[20]=0xFF;
  80. led[21]=0xFF;
  81.  
  82. led[22]=0xFF;
  83. led[23]=0xFF;
  84. led[24]=0xFF;
  85.  
  86. led[25]=0xFF;
  87. led[26]=0xFF;
  88. led[27]=0xFF;
  89.  
  90. led[28]=0x00;
  91. led[29]=0x00;
  92. led[30]=0x00;
  93.  
  94. portStateB = 0x00;
  95. portStateD = 0x00;
  96. portStateA = 0x00;
  97.  
  98. DDRB = 0xFF;
  99. DDRD = 0x7C;
  100. DDRA = 0x03;
  101.  
  102. sei();
  103.  
  104. // TIMER INIT
  105. TCCR0A = (1 << CS02)|(CS01); // Prescaler FCPU / 1024
  106.  
  107. TCNT0 = 0xFF; // Set timer to initial value 0;
  108. TCCR0B = (0<<CS01) | (1<<CS00); // prescaler
  109.  
  110.  
  111. OCR0A = 0xFF;
  112.  
  113. TIMSK |= (1 << TOIE0); // Overflow interrup enabled
  114.  
  115. PORTB = portStateB;
  116. PORTD = portStateD;
  117. PORTA = portStateA;
  118.  
  119. while (1) {
  120.  
  121. }
  122. return 0;
  123. }
  124.  
  125. ISR (TIMER0_OVF_vect) {
  126.  
  127. if (counter == 0xFF) {
  128. counter = 0x00;
  129. }
  130. else {
  131. counter++;
  132. }
  133.  
  134. portStateB = 0x00;
  135. portStateD = 0x00;
  136. portStateA = 0x00;
  137.  
  138. // Piiri 1
  139. //CHANNEL1
  140. if( counter < led[1] )
  141. portStateD += C1B;
  142.  
  143. if( counter < led[2])
  144. portStateB += C1R;
  145.  
  146. if( counter < led[3])
  147. portStateB += C1G;
  148.  
  149. //CHANNEL2
  150. if( counter < led[4])
  151. portStateB += C2B;
  152.  
  153. if( counter < led[5])
  154. portStateB += C2R;
  155.  
  156. if( counter < led[6])
  157. portStateB += C2G;
  158.  
  159.  
  160. //CHANNEL3
  161. if( counter < led[7])
  162. portStateB += C3B;
  163.  
  164. if( counter < led[8])
  165. portStateB += C3R;
  166.  
  167. if( counter < led[9])
  168. portStateB += C3G;
  169.  
  170. //CHANNEL4
  171. if( counter < led[10])
  172. portStateA += C4B;
  173.  
  174. if( counter < led[11])
  175. portStateD += C4R;
  176.  
  177. if( counter < led[12])
  178. portStateA += C4G;
  179.  
  180. //CHANNEL5
  181. if( counter < led[13])
  182. portStateD += C5B;
  183.  
  184. if( counter < led[14])
  185. portStateD += C5R;
  186.  
  187. if( counter < led[15])
  188. portStateD += C5G;
  189.  
  190. PORTB = portStateB;
  191. PORTD = portStateD;
  192. PORTA = portStateA;
  193.  
  194. }
  195.  
  196. void USART_Init(void) {
  197. // Set baud rate: clock / 16(UBBR + 1) -> 19231
  198. UBRRL = 25; //25=19200 12=38400
  199. UBRRH = 0;
  200. // Reciever, tramsmitter and complete interrupt enabled
  201. UCSRB = (1 << RXEN) | (1<<RXCIE) | (1<<TXEN);
  202. // 8bit
  203. UCSRC = (1<<UCSZ1) | (1<<UCSZ0);
  204. }
  205.  
  206.  
  207. ISR(USART_RX_vect) {
  208. r = UDR;
  209.  
  210. //USARTWriteChar(r);
  211.  
  212. if (r == 0xFF && rxSync == 0x00) {
  213. rxSync = 0x01;
  214. return;
  215. }
  216.  
  217. else {
  218. led[rxSync] = r;
  219. rxSync++;
  220. }
  221.  
  222. if (rxSync == 0x1C)
  223. rxSync = 0x00;
  224. return;
  225.  
  226. }
  227.  
  228.  
  229. void USARTWriteChar(char data) {
  230. // Wait until transitter is ready
  231.  
  232. while(!(UCSRA & (1<<UDRE)))
  233. {
  234. // Waiting
  235. }
  236.  
  237. // Write to USART buffer
  238. UDR=data;
  239. }
Add Comment
Please, Sign In to add comment