Advertisement
hugol

Untitled

Jul 11th, 2014
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.63 KB | None | 0 0
  1. // zdefiniowane w makefile
  2. //#define F_CPU 1000000L
  3.  
  4. #include <avr/io.h>
  5. #include <util/delay.h>
  6. #include <stdlib.h>
  7.  
  8. #define SEGMENT_COUNT 8
  9. #define false 0
  10. #define true 1
  11.  
  12. // liczby
  13. uint8_t liczby[10];
  14.  
  15. void init(void)
  16. {
  17. // b0 jako wyjscie
  18. DDRB = _BV(0);
  19. // b0 na wysoki
  20. PORTB |= _BV(0);
  21.  
  22. // b1 jako wejscie
  23. DDRB &= ~_BV(1);
  24. // b1 na wysoki
  25. PORTB |= _BV(1);
  26.  
  27. // b2 jako wejscie
  28. DDRB &= ~_BV(2);
  29. // b2 na wysoki
  30. PORTB |= _BV(2);
  31.  
  32. // b3 jako wejscie
  33. DDRB &= ~_BV(3);
  34. // b3 na wysoki
  35. PORTB |= _BV(3);
  36.  
  37. // b4 jako wyjscie
  38. DDRB |= _BV(4);
  39. // b3 na wysoki
  40. PORTB |= _BV(4);
  41.  
  42. // a jako wyjscie
  43. DDRA = 0xFF;
  44. // D jako wyjscie
  45. DDRD = 0xFF;
  46. }
  47.  
  48. void powitanie(void)
  49. {
  50. PORTA = 0;
  51. PORTD = 0;
  52. _delay_ms(1000);
  53. PORTA = 0xFF;
  54. PORTD = 0xFF;
  55. _delay_ms(500);
  56. PORTA = 0;
  57. PORTD = 0;
  58. _delay_ms(1000);
  59.  
  60. for (uint8_t i=0; i<SEGMENT_COUNT; ++i)
  61. {
  62. _delay_ms(100);
  63. // na wysoki
  64. PORTA |= _BV(i);
  65. PORTD |= _BV(i);
  66. }
  67. // zapalenie kropki
  68. PORTA &= ~_BV(3);
  69. PORTD &= ~_BV(3);
  70. }
  71.  
  72. void flash(uint8_t **reg, uint8_t count, uint8_t delay)
  73. {
  74. uint8_t *before = malloc(sizeof(uint8_t) * count);
  75. for (uint8_t i = 0; i < count; ++i)
  76. {
  77. before[i] = *(reg[i]);
  78. *(reg[i]) = 0xFF;
  79. }
  80. _delay_ms(delay);
  81. for (uint8_t i = 0; i < count; ++i)
  82. {
  83. *(reg[i]) = before[i];
  84. }
  85.  
  86. free(before);
  87. }
  88.  
  89. void sound(uint16_t freq, uint32_t time_ms)
  90. {
  91. if (freq != 0)
  92. {
  93. uint16_t period = 1000000/freq;
  94. uint32_t time = 0;
  95. while(time < time_ms*1000)
  96. {
  97. PORTB ^= _BV(4);
  98. _delay_us(period);
  99. time += period;
  100. }
  101. }
  102. else
  103. {
  104. _delay_ms(time_ms);
  105. }
  106. }
  107.  
  108. uint8_t menu(uint8_t max)
  109. {
  110. uint8_t wybor = 1;
  111. uint8_t clicked = false;
  112. while(1)
  113. {
  114. wybor %= max;
  115.  
  116. PORTA = liczby[(wybor/10)%10];
  117. PORTD = liczby[wybor%10] & ~_BV(3);
  118. // b1 niski
  119. if (((PINB & _BV(3)) == 0) && !clicked)
  120. {
  121. _delay_ms(1);
  122. if ((PINB & _BV(3)) == 0)
  123. {
  124. ++wybor;
  125. clicked = true;
  126. }
  127. }
  128. else if (((PINB & _BV(2)) == 0) && !clicked)
  129. {
  130. _delay_ms(1);
  131. if ((PINB & _BV(2)) == 0)
  132. {
  133. --wybor;
  134. clicked = true;
  135. }
  136. }
  137. else if (((PINB & _BV(1)) == 0) && !clicked)
  138. {
  139. _delay_ms(1);
  140. if ((PINB & _BV(1)) == 0)
  141. {
  142. return wybor;
  143. }
  144. }
  145. else if (clicked)
  146. {
  147. _delay_ms(1);
  148. if ((PINB & _BV(3)) != 0 && (PINB & _BV(2)) != 0 && (PINB & _BV(3)) != 0)
  149. {
  150. clicked = false;
  151. }
  152. }
  153. }
  154. }
  155.  
  156. int main(void)
  157. {
  158.  
  159. liczby[0] = (uint8_t)~_BV(0) & ~_BV(1) & ~_BV(2) & ~_BV(4) & ~_BV(5) & ~_BV(6);
  160. liczby[1] = (uint8_t)~_BV(2) & ~_BV(4);
  161. liczby[2] = (uint8_t)~_BV(5) & ~_BV(4) & ~_BV(7) & ~_BV(0) & ~_BV(1);
  162. liczby[3] = (uint8_t)~_BV(1) & ~_BV(2) & ~_BV(4) & ~_BV(5) & ~_BV(7);
  163. liczby[4] = (uint8_t)~_BV(2) & ~_BV(4) & ~_BV(6) & ~_BV(7);
  164. liczby[5] = (uint8_t)~_BV(1) & ~_BV(2) & ~_BV(5) & ~_BV(6) & ~_BV(7);
  165. liczby[6] = (uint8_t)~_BV(0) & ~_BV(1) & ~_BV(2) & ~_BV(5) & ~_BV(6) & ~_BV(7);
  166. liczby[7] = (uint8_t)~_BV(2) & ~_BV(4) & ~_BV(5);
  167. liczby[8] = (uint8_t)_BV(3);
  168. liczby[9] = (uint8_t)_BV(3) | _BV(0);
  169.  
  170. init();
  171. powitanie();
  172.  
  173. #define PAUZA 0
  174. #define C 262
  175. #define D 294
  176. #define E 329
  177. #define F 350
  178. #define G 392
  179. #define A 440
  180. #define H 494
  181. #define C2 523
  182. #define D2 (D*2)
  183. #define G0 196
  184. #define ES 311
  185. #define GES 370
  186. #define HES 466
  187. #define E2S 621
  188.  
  189. uint16_t tempo = 120;
  190.  
  191. #define CWIERCNUTA 60000
  192. typedef struct nuta
  193. {
  194. uint16_t freq;
  195. uint16_t time_ms;
  196. };
  197.  
  198. struct piosenki_T
  199. {
  200. uint8_t dlugosc;
  201. struct nuta* melodia;
  202. } piosenki[2];
  203.  
  204.  
  205.  
  206. struct nuta koleda[] =
  207. {
  208. {C, CWIERCNUTA},
  209. {C, CWIERCNUTA/2},
  210. {G0, CWIERCNUTA/2},
  211. {C, CWIERCNUTA/2},
  212. {D, CWIERCNUTA/2},
  213. {E, CWIERCNUTA},
  214. {E, CWIERCNUTA/2},
  215. {D, CWIERCNUTA/2},
  216. {E, CWIERCNUTA/2},
  217. {F, CWIERCNUTA/2},
  218. {G, CWIERCNUTA/2},
  219. {A, CWIERCNUTA/2},
  220. {G, CWIERCNUTA},
  221. {F, CWIERCNUTA},
  222. {E, CWIERCNUTA},
  223. {D, CWIERCNUTA},
  224. {PAUZA, CWIERCNUTA},
  225. {C, CWIERCNUTA},
  226. {C, CWIERCNUTA/2},
  227. {G0, CWIERCNUTA/2},
  228. {C, CWIERCNUTA/2},
  229. {D, CWIERCNUTA/2},
  230. {E, CWIERCNUTA},
  231. {E, CWIERCNUTA/2},
  232. {D, CWIERCNUTA/2},
  233. {E, CWIERCNUTA/2},
  234. {F, CWIERCNUTA/2},
  235. {G, CWIERCNUTA/2},
  236. {A, CWIERCNUTA/2},
  237. {G, CWIERCNUTA},
  238. {F, CWIERCNUTA},
  239. {E, CWIERCNUTA},
  240. {D, CWIERCNUTA},
  241. {PAUZA, CWIERCNUTA},
  242. {G, CWIERCNUTA},
  243. {G, CWIERCNUTA/2},
  244. {F, CWIERCNUTA/2},
  245. {E, CWIERCNUTA/2},
  246. {D, CWIERCNUTA/2},
  247. {C, CWIERCNUTA},
  248. {C, CWIERCNUTA/2},
  249. {G0, CWIERCNUTA/2},
  250. {C, CWIERCNUTA/2},
  251. {D, CWIERCNUTA/2},
  252. {G, CWIERCNUTA},
  253. {G, CWIERCNUTA/2},
  254. {F, CWIERCNUTA/2},
  255. {E, CWIERCNUTA/2},
  256. {D, CWIERCNUTA/2},
  257. {C, CWIERCNUTA},
  258. {C, CWIERCNUTA/2},
  259. {G0, CWIERCNUTA/2},
  260. {C, CWIERCNUTA/2},
  261. {D, CWIERCNUTA/2},
  262. {G, CWIERCNUTA/2},
  263. {A, CWIERCNUTA/2},
  264. {G, CWIERCNUTA/2},
  265. {F, CWIERCNUTA/2},
  266. {E, CWIERCNUTA/2},
  267. {F, CWIERCNUTA/2},
  268. {G, CWIERCNUTA/2},
  269. {A, CWIERCNUTA/2},
  270. {G, CWIERCNUTA/2},
  271. {F, CWIERCNUTA/2},
  272. {E, CWIERCNUTA/2},
  273. {F, CWIERCNUTA/2},
  274. {G, CWIERCNUTA/2},
  275. {G, CWIERCNUTA/2},
  276. {A, CWIERCNUTA},
  277. {G, CWIERCNUTA},
  278. {F, CWIERCNUTA/2},
  279. {E, CWIERCNUTA/2},
  280. {D, CWIERCNUTA},
  281. {C, CWIERCNUTA}
  282. };
  283. piosenki[0].dlugosc = sizeof(koleda)/(sizeof(struct nuta));
  284. piosenki[0].melodia = koleda;
  285.  
  286. struct nuta imperial[] =
  287. {
  288. {G, CWIERCNUTA},
  289. {G, CWIERCNUTA},
  290. {G, CWIERCNUTA},
  291. {ES, (CWIERCNUTA*3)/4},
  292. {HES, (CWIERCNUTA/4)},
  293. {G, CWIERCNUTA},
  294. {ES, (CWIERCNUTA*3)/4},
  295. {HES, (CWIERCNUTA/4)},
  296. {G, CWIERCNUTA*2},
  297. {D2, CWIERCNUTA},
  298. {D2, CWIERCNUTA},
  299. {D2, CWIERCNUTA},
  300. {E2S, (CWIERCNUTA*3)/4},
  301. {HES, (CWIERCNUTA/4)},
  302. {GES, (CWIERCNUTA*3)/2},
  303. {ES, (CWIERCNUTA*3)/4},
  304. {HES, (CWIERCNUTA/4)},
  305. {G, CWIERCNUTA*2}
  306. };
  307.  
  308. piosenki[1].dlugosc = sizeof(imperial)/(sizeof(struct nuta));
  309. piosenki[1].melodia = imperial;
  310.  
  311.  
  312. while(1)
  313. {
  314. uint8_t wybor = menu(3);
  315.  
  316. // tempo
  317. if (wybor == 0)
  318. {
  319. do
  320. {
  321. powitanie();
  322. tempo = menu(100) * 10;
  323. } while(tempo == 0);
  324. }
  325. else
  326. {
  327. uint16_t licznik = 1;
  328. while (1)
  329. {
  330. if (((PINB & _BV(3)) == 0))
  331. {
  332. _delay_ms(1);
  333. if ((PINB & _BV(3)) == 0)
  334. {
  335. ++tempo;
  336. }
  337. }
  338. else if ((PINB & _BV(2)) == 0)
  339. {
  340. _delay_ms(1);
  341. if ((PINB & _BV(2)) == 0)
  342. {
  343. ++tempo;
  344. }
  345. }
  346.  
  347. if (licznik > piosenki[wybor-1].dlugosc)
  348. {
  349. break;
  350. }
  351.  
  352. PORTA = liczby[(licznik/10)%10];
  353. PORTD = liczby[licznik%10];
  354. sound(piosenki[wybor-1].melodia[licznik-1].freq, piosenki[wybor-1].melodia[licznik-1].time_ms/tempo);
  355.  
  356. ++licznik;
  357. }
  358. }
  359. }
  360. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement