Guest User

Untitled

a guest
Jul 15th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.85 KB | None | 0 0
  1. #include <avr/io.h>
  2. #include <avr/interrupt.h>
  3.  
  4. void pwm_init(void)
  5. {
  6.     TCCR0 |= (1<<COM01) | (1<<CS01) | (1<<WGM01) | (1<<WGM00); //4KHZ, NON-INVERTING MODE, FAST PWM MODE,
  7. }
  8.  
  9. void adcinit (void){
  10.     ADCSRA |= (1 << ADPS2) | (1 << ADPS1); // | (1 << ADPS0); // Set ADC prescalar to 64(128) - 125KHz sample rate @ 16MHz
  11.     ADMUX |= (1 << REFS0); // Set ADC reference to AVCC
  12.     ADMUX |= (1 << ADLAR); // Left adjust ADC result to allow easy 8 bit reading
  13.     ADCSRA |= (1 << ADATE);  // Set ADC to Free-Running Mode
  14.     ADCSRA |= (1 << ADEN);  // Enable ADC
  15.     ADCSRA |= (1 << ADIE);  // Enable ADC Interrupt
  16.     ADCSRA |= (1 << ADSC);  // Start A2D Conversions
  17.    
  18. }
  19.    
  20. int main(void)
  21. {
  22.     DDRB |= (1<<PB3);
  23.     pwm_init();
  24.     adcinit();
  25.     OCR0 = 128;
  26.     //sei();
  27.    
  28.     for(;;){}
  29.    
  30.     return 0;
  31. }
  32.  
  33. ISR(ADC_vect)
  34. {
  35.    
  36.     int adc_val = ADCH;
  37.    
  38.     OCR0 = adc_val;
  39.    
  40. }
Add Comment
Please, Sign In to add comment