Problem with cycling though PWM outputs on timer

LLofYorkshire
Posts: 3
Joined: Mon Jan 20, 2020 11:21 pm

Problem with cycling though PWM outputs on timer

Postby LLofYorkshire » Mon Jan 20, 2020 11:33 pm

I'm getting some strange behaviour with this, that maybe somebody could help with please?
The purpose of the code is to cycle sequentially though 11 different configurations, with each configuration (I call it a crank position) calling for 3 out of 11 PWM channels to be activated. A different 3 channels for each crank position. I don't have much experience of using timers, or coding in general.

The behavior I am trying for is that the crank position should cycle through positions every 50ms then when it gets to position 11 should go back to position 1, so a cycle time of 550ms. The problem is that I am not getting PWM on the last 3 channels (8,9,10), so not reaching Crank Pos 7. Even though if I put a print statement in the relevant case I can confirm that the case is being executed.

I checked things in general before introducing the timers and Crank feature by just pulsing each channel simultaneously and attaching to LEDs and that worked fine, so the hardware is all ok.
I don't have a scope unfortunately for any debug.

The code:
  1. // initiallise the timer conditions
  2. hw_timer_t * timer = NULL;
  3. hw_timer_t * timer2 = NULL;
  4. volatile bool gGetADCflag;
  5. volatile bool gCrankPosChangeflag;
  6. portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED;
  7.  
  8. // PWMs are for 11 channel individual Mosfets
  9.  
  10. // setting things up for PWM out - use first channel of 16 channels (started from zero)
  11. #define LEDC_CHANNEL_0     0
  12. #define LEDC_CHANNEL_1     1
  13. #define LEDC_CHANNEL_2     2
  14. #define LEDC_CHANNEL_3     3
  15. #define LEDC_CHANNEL_4     4
  16. #define LEDC_CHANNEL_5     5
  17. #define LEDC_CHANNEL_6     6
  18. #define LEDC_CHANNEL_7     7
  19. #define LEDC_CHANNEL_8     8
  20. #define LEDC_CHANNEL_9     9
  21. #define LEDC_CHANNEL_10    10
  22.  
  23.  
  24.  
  25. // use 13 bit precission for LEDC timer
  26. #define LEDC_TIMER_13_BIT  13
  27.  
  28. // use 5000 Hz as a LEDC base frequency
  29. #define LEDC_BASE_FREQ     25000  // max frequency for IBT-2 is 25kHz
  30.  
  31. // fade LED PIN (replace with LED_BUILTIN constant for built-in LED)
  32. #define PWM1               21
  33. #define PWM2               32 // was 1
  34. #define PWM3               4   // was 3
  35. #define PWM4               22
  36. #define PWM5               19
  37. #define PWM6               23
  38. #define PWM7               18
  39. #define PWM8               5
  40. #define PWM9               15
  41. #define PWM10              25
  42. #define PWM11              33
  43.  
  44. unsigned long durationNow = 0;
  45. unsigned long previous = 0;
  46.  
  47. int PWMVal = 120;    // 0-255
  48. int analog_value = 0;   // 0-4095
  49.  
  50. int outputValue = 0;        // value output to the PWM (analog out)
  51. int rateOfChange = 0;   // Capture the rate of change of the input at each time step (in theory could be a deimal but if <1 then 0 is fine)
  52. int rateLimit = 50;         // ADC increments
  53. int oldDemand = 0;
  54. int newDemand = 0;
  55. int mAmp = 0;
  56.  
  57.  
  58.  
  59. String gcommand;
  60. int gDuty=0;
  61. int gCrankPos = 1; //  initialisation of the crank position (1-11 variable)
  62.  
  63. // timer related functions
  64. void ADCTimer(){
  65.   gGetADCflag = true;
  66. }
  67.  
  68. void startADCTimer() {
  69.   timer = timerBegin(0, 80, true); // timer_id = 0; divider=80; countUp = true;
  70.   timerAttachInterrupt(timer, &ADCTimer, true); // edge = true
  71.   timerAlarmWrite(timer, 1000, true);  //1 ms
  72.   timerAlarmEnable(timer);
  73. }
  74.  
  75. void endADCTimer() {
  76.   timerEnd(timer);
  77.   timer = NULL;
  78. }
  79.  
  80. void crankTimer(){
  81.   gCrankPosChangeflag = true;
  82. }
  83.  
  84. void startCrankTimer() {
  85.   timer2 = timerBegin(1, 80, true); // timer_id = 1; divider=80; countUp = true;
  86.   timerAttachInterrupt(timer2, &crankTimer, true); // edge = true
  87.   timerAlarmWrite(timer2, 50000, true);  //50 ms (0.5s)
  88.   timerAlarmEnable(timer2);
  89. }
  90.  
  91. void endCrankTimer() {
  92.   timerEnd(timer2);
  93.   timer2 = NULL;
  94. }
  95.  
  96. void incCrankPos(){
  97.   // anything in here that involves shifting around the 11 channel postions
  98.   Serial.print("Crank Pos:  ");
  99.   Serial.println(gCrankPos);
  100.   if (gCrankPos >= 11) gCrankPos = 0;
  101.   gCrankPos++; //
  102.   gCrankPosChangeflag = false;
  103. }
  104.  
  105. /*
  106. // the function that get's Called once the Timer Interrupt flag is True
  107. void getADC() {
  108.   // do suff in here that runs outside of the ISR but is triggered by the ISR
  109.   // the timer should be set so that it triggers 10x faster than the data tare you want to see, as it will be averaged then thrown away every 10 cycles
  110. // Note that this simplified approach will introduce a phase shift or delay to the figures, as the average figure is being reported against the final sample timestamp.  a rolling average should be used if needs to be avoided
  111.  
  112.   // any variables that need to not be reset each time the function is called need to be declared as static
  113.   static unsigned int counter = 1;
  114.   static int rawInput[NOOFAVERAGES] = {0}; // array for storing the 10 values
  115.   static int aveADC = 0; // variable for storing the potentiometer value
  116.   static int sumInput = 0; // variable to hold the sum of inputs prior to averaging
  117.   static int current1 = 0; // convert ADC counts to current mA, with offset and multiplier
  118.   static int offset1 = -210; // ADC offset for 1st ACS725 sensor
  119.  
  120.  
  121.   rawInput[counter-1] = analogRead(ANALOG_PIN_1);
  122.   sumInput = sumInput + rawInput[counter-1];
  123. // take the NOOFAVERAGES readings from the Array and average them
  124.   if (counter == NOOFAVERAGES){
  125.    
  126.     aveADC = sumInput/NOOFAVERAGES; // add up and divide by 10
  127.     current1 = (aveADC+offset1)*1000/300;  // Convert ADC to mA, 264mV/A - chaged to 300mV/A
  128.    
  129.     Serial.print("ADCTimer ");
  130.     Serial.print(counter);
  131.     Serial.print(" at ");
  132.     Serial.print(millis());
  133.     Serial.print(" ms ");
  134.     Serial.print(" ADC value ");
  135.     Serial.print(aveADC);
  136.     Serial.print(" Current(mA) ");
  137.     Serial.println(current1);
  138.     sumInput = 0;
  139.     counter = 0;
  140.    }
  141.  
  142.   gGetADCflag = false;
  143.   counter++;
  144.  
  145. }
  146. */
  147.  
  148. // Arduino like analogWrite
  149. // value has to be between 0 and valueMax
  150.  
  151. void ledcAnalogWrite(uint8_t channel, uint32_t value, uint32_t valueMax = 255) {
  152.   // calculate duty, 8191 from 2 ^ 13 - 1
  153.   uint32_t duty = (8191 / valueMax) * min(value, valueMax);
  154.  
  155.   // write duty to LEDC
  156.   ledcWrite(channel, duty);
  157. }
  158.  
  159. void setup() {
  160.   delay(1000);
  161.   Serial.begin(115200);
  162.   delay(1000);
  163. //  SerialBT.begin("ESP32hula"); //Bluetooth device name
  164.   Serial.println("The device started, now you can pair it with bluetooth!");
  165.   // Setup timer and attach timer to a pwm pin
  166.   ledcSetup(LEDC_CHANNEL_0, LEDC_BASE_FREQ, LEDC_TIMER_13_BIT);
  167.   ledcSetup(LEDC_CHANNEL_1, LEDC_BASE_FREQ, LEDC_TIMER_13_BIT);
  168.   ledcSetup(LEDC_CHANNEL_2, LEDC_BASE_FREQ, LEDC_TIMER_13_BIT);
  169.   ledcSetup(LEDC_CHANNEL_3, LEDC_BASE_FREQ, LEDC_TIMER_13_BIT);
  170.   ledcSetup(LEDC_CHANNEL_4, LEDC_BASE_FREQ, LEDC_TIMER_13_BIT);
  171.   ledcSetup(LEDC_CHANNEL_5, LEDC_BASE_FREQ, LEDC_TIMER_13_BIT);
  172.   ledcSetup(LEDC_CHANNEL_6, LEDC_BASE_FREQ, LEDC_TIMER_13_BIT);
  173.   ledcSetup(LEDC_CHANNEL_7, LEDC_BASE_FREQ, LEDC_TIMER_13_BIT);
  174.   ledcSetup(LEDC_CHANNEL_8, LEDC_BASE_FREQ, LEDC_TIMER_13_BIT);
  175.   ledcSetup(LEDC_CHANNEL_9, LEDC_BASE_FREQ, LEDC_TIMER_13_BIT);
  176.   ledcSetup(LEDC_CHANNEL_10, LEDC_BASE_FREQ, LEDC_TIMER_13_BIT);
  177.   ledcAttachPin(PWM1, LEDC_CHANNEL_0);
  178.   ledcAttachPin(PWM2, LEDC_CHANNEL_1);
  179.   ledcAttachPin(PWM3, LEDC_CHANNEL_2);
  180.   ledcAttachPin(PWM4, LEDC_CHANNEL_3);
  181.   ledcAttachPin(PWM5, LEDC_CHANNEL_4);
  182.   ledcAttachPin(PWM6, LEDC_CHANNEL_5);
  183.   ledcAttachPin(PWM7, LEDC_CHANNEL_6);
  184.   ledcAttachPin(PWM8, LEDC_CHANNEL_7);
  185.   ledcAttachPin(PWM9, LEDC_CHANNEL_8);
  186.   ledcAttachPin(PWM10, LEDC_CHANNEL_9);
  187.   ledcAttachPin(PWM11, LEDC_CHANNEL_10);
  188. //  pinMode(ANALOG_PIN_0, INPUT);
  189. //  pinMode(ANALOG_PIN_1, INPUT);
  190. //  analogSetPinAttenuation(ANALOG_PIN_1, ADC_11db);  // for ESP32 max vooltages are 0db = 1.1v, 2.5db = 1.4v, 6db = 1.9v, 11db = 3.2v, representing 4095 ADC (12bit)
  191.  
  192.   Serial.println("pin modes set");
  193.   startADCTimer();
  194.   startCrankTimer();
  195.   delay(1000);
  196.   Serial.println("exiting setup");
  197. }
  198.  
  199. /*
  200. void parseCommand (String com) {
  201.   String part1;
  202.   String part2;
  203.  
  204.   // split into 2 parts.  1st part will be a letter, either an override status or a motor speed change.  2nd part will be either override on/off or motor speed 0-100.
  205.   part1 = com.substring(0,1);
  206.   part2 = com.substring(1,com.indexOf("."));
  207. //  Serial.print("part1= ");
  208. //  Serial.println(part1);
  209. //  Serial.print("part2= ");
  210. //  Serial.println(part2);
  211.   if (part1.equalsIgnoreCase("o")){
  212.     gOverrideStatus = part2.toInt();  // change the global override status to either 1 or 0;
  213. //    Serial.print("gOverride status changed to ");
  214. //    Serial.println(gOverrideStatus);
  215.   }
  216.   else if(part1.equalsIgnoreCase("v")){
  217.     if (gOverrideStatus == 1){
  218.       gDuty = part2.toInt();
  219. //      Serial.print("duty (%) changed to ");
  220. //      Serial.println(gDuty);
  221.     }
  222.   }
  223.   else{
  224.     part1="";
  225.     part2="";
  226.     gcommand="";
  227.   }
  228.  
  229. }
  230. */
  231. /*
  232. int rateLimiter(int latestValue){
  233.     durationNow = millis();
  234.   // check the input value:
  235.   int sensorValue = latestValue;
  236.   newDemand = sensorValue;
  237.   rateOfChange = (newDemand-oldDemand)/(durationNow-previous);
  238.  
  239.   if (rateOfChange > rateLimit)
  240.   {
  241.     if (newDemand>oldDemand)
  242.     {
  243.       newDemand = oldDemand+rateLimit;
  244.     }
  245.     if (newDemand<=oldDemand)
  246.     {
  247.       newDemand = oldDemand-rateLimit;
  248.     }
  249.   }
  250.  
  251.   else
  252.   {
  253.     newDemand = newDemand;
  254.   }
  255.   //reset for next loop
  256.   oldDemand = newDemand;
  257.   previous = durationNow;
  258.   return newDemand;
  259. }
  260. */
  261.  
  262. void loop() {
  263.  
  264.  
  265.  
  266.   // Reading ADC value (current sense input)
  267.   if (gGetADCflag == true) {
  268.   //  Serial.println("ADCFlag = TRUE");
  269.   //  getADC();
  270.   }
  271.  
  272.   if (gCrankPosChangeflag == true) {
  273.     incCrankPos();
  274.   }
  275.  
  276.  
  277. // check motor speed is within acceptable boundaries and limit
  278.   if (PWMVal <= 0){
  279.     PWMVal = 0;
  280.   }
  281.   if (PWMVal >120){
  282.     PWMVal = 120;  // set to be approx 2A max (true maximum 255, but would result in peak current draw)
  283.   }
  284.    
  285.   // call PWM funtions for each coil
  286.   // use switch cases depeding on crank position
  287.   switch (gCrankPos) {
  288.   case 1:
  289.     ledcAnalogWrite(LEDC_CHANNEL_0, PWMVal);
  290.     ledcAnalogWrite(LEDC_CHANNEL_1, PWMVal);
  291.     ledcAnalogWrite(LEDC_CHANNEL_2, PWMVal);
  292.     ledcAnalogWrite(LEDC_CHANNEL_3, 0);
  293.     ledcAnalogWrite(LEDC_CHANNEL_4, 0);
  294.     ledcAnalogWrite(LEDC_CHANNEL_5, 0);
  295.     ledcAnalogWrite(LEDC_CHANNEL_6, 0);
  296.     ledcAnalogWrite(LEDC_CHANNEL_7, 0);
  297.     ledcAnalogWrite(LEDC_CHANNEL_8, 0);
  298.     ledcAnalogWrite(LEDC_CHANNEL_9, 0);
  299.     ledcAnalogWrite(LEDC_CHANNEL_10, 0);
  300.     break;
  301.   case 2:
  302.     ledcAnalogWrite(LEDC_CHANNEL_0, 0);
  303.     ledcAnalogWrite(LEDC_CHANNEL_1, PWMVal);
  304.     ledcAnalogWrite(LEDC_CHANNEL_2, PWMVal);
  305.     ledcAnalogWrite(LEDC_CHANNEL_3, PWMVal);
  306.     ledcAnalogWrite(LEDC_CHANNEL_4, 0);
  307.     ledcAnalogWrite(LEDC_CHANNEL_5, 0);
  308.     ledcAnalogWrite(LEDC_CHANNEL_6, 0);
  309.     ledcAnalogWrite(LEDC_CHANNEL_7, 0);
  310.     ledcAnalogWrite(LEDC_CHANNEL_8, 0);
  311.     ledcAnalogWrite(LEDC_CHANNEL_9, 0);
  312.     ledcAnalogWrite(LEDC_CHANNEL_10, 0);
  313.     break;
  314.   case 3:
  315.     ledcAnalogWrite(LEDC_CHANNEL_0, 0);
  316.     ledcAnalogWrite(LEDC_CHANNEL_1, 0);
  317.     ledcAnalogWrite(LEDC_CHANNEL_2, PWMVal);
  318.     ledcAnalogWrite(LEDC_CHANNEL_3, PWMVal);
  319.     ledcAnalogWrite(LEDC_CHANNEL_4, PWMVal);
  320.     ledcAnalogWrite(LEDC_CHANNEL_5, 0);
  321.     ledcAnalogWrite(LEDC_CHANNEL_6, 0);
  322.     ledcAnalogWrite(LEDC_CHANNEL_7, 0);
  323.     ledcAnalogWrite(LEDC_CHANNEL_8, 0);
  324.     ledcAnalogWrite(LEDC_CHANNEL_9, 0);
  325.     ledcAnalogWrite(LEDC_CHANNEL_10, 0);
  326.     break;
  327.   case 4:
  328.     ledcAnalogWrite(LEDC_CHANNEL_0, 0);
  329.     ledcAnalogWrite(LEDC_CHANNEL_1, 0);
  330.     ledcAnalogWrite(LEDC_CHANNEL_2, 0);
  331.     ledcAnalogWrite(LEDC_CHANNEL_3, PWMVal);
  332.     ledcAnalogWrite(LEDC_CHANNEL_4, PWMVal);
  333.     ledcAnalogWrite(LEDC_CHANNEL_5, PWMVal);
  334.     ledcAnalogWrite(LEDC_CHANNEL_6, 0);
  335.     ledcAnalogWrite(LEDC_CHANNEL_7, 0);
  336.     ledcAnalogWrite(LEDC_CHANNEL_8, 0);
  337.     ledcAnalogWrite(LEDC_CHANNEL_9, 0);
  338.     ledcAnalogWrite(LEDC_CHANNEL_10, 0);
  339.     break;
  340.   case 5:
  341.     ledcAnalogWrite(LEDC_CHANNEL_0, 0);
  342.     ledcAnalogWrite(LEDC_CHANNEL_1, 0);
  343.     ledcAnalogWrite(LEDC_CHANNEL_2, 0);
  344.     ledcAnalogWrite(LEDC_CHANNEL_3, 0);
  345.     ledcAnalogWrite(LEDC_CHANNEL_4, PWMVal);
  346.     ledcAnalogWrite(LEDC_CHANNEL_5, PWMVal);
  347.     ledcAnalogWrite(LEDC_CHANNEL_6, PWMVal);
  348.     ledcAnalogWrite(LEDC_CHANNEL_7, 0);
  349.     ledcAnalogWrite(LEDC_CHANNEL_8, 0);
  350.     ledcAnalogWrite(LEDC_CHANNEL_9, 0);
  351.     ledcAnalogWrite(LEDC_CHANNEL_10, 0);
  352.     break;
  353.   case 6:
  354.     ledcAnalogWrite(LEDC_CHANNEL_0, 0);
  355.     ledcAnalogWrite(LEDC_CHANNEL_1, 0);
  356.     ledcAnalogWrite(LEDC_CHANNEL_2, 0);
  357.     ledcAnalogWrite(LEDC_CHANNEL_3, 0);
  358.     ledcAnalogWrite(LEDC_CHANNEL_4, 0);
  359.     ledcAnalogWrite(LEDC_CHANNEL_5, PWMVal);
  360.     ledcAnalogWrite(LEDC_CHANNEL_6, PWMVal);
  361.     ledcAnalogWrite(LEDC_CHANNEL_7, PWMVal);
  362.     ledcAnalogWrite(LEDC_CHANNEL_8, 0);
  363.     ledcAnalogWrite(LEDC_CHANNEL_9, 0);
  364.     ledcAnalogWrite(LEDC_CHANNEL_10, 0);
  365.     break;
  366.   case 7:
  367.     ledcAnalogWrite(LEDC_CHANNEL_0, 0);
  368.     ledcAnalogWrite(LEDC_CHANNEL_1, 0);
  369.     ledcAnalogWrite(LEDC_CHANNEL_2, 0);
  370.     ledcAnalogWrite(LEDC_CHANNEL_3, 0);
  371.     ledcAnalogWrite(LEDC_CHANNEL_4, 0);
  372.     ledcAnalogWrite(LEDC_CHANNEL_5, 0);
  373.     ledcAnalogWrite(LEDC_CHANNEL_6, PWMVal);
  374.     ledcAnalogWrite(LEDC_CHANNEL_7, PWMVal);
  375.     ledcAnalogWrite(LEDC_CHANNEL_8, PWMVal);
  376.     ledcAnalogWrite(LEDC_CHANNEL_9, 0);
  377.     ledcAnalogWrite(LEDC_CHANNEL_10, 0);
  378.     break;
  379.   case 8:
  380.     ledcAnalogWrite(LEDC_CHANNEL_0, 0);
  381.     ledcAnalogWrite(LEDC_CHANNEL_1, 0);
  382.     ledcAnalogWrite(LEDC_CHANNEL_2, 0);
  383.     ledcAnalogWrite(LEDC_CHANNEL_3, 0);
  384.     ledcAnalogWrite(LEDC_CHANNEL_4, 0);
  385.     ledcAnalogWrite(LEDC_CHANNEL_5, 0);
  386.     ledcAnalogWrite(LEDC_CHANNEL_6, 0);
  387.     ledcAnalogWrite(LEDC_CHANNEL_7, PWMVal);
  388.     ledcAnalogWrite(LEDC_CHANNEL_8, PWMVal);
  389.     ledcAnalogWrite(LEDC_CHANNEL_9, PWMVal);
  390.     ledcAnalogWrite(LEDC_CHANNEL_10, 0);
  391.     break;
  392.   case 9:
  393.     ledcAnalogWrite(LEDC_CHANNEL_0, 0);
  394.     ledcAnalogWrite(LEDC_CHANNEL_1, 0);
  395.     ledcAnalogWrite(LEDC_CHANNEL_2, 0);
  396.     ledcAnalogWrite(LEDC_CHANNEL_3, 0);
  397.     ledcAnalogWrite(LEDC_CHANNEL_4, 0);
  398.     ledcAnalogWrite(LEDC_CHANNEL_5, 0);
  399.     ledcAnalogWrite(LEDC_CHANNEL_6, 0);
  400.     ledcAnalogWrite(LEDC_CHANNEL_7, 0);
  401.     ledcAnalogWrite(LEDC_CHANNEL_8, PWMVal);
  402.     ledcAnalogWrite(LEDC_CHANNEL_9, PWMVal);
  403.     ledcAnalogWrite(LEDC_CHANNEL_10, PWMVal);
  404.     break;
  405.   case 10:
  406.     ledcAnalogWrite(LEDC_CHANNEL_0, PWMVal);
  407.     ledcAnalogWrite(LEDC_CHANNEL_1, 0);
  408.     ledcAnalogWrite(LEDC_CHANNEL_2, 0);
  409.     ledcAnalogWrite(LEDC_CHANNEL_3, 0);
  410.     ledcAnalogWrite(LEDC_CHANNEL_4, 0);
  411.     ledcAnalogWrite(LEDC_CHANNEL_5, 0);
  412.     ledcAnalogWrite(LEDC_CHANNEL_6, 0);
  413.     ledcAnalogWrite(LEDC_CHANNEL_7, 0);
  414.     ledcAnalogWrite(LEDC_CHANNEL_8, 0);
  415.     ledcAnalogWrite(LEDC_CHANNEL_9, PWMVal);
  416.     ledcAnalogWrite(LEDC_CHANNEL_10, PWMVal);
  417.     break;
  418.   case 11:
  419.     ledcAnalogWrite(LEDC_CHANNEL_0, PWMVal);
  420.     ledcAnalogWrite(LEDC_CHANNEL_1, PWMVal);
  421.     ledcAnalogWrite(LEDC_CHANNEL_2, 0);
  422.     ledcAnalogWrite(LEDC_CHANNEL_3, 0);
  423.     ledcAnalogWrite(LEDC_CHANNEL_4, 0);
  424.     ledcAnalogWrite(LEDC_CHANNEL_5, 0);
  425.     ledcAnalogWrite(LEDC_CHANNEL_6, 0);
  426.     ledcAnalogWrite(LEDC_CHANNEL_7, 0);
  427.     ledcAnalogWrite(LEDC_CHANNEL_8, 0);
  428.     ledcAnalogWrite(LEDC_CHANNEL_9, 0);
  429.     ledcAnalogWrite(LEDC_CHANNEL_10, PWMVal);
  430.     break;
  431.  
  432.   default:
  433.     // if nothing else matches, do the default
  434.     ledcAnalogWrite(LEDC_CHANNEL_0, 0);
  435.     ledcAnalogWrite(LEDC_CHANNEL_1, 0);
  436.     ledcAnalogWrite(LEDC_CHANNEL_2, 0);
  437.     ledcAnalogWrite(LEDC_CHANNEL_3, 0);
  438.     ledcAnalogWrite(LEDC_CHANNEL_4, 0);
  439.     ledcAnalogWrite(LEDC_CHANNEL_5, 0);
  440.     ledcAnalogWrite(LEDC_CHANNEL_6, 0);
  441.     ledcAnalogWrite(LEDC_CHANNEL_7, 0);
  442.     ledcAnalogWrite(LEDC_CHANNEL_8, 0);
  443.     ledcAnalogWrite(LEDC_CHANNEL_9, 0);
  444.     ledcAnalogWrite(LEDC_CHANNEL_10, 0);
  445.     break;
  446.   }
  447. }
Last edited by LLofYorkshire on Tue Jan 21, 2020 1:54 pm, edited 1 time in total.

LLofYorkshire
Posts: 3
Joined: Mon Jan 20, 2020 11:21 pm

Re: Problem with cycling though PWM outputs on timer

Postby LLofYorkshire » Tue Jan 21, 2020 12:38 pm

The last 3 PWM channels are at a steady 1.6V.
PWM9 pin 15
PWM10 pin 25
PWM11 pin 33

If I swa p pin configs around then the problem stays with the PWM channel rather than the pin

LLofYorkshire
Posts: 3
Joined: Mon Jan 20, 2020 11:21 pm

Re: Problem with cycling though PWM outputs on timer

Postby LLofYorkshire » Mon Jan 27, 2020 2:51 pm

A follow up to close this.

I still don't know exactly what was causing this issue, but I suspect it is something to do with the Arduino wrapper and the implementation of the 8 "Low Speed" PWM channels, as I was only getting problems on those channels and only when I was also using a separate timer interrupt to change the PWM duty.
I got around the problem by using some IDF code to control the PWMs more closely. The code is more unwieldy but I get the result I need. Foe reference:
  1. #include "driver/ledc.h"
  2.  
  3. //https://github.com/espressif/arduino-esp32/blob/master/cores/esp32/esp32-hal-ledc.c#L240
  4. /*
  5.  * LEDC Chan to Group/Channel/Timer Mapping
  6. ** ledc: 0  => Group: 0, Channel: 0, Timer: 0
  7. ** ledc: 1  => Group: 0, Channel: 1, Timer: 0
  8. ** ledc: 2  => Group: 0, Channel: 2, Timer: 1
  9. ** ledc: 3  => Group: 0, Channel: 3, Timer: 1
  10. ** ledc: 4  => Group: 0, Channel: 4, Timer: 2
  11. ** ledc: 5  => Group: 0, Channel: 5, Timer: 2
  12. ** ledc: 6  => Group: 0, Channel: 6, Timer: 3
  13. ** ledc: 7  => Group: 0, Channel: 7, Timer: 3
  14. ** ledc: 8  => Group: 1, Channel: 0, Timer: 0
  15. ** ledc: 9  => Group: 1, Channel: 1, Timer: 0
  16. ** ledc: 10 => Group: 1, Channel: 2, Timer: 1
  17. ** ledc: 11 => Group: 1, Channel: 3, Timer: 1
  18. ** ledc: 12 => Group: 1, Channel: 4, Timer: 2
  19. ** ledc: 13 => Group: 1, Channel: 5, Timer: 2
  20. ** ledc: 14 => Group: 1, Channel: 6, Timer: 3
  21. ** ledc: 15 => Group: 1, Channel: 7, Timer: 3
  22. */
  23.  
  24. #define PWM1               21
  25. #define PWM2               32 // was 1
  26. #define PWM3               4  // was 3
  27. #define PWM4               22
  28. #define PWM5               19
  29. #define PWM6               23
  30. #define PWM7               18
  31. #define PWM8               5
  32. #define PWM9               15
  33. #define PWM10              25
  34. #define PWM11              33
  35.  
  36. // initiallise the timer conditions
  37. hw_timer_t * timer2 = NULL;
  38.  
  39. volatile bool gCrankPosChangeflag;
  40. portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED;
  41. int gCrankPos = 1; //  initialisation of the crank position (1-11 variable)
  42.  
  43.  
  44.  
  45. int brightness = 4;    // how bright the LED is
  46. int fadeAmount = 5;    // how many points to fade the LED by
  47.  
  48. // Arduino like analogWrite
  49. // value has to be between 0 and valueMax
  50. void ledcAnalogWrite(uint8_t channel, uint32_t value, uint32_t valueMax = 255) {
  51.   // calculate duty, 8191 from 2 ^ 13 - 1
  52.   uint32_t duty = (8191 / valueMax) * min(value, valueMax);
  53.  
  54.   // write duty to LEDC
  55.   ledcWrite(channel, duty);
  56. }
  57.  
  58. void crankTimer(){
  59.   gCrankPosChangeflag = true;
  60. }
  61.  
  62. void startCrankTimer() {
  63.   timer2 = timerBegin(1, 80, true); // timer_id = 1; divider=80; countUp = true;
  64.   timerAttachInterrupt(timer2, &crankTimer, true); // edge = true
  65.   timerAlarmWrite(timer2, 1000000, true);  //500 ms (0.5s)
  66.   timerAlarmEnable(timer2);
  67. }
  68.  
  69. void endCrankTimer() {
  70.   timerEnd(timer2);
  71.   timer2 = NULL;
  72. }
  73.  
  74. void incCrankPos(){
  75.   // anything in here that involves shifting around the 11 channel postions
  76.  
  77.   if (gCrankPos > 11) gCrankPos = 1;
  78.   Serial.print("Crank Pos:  ");
  79.   Serial.println(gCrankPos);
  80.      
  81.   // call PWM funtions for each coil
  82.   // use switch cases depeding on crank position
  83.   if (gCrankPos==1) {
  84.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0, 4) );
  85.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0) );
  86.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_1, 4) );
  87.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_1) );
  88.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_2, 4) );
  89.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_2) );
  90.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_3, 0) );
  91.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_3) );
  92.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_4, 0) );
  93.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_4) );
  94.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_5, 0) );
  95.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_5) );
  96.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_6, 0) );
  97.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_6) );
  98.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_7, 0) );
  99.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_7) );
  100.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0, 0) );
  101.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0) );
  102.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_1, 0) );
  103.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_1) );
  104.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_2, 0) );
  105.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_2) );
  106.     Serial.println("in Case1");
  107.   }
  108.   else if (gCrankPos==2) {
  109.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0, 0) );
  110.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0) );
  111.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_1, 4) );
  112.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_1) );
  113.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_2, 4) );
  114.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_2) );
  115.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_3, 4) );
  116.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_3) );
  117.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_4, 0) );
  118.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_4) );
  119.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_5, 0) );
  120.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_5) );
  121.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_6, 0) );
  122.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_6) );
  123.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_7, 0) );
  124.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_7) );
  125.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0, 0) );
  126.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0) );
  127.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_1, 0) );
  128.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_1) );
  129.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_2, 0) );
  130.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_2) );    
  131.     Serial.println("in Case2");
  132.   }
  133.   else if (gCrankPos==3) {
  134.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0, 0) );
  135.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0) );
  136.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_1, 0) );
  137.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_1) );
  138.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_2, 4) );
  139.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_2) );
  140.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_3, 4) );
  141.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_3) );
  142.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_4, 4) );
  143.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_4) );
  144.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_5, 0) );
  145.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_5) );
  146.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_6, 0) );
  147.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_6) );
  148.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_7, 0) );
  149.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_7) );
  150.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0, 0) );
  151.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0) );
  152.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_1, 0) );
  153.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_1) );
  154.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_2, 0) );
  155.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_2) );    
  156.     Serial.println("in Case3");
  157.   }
  158.   else if (gCrankPos==4) {
  159.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0, 0) );
  160.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0) );
  161.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_1, 0) );
  162.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_1) );
  163.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_2, 0) );
  164.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_2) );
  165.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_3, 4) );
  166.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_3) );
  167.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_4, 4) );
  168.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_4) );
  169.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_5, 4) );
  170.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_5) );
  171.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_6, 0) );
  172.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_6) );
  173.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_7, 0) );
  174.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_7) );
  175.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0, 0) );
  176.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0) );
  177.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_1, 0) );
  178.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_1) );
  179.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_2, 0) );
  180.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_2) );    
  181.     Serial.println("in Case4");
  182.   }
  183.   else if (gCrankPos==5) {
  184.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0, 0) );
  185.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0) );
  186.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_1, 0) );
  187.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_1) );
  188.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_2, 0) );
  189.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_2) );
  190.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_3, 0) );
  191.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_3) );
  192.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_4, 4) );
  193.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_4) );
  194.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_5, 4) );
  195.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_5) );
  196.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_6, 4) );
  197.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_6) );
  198.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_7, 0) );
  199.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_7) );
  200.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0, 0) );
  201.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0) );
  202.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_1, 0) );
  203.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_1) );
  204.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_2, 0) );
  205.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_2) );        
  206.     Serial.println("in Case5");
  207.   }
  208.   else if (gCrankPos==6) {
  209.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0, 0) );      
  210.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0) );
  211.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_1, 0) );
  212.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_1) );
  213.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_2, 0) );
  214.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_2) );
  215.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_3, 0) );
  216.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_3) );
  217.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_4, 0) );
  218.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_4) );
  219.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_5, 4) );
  220.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_5) );
  221.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_6, 4) );
  222.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_6) );
  223.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_7, 4) );
  224.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_7) );
  225.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0, 0) );
  226.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0) );
  227.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_1, 0) );
  228.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_1) );
  229.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_2, 0) );
  230.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_2) );    
  231.     Serial.println("in Case6");
  232.   }
  233.   else if (gCrankPos==7) {
  234.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0, 0) );      
  235.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0) );
  236.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_1, 0) );
  237.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_1) );
  238.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_2, 0) );
  239.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_2) );
  240.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_3, 0) );
  241.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_3) );
  242.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_4, 0) );
  243.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_4) );
  244.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_5, 0) );
  245.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_5) );
  246.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_6, 4) );
  247.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_6) );
  248.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_7, 4) );
  249.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_7) );
  250.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0, 4) );
  251.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0) );
  252.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_1, 0) );
  253.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_1) );
  254.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_2, 0) );
  255.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_2) );        
  256.     Serial.println("in Case7");
  257.   }
  258.   else if (gCrankPos==8) {
  259.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0, 0) );      
  260.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0) );
  261.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_1, 0) );
  262.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_1) );
  263.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_2, 0) );
  264.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_2) );
  265.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_3, 0) );
  266.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_3) );
  267.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_4, 0) );
  268.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_4) );
  269.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_5, 0) );
  270.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_5) );
  271.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_6, 0) );
  272.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_6) );
  273.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_7, 4) );
  274.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_7) );
  275.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0, 4) );
  276.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0) );
  277.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_1, 4) );
  278.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_1) );
  279.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_2, 0) );
  280.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_2) );          
  281.     Serial.println("in Case8");
  282.   }
  283.   else if (gCrankPos==9) {
  284.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0, 0) );
  285.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0) );
  286.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_1, 0) );
  287.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_1) );
  288.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_2, 0) );
  289.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_2) );
  290.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_3, 0) );
  291.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_3) );
  292.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_4, 0) );
  293.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_4) );
  294.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_5, 0) );
  295.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_5) );
  296.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_6, 0) );
  297.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_6) );
  298.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_7, 0) );
  299.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_7) );
  300.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0, 4) );
  301.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0) );
  302.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_1, 4) );
  303.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_1) );
  304.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_2, 4) );
  305.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_2) );        
  306.     Serial.println("in Case9");
  307.   }
  308.   else if (gCrankPos==10) {
  309.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0, 4) );
  310.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0) );
  311.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_1, 0) );
  312.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_1) );
  313.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_2, 0) );
  314.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_2) );
  315.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_3, 0) );
  316.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_3) );
  317.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_4, 0) );
  318.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_4) );
  319.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_5, 0) );
  320.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_5) );
  321.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_6, 0) );
  322.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_6) );
  323.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_7, 0) );
  324.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_7) );
  325.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0, 0) );
  326.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0) );
  327.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_1, 4) );
  328.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_1) );
  329.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_2, 4) );
  330.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_2) );
  331.                      
  332.     Serial.println("in Case10");
  333.   }
  334.   else if (gCrankPos==11) {
  335.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0, 4) );      
  336.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0) );
  337.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_1, 4) );
  338.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_1) );
  339.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_2, 4) );
  340.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_2) );
  341.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_3, 0) );
  342.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_3) );
  343.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_4, 0) );
  344.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_4) );
  345.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_5, 0) );
  346.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_5) );
  347.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_6, 0) );
  348.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_6) );
  349.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_7, 0) );
  350.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_7) );
  351.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0, 0) );
  352.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0) );
  353.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_1, 0) );
  354.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_1) );
  355.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_2, 4) );
  356.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_2) );    
  357.     Serial.println("in Case11");
  358.   }
  359.   else {
  360.    
  361.     // if nothing else matches, do the default
  362.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0) );
  363.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_1, 0) );
  364.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_1) );
  365.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_2, 0) );
  366.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_2) );
  367.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_3, 0) );
  368.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_3) );
  369.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_4, 0) );
  370.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_4) );
  371.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_5, 0) );
  372.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_5) );
  373.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_6, 0) );
  374.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_6) );
  375.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_7, 0) );
  376.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_7) );
  377.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0, 0) );
  378.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0) );
  379.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_1, 0) );
  380.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_1) );
  381.       ESP_ERROR_CHECK( ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_2, 0) );
  382.       ESP_ERROR_CHECK( ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_2) );        
  383.     Serial.println("in Case default");
  384.   }
  385.  
  386.  
  387.  
  388.   gCrankPos++; //
  389.   gCrankPosChangeflag = false;
  390. }
  391.  
  392. void setup() {
  393.   Serial.begin(115200);
  394.  
  395.   // High Speed LEDTimer0 setup
  396.   ledc_timer_config_t ledc_timer0;
  397.   ledc_timer0.speed_mode = LEDC_HIGH_SPEED_MODE;           // timer mode
  398.   ledc_timer0.duty_resolution = LEDC_TIMER_3_BIT; // resolution of PWM duty
  399.   ledc_timer0.timer_num = LEDC_TIMER_0;            // timer index
  400.   ledc_timer0.freq_hz = 20000;                      // frequency of PWM signal
  401.   // Set configuration of timer0 for high speed channels
  402.   esp_err_t result = ledc_timer_config(&ledc_timer0);
  403.   if (result == ESP_OK)
  404.      Serial.printf("timer0 frequency: %d", ledc_get_freq(LEDC_HIGH_SPEED_MODE, LEDC_TIMER_0));
  405.  
  406.  
  407. // High Speed LEDTimer1 setup
  408.   ledc_timer_config_t ledc_timer1;
  409.   ledc_timer1.speed_mode = LEDC_HIGH_SPEED_MODE;           // timer mode
  410.   ledc_timer1.duty_resolution = LEDC_TIMER_3_BIT; // resolution of PWM duty
  411.   ledc_timer1.timer_num = LEDC_TIMER_1;            // timer index
  412.   ledc_timer1.freq_hz = 20000;                      // frequency of PWM signal
  413.   // Set configuration of timer2 for high speed channels
  414.   esp_err_t result1 = ledc_timer_config(&ledc_timer1);
  415.   if (result1 == ESP_OK)
  416.      Serial.printf("timer2 frequency: %d", ledc_get_freq(LEDC_HIGH_SPEED_MODE, LEDC_TIMER_1));
  417.  
  418. // High Speed LEDTimer2 setup
  419.   ledc_timer_config_t ledc_timer2;
  420.   ledc_timer2.speed_mode = LEDC_HIGH_SPEED_MODE;           // timer mode
  421.   ledc_timer2.duty_resolution = LEDC_TIMER_3_BIT; // resolution of PWM duty
  422.   ledc_timer2.timer_num = LEDC_TIMER_2;            // timer index
  423.   ledc_timer2.freq_hz = 20000;                      // frequency of PWM signal
  424.   // Set configuration of timer3 for high speed channels
  425.   esp_err_t result2 = ledc_timer_config(&ledc_timer2);
  426.   if (result2 == ESP_OK)
  427.      Serial.printf("timer3 frequency: %d", ledc_get_freq(LEDC_HIGH_SPEED_MODE, LEDC_TIMER_2));
  428.  
  429.  
  430. // High Speed LEDTimer3 setup
  431.   ledc_timer_config_t ledc_timer3;
  432.   ledc_timer3.speed_mode = LEDC_HIGH_SPEED_MODE;           // timer mode
  433.   ledc_timer3.duty_resolution = LEDC_TIMER_3_BIT; // resolution of PWM duty
  434.   ledc_timer3.timer_num = LEDC_TIMER_3;            // timer index
  435.   ledc_timer3.freq_hz = 20000;                      // frequency of PWM signal
  436.   // Set configuration of timer1 for high speed channels
  437.   esp_err_t result3 = ledc_timer_config(&ledc_timer3);
  438.   if (result3 == ESP_OK)
  439.      Serial.printf("timer4 frequency: %d", ledc_get_freq(LEDC_HIGH_SPEED_MODE, LEDC_TIMER_3));
  440.  
  441.   Serial.println("Low Speed LEDC Channel setup:");
  442.  
  443.  
  444.  
  445.  // Low Speed LEDTimer4 setup
  446.   ledc_timer_config_t ledc_timer4;
  447.   ledc_timer4.speed_mode = LEDC_LOW_SPEED_MODE;           // timer mode
  448.   ledc_timer4.duty_resolution = LEDC_TIMER_3_BIT; // resolution of PWM duty
  449.   ledc_timer4.timer_num = LEDC_TIMER_0;            // timer index
  450.   ledc_timer4.freq_hz = 20000;                      // frequency of PWM signal
  451.   // Set configuration of timer1 for high speed channels
  452.   esp_err_t result4 = ledc_timer_config(&ledc_timer4);
  453.   if (result4 == ESP_OK)
  454.      Serial.printf("timer4 frequency: %d", ledc_get_freq(LEDC_LOW_SPEED_MODE, LEDC_TIMER_0));
  455.  
  456.  
  457.   // Low Speed LEDTimer5 setup
  458.   ledc_timer_config_t ledc_timer5;
  459.   ledc_timer5.speed_mode = LEDC_LOW_SPEED_MODE;           // timer mode
  460.   ledc_timer5.duty_resolution = LEDC_TIMER_3_BIT; // resolution of PWM duty
  461.   ledc_timer5.timer_num = LEDC_TIMER_1;            // timer index
  462.   ledc_timer5.freq_hz = 20000;                      // frequency of PWM signal
  463.   // Set configuration of timer1 for low speed channels
  464.   esp_err_t result5 = ledc_timer_config(&ledc_timer5);
  465.   if (result5 == ESP_OK)
  466.      Serial.printf("timer5 frequency: %d", ledc_get_freq(LEDC_LOW_SPEED_MODE, LEDC_TIMER_1));
  467.  
  468. // Low Speed LEDTimer6 setup
  469.   ledc_timer_config_t ledc_timer6;
  470.   ledc_timer6.speed_mode = LEDC_LOW_SPEED_MODE;           // timer mode
  471.   ledc_timer6.duty_resolution = LEDC_TIMER_3_BIT; // resolution of PWM duty
  472.   ledc_timer6.timer_num = LEDC_TIMER_2;            // timer index
  473.   ledc_timer6.freq_hz = 20000;                      // frequency of PWM signal
  474.   // Set configuration of timer2 for low speed channels
  475.   esp_err_t result6 = ledc_timer_config(&ledc_timer6);
  476.   if (result6 == ESP_OK)
  477.      Serial.printf("timer6 frequency: %d", ledc_get_freq(LEDC_LOW_SPEED_MODE, LEDC_TIMER_2));
  478.  
  479. // Low Speed LEDTimer7 setup
  480.   ledc_timer_config_t ledc_timer7;
  481.   ledc_timer7.speed_mode = LEDC_LOW_SPEED_MODE;           // timer mode
  482.   ledc_timer7.duty_resolution = LEDC_TIMER_3_BIT; // resolution of PWM duty
  483.   ledc_timer7.timer_num = LEDC_TIMER_3;            // timer index
  484.   ledc_timer7.freq_hz = 20000;                      // frequency of PWM signal
  485.   // Set configuration of timer3 for low speed channels
  486.   esp_err_t result7 = ledc_timer_config(&ledc_timer7);
  487.   if (result7 == ESP_OK)
  488.      Serial.printf("timer7 frequency: %d", ledc_get_freq(LEDC_LOW_SPEED_MODE, LEDC_TIMER_3));
  489.  
  490. // LEDC Channel 0 setup:  
  491.   ledc_channel_config_t ledc_channel0 = {
  492.           .gpio_num   = PWM1,
  493.           .speed_mode = LEDC_HIGH_SPEED_MODE,
  494.           .channel    = LEDC_CHANNEL_0,
  495.           .intr_type  = LEDC_INTR_DISABLE,
  496.           .timer_sel  = LEDC_TIMER_0,
  497.           .duty       = 4,
  498.           .hpoint     = 0
  499.   };
  500.   // Set LED Controller with previously prepared configuration
  501.   ledc_channel_config(&ledc_channel0);
  502.   Serial.println("Channel0 set");
  503.  
  504. // LEDC Channel 1 setup:  
  505.   ledc_channel_config_t ledc_channel1 = {
  506.           .gpio_num   = PWM2,
  507.           .speed_mode = LEDC_HIGH_SPEED_MODE,
  508.           .channel    = LEDC_CHANNEL_1,
  509.           .intr_type  = LEDC_INTR_DISABLE,
  510.           .timer_sel  = LEDC_TIMER_0,
  511.           .duty       = 4,
  512.           .hpoint     = 0
  513.   };
  514.   // Set LED Controller with previously prepared configuration
  515.   ledc_channel_config(&ledc_channel1);
  516.   Serial.println("Channel1 set");
  517.  
  518.   // LEDC Channel 2 setup:  
  519.   ledc_channel_config_t ledc_channel2 = {
  520.           .gpio_num   = PWM3,
  521.           .speed_mode = LEDC_HIGH_SPEED_MODE,
  522.           .channel    = LEDC_CHANNEL_2,
  523.           .intr_type  = LEDC_INTR_DISABLE,
  524.           .timer_sel  = LEDC_TIMER_1,
  525.           .duty       = 4,
  526.           .hpoint     = 0
  527.   };
  528.   // Set LED Controller with previously prepared configuration
  529.   ledc_channel_config(&ledc_channel2);
  530.   Serial.println("Channel2 set");
  531.  
  532.   // LEDC Channel 3 setup:  
  533.   ledc_channel_config_t ledc_channel3 = {
  534.           .gpio_num   = PWM4,
  535.           .speed_mode = LEDC_HIGH_SPEED_MODE,
  536.           .channel    = LEDC_CHANNEL_3,
  537.           .intr_type  = LEDC_INTR_DISABLE,
  538.           .timer_sel  = LEDC_TIMER_1,
  539.           .duty       = 4,
  540.           .hpoint     = 0
  541.   };
  542.   // Set LED Controller with previously prepared configuration
  543.   ledc_channel_config(&ledc_channel3);
  544.   Serial.println("Channel3 set");
  545.  
  546.    // LEDC Channel 4 setup:  
  547.   ledc_channel_config_t ledc_channel4 = {
  548.           .gpio_num   = PWM5,
  549.           .speed_mode = LEDC_HIGH_SPEED_MODE,
  550.           .channel    = LEDC_CHANNEL_4,
  551.           .intr_type  = LEDC_INTR_DISABLE,
  552.           .timer_sel  = LEDC_TIMER_2,
  553.           .duty       = 4,
  554.           .hpoint     = 0
  555.   };
  556.   // Set LED Controller with previously prepared configuration
  557.   ledc_channel_config(&ledc_channel4);
  558.   Serial.println("Channel4 set");
  559.  
  560.      // LEDC Channel 5 setup:  
  561.   ledc_channel_config_t ledc_channel5 = {
  562.           .gpio_num   = PWM6,
  563.           .speed_mode = LEDC_HIGH_SPEED_MODE,
  564.           .channel    = LEDC_CHANNEL_5,
  565.           .intr_type  = LEDC_INTR_DISABLE,
  566.           .timer_sel  = LEDC_TIMER_2,
  567.           .duty       = 4,
  568.           .hpoint     = 0
  569.   };
  570.   // Set LED Controller with previously prepared configuration
  571.   ledc_channel_config(&ledc_channel5);
  572.   Serial.println("Channel5 set");
  573.  
  574.      // LEDC Channel 6 setup:  
  575.   ledc_channel_config_t ledc_channel6 = {
  576.           .gpio_num   = PWM7,
  577.           .speed_mode = LEDC_HIGH_SPEED_MODE,
  578.           .channel    = LEDC_CHANNEL_6,
  579.           .intr_type  = LEDC_INTR_DISABLE,
  580.           .timer_sel  = LEDC_TIMER_3,
  581.           .duty       = 4,
  582.           .hpoint     = 0
  583.   };
  584.   // Set LED Controller with previously prepared configuration
  585.   ledc_channel_config(&ledc_channel6);
  586.   Serial.println("Channel6 set");
  587.  
  588.      // LEDC Channel 7 setup:  
  589.   ledc_channel_config_t ledc_channel7 = {
  590.           .gpio_num   = PWM8,
  591.           .speed_mode = LEDC_HIGH_SPEED_MODE,
  592.           .channel    = LEDC_CHANNEL_7,
  593.           .intr_type  = LEDC_INTR_DISABLE,
  594.           .timer_sel  = LEDC_TIMER_3,
  595.           .duty       = 4,
  596.           .hpoint     = 0
  597.   };
  598.   // Set LED Controller with previously prepared configuration
  599.   ledc_channel_config(&ledc_channel7);
  600.   Serial.println("Channel7 set");  
  601.  
  602. //  **************************  Low Speed channels   *********************
  603.  
  604. // LEDC Channel 8 setup:  
  605.   ledc_channel_config_t ledc_channel8 = {
  606.           .gpio_num   = PWM9,
  607.           .speed_mode = LEDC_LOW_SPEED_MODE,
  608.           .channel    = LEDC_CHANNEL_0,
  609.           .intr_type  = LEDC_INTR_DISABLE,
  610.           .timer_sel  = LEDC_TIMER_0,
  611.           .duty       = 4,
  612.           .hpoint     = 0
  613.   };
  614.   // Set LED Controller with previously prepared configuration
  615.   ledc_channel_config(&ledc_channel8);
  616.   Serial.println("Channel8 set");
  617.  
  618. // LEDC Channel 9 setup:  
  619.   ledc_channel_config_t ledc_channel9 = {
  620.           .gpio_num   = PWM10,
  621.           .speed_mode = LEDC_LOW_SPEED_MODE,
  622.           .channel    = LEDC_CHANNEL_1,
  623.           .intr_type  = LEDC_INTR_DISABLE,
  624.           .timer_sel  = LEDC_TIMER_0,
  625.           .duty       = 4,
  626.           .hpoint     = 0
  627.   };
  628.   // Set LED Controller with previously prepared configuration
  629.   ledc_channel_config(&ledc_channel9);
  630.   Serial.println("Channel9 set");
  631.  
  632.   // LEDC Channel 10 setup:  
  633.   ledc_channel_config_t ledc_channel10 = {
  634.           .gpio_num   = PWM11,
  635.           .speed_mode = LEDC_LOW_SPEED_MODE,
  636.           .channel    = LEDC_CHANNEL_2,
  637.           .intr_type  = LEDC_INTR_DISABLE,
  638.           .timer_sel  = LEDC_TIMER_1,
  639.           .duty       = 4,
  640.           .hpoint     = 0
  641.   };
  642.   // Set LED Controller with previously prepared configuration
  643.   ledc_channel_config(&ledc_channel10);
  644.   Serial.println("Channel10 set");
  645.  
  646.   startCrankTimer();
  647.   ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0, 8) );
  648.   ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0) );
  649.   ESP_ERROR_CHECK( ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0, 8) );
  650.   ESP_ERROR_CHECK( ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0) );
  651.   Serial.println("Duty Updated");
  652.  
  653. }
  654.  
  655. void loop() {
  656.  
  657.   if (gCrankPosChangeflag == true) {
  658.     incCrankPos();
  659.   }
  660. }

Who is online

Users browsing this forum: Baidu [Spider] and 77 guests