JustinCase2020

Potentiomètre + Servo x3

May 4th, 2021
957
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * Robot-168-01.cpp
  3.  *
  4.  * Created: 01-May-21 18:22:03
  5.  * Author : Justin Castilloux
  6.  */
  7.  
  8. #include <avr/io.h>
  9.  
  10. void SetupConfigRegisters()
  11. {
  12.     // ADC
  13.     ADMUX        = 0x40;                                        // AN0 Selected, ref=AVcc, Right adjusted
  14.     ADCSRA      |= (1 << ADPS1) | (1 << ADPS2) | (1 << ADEN);   // Prescaler 1/64 (125kHz)
  15.     DIDR0       |= (1 << ADC0D) | (1 << ADC1D) | (1 << ADC2D);
  16.  
  17.     // TIMER1 - Servo #1 & #3
  18.     TCCR1A = 0xA2;                      // OC1A/B Non-inverting, Fast PWM, TOP=ICR1
  19.     TCCR1B = 0x1A;                      // 1:8 prescaler (1us/cycle)
  20.     ICR1 = 20000;                       // 20ms = 50 Hz
  21.     OCR1A = ((2300 - 700) / 2) + 700;   // Approximativement au centre
  22.     OCR1B = ((1800 - 600) / 2) + 600;   // Approximativement au centre
  23.     DDRB |= (1 << PB1) | (1 << PB2);    // OC1A/B Output
  24.    
  25.     // TIMER2 - Servo #2
  26.     TCCR2A = 0x23;                      // OCR2B Non-inverting, Fast PWM, TOP=OCR2A
  27.     TCCR2B = 0x0F;                      // Prescaler 1:1024
  28.     OCR2A = 156;                        // 8M / 50Hz = 160k, 160k / 1024 = 156.25
  29.     OCR2B = 14;                         // Approximativement au centre
  30.     DDRD |= (1 << PD3);                 // OC2B Output
  31. }
  32.  
  33.  
  34. int main(void)
  35. {
  36.     SetupConfigRegisters();
  37.    
  38.     uint16_t A0, A1, A2;
  39.  
  40.     while (1)
  41.     {
  42.         ADMUX = 0x40;
  43.         ADCSRA |= (1 << ADSC);
  44.         while (ADCSRA & (1 << ADSC))
  45.             ;
  46.         A0 = ADC;
  47.        
  48.         ADMUX = 0x41;
  49.         ADCSRA |= (1 << ADSC);
  50.         while (ADCSRA & (1 << ADSC))
  51.             ;
  52.         A1 = ADC;
  53.        
  54.         ADMUX = 0x42;
  55.         ADCSRA |= (1 << ADSC);
  56.         while (ADCSRA & (1 << ADSC))
  57.             ;
  58.         A2 = ADC;
  59.        
  60.         OCR1A = ((A0 * 1600UL) >> 10) + 700;    // 700 -> 2300
  61.         OCR1B = ((A2 * 1200UL) >> 10) + 600;    // 600 -> 1800
  62.         OCR2B = (((A1 >> 2) * 4UL) >> 8) + 14;  // 14 -> 18 ????
  63.     }
  64. }
  65.  
  66.  
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×