Advertisement
Guest User

Untitled

a guest
Jun 28th, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.36 KB | None | 0 0
  1. /*
  2. * lcd+lm35.c
  3. *
  4. * Created: 2016-06-10 00:04:53
  5. * Author : Pat
  6. */
  7.  
  8. #define F_CPU 1000000L
  9. #include <avr/delay.h>
  10. #include <avr/io.h>
  11. #include <string.h>
  12. #include <avr/interrupt.h>
  13.  
  14.  
  15. /****************************************************************************************************
  16. Podłączenie pinów
  17.  
  18. LCD
  19.  
  20. vss GND
  21. vdd Vcc
  22. v0 potencjometr
  23. rs PC5
  24. rw PC6
  25. e PC7
  26. D0...D7 PB0...PB7
  27. A Vcc
  28. K GND
  29.  
  30. LM35
  31.  
  32. Vo PA5
  33.  
  34. *****************************************************************************************************/
  35.  
  36. /*LCD function declarations */
  37. void LCD_send_command(unsigned char cmnd);
  38. void LCD_send_data(unsigned char data);
  39. void LCD_init();
  40. void LCD_goto(unsigned char y, unsigned char x);
  41. void LCD_print(char *string);
  42.  
  43. #define LCD_DATA_PORT PORTB
  44. #define LCD_DATA_DDR DDRB
  45. #define LCD_DATA_PIN PINB
  46.  
  47. #define LCD_CNTRL_PORT PORTC
  48. #define LCD_CNTRL_DDR DDRC
  49. #define LCD_CNTRL_PIN PINC
  50.  
  51. #define LCD_RS_PIN 5
  52. #define LCD_RW_PIN 6
  53. #define LCD_ENABLE_PIN 7
  54.  
  55. #define SET_HOUR 3
  56. #define SET_MINUTE 4
  57.  
  58. //volatile zmienna=ADMUX;
  59.  
  60. int main(void)
  61. {
  62.  
  63.  
  64. init_ADC();
  65.  
  66. init_timer();
  67.  
  68. LCD_init();
  69. //LCD_goto(2,1);
  70. //LCD_print("Nap: ");
  71. //LCD_goto(1,1);
  72. //LCD_print("Temp:");
  73. sei();
  74.  
  75. LCD_CNTRL_PORT = (1<<SET_HOUR | 1<<SET_MINUTE);
  76. //ADMUX |=(1<<MUX0)|(1<<MUX2)|(1<<MUX1);
  77.  
  78. while (1)
  79. {
  80. }
  81. }
  82.  
  83. ISR(TIMER0_OVF_vect)
  84. {
  85. float pomiar, volty;
  86. float pomiar_lm;
  87. char str[8], str1[8];
  88. char str_lm[8];
  89. ADCSRA |= (1 << ADSC); // rozpocznij przetwarzanie
  90.  
  91. while(ADCSRA & (1<<ADSC)); // czekaj na koniec przetwarzania
  92.  
  93.  
  94.  
  95. volty = ADC;
  96. volty *= 0.0049; // bity na volty
  97.  
  98.  
  99. pomiar=volty-2.3 ; // volty na stopnie(-Vw0)
  100. pomiar/=0.0100; //0.01V/1 C
  101. _delay_ms(500);
  102.  
  103. ADMUX^=(1<<MUX0);
  104.  
  105.  
  106. ADCSRA |= (1 << ADSC); // rozpocznij przetwarzanie
  107.  
  108. while(ADCSRA & (1<<ADSC)); // czekaj na koniec przetwarzania
  109. pomiar_lm=ADCW;
  110. pomiar_lm*=0.0049;
  111. pomiar_lm*=100;
  112. _delay_ms(500);
  113.  
  114.  
  115.  
  116.  
  117.  
  118. dtostrf(volty, 8, 2, str);
  119. dtostrf(pomiar, 8, 2, str1);
  120. dtostrf(pomiar_lm, 8, 2, str_lm);
  121.  
  122. LCD_goto(1,6);
  123. LCD_print(str1);
  124.  
  125. LCD_goto(2,6);
  126. LCD_print(str);
  127.  
  128. LCD_goto(1,15);
  129. LCD_print("*C");
  130.  
  131. LCD_goto(2,1);
  132. LCD_print(str_lm);
  133.  
  134. _delay_ms(500);
  135.  
  136.  
  137. TCNT0 = 255;
  138. }
  139.  
  140.  
  141. void init_timer (void)
  142. {
  143. //Inicjalizacja Timera
  144. TIMSK |= (1<<TOIE0) | (1<<TOIE1); //Przerwanie overflow przepełnienie timera
  145. TCCR0 |= (1<<CS02) | (1<<CS00) ; // źródłem CLK, preskaler 1024 (~1000 Hz)/0.001s
  146. TCNT0 = 255; //Początkowa wartość licznika
  147. }
  148.  
  149. void init_ADC (void)
  150. {
  151. ADMUX |= (1 << MUX0); // wybor wejscia dla przetwornika (ADC5-PA5)
  152. ADMUX |= (1 << MUX2);
  153. //
  154. //ADMUX |= (1 << MUX0); // wybor wejscia dla przetwornika (ADC5-PA7)
  155. //ADMUX |= (1 << MUX2);
  156. //ADMUX |= (1 << MUX1);
  157. //
  158.  
  159. ADMUX |= (1 << REFS0); // ustaw wewnetrzne napiecie odniesienia na AVCC
  160.  
  161. ADCSRA |= (1 << ADPS2); // wybór czestotliwosci pracy przetwornika preskaler=128
  162. ADCSRA |= (1 << ADPS1);
  163. ADCSRA |= (1 << ADPS0);
  164.  
  165. ADCSRA |= (1<<ADIE); //uruchomienie zgłaszania przerwań
  166.  
  167. DDRA &= ~(1<<PA5); //PA5 jak wejście
  168. //
  169. DDRA &= ~(1<<PA7); //PA7 jak wejście
  170. //
  171. ADCSRA |= (1 <<ADEN); // zezwolenie na prace ADC
  172. }
  173.  
  174.  
  175.  
  176. void LCD_send_command(unsigned char cmnd)
  177. {
  178. LCD_DATA_PORT = cmnd;
  179. LCD_CNTRL_PORT &= ~(1<<LCD_RW_PIN);
  180. LCD_CNTRL_PORT &= ~(1<<LCD_RS_PIN);
  181.  
  182. LCD_CNTRL_PORT |= (1<<LCD_ENABLE_PIN);
  183. _delay_us(2);
  184. LCD_CNTRL_PORT &= ~(1<<LCD_ENABLE_PIN);
  185. _delay_us(100);
  186. }
  187.  
  188. void LCD_send_data(unsigned char data)
  189. {
  190. LCD_DATA_PORT = data;
  191. LCD_CNTRL_PORT &= ~(1<<LCD_RW_PIN);
  192. LCD_CNTRL_PORT |= (1<<LCD_RS_PIN);
  193.  
  194. LCD_CNTRL_PORT |= (1<<LCD_ENABLE_PIN);
  195. _delay_us(2);
  196. LCD_CNTRL_PORT &= ~(1<<LCD_ENABLE_PIN);
  197. _delay_us(100);
  198. }
  199.  
  200. void LCD_init()
  201. {
  202. LCD_CNTRL_DDR = 0xFF;
  203. LCD_CNTRL_PORT = 0x00;
  204. LCD_DATA_DDR = 0xFF;
  205. LCD_DATA_PORT = 0x00;
  206.  
  207. _delay_ms(10);
  208. LCD_send_command(0x38);
  209. LCD_send_command(0x0C);
  210. LCD_send_command(0x01);
  211. _delay_ms(10);
  212. LCD_send_command(0x06);
  213. }
  214.  
  215. void LCD_goto(unsigned char y, unsigned char x)
  216. {
  217. unsigned char firstAddress[] = {0x80,0xC0,0x94,0xD4};
  218.  
  219. LCD_send_command(firstAddress[y-1] + x-1);
  220. _delay_ms(10);
  221. }
  222.  
  223. void LCD_print(char *string)
  224. {
  225. unsigned char i=0;
  226.  
  227. while(string[i]!=0)
  228. {
  229. LCD_send_data(string[i]);
  230. i++;
  231. }
  232. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement