Advertisement
Guest User

Untitled

a guest
May 21st, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.05 KB | None | 0 0
  1. /*
  2. * Lab7.c
  3. *
  4. * Created: 2019-05-14 10:06:10
  5. * Author : JARACZ&GRUZEG
  6. */
  7. #define F_CPU 16000000
  8.  
  9. #include <avr/io.h>
  10. #include <avr/interrupt.h>
  11. #include <util/delay.h>
  12.  
  13. void InicjalizacjaADC(void);
  14. void InicjalizacjaTIMER1(void);
  15. void PomiarADC(void);
  16.  
  17. uint8_t napjed;
  18. uint8_t napdzies;
  19. uint8_t napset;
  20.  
  21. uint8_t zrodlo1 = 0;
  22. uint8_t zrodlo2 = 0;
  23. uint8_t zrodlo3 = 0;
  24. uint8_t zrodlo4 = 0;
  25. uint8_t avg = 0;
  26. volatile uint8_t is_avg = 0;
  27.  
  28. volatile uint8_t wyswietlacz = 0;
  29.  
  30. unsigned char WyswietlanieLED[] = {0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8, 0x80, 0x90, 0xFF };
  31.  
  32. uint16_t cwiercsekundy=60; //podzial z prescalera 16000000/256=62500 /4=15625
  33.  
  34. void HandleBtn(uint8_t* btn, uint8_t val)
  35. {
  36. switch(*btn)
  37. {
  38. // Jesli klikniety
  39. case 0: if ((PIND & val) == 0) *btn = 1;
  40. break;
  41.  
  42. // Jesli dalej klikniety
  43. case 1: if ((PIND & val) == 0) *btn = 2;
  44. else *btn = 0;
  45. break;
  46.  
  47. // Obsluga kolegi w main
  48. case 2: break;
  49.  
  50. // Zostal klikniety RAZ
  51. case 3: if ((PIND & val) != 0) *btn = 4;
  52. break;
  53.  
  54. // Zostal odklikniety LUB nie to czeka
  55. case 4: if ((PIND & val) != 0) *btn = 0;
  56. else *btn = 3;
  57. break;
  58.  
  59. default: *btn = 0;
  60. }
  61. }
  62.  
  63. ISR (TIMER1_COMPA_vect)
  64. {
  65. if(is_avg == 0)
  66. PomiarADC();
  67. else {
  68. uint16_t n_sum = 0;
  69.  
  70. ADCSRA &= (0<<ADEN);
  71. ADMUX = (1<<REFS1) | (1<<REFS0) | 0x00;
  72. ADCSRA = (1<<ADEN) | (1<<ADPS2) | (1<<ADPS0);
  73. ADCSRA |= (1<<ADSC);
  74. while((ADCSRA & (1<<ADSC)));
  75. n_sum += ADC;
  76. ADCSRA &= (0<<ADEN);
  77. ADMUX = (1<<REFS1) | (1<<REFS0) | 0x01;
  78. ADCSRA = (1<<ADEN) | (1<<ADPS2) | (1<<ADPS0);
  79. ADCSRA |= (1<<ADSC);
  80. while((ADCSRA & (1<<ADSC)));
  81. n_sum += ADC;
  82. ADCSRA &= (0<<ADEN);
  83. ADMUX = (1<<REFS1) | (1<<REFS0) | 0x02;
  84. ADCSRA = (1<<ADEN) | (1<<ADPS2) | (1<<ADPS0);
  85. ADCSRA |= (1<<ADSC);
  86. while((ADCSRA & (1<<ADSC)));
  87. n_sum += ADC;
  88. ADCSRA &= (0<<ADEN);
  89. ADMUX = (1<<REFS1) | (1<<REFS0) | 0x03;
  90. ADCSRA = (1<<ADEN) | (1<<ADPS2) | (1<<ADPS0);
  91. ADCSRA |= (1<<ADSC);
  92. while((ADCSRA & (1<<ADSC)));
  93. n_sum += ADC;
  94. n_sum *= 2.5;
  95. n_sum /= 4;
  96. uint16_t napiecie = n_sum; // 2560
  97.  
  98. /*napset = napiecie % 10;
  99. napiecie = napiecie/10;
  100. napdzies = napiecie % 10;
  101. napiecie = napiecie/10;
  102. napjed = napiecie;*/
  103. napset=(napiecie/10)%10 + (napiecie%10)/2;
  104. uint8_t carry = 0;
  105. if(napset > 9) {
  106. carry = 1;
  107. napset = 0;
  108. }
  109. napdzies= (napiecie/100)%10 + carry;
  110. if(napdzies > 9) {
  111. carry = 1;
  112. napdzies = 0;
  113. } else carry = 0;
  114. napjed=napiecie/1000 + carry;
  115. }
  116.  
  117. PORTB = ~8;
  118. _delay_ms(2);
  119. PORTC = WyswietlanieLED[napjed] & 0x7F;
  120.  
  121. PORTB = ~4;
  122. _delay_ms(2);
  123. PORTC = WyswietlanieLED[napdzies];
  124.  
  125. PORTB = ~2;
  126. _delay_ms(2);
  127. PORTC = WyswietlanieLED[napset];
  128.  
  129.  
  130. HandleBtn(&zrodlo1, 0x04);
  131. HandleBtn(&zrodlo2, 0x08);
  132. HandleBtn(&zrodlo3, 0x10);
  133. HandleBtn(&zrodlo4, 0x20);
  134. HandleBtn(&avg, 0x02);
  135.  
  136. PORTB = ~1;
  137. _delay_ms(2);
  138. PORTC = WyswietlanieLED[wyswietlacz + 1];
  139. }
  140.  
  141. int main(void)
  142. {
  143. DDRC = 0xFF;
  144. DDRB = 0x0F;
  145.  
  146. //Przyciski
  147. PORTD = 0xFF;
  148. DDRD = 0x00;
  149.  
  150. InicjalizacjaTIMER1();
  151. InicjalizacjaADC();
  152. ADCSRA |= (1<<ADSC);
  153.  
  154. while (1)
  155. {
  156. if(avg == 2)
  157. {
  158. is_avg = 1;
  159. wyswietlacz = 9;
  160. avg = 3;
  161. }
  162.  
  163. if(zrodlo1 == 2)
  164. {
  165. is_avg = 0;
  166. ADCSRA &= (0<<ADEN);
  167. ADMUX = (1<<REFS1) | (1<<REFS0) | 0x00;
  168. ADCSRA = (1<<ADEN) | (1<<ADPS2) | (1<<ADPS0);
  169. zrodlo1 = 3;
  170. wyswietlacz = 0;
  171. }
  172. if(zrodlo2 == 2)
  173. {
  174. is_avg = 0;
  175. ADCSRA &= (0<<ADEN);
  176. ADMUX = (1<<REFS1) | (1<<REFS0) | 0x01;
  177. ADCSRA = (1<<ADEN) | (1<<ADPS2) | (1<<ADPS0);
  178. zrodlo2 = 3;
  179. wyswietlacz = 1;
  180. }
  181. if(zrodlo3 == 2)
  182. {
  183. is_avg = 0;
  184. ADCSRA &= (0<<ADEN);
  185. ADMUX = (1<<REFS1) | (1<<REFS0) | 0x02;
  186. ADCSRA = (1<<ADEN) | (1<<ADPS2) | (1<<ADPS0);
  187. zrodlo3 = 3;
  188. wyswietlacz = 2;
  189. }
  190. if(zrodlo4 == 2)
  191. {
  192. is_avg = 0;
  193. ADCSRA &= (0<<ADEN);
  194. ADMUX = (1<<REFS1) | (1<<REFS0) | 0x03;
  195. ADCSRA = (1<<ADEN) | (1<<ADPS2) | (1<<ADPS0);
  196. zrodlo4 = 3;
  197. wyswietlacz = 3;
  198. }
  199. }
  200. }
  201.  
  202. void InicjalizacjaADC()
  203. {
  204. DDRA &=~(1<<PA1); //wejscie dla ADC0
  205. ADMUX = (1<<REFS1) | (1<<REFS0) | 0x00; //wewnetrzne zrodlo 2,56V
  206. //wybor kanalu ADC0
  207.  
  208. ADCSRA = (1<<ADEN) | (1<<ADPS2) | (1<<ADPS0); //wlaczenie ADC
  209. //prescaler /32 //ADIE - odblokowanie przerwania
  210. sei();
  211. }
  212. void PomiarADC()
  213. {
  214. int16_t napiecie;
  215.  
  216. ADCSRA |= (1<<ADSC);
  217. while((ADCSRA & (1<<ADSC)));
  218.  
  219. napiecie = ADC * 2.5; // 2560
  220.  
  221. /*napset = napiecie % 10;
  222. napiecie = napiecie/10;
  223. napdzies = napiecie % 10;
  224. napiecie = napiecie/10;
  225. napjed = napiecie;*/
  226. napset=(napiecie/10)%10 + (napiecie%10);
  227. uint8_t carry = 0;
  228. if(napset > 9) {
  229. carry = 1;
  230. napset = 0;
  231. }
  232. napdzies= (napiecie/100)%10 + carry;
  233. if(napdzies > 9) {
  234. carry = 1;
  235. napdzies = 0;
  236. } else carry = 0;
  237. napjed=napiecie/1000 + carry;
  238.  
  239.  
  240. }
  241.  
  242. void InicjalizacjaTIMER1()
  243. {
  244. TCCR1B |= (1<<CS12); //preskaler 256
  245. TCCR1B |= (1<<WGM12); //praca CTC
  246.  
  247. OCR1A = cwiercsekundy;
  248. TIMSK |= (1<<OCIE1A); // odblokowanie przerwania TIMER1_COMPA
  249.  
  250. sei();
  251. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement