Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.31 KB | None | 0 0
  1. #include <avr/io.h>
  2. #include <stdint.h>
  3. #include <stdio.h>
  4. #include <avr/interrupt.h>
  5.  
  6. //****************Beginn Deklarationen und Definitionen f?r LCD*******************
  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. }
  48.  
  49. int main(void)
  50. {
  51. DDRD = 0x00;
  52. DDRC = 0xFF;
  53.  
  54. //Timer1 init.
  55. OCR1A = 62500;
  56. TIMSK|= (1 << OCIE1A);
  57. TCCR1B|= (1 << WGM12);
  58. TCCR1B|= (1<<CS12);
  59. sei();
  60.  
  61. while(1);
  62. }
  63.  
  64. ISR(TIMER1_COMPA_vect)
  65. {
  66. PORTC = ~PORTC;
  67. }
  68.  
  69. //*****************Beginn Implementierung der LCD-Funktionen**************
  70. //Initialisierung des LCDs im 2 Zeilen-Modus, blinkender Cursor, schreiben nach rechts
  71. void LCDInit()
  72. {
  73. delay_cycles(2);
  74. DDRC=0x7f;
  75. delay_cycles(17400);
  76. PORTC=0x03;
  77. e_active();
  78. delay_cycles(4700);
  79.  
  80.  
  81. e_active();
  82.  
  83. delay_cycles(150);
  84.  
  85. e_active();
  86.  
  87.  
  88. PORTC=0x02;
  89. e_active();
  90.  
  91. LCDComOut(0x28);
  92. LCDComOut(0x0F);
  93. LCDComOut(0x01);
  94. LCDComOut(0x06);
  95. //delay_cycles(40000);
  96. }
  97.  
  98. //L?scht alle Zeichen im Display
  99. void LCDClearDisplay()
  100. {
  101. LCDComOut(0x01);
  102. }
  103.  
  104. //Setzt den Cursor: 2 Zeilen mit jeweils 16 Stellen sind verf?gbar
  105. void LCDSetCursor(uint8_t zeile, uint8_t spalte)
  106. {
  107. uint8_t wert = 0;
  108. if((zeile < 2) && (zeile >=0) && (spalte >=0) && (spalte < 16))
  109. wert=zeile*64+spalte;
  110. wert=wert | 0x80;
  111. LCDComOut(wert);
  112. }
  113.  
  114. //Gibt einen String an der Position des Cursors aus.
  115. void LCDStringOut(char *pbuf)
  116. {
  117. while( *pbuf != '\0')
  118. {
  119. LCDDataOut(*pbuf);
  120. pbuf++;
  121. }
  122. }
  123.  
  124. //Hilfsfunktion: Befehl zum LCD ?bertragen
  125. void LCDComOut(uint8_t data)
  126. {
  127. uint8_t wert;
  128.  
  129. do
  130. {
  131. wert=LCDGetCom();
  132. }
  133. while((wert & 0x80) != 0);
  134.  
  135. DDRC=0x7F;
  136. wert=data & 0xF0;
  137. wert=wert >> 4;
  138.  
  139. PORTC=wert;
  140. e_active();
  141. wert=data & 0x0F;
  142. PORTC=wert;
  143. e_active();
  144.  
  145. }
  146.  
  147. //Hilfsfunktion: Daten vom LCD lesen im 4-Bit-Modus
  148. uint8_t LCDGetCom()
  149. {
  150. uint8_t wert,wert2,oldddrc;
  151.  
  152. oldddrc=DDRC;
  153. DDRC=0x70;
  154.  
  155. PORTC=0x20;
  156. delay_cycles(2);
  157. PORTC |= (1 << E);
  158. delay_cycles(2);
  159. wert=PINC;
  160. wert = wert & 0x0F;
  161.  
  162. PORTC &= ~(1 << E);
  163. delay_cycles(2);
  164.  
  165. PORTC |= (1 << E);
  166. delay_cycles(2);
  167. wert2=PINC;
  168. wert2=wert2 & 0x0F;
  169. wert = wert << 4;
  170. wert = wert | wert2;
  171. PORTC &= ~(1 << E);
  172. DDRC=oldddrc;
  173.  
  174. return wert;
  175.  
  176. }
  177.  
  178. //Hilfsfunktion: (Anzeige-)Daten zum LCD ?bertragen im 4-Bit-Modus.
  179. void LCDDataOut(char data)
  180. {
  181. uint8_t wert;
  182.  
  183. do
  184. {
  185. wert=LCDGetCom();
  186. }
  187. while((wert & 0x80) != 0);
  188.  
  189. DDRC=0x7F;
  190.  
  191. wert=data >> 4;
  192. wert=wert | (1 << RS) ;
  193.  
  194. PORTC=wert;
  195. e_active();
  196. wert=data & 0x0F;
  197. wert=wert | (1 << RS);
  198. PORTC=wert;
  199. e_active();
  200.  
  201. }
  202.  
  203. //Hilfsfunktion: Realisiert Timing der Steuerleitung E f?r Daten?bertragung
  204. void e_active()
  205. {
  206. delay_cycles(2);
  207. PORTC |= (1<<E);
  208. delay_cycles(2);
  209. PORTC &= ~(1 << E);
  210. delay_cycles(2);
  211. }
  212.  
  213. //Hilfsfunktion: Zeitschleife - Wartet bestimmte Anzahl von Zyklen.
  214. void delay_cycles(unsigned int zyklen)
  215. {
  216. asm volatile("L_1:" "\tSBIW R24,0x00\n"
  217. "\tNOP\n"
  218. "\tBREQ L_2\n"
  219. "\tSBIW R24,0x01\n"
  220. "\tRJMP L_1\n"
  221. "L_2:\t\n"
  222. ::);
  223. }
  224.  
  225. //Hilfsfunktion: Zeitschleife - Wartet bestimmte Anzahl an Millisekunden
  226. void delay_ms(unsigned int ms)
  227. {
  228. unsigned char f_cpu_MHz = 16;
  229. //f_cpu in MHz -> f_cpu:MHz*1000 Zyklen pro ms
  230.  
  231. //Laufzeitkorrektor
  232. ms = ms/8;
  233.  
  234. //Ausf?hrungszeit = 1ms * ms
  235. for(int j = 0; j < ms; j++)
  236. {
  237. //Ausf?hrungszeit = 1 ms
  238. delay_cycles(f_cpu_MHz*1000);
  239. }
  240. }
  241. //*****************Ende Implementierung der LCD-Funktionen**************
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement