Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.49 KB | None | 0 0
  1. #include <avr/io.h>
  2.  
  3. //****************Beginn Deklarationen und Definitionen f?r LCD*******************
  4. #include <stdint.h>
  5. #include <stdio.h>
  6. #include <avr/interrupt.h>
  7. //Signale f?r das LCD an PortC
  8. #define RS 6 // 0= Kommando, 1= Datenwert
  9. #define RW 5 // 0= Schreiben ins Display, 1= Lesen vom Display
  10. #define E 4 // enable, active high
  11. //Funktionen f?r LCD
  12. void LCDInit();
  13. void LCDClearDisplay();
  14. void LCDSetCursor(uint8_t, uint8_t);
  15. void LCDStringOut(char *);
  16. //Hilfsfunktionen, nicht zur eigenen Verwendung vorgesehen
  17. uint8_t LCDGetCom();
  18. void LCDDataOut(char);
  19. void e_active();
  20. void LCDComOut(uint8_t);
  21. void delay_cycles(unsigned int zyklen);
  22. void delay_ms(unsigned int ms);
  23. //****************Ende Deklarationen und Definitionen f?r LCD*********************
  24.  
  25. unsigned int getAnalogValue(unsigned char channel)
  26. {
  27. //ADMUX ADCMultiplex Selection Register -- Kanalauswahl
  28. //Bit 0 1 2 legen den Kanal fest. bsp. Kanal.5 = Bit 101
  29.  
  30. //ADCRSA ADC Control and Status Register
  31. //Steuerung und Konfiguration des ADC
  32.  
  33. ADMUX=channel & 0x07;
  34. ADCSRA |= (1 << ADEN) //ADC Enable -> Bit 7 - An - Aus Schalter vom ADC (wenn noch eine Bearbeitung läuft wird sie beendet und gelöscht)
  35. | (1 << ADSC) //ADC Conversion -> Bit 6 -Starte Umwandung
  36. | (1 << ADPS2) //ADC Prescaler Select Bits -> Bit 2 - (Vorteiler) Prescaler einstellen nach Taktfrequenz -> Register
  37. | (1 << ADPS1) //ADC Prescaler Select Bits -> Bit 1 -(Vorteiler) Prescaler einstellen nach Taktfrequenz -> Register
  38. | (1 << ADPS0);
  39. //ADPS0 - ADPS1 legen Fest welcher Vorteiler für die Taktfrequenz verwendet wird
  40. //In unserem Falls Bit2=1 Bit1=1 Bit0=1 der Vorteiler ist hier 128
  41. //Siehe Seite 222
  42. //Bei einer µc-Taktfrequenz von 16 MHz -> 16MHz / 64 = 125 KHz
  43.  
  44. while((ADCSRA & (1 << ADSC)) != 0); //Wartet bis ADSC auf 0 gesetzt ist. Wartet bis Umwandlung fertig ist
  45. return ADC; //ADC Data Register enthält den Umgewandelten Dual Wert als 10 Bit Zahl.
  46.  
  47. volatile int zaehler;
  48. }
  49.  
  50. int main(void)
  51. {
  52. DDRD = 0x00; //Port A ist Eingang
  53.  
  54. int sek;
  55. OCR= = 156;
  56. TIMSK |= (1 << OCIE0);
  57. Interputs Timer0
  58. TCCR0 |= (1 << WGM01);
  59. TCCR0 |= (1<<CS00) | (1<<CS02);
  60. sei();
  61.  
  62. while(1)
  63. {
  64. if(zaehler>=100)
  65. {
  66. sek++;
  67. zaehler=0;
  68. }
  69.  
  70. }
  71. }
  72.  
  73. ISR(TIMER0_COMP_vect)
  74. {
  75. zaehler++;
  76. }
  77.  
  78. //*****************Beginn Implementierung der LCD-Funktionen**************
  79. //Initialisierung des LCDs im 2 Zeilen-Modus, blinkender Cursor, schreiben nach rechts
  80. void LCDInit()
  81. {
  82. delay_cycles(2);
  83. DDRC=0x7f;
  84. delay_cycles(17400);
  85. PORTC=0x03;
  86. e_active();
  87. delay_cycles(4700);
  88.  
  89.  
  90. e_active();
  91.  
  92. delay_cycles(150);
  93.  
  94. e_active();
  95.  
  96.  
  97. PORTC=0x02;
  98. e_active();
  99.  
  100. LCDComOut(0x28);
  101. LCDComOut(0x0F);
  102. LCDComOut(0x01);
  103. LCDComOut(0x06);
  104. //delay_cycles(40000);
  105. }
  106.  
  107. //L?scht alle Zeichen im Display
  108. void LCDClearDisplay()
  109. {
  110. LCDComOut(0x01);
  111. }
  112.  
  113. //Setzt den Cursor: 2 Zeilen mit jeweils 16 Stellen sind verf?gbar
  114. void LCDSetCursor(uint8_t zeile, uint8_t spalte)
  115. {
  116. uint8_t wert = 0;
  117. if((zeile < 2) && (zeile >=0) && (spalte >=0) && (spalte < 16))
  118. wert=zeile*64+spalte;
  119. wert=wert | 0x80;
  120. LCDComOut(wert);
  121. }
  122.  
  123. //Gibt einen String an der Position des Cursors aus.
  124. void LCDStringOut(char *pbuf)
  125. {
  126. while( *pbuf != '\0')
  127. {
  128. LCDDataOut(*pbuf);
  129. pbuf++;
  130. }
  131. }
  132.  
  133. //Hilfsfunktion: Befehl zum LCD ?bertragen
  134. void LCDComOut(uint8_t data)
  135. {
  136. uint8_t wert;
  137.  
  138. do
  139. {
  140. wert=LCDGetCom();
  141. }
  142. while((wert & 0x80) != 0);
  143.  
  144. DDRC=0x7F;
  145. wert=data & 0xF0;
  146. wert=wert >> 4;
  147.  
  148. PORTC=wert;
  149. e_active();
  150. wert=data & 0x0F;
  151. PORTC=wert;
  152. e_active();
  153.  
  154. }
  155.  
  156. //Hilfsfunktion: Daten vom LCD lesen im 4-Bit-Modus
  157. uint8_t LCDGetCom()
  158. {
  159. uint8_t wert,wert2,oldddrc;
  160.  
  161. oldddrc=DDRC;
  162. DDRC=0x70;
  163.  
  164. PORTC=0x20;
  165. delay_cycles(2);
  166. PORTC |= (1 << E);
  167. delay_cycles(2);
  168. wert=PINC;
  169. wert = wert & 0x0F;
  170.  
  171. PORTC &= ~(1 << E);
  172. delay_cycles(2);
  173.  
  174. PORTC |= (1 << E);
  175. delay_cycles(2);
  176. wert2=PINC;
  177. wert2=wert2 & 0x0F;
  178. wert = wert << 4;
  179. wert = wert | wert2;
  180. PORTC &= ~(1 << E);
  181. DDRC=oldddrc;
  182.  
  183. return wert;
  184.  
  185. }
  186.  
  187. //Hilfsfunktion: (Anzeige-)Daten zum LCD ?bertragen im 4-Bit-Modus.
  188. void LCDDataOut(char data)
  189. {
  190. uint8_t wert;
  191.  
  192. do
  193. {
  194. wert=LCDGetCom();
  195. }
  196. while((wert & 0x80) != 0);
  197.  
  198. DDRC=0x7F;
  199.  
  200. wert=data >> 4;
  201. wert=wert | (1 << RS) ;
  202.  
  203. PORTC=wert;
  204. e_active();
  205. wert=data & 0x0F;
  206. wert=wert | (1 << RS);
  207. PORTC=wert;
  208. e_active();
  209.  
  210. }
  211.  
  212. //Hilfsfunktion: Realisiert Timing der Steuerleitung E f?r Daten?bertragung
  213. void e_active()
  214. {
  215. delay_cycles(2);
  216. PORTC |= (1<<E);
  217. delay_cycles(2);
  218. PORTC &= ~(1 << E);
  219. delay_cycles(2);
  220. }
  221.  
  222. //Hilfsfunktion: Zeitschleife - Wartet bestimmte Anzahl von Zyklen.
  223. void delay_cycles(unsigned int zyklen)
  224. {
  225. asm volatile("L_1:" "\tSBIW R24,0x00\n"
  226. "\tNOP\n"
  227. "\tBREQ L_2\n"
  228. "\tSBIW R24,0x01\n"
  229. "\tRJMP L_1\n"
  230. "L_2:\t\n"
  231. ::);
  232. }
  233.  
  234. //Hilfsfunktion: Zeitschleife - Wartet bestimmte Anzahl an Millisekunden
  235. void delay_ms(unsigned int ms)
  236. {
  237. unsigned char f_cpu_MHz = 16;
  238. //f_cpu in MHz -> f_cpu:MHz*1000 Zyklen pro ms
  239.  
  240. //Laufzeitkorrektor
  241. ms = ms/8;
  242.  
  243. //Ausf?hrungszeit = 1ms * ms
  244. for(int j = 0; j < ms; j++)
  245. {
  246. //Ausf?hrungszeit = 1 ms
  247. delay_cycles(f_cpu_MHz*1000);
  248. }
  249. }
  250. //*****************Ende Implementierung der LCD-Funktionen**************
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement