Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <avr/io.h>
- uint16_t adc_read(uint8_t pinc){
- ADMUX = (1<<REFS1)|(1<<REFS0)|pinc;
- ADCSRA |= (1<<ADSC);
- while( ADCSRA & (1 << ADSC )){};
- uint8_t lsb = ADCL;
- uint8_t msb = ADCH;
- uint16_t t = lsb + (msb<<8);
- return t;
- };
- int main(void)
- {
- DDRB = (1<<PINB1);
- PORTB = (0<<PINB1);
- TCCR1A = (1<<COM1A1)|(0<<COM1A0)|(0<<COM1B1)|(0<<COM1B0)|(1<<WGM11);
- TCCR1B = (1<<WGM13)|(1<<CS10);
- TCNT1 = 0x00F0;
- ICR1 = 160; // top / duty
- OCR1A = 120; // compare /
- PRR = 0;
- ADCSRA = (1<<ADEN)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0);
- ADMUX = (1<<REFS1)|(1<<REFS0);
- uint16_t ocr1a = 100;
- uint16_t set = 512;
- while (1)
- {
- uint16_t temp = adc_read(0);
- if( temp > set ){
- ocr1a += 1;
- }else{
- ocr1a -= 1;
- }
- if( ocr1a < 50 ){
- ocr1a = 50;
- }
- if( ocr1a > 160 ){
- ocr1a = 160;
- }
- OCR1A = ocr1a;
- };
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement