Advertisement
Guest User

ADC_Atmega8

a guest
Aug 15th, 2014
434
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.00 KB | None | 0 0
  1. #define F_CPU 8000000UL
  2. #include <avr/io.h>
  3. #include <avr/interrupt.h>
  4. #include <util/delay.h>
  5. #include <lcd.h>
  6. #include <stdlib.h>
  7. #include <avr/eeprom.h>
  8. #include <stdio.h>
  9. #include <math.h>
  10.  
  11. char buffer[10];
  12. char buffer1[10];
  13. char buffer2[10];
  14. char buffer3[10];
  15. char buffer4[10];
  16. char buffer5[10];
  17. char buffer6[10];
  18.  
  19. volatile uint16_t motor_counter = 0;
  20. volatile uint16_t delay_counter = 0;
  21. volatile int MENU;
  22. volatile float values;
  23. volatile uint16_t on_timer_value = 5;
  24. volatile uint16_t off_timer_value = 5;
  25. volatile float f,ff; // live current load
  26. volatile float f1 = 2.2; // less than amp float values
  27. volatile float f2 = 3.0; // greater than amp float values
  28.  
  29.  
  30. volatile uint8_t x = 0, y = 0, z = 0, x1 = 0, ESD = 0;
  31.  
  32. ISR(ADC_vect)
  33. {
  34.     f = ((ADC * 0.0049) - 2.52);
  35. }
  36.  
  37. int main(void)
  38. {  
  39.    
  40.     DDRB = 0x00; // ALL PORT I/O ARE INPUT
  41.     PORTB = 0b00111111; // ENABLE PULL UP FOR ALL INPUT PINS
  42.     DDRC = 0xEE; // all port c i/o are output, C0 AND C4 ARE INPUT
  43.     PORTC |=(1<<4); // TURN ON PULL FOR PORTC PIN 4 MANUAL START BUTTON
  44.     DDRD = 0XFF; // ALL PORTD OUTPUT DIGITAL
  45.     InitTimer1();
  46.     lcd_init(LCD_DISP_ON);  
  47.      lcd_puts("SYSTEM INIT....");
  48.      _delay_ms(500);
  49.      lcd_clrscr();
  50.     lcd_screen_backlight_on();
  51.  
  52.  
  53. // A2D Conversion Initialization
  54.    ADCSRA |= (1 << ADPS2)|(1 << ADPS1); // Set ADC prescalar to 64 @ 8mhz
  55.  
  56.    ADMUX |= (1 << REFS0); // Set ADC reference to AVCC
  57.   // ADMUX |= (1 << ADLAR); // Left adjust ADC result to allow easy 8 bit reading
  58.    
  59.  
  60.    // No MUX values needed to be changed to use ADC0
  61.    ADCSRA |= (1 << ADIE);
  62.    ADCSRA |= (1 << ADFR);  // Set ADC to Free-Running Mode
  63.    ADCSRA |= (1 << ADEN);  // Enable ADC
  64.    ADCSRA |= (1 << ADSC);  // Start A2D Conversions
  65.    
  66.    // A2D Conversion Initialization
  67.  
  68. while(1)
  69. {
  70.         lcd_gotoxy(0,0);
  71.         lcd_puts("MOTOR CURRENT");
  72.         lcd_gotoxy(0,1);
  73.         ff = f/0.066;
  74.        
  75.         float avg = 0;
  76.         for(int i=0;i<1000;i++)
  77.         {
  78.             avg = avg + (ff)/1000;
  79.         }
  80.         sprintf(buffer3, "%f", ff);
  81.         lcd_puts(buffer3);
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement