Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2019
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. #define PULSE_PIN 45
  2. void setup() {
  3. // IMPORTANT: Clear Timer/Counter Control Registers
  4. TCCR5A = 0;
  5. TCCR5B = 0;
  6.  
  7. // Set Timer/Counter Control Registers
  8. TCCR5A = B00101001; // Phase and frequency correct PWM change at OCRA
  9. TCCR5B = B00010010; // least sig bits sets "carrier frequency" by dividing CPU clock
  10. // Observation:
  11. // TCCR5B = B00010010 and OCR5A = 16667 results in period of 16.70 mSec
  12. // meaning OCR5A is period in uSec when TCCR5B = B00010010
  13. // TCCR5B = B00010001 : OCR5A = 20000 results in period = 2.5 mSec 399.3Hz
  14.  
  15. pinMode(PULSE_PIN, OUTPUT);
  16. }
  17.  
  18. void setRpm(){
  19. // Getting to period in uSec from rpm the long way to keep
  20. // the answer in range of the data type:
  21. unsigned int rpm = map(pot_adc, 0, 1023, RPM_MIN, RPM_MAX);
  22. unsigned long period = rpm/50;
  23. period = 24000/period;
  24. period = period*50;
  25. // The trigger lasts 26°, so the PW is 26/360 * period.
  26. dwell = period/36;
  27. dwell = dwell*26;
  28. dwell = dwell/10;
  29.  
  30. // OCR5A is a counter the carrier frequency counts up to,
  31. // which sets output period / frequency
  32. // meaning OCR5A is period in uSec when TCCR5B = B00010010
  33. OCR5A = period; // OCR5A is period in uSec when TCCR5B = B00010010
  34. // OCR5B is the width of the pulse. If we used floats,
  35. // we could use duty cycle * period.
  36. OCR5B = dwell;
  37. }
  38.  
  39. // WGM modes:
  40. // TCCR1A
  41. // Bit 7 6 5 4 3 2 1 0
  42. // Bit Name COM1A1 COM1A0 COM1B1 COM1B0 ----- ----- WGM11 WGM10
  43. // Initial Value 0 0 0 0 0 0 0 0
  44. // changed to 1 1 1 1 0 0 0 1
  45.  
  46. // TCCR1B
  47. // Bit 7 6 5 4 3 2 1 0
  48. // Bit Name ICNC1 ICES1 ----- WGM13 WGM12 CS12 CS11 CS10
  49. // Initial Value 0 0 0 0 0 0 0 0
  50. // changed to 0 0 0 0 1 0 0 1
  51. // CS12,CS11,CS10 = (0,1,1) = prescaler divide by 64
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement