Last active
February 11, 2020 21:26
-
-
Save tokolist/8319281 to your computer and use it in GitHub Desktop.
Use an AVR Microcontroller and Potentiometer to Control a Servo (ADC and PWM) http://youtu.be/vNwx_W2daJs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define F_CPU 16000000 //16MHz | |
#include <avr/io.h> | |
#include <util/delay.h> | |
#include <avr/interrupt.h> | |
#include <stdlib.h> | |
//#include <stdio.h> | |
//#include "uart/uart.h" | |
#define PWM_PRESCALLER 64 | |
#define ICR_MAX (long double)F_CPU/PWM_PRESCALLER/50 | |
#define OCR_MIN ICR_MAX/20 | |
#define OCR_MAX ICR_MAX/10 | |
//char uart_s[150]; | |
volatile unsigned long adc_val=0; | |
volatile unsigned long counter=0; | |
unsigned long curr_adc=0; | |
ISR (ADC_vect) | |
{ | |
adc_val += ADC; | |
counter++; | |
} | |
int main(void) | |
{ | |
//uart_init(uart_calc_ubrr(F_CPU)); | |
DDRB |= (1<<DDB1); | |
ICR1 = ICR_MAX; | |
OCR1A = OCR_MIN; | |
TCCR1A = (1 << COM1A1) | (1<<WGM11); | |
TCCR1B = (1<<WGM13) | (1<<WGM12) | (1<<CS11) | (1<<CS10); | |
ADMUX = (1 << REFS0); | |
ADCSRA = (1 << ADEN)|(1 << ADFR)|(1 << ADIE)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0); | |
sei(); | |
ADCSRA |= (1<<ADSC); | |
while(1) | |
{ | |
if(counter >= 500) | |
{ | |
cli(); | |
unsigned long round_val = round(adc_val / counter); | |
adc_val=0; | |
counter=0; | |
if(abs(round_val - curr_adc) > 1) | |
{ | |
curr_adc = round_val; | |
} | |
long double ocr = OCR_MIN + ((long double)curr_adc * (OCR_MAX - OCR_MIN)/1024); | |
/*sprintf(uart_s, "ADC = %8lu, OCR = %f, rounded = %d\r\n", curr_adc, ocr, (int)round(ocr)); | |
uart_send_string((unsigned char*) uart_s);*/ | |
OCR1A = (int)round(ocr); | |
//_delay_ms(100); | |
sei(); | |
} | |
} | |
} |
I tested it on ATMega8-16PU (see schematics in video description), however I believe it will work for any AVR with some minor corrections.
Hello i am also trying someting like this. But i cant open the schematics, google + doesnt allow it or something (consumers acounts)...
Can you help me with that?
hi google+ got shut down, uploaded it to google photos, you can find new link in video description, thanks for letting me know
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For which controller this code has been written?