Guest User

Untitled

a guest
Apr 20th, 2018
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.95 KB | None | 0 0
  1. /*
  2.  * ATmega32L_ADC_test.c
  3.  *
  4.  * Created: 03.12.2011 18:53:45
  5.  *  Author: Sabesto
  6.  */
  7.  
  8. #define F_CPU 8000000UL
  9. #define USART_BAUDRATE 9600
  10. #define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1)
  11. #include <avr/io.h>
  12. #include <util/delay.h>
  13. #include <avr/interrupt.h>
  14.  
  15. volatile uint8_t newSampleReady = 0;
  16. volatile int8_t channel = 6;
  17. volatile uint8_t ad[8];
  18. volatile uint8_t at[8] = {0,20,20,20,100,200,120,255};
  19. uint32_t teller = 0;
  20. uint32_t nevner = 0;
  21. uint8_t posisjon = 0;
  22. uint16_t time = 0;
  23. uint16_t lastTime = 0;
  24. int16_t lastErr = 0;
  25. volatile uint64_t millis = 0;
  26. int8_t Setpoint = 50;
  27. int8_t kp, ki, kd;
  28. int16_t output = 0;
  29. int32_t errSum = 0;
  30. int32_t error = 0;
  31.  
  32. void timer1init(void){
  33.     TCCR1B |= (1<<CS10);
  34.     //TCCR1B |= (1<<CS11);  // prescaler 1024
  35.     TIMSK |= (1<<TOIE1);    // timer1 overflow interrupt enabled
  36. }
  37. void uartinit (void)
  38. {
  39.     UCSRB |= (1 << RXEN) | (1 << TXEN);  
  40.     UCSRC |= (1 << UCSZ0) | (1 << UCSZ1);
  41.     UBRRH = (BAUD_PRESCALE >> 8);
  42.     UBRRL = BAUD_PRESCALE;
  43. }
  44. void USART1_Transmit(unsigned char data)
  45. {
  46.    while (!(UCSRA & (1<<UDRE)));                        
  47.    UDR = data;                
  48. }
  49. void USART1_Transmituint16(uint16_t data)
  50. {
  51.     uint8_t low = data;
  52.     uint8_t high = (data>>8);
  53.    
  54.    while (!(UCSRA & (1<<UDRE)));                        
  55.    UDR = high;
  56.    while (!(UCSRA & (1<<UDRE)));                        
  57.    UDR = low;                  
  58. }
  59. void USART1_TransmitString (const char *str) {
  60.  
  61.     while (*str)
  62.     {
  63.         USART1_Transmit(*str);
  64.         str++;
  65.     }    
  66. }
  67. void SetTunings(uint32_t Kp, uint32_t Ki, uint32_t Kd)
  68. {
  69.    kp = Kp;
  70.    ki = Ki;
  71.    kd = Kd;
  72. }
  73. void adcinit (void){
  74.     ADCSRA |= (1 << ADPS2) | (1 << ADPS1); // | (1 << ADPS0); // Set ADC prescalar to 64(128) - 125KHz sample rate @ 16MHz
  75.     ADMUX = 0b01100000;
  76.     //ADMUX |= (1 << REFS0); // Set ADC reference to AVCC
  77.     //ADMUX |= (1 << ADLAR); // Left adjust ADC result to allow easy 8 bit reading
  78.     ADCSRA |= (1 << ADATE);  // Set ADC to Free-Running Mode
  79.     ADCSRA |= (1 << ADEN);  // Enable ADC
  80.     ADCSRA |= (1 << ADIE);  // enable adc interrupt
  81.     ADCSRA |= (1 << ADSC);  // Start A2D Conversions
  82.    
  83. }
  84. int main (void)
  85. {
  86.     SetTunings(2,0,100);
  87.     timer1init();
  88.     DDRC = 0xff;
  89.     DDRD = 0b10000000;
  90.     uartinit();
  91.     adcinit();
  92.     sei();
  93.     while(1)
  94.     {
  95.         if (newSampleReady == 1)
  96.         {
  97.             PORTD |= (1<<PD7);
  98.             teller=0;
  99.             nevner=0;
  100.             posisjon=0;
  101.             for (uint16_t x=1;x<8;x++)
  102.             {
  103.                 if(ad[x]<30){ad[x]=0;}
  104.                 else {ad[x] -= 30;}
  105.                 teller += ((x-1)<<4)*(ad[x]);  
  106.             }
  107.             for (uint8_t x=1;x<8;x++)
  108.             {
  109.                 nevner += ad[x];
  110.             }
  111.             posisjon = (teller/nevner);
  112.             //PID--------------------------
  113.             uint32_t now = millis;
  114.             uint32_t timeChange = (uint32_t)(now - lastTime);
  115.             error = Setpoint - posisjon;
  116.             errSum += (error * timeChange);
  117.             uint32_t dErr = (error - lastErr) / timeChange;
  118.             output = kp * error + ki * errSum + kd * dErr;
  119.             lastErr = error;
  120.             lastTime = now;
  121.             //PID------------------------------
  122.             /*
  123.             if(posisjon < 14){PORTC = 0b00000001;}
  124.             if(posisjon > 14 && posisjon < 28){PORTC =  0b00000010;}
  125.             if(posisjon > 28 && posisjon < 42){PORTC =  0b00000100;}
  126.             if(posisjon > 42 && posisjon < 56){PORTC =  0b00001000;}
  127.             if(posisjon > 56 && posisjon < 70){PORTC =  0b00010000;}
  128.             if(posisjon > 70 && posisjon < 82){PORTC =  0b00100000;}
  129.             if(posisjon > 82 && posisjon < 100){PORTC = 0b01000000;}
  130.             */
  131.             USART1_Transmituint16(output>>3);
  132.            
  133.             newSampleReady = 0;
  134.             ADCSRA |= (1<<ADIE);
  135.             PORTD &= ~(1<<PD7);
  136.         }
  137.        
  138.     }
  139. }
  140.  
  141. ISR(ADC_vect)
  142. {
  143.     ad[channel] = ADCH; // Store the result (8-bits, discard the 3 LSB)
  144.     // ADCSRA &= ~(1 << ADEN); // Disable the ADC
  145.     channel ++;
  146.    
  147.        
  148.     if (channel == 8)
  149.     {
  150.         ADCSRA &= ~(1<<ADIE);
  151.         // disable interrupts for now
  152.         newSampleReady = 1; // 7 channels sampled
  153.         channel = 0; // Reset to channel 0
  154.     }  
  155.     ADMUX &= 0xE0;//Update the ADMUX register
  156.     ADMUX |= channel;
  157. }
  158. SIGNAL(TIMER1_OVF_vect)
  159. {
  160.    TCNT1 = 0xF0FF;
  161.    millis++;
  162. }
Add Comment
Please, Sign In to add comment