Advertisement
Guest User

Untitled

a guest
May 19th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 KB | None | 0 0
  1. /*
  2. * Program2.c
  3. *
  4. * Created: 2019-05-19 11:49:13
  5. * Author : oem1
  6. */
  7. #define F_CPU 16000000L
  8. #include <avr/io.h>
  9. #include <util/delay.h>
  10.  
  11.  
  12. void InicjalizacjaLCD();
  13. void InicjalizacjaADC();
  14. void PomiarADC(void);
  15.  
  16.  
  17. void WyswietlanieLCD(char*, int8_t);
  18. void ZapisLCD(int8_t);
  19. void Wyswietlanie_nizej(char*, int8_t);
  20. void przesuwanie(int8_t);
  21. void Znak(const char*, int8_t);
  22.  
  23.  
  24. char napiecie[4] = "U = ";
  25. int8_t napjedascii = 0x30;
  26. char kropka[1] = ".";
  27. int8_t napdziesascii = 0x30;
  28. int8_t napsetascii = 0x30;
  29. char napieciee[4] = " [V]";
  30.  
  31. const char kod[8] = {0b11111,
  32. 0b10101,
  33. 0b11111,
  34. 0b11011,
  35. 0b11011,
  36. 0b01110,
  37. 0b10001,
  38. 0b11111};
  39.  
  40. int main(void)
  41. {
  42. InicjalizacjaLCD();
  43.  
  44. InicjalizacjaADC();
  45.  
  46. PomiarADC();
  47.  
  48. WyswietlanieLCD(napiecie, 4);
  49.  
  50. ZapisLCD(napjedascii);
  51.  
  52. WyswietlanieLCD(kropka, 1);
  53.  
  54. ZapisLCD(napdziesascii);
  55.  
  56. ZapisLCD(napsetascii);
  57.  
  58. WyswietlanieLCD(napieciee, 4);
  59.  
  60. for(;;);
  61.  
  62. }
  63. void InicjalizacjaLCD()
  64. {
  65. DDRB = 0xff;
  66. DDRA |= (1<<PA4)|(1<<PA6);
  67.  
  68. PORTB = (PORTB & 0x0F);
  69.  
  70. PORTA &= ~(1<<PA4);
  71. PORTA &= ~(1<<PA6);
  72.  
  73. _delay_ms(40);
  74.  
  75. PORTA |= (1<<PA6);
  76. PORTB = 0b00110000;
  77. PORTA &= ~(1<<PA6);
  78.  
  79. _delay_ms(4.1);
  80.  
  81. PORTA |= (1<<PA6);
  82. PORTA &= ~(1<<PA6);
  83.  
  84. _delay_us(100);
  85.  
  86. PORTA |= (1<<PA6);
  87. PORTA &= ~(1<<PA6);
  88.  
  89. _delay_us(100);
  90.  
  91. PORTA |= (1<<PA6);
  92. PORTB &= ~(1<<PB4);
  93. PORTA &= ~(1<<PA6);
  94.  
  95. ZapisLCD(0b00101000);
  96.  
  97. ZapisLCD(0b00001111);
  98.  
  99. ZapisLCD(1);
  100.  
  101. _delay_ms(1.64);
  102.  
  103. ZapisLCD(0b00000110);
  104. }
  105. void WyswietlanieLCD(char* co, int8_t ile)
  106. {
  107. int8_t k=0;
  108.  
  109. PORTA |= (1<<PA4);
  110.  
  111. while(k<ile)
  112. {
  113. ZapisLCD(co[k]);
  114. k++;
  115. }
  116. }
  117.  
  118. /*void Wyswietlanie_nizej(char* co,int8_t ile)
  119. {
  120. int8_t k=0;
  121. PORTA &= ~(1<<PA4);
  122. ZapisLCD(0b11000000);
  123. PORTA |= (1<<PA4);
  124.  
  125. while(k<ile)
  126. {
  127. ZapisLCD(napiss[k]);
  128.  
  129. k++;
  130. }
  131.  
  132. } */
  133.  
  134. void ZapisLCD(int8_t bajt)
  135. {
  136. PORTA |= (1<<PA6);
  137. PORTB = (bajt& 0xf0)|(PORTB & 0x0f);
  138. PORTA &= ~(1<<PA6);
  139.  
  140. asm volatile("nop");
  141.  
  142. PORTA |= (1<<PA6);
  143. PORTB = ((bajt& 0x0f)<<4)|(PORTB & 0x0f);
  144. PORTA &= ~(1<<PA6);
  145.  
  146. _delay_us(40);
  147.  
  148. }
  149. void przesuwanie(int8_t ile)
  150. {
  151. int8_t i=0;
  152. int8_t k=0;
  153.  
  154. PORTA &= ~(1<<PA4);
  155.  
  156. while(i<ile)
  157. {
  158. ZapisLCD(0b00011100);
  159. _delay_ms(250);
  160. i++;
  161. }
  162. while (k<ile)
  163. {
  164. ZapisLCD(0b00011000);
  165. _delay_ms(250);
  166. k++;
  167. }
  168. PORTA |= (1<<PA4);
  169. }
  170. void Znak(const char *kod, int8_t AAA)
  171. {
  172. int8_t k;
  173. PORTA &= ~(1<<PA4);
  174. ZapisLCD((0x40)|(AAA<<3));
  175. PORTA |= (1<<PA4);
  176.  
  177. _delay_us(40);
  178.  
  179. for (k=0; k<8; k++) ZapisLCD(kod[k]);
  180.  
  181. PORTA &= ~(1<<PA4);
  182. ZapisLCD (0x80);
  183. PORTA |= (1<<PA4);
  184.  
  185. _delay_us(40);
  186. }
  187. void InicjalizacjaADC()
  188. {
  189. DDRA &= ~(1<<PA0);
  190. ADMUX = (1<<REFS1) | (1<<REFS0);
  191.  
  192. ADCSRA = (1<<ADEN) | (1<<ADPS2) | (1<<ADPS0);
  193. }
  194. void PomiarADC()
  195. {
  196. int16_t nap = 0;
  197. int8_t napjed = 0;
  198. int8_t napdzies = 0;
  199. int8_t napset = 0;
  200.  
  201. ADCSRA |= (1<<ADSC);
  202.  
  203. while(bit_is_set(ADCSRA, ADSC));
  204.  
  205. nap=ADC/4;
  206.  
  207. napjed= nap/100;
  208. napjedascii = napjed + 0x30;
  209.  
  210. napdzies = (nap - napjed*100)/10;
  211. napdziesascii = napdzies + 0x30;
  212.  
  213. napset = nap - napjed*100 - napdzies*10;
  214. napsetascii = napset + 0x30;
  215.  
  216. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement