Page 1 of 1

PWM on MINI-M4 for STM32

Posted: 10 Oct 2014 20:58
by SeanH
Hi

Okay 1 day down using the MikroBasic Pro for Arm along with the MINI-M4 for STM32

Success in getting the USB Hid routines to send packets back and forth and getting head around long GPIO routines to set up pins to switch on and off LEDS.

Next I wanted to kick the PWM off but all I ever see on the scope, via Port PA8 is 100MHz pulse.

Here is very rough cut and paste code

Code: Select all

program Testing
dim STAT_LED as sbit at ODR13_GPIOC_ODR_bit
dim DATA_LED as sbit at ODR12_GPIOC_ODR_bit
dim cnt as byte
dim readbuff as byte[64]
dim writebuff as byte[64]
dim current_duty as word
pwm_period1 as word
    
sub procedure USB1Interrupt() iv IVT_INT_OTG_FS
  USB_Interrupt_Proc()
end sub

main:
GPIO_Digital_Output(@GPIOC_BASE, _GPIO_PINMASK_12 or _GPIO_PINMASK_13)

current_duty  = 50                         ' initial value for current_duty
pwm_period1 = PWM_TIM1_Init(2000)


PWM_TIM1_Set_Duty(current_duty,  _PWM_NON_INVERTED, _PWM_CHANNEL1)  ' Set current duty for PWM_TIM1
PWM_TIM1_Start(_PWM_CHANNEL1, @_GPIO_MODULE_TIM1_CH1_PA8)



  HID_Enable(@readbuff,@writebuff)

loopit:
    if hid_read=0 then
  else



   if readbuff[0]="1" then
   Delay_ms(1)
      current_duty = current_duty + 5        ' increment current_duty
      if (current_duty > pwm_period1) then   ' if we increase current_duty greater then possible pwm_period1 value
        current_duty = 0                     ' reset current_duty value to zero
      end if
      PWM_TIM1_Set_Duty(current_duty,  _PWM_NON_INVERTED, _PWM_CHANNEL1) ' set newly acquired duty ratio
      
    STAT_LED = 1
   end if
   
   if readbuff[0]="2" then

    Delay_ms(1)
      current_duty = current_duty - 5        ' decrement current_duty
      if (current_duty > pwm_period1) then   ' if we decrease current_duty greater then possible pwm_period1 value (overflow)
        current_duty = pwm_period1           ' set current_duty to max possible value
      end if
      PWM_TIM1_Set_Duty(current_duty,  _PWM_NON_INVERTED, _PWM_CHANNEL1) ' set newly acquired duty ratio
      DATA_LED = 1
   end if

                writebuff[0]=lo(current_duty)
                writebuff[1]=hi(current_duty)
                

                
   HID_Write(@writebuff,64)
   end if
        DATA_LED = 0
        STAT_LED = 0


goto loopit
end.
But I suspect it is in the Project configuration, here are screen shots

Image

Image

I set speed to 120MHz as using the bootloader.

Any help or tips appreciated.

Cheers

Sean.

Re: PWM on MINI-M4 for STM32

Posted: 13 Oct 2014 12:31
by SeanH
Sorted, I switch to Timer4 and worked fine with PB7

Code: Select all

PWM_TIM4_Set_Duty(current_duty,  _PWM_NON_INVERTED, _PWM_CHANNEL2)  ' Set current duty for PWM_TIM4 on Channel2
PWM_TIM4_Start(_PWM_CHANNEL2, @_GPIO_MODULE_TIM4_CH2_PB7)
Not back to trying to get external interrupts working correctly.

Re: PWM on MINI-M4 for STM32

Posted: 13 Oct 2014 12:55
by marina.petrovic
Hi,

To be sure whether you use correct project setting the best is to save scheme (project settings) from the working project:
Project -> Edit Project -> Save Scheme, and then simple load saved scheme into new project: Project -> Edit Project -> Load Scheme.

Also, please, take a look at PWM example from compiler (written for STM32F407VG):
...mikroBasic PRO for ARM\Examples\ST\Internal MCU modules\STM32F407VG\PWM

You can adjust the example for microcontroller which you use with the help of microcontroller datasheet/Reference Manuel.

Best regards,
Marina

Re: PWM on MINI-M4 for STM32

Posted: 13 Oct 2014 20:16
by SeanH
Thanks Marina

I spent all weekend reading the processor spec PDF DM00035129.pdf (Very long) over the last 3 days !

The only thing I could put it down to was the alternative function mapping as PA8 seems to be at MCO1 and I need to change it to AF1 ?
But using USB it looks like it defaults to MCO for clock out.

But in saying that Timer4 is listed as AF2 and did not need changing.

But it works now very well :) and there are plenty of pins left !

Cheers

Sean

Re: PWM on MINI-M4 for STM32

Posted: 14 Oct 2014 11:02
by marina.petrovic
Hi,

I am glad that you manage to solve your problem.

Best regards,
Marina