Advertisement
muhammad_nasif

Untitled

Jun 5th, 2021
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 KB | None | 0 0
  1. #ifndef F_CPU
  2. #define F_CPU 1000000UL // 16 MHz clock speed
  3. #endif
  4. #define D4 eS_PORTD4
  5. #define D5 eS_PORTD5
  6. #define D6 eS_PORTD6
  7. #define D7 eS_PORTD7
  8. #define RS eS_PORTC6
  9. #define EN eS_PORTC7
  10.  
  11. #include <string.h>
  12. #include <stdio.h>
  13. #include <avr/io.h>
  14. #include <util/delay.h>
  15. #include "lcd.h" //Can be download from the bottom of this article
  16.  
  17. int main(void)
  18. {
  19. DDRD = 0xFF;
  20. DDRC = 0xFF;
  21. int i;
  22. Lcd4_Init();
  23. int result_h;
  24. int result_l;
  25. unsigned char output_l;
  26. unsigned char output_h=0b00000000;
  27.  
  28. DDRB = 0xFF;
  29. //Configure the ADC module of the ATmega16
  30. ADMUX = 0b00000011; //REFS1:0 = 01 -> AVCC as reference
  31. //ADLAR = 0 -> Right adjust
  32. //MUX4:0 = 00011 -> ADC3 as input
  33.  
  34. ADCSRA = 0b10000100;
  35.  
  36. while(1)
  37. {
  38.  
  39. ADCSRA |= (1 << ADSC);
  40. while(ADCSRA & (1 << ADSC)){;}
  41.  
  42. output_l = ADCL;
  43. result_l = ADCL;
  44. //result_h = ADCH;
  45. output_h = ADCH | output_h;
  46. result_h = output_h;
  47.  
  48. //result_h = result_h << 8;
  49. //result_h = result_h | result_l;
  50.  
  51. //output_h = output_h << 6;
  52. //output_l = output_l >> 2;
  53.  
  54. //output_h = output_h | output_l;
  55.  
  56. float voltage = ((output_h << 8) | output_l )*4/1024;
  57.  
  58. //char outLED[10];
  59. //char vt[] = "voltage:";
  60. //gcvt(voltage, 3, outLED);
  61. //sprintf(outLED,"voltage:%1.2f",voltage);
  62.  
  63. //1.65
  64.  
  65. int a = (int)voltage;
  66. int b = (int)(voltage * 10);
  67. int c = (int)(voltage * 100);
  68.  
  69. a = a % 10;
  70. b = b % 10;
  71. c = c % 10;
  72.  
  73. unsigned char num_1 = a + '0';
  74. unsigned char num_2 = b + '0';
  75. unsigned char num_3 = c + '0';
  76.  
  77.  
  78. Lcd4_Set_Cursor(1,1);
  79. /*for(int i=0;i<8;i++ ){
  80. Lcd4_Write_Char(vt[i]);
  81. }
  82. */
  83. Lcd4_Write_Char(num_1);
  84. Lcd4_Write_Char('.');
  85. Lcd4_Write_Char(num_2);
  86. Lcd4_Write_Char(num_3);
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93. PORTB = output_l;
  94.  
  95.  
  96.  
  97. //Lcd4_Write_String("electroSome LCD Hello World");
  98. //Lcd4_Write_String(outLED);
  99. /*
  100. for(i=0;i<15;i++)
  101. {
  102. _delay_ms(250);
  103. // Lcd4_Shift_Left();
  104. }
  105. for(i=0;i<15;i++)
  106. {
  107. _delay_ms(250);
  108. // Lcd4_Shift_Right();
  109. }
  110. Lcd4_Clear();
  111. //Lcd4_Set_Cursor(2,1);
  112. //Lcd4_Write_Char('e');
  113. //Lcd4_Write_Char('S');
  114. _delay_ms(1000);
  115. */
  116.  
  117. }
  118.  
  119.  
  120.  
  121. /*
  122. //long long int result_h;
  123. //long long int result_l;
  124. //unsigned char output_l;
  125. //unsigned char output_h;
  126.  
  127.  
  128. //DDRB = 0xFF;
  129. //Configure the ADC module of the ATmega16
  130. //ADMUX = 0b00000011; //REFS1:0 = 01 -> AVCC as reference
  131. //ADLAR = 0 -> Right adjust
  132. //MUX4:0 = 00000 -> ADC0 as input
  133.  
  134. //ADCSRA = 0b10000100; //ADEN = 1 : enable
  135. //ADSC = 0 :
  136. while(1)
  137. {
  138. ADCSRA |= (1 << ADSC);
  139. while(ADCSRA & (1 << ADSC)){;}
  140.  
  141.  
  142.  
  143. output_l = ADCL;
  144. result_l = ADCL;
  145. result_h = ADCH;
  146. output_h = ADCH;
  147.  
  148. result_h = result_h << 8;
  149. result_h = result_h | result_l;
  150.  
  151. output_h = output_h << 6;
  152. output_l = output_l >> 2;
  153.  
  154. output_h = output_h | output_l;
  155.  
  156. float voltage = (result_h)*4/1024;
  157. PORTB = ~output_h;
  158.  
  159. }
  160. */
  161. return 0;
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement