Advertisement
Guest User

sepic

a guest
Oct 24th, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.92 KB | None | 0 0
  1. #include <avr/io.h>
  2.  
  3. uint16_t adc_read(uint8_t pinc){
  4.     ADMUX = (1<<REFS1)|(1<<REFS0)|pinc;
  5.     ADCSRA |= (1<<ADSC);
  6.     while( ADCSRA & (1 << ADSC )){};
  7.     uint8_t lsb = ADCL;
  8.     uint8_t msb = ADCH;
  9.     uint16_t t = lsb + (msb<<8);
  10.     return t;
  11. };
  12.  
  13.  
  14. int main(void)
  15. {
  16.     DDRB = (1<<PINB1);
  17.     PORTB = (0<<PINB1);
  18.    
  19.     TCCR1A = (1<<COM1A1)|(0<<COM1A0)|(0<<COM1B1)|(0<<COM1B0)|(1<<WGM11);
  20.     TCCR1B = (1<<WGM13)|(1<<CS10);
  21.    
  22.     TCNT1 = 0x00F0;
  23.    
  24.     ICR1 = 160; // top / duty
  25.    
  26.     OCR1A = 120; // compare /
  27.    
  28.     PRR = 0;
  29.     ADCSRA = (1<<ADEN)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0);
  30.     ADMUX = (1<<REFS1)|(1<<REFS0);
  31.    
  32.     uint16_t ocr1a = 100;
  33.     uint16_t set = 512;
  34.    
  35.     while (1)
  36.     {
  37.         uint16_t temp = adc_read(0);
  38.        
  39.         if( temp > set ){
  40.             ocr1a += 1;
  41.         }else{
  42.             ocr1a -= 1;
  43.         }
  44.        
  45.         if( ocr1a < 50  ){
  46.             ocr1a = 50;
  47.         }
  48.        
  49.         if( ocr1a > 160 ){
  50.             ocr1a = 160;
  51.         }
  52.        
  53.         OCR1A = ocr1a;
  54.        
  55.     };
  56. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement