PWM and RioRand H Bridge

AgentSmithers
Posts: 28
Joined: Sat Apr 01, 2017 1:21 am

PWM and RioRand H Bridge

Postby AgentSmithers » Fri Nov 24, 2017 11:05 am

https://www.amazon.com/RioRand-Controll ... oller+dual

Hi Everyone,
I use a Riorand Hbridge for a project of mine with the ESP8266 at 5V. I manage to get all my stuff converted over to the ESP8266 except for this so far.

When I use my ATTiny with this Bridge this works fine

Code: Select all

TCCR0A = (1 << COM0A1) | (1 << WGM00);  // phase correct PWM mode
OCR0A  = 0x00;                          // initial PWM pulse width set to nothing
TCCR0B = (1 << CS01);   // clock source = CLK/8, start PWM
 for(int i=0;i<=0xff;i++)
      {
      OCR0A  = i;    //Starts rolling PWM from 0 to 100%
      }


The ATTiny dosnt have a spot to pick a FREQ. However on the ESP it does so I need some assistance. I think the Arduino runs at like 490hz.

Now I read through the API doc and it seems that if I set the peroid to 1000 then that sets the FREQ to 1khz correct?
Is that because 1 second / 1000 = 1khz?
So if I want the same FREQ as the arduino then I need to do 1 / 1694 = 0.5903khz, and set the peroid to 1694? Then from there my for loop from 0% to 100% will be 0 to 14750 on the duty cycle?
590 * 1000 / 40 generates the Duty Cycle range correct?
When PWM is set to zero is the effect that the GPIO will be pushing zero volts or full voltage, in this case 3.3v? then as PWM duty cycle is increased the voltage will either raise or drop?

My ultimate goal other then to understand pwm is to get my Hbridge operating to control the speed.
Thank you!!

Code: Select all

//PIN SETTINGS FOR PWM
#define PWM_D6_OUT_IO_MUX PERIPHS_IO_MUX_MTDI_U
#define PWM_D6_OUT_IO_NUM 12
#define PWM_D6_OUT_IO_FUNC  FUNC_GPIO12

#define PWM_D7_OUT_IO_MUX PERIPHS_IO_MUX_MTCK_U
#define PWM_D7_OUT_IO_NUM 13
#define PWM_D7_OUT_IO_FUNC  FUNC_GPIO13

#define PWM_D5_OUT_IO_MUX PERIPHS_IO_MUX_MTMS_U
#define PWM_D5_OUT_IO_NUM 14
#define PWM_D5_OUT_IO_FUNC  FUNC_GPIO14

#define PWM_D8_OUT_IO_MUX PERIPHS_IO_MUX_MTDO_U
#define PWM_D8_OUT_IO_NUM 15
#define PWM_D8_OUT_IO_FUNC  FUNC_GPIO15

#define PWM_D2_OUT_IO_MUX PERIPHS_IO_MUX_GPIO4_U
#define PWM_D2_OUT_IO_NUM 4
#define PWM_D2_OUT_IO_FUNC  FUNC_GPIO4

#define PWM_CHANNELS    1 //number of PWM Channels

void ICACHE_FLASH_ATTR PWM()
{

   os_printf("[%s]\n", __func__);
   uint32_t i = 0;
       
   const uint32_t period = 100000; // * 200ns ^= 1 kHz

   // PWM setup
   uint32_t io_info[PWM_CHANNELS][3] = {
           // { PWM_D6_OUT_IO_MUX, PWM_D6_OUT_IO_FUNC, PWM_D6_OUT_IO_NUM }, //Channel 0
           // { PWM_D7_OUT_IO_MUX, PWM_D7_OUT_IO_FUNC, PWM_D7_OUT_IO_NUM }, //Channel 1
            { PWM_D5_OUT_IO_MUX, PWM_D5_OUT_IO_FUNC, PWM_D5_OUT_IO_NUM }, //Channel 2
            //{ PWM_D8_OUT_IO_MUX, PWM_D8_OUT_IO_FUNC, PWM_D8_OUT_IO_NUM }, //Channel 3
            //{ PWM_D2_OUT_IO_MUX, PWM_D2_OUT_IO_FUNC, PWM_D2_OUT_IO_NUM }, //Channel 4
   };

   // initial duty: all off
   //uint32_t pwm_duty_init[PWM_CHANNELS] = {0, 0, 0, 0, 0};
   uint32_t pwm_duty_init[PWM_CHANNELS] = {0};
   pwm_init(period, pwm_duty_init, PWM_CHANNELS, io_info);
   pwm_start();
   /*
   while(true)
   {
      for (i = 1; i < 250; i++) {
         os_printf("PWM: %d\r\n", i);
         pwm_set_duty(i, 0);
         pwm_start();
         system_soft_wdt_feed();
         delay_second();
         i += 10;
      }
   system_soft_wdt_feed();
   }
   */
   os_printf("PWM: Returning\r\n");
   return;
}

Who is online

Users browsing this forum: beegee1962 and 2 guests