Advertisement
Guest User

Untitled

a guest
Jun 28th, 2016
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.86 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. volatile float pomiar;
  61. volatile float pomiar_lm;
  62. volatile int licznik=0;
  63. volatile char str1[8];
  64. volatile char str_lm[8];
  65.  
  66. int main(void)
  67. {
  68.  
  69.  
  70. init_ADC();
  71. LCD_init();
  72. LCD_goto(2,1);
  73. LCD_print("Zimny ");
  74. LCD_goto(1,1);
  75. LCD_print("Cieply:");
  76. sei();
  77.  
  78. LCD_CNTRL_PORT = (1<<SET_HOUR | 1<<SET_MINUTE);
  79.  
  80. while (1)
  81. {
  82.  
  83. pomiar*=0.0049;
  84. pomiar=pomiar-2.3 ; // volty na stopnie(-Vw0)
  85. pomiar/=0.0100; //0.01V/1 C
  86. dtostrf(pomiar, 8, 1, str1);
  87.  
  88. pomiar_lm*=0.0049;
  89. pomiar_lm*=100;
  90. dtostrf(pomiar_lm, 8, 1, str_lm);
  91.  
  92. LCD_goto(1,7);
  93. LCD_print(str1);
  94.  
  95. LCD_goto(1,15);
  96. LCD_print("*C");
  97.  
  98. LCD_goto(2,7);
  99. LCD_print(str_lm);
  100.  
  101. LCD_goto(2,15);
  102. LCD_print("*C");
  103.  
  104. _delay_ms(500);
  105. }
  106. }
  107.  
  108. ISR(ADC_vect)
  109. {
  110.  
  111. if (licznik==0) //termo
  112. {
  113. ADMUX |= (1 << MUX0); // wybor wejscia dla przetwornika (ADC5-PA5)
  114. ADMUX |= (1 << MUX2);
  115. ADMUX &=~(1<<MUX1);
  116. pomiar = ADC;
  117. licznik=1;
  118.  
  119. }
  120. else //lm35
  121. {
  122. ADMUX |= (1 << MUX0); // wybor wejscia dla przetwornika (ADC5-PA5)
  123. ADMUX |= (1 << MUX2);
  124. ADMUX |= (1<<MUX1);
  125. pomiar_lm=ADC;
  126. licznik=0;
  127.  
  128. }
  129.  
  130. }
  131.  
  132. void init_ADC (void)
  133. {
  134.  
  135. ADMUX |= (1 << REFS0); // ustaw wewnetrzne napiecie odniesienia na AVCC
  136.  
  137. ADCSRA |= (1 << ADPS2); // wybór czestotliwosci pracy przetwornika preskaler=128
  138. ADCSRA |= (1 << ADPS1);
  139. ADCSRA |= (1 << ADPS0);
  140. ADCSRA |= (1<<ADATE);
  141.  
  142. ADCSRA |= (1<<ADIE); //uruchomienie zgłaszania przerwań
  143.  
  144. DDRA &= ~(1<<PA5); //PA5 jak wejście
  145. //
  146. DDRA &= ~(1<<PA7); //PA7 jak wejście
  147. //
  148. ADCSRA |= (1 <<ADEN);
  149. ADCSRA |= (1 <<ADSC); // zezwolenie na prace ADC
  150.  
  151. }
  152.  
  153.  
  154.  
  155. void LCD_send_command(unsigned char cmnd)
  156. {
  157. LCD_DATA_PORT = cmnd;
  158. LCD_CNTRL_PORT &= ~(1<<LCD_RW_PIN);
  159. LCD_CNTRL_PORT &= ~(1<<LCD_RS_PIN);
  160.  
  161. LCD_CNTRL_PORT |= (1<<LCD_ENABLE_PIN);
  162. _delay_us(2);
  163. LCD_CNTRL_PORT &= ~(1<<LCD_ENABLE_PIN);
  164. _delay_us(100);
  165. }
  166.  
  167. void LCD_send_data(unsigned char data)
  168. {
  169. LCD_DATA_PORT = data;
  170. LCD_CNTRL_PORT &= ~(1<<LCD_RW_PIN);
  171. LCD_CNTRL_PORT |= (1<<LCD_RS_PIN);
  172.  
  173. LCD_CNTRL_PORT |= (1<<LCD_ENABLE_PIN);
  174. _delay_us(2);
  175. LCD_CNTRL_PORT &= ~(1<<LCD_ENABLE_PIN);
  176. _delay_us(100);
  177. }
  178.  
  179. void LCD_init()
  180. {
  181. LCD_CNTRL_DDR = 0xFF;
  182. LCD_CNTRL_PORT = 0x00;
  183. LCD_DATA_DDR = 0xFF;
  184. LCD_DATA_PORT = 0x00;
  185.  
  186. _delay_ms(10);
  187. LCD_send_command(0x38);
  188. LCD_send_command(0x0C);
  189. LCD_send_command(0x01);
  190. _delay_ms(10);
  191. LCD_send_command(0x06);
  192. }
  193.  
  194. void LCD_goto(unsigned char y, unsigned char x)
  195. {
  196. unsigned char firstAddress[] = {0x80,0xC0,0x94,0xD4};
  197.  
  198. LCD_send_command(firstAddress[y-1] + x-1);
  199. _delay_ms(10);
  200. }
  201.  
  202. void LCD_print(char *string)
  203. {
  204. unsigned char i=0;
  205.  
  206. while(string[i]!=0)
  207. {
  208. LCD_send_data(string[i]);
  209. i++;
  210. }
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement