Advertisement
Guest User

Untitled

a guest
Dec 14th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.24 KB | None | 0 0
  1. #include <avr/io.h>
  2. #include <stdlib.h>
  3. #include "uart.h"
  4. #define F_CPU 16000000
  5. #include <util/delay.h>
  6. #include "switch.h"
  7.  
  8.  
  9.  
  10. unsigned short checksum;
  11.  
  12. /*
  13. $S = start code - always 0x7E
  14. cmd = unique command
  15. feedback = whether feedback is to be sent - either 0 or 1
  16. param1 = first parameter, if any
  17. param2 = second parameter, if any
  18. checksum1 = first byte of checksum
  19. checksum2 = second byte of checksum
  20. $0 = end code - always 0xEF
  21. */
  22.  
  23. // Command-array: [$S, cmd, feedback, param1, param2, checksum1, checksum2, $0]
  24. unsigned char reset[] = { 0x7E, 0x0A, 0x00, 0x00, 0x00, 0x00, 0x00, 0xEF };
  25. unsigned char source[] = { 0x7E, 0x09, 0x00, 0x00, 0x02, 0x00, 0x00, 0xEF };
  26. unsigned char play[] = { 0x7E, 0x0D, 0x00, 0x00, 0x00, 0x00, 0x00, 0xEF };
  27. unsigned char volume[] = { 0x7E, 0x06, 0x00, 0x00, 0x14, 0x00, 0x00, 0xEF };
  28. unsigned char playTrack[] = { 0x7E, 0x03, 0x00, 0x00, 0x02, 0x00, 0x00, 0xEF };
  29. unsigned char stop[] = { 0x7E, 0x16, 0x00, 0x00, 0x00, 0x00, 0x00, 0xEF };
  30. unsigned char specify[] = { 0x7E, 0x0F, 0x00, 0x00, 0x02, 0x00, 0x00, 0xEF };
  31. unsigned char next [] = {0x7E, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0xEF};
  32. unsigned char volumeDown[] = {0x7E, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0xEF};
  33.  
  34.  
  35.  
  36. // Returns the checksum in a 16-bit unsigned short
  37. unsigned short checksumCalc(unsigned char CMD, unsigned char feedback, unsigned char para1, unsigned char para2) {
  38. return (0xFFFF - ((unsigned short)CMD + (unsigned short)feedback + (unsigned short)para1 + (unsigned short)para2) + 1);
  39. }
  40.  
  41. void initSomo(void) {
  42. //Reset[] checksum calculation
  43. checksum = checksumCalc(reset[1], reset[2], reset[3], reset[4]);
  44. reset[5] = (unsigned char)(checksum >> 8);
  45. reset[6] = (unsigned char)checksum;
  46.  
  47. // source[] checksum calculation
  48. checksum = checksumCalc(source[1], source[2], source[3], source[4]);
  49. source[5] = (unsigned char)(checksum >> 8);
  50. source[6] = (unsigned char)checksum;
  51.  
  52. // play[] checksum calculation
  53. checksum = checksumCalc(play[1], play[2], play[3], play[4]);
  54. play[5] = (unsigned char)(checksum >> 8);
  55. play[6] = (unsigned char)checksum;
  56.  
  57. // volume[] checksum calculation
  58. checksum = checksumCalc(volume[1], volume[2], volume[3], volume[4]);
  59. volume[5] = (unsigned char)(checksum >> 8);
  60. volume[6] = (unsigned char)checksum;
  61.  
  62. // playTrack[] checksum calculation
  63. checksum = checksumCalc(playTrack[1], playTrack[2], playTrack[3], playTrack[4]);
  64. playTrack[5] = (unsigned char)(checksum >> 8);
  65. playTrack[6] = (unsigned char)checksum;
  66.  
  67. // stop[] checksum calculation
  68. checksum = checksumCalc(stop[1], stop[2], stop[3], stop[4]);
  69. stop[5] = (unsigned char)(checksum >> 8);
  70. stop[6] = (unsigned char)checksum;
  71.  
  72. // specify[] checksum calculation
  73. checksum = checksumCalc(specify[1], specify[2], specify[3], specify[4]);
  74. specify[5] = (unsigned char)(checksum >> 8);
  75. specify[6] = (unsigned char)checksum;
  76.  
  77. //Next checksum calculation
  78. checksum = checksumCalc(next[1], next[2], next[3], next[4]);
  79. next[5] = (unsigned char)(checksum >> 8);
  80. next[6] = (unsigned char)checksum;
  81.  
  82. //Volume down calculation
  83. checksum = checksumCalc(volumeDown[1], volumeDown[2], volumeDown[3], volumeDown[4]);
  84. volumeDown[5] = (unsigned char)(checksum >> 8);
  85. volumeDown[6] = (unsigned char)checksum;
  86.  
  87. InitUART(9600, 8, 'N', 0);
  88. }
  89.  
  90.  
  91.  
  92. void spilsang() //Modtager 0x01 =startmelodi, 0x02 = reflekslyd eller 0x03 = slutmelodi og afspiller
  93. {
  94.  
  95. for (int i = 0; i < 8; i++)
  96. {
  97. SendChar(playTrack[i]);
  98. }
  99. _delay_ms(100);
  100. }
  101.  
  102. int main(void)
  103. {
  104. initSomo();
  105. initSwitchPort();
  106.  
  107. while (1)
  108. {
  109.  
  110. if (switchOn(7))//stop
  111. {
  112. for (int i = 0; i < 8; i++)
  113. {
  114. SendChar(stop[i]);
  115. }
  116. }
  117.  
  118. if (switchOn(6)) //next
  119. {
  120. for (int i = 0; i < 8; i++)
  121. {
  122. SendChar(next[i]);
  123. }
  124. _delay_ms(500);
  125. }
  126.  
  127. if (switchOn(5)) //volume 20
  128. {
  129. for (int i = 0; i < 8; i++)
  130. {
  131. SendChar(volume[i]);
  132. }
  133. }
  134. if (switchOn(4)) //volume--
  135. {
  136. for (int i = 0; i < 8; i++)
  137. {
  138. SendChar(volumeDown[i]);
  139. }
  140. _delay_ms(100);
  141. }
  142. if (switchOn(3)) //afspil track #2.
  143. {
  144. spilsang();
  145. }
  146.  
  147. if (switchOn(2)) //afspil track #1
  148. {
  149. spilsang();
  150. }
  151.  
  152. if (switchOn(1)) //afspil track #3.
  153. {
  154. spilsang();
  155. }
  156.  
  157.  
  158.  
  159. //End of while
  160. }
  161.  
  162. //End of main
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement