From 4a89e24804b91902def506a9e898293fa36ccb59 Mon Sep 17 00:00:00 2001
From: tao_z <tzj0429@163.com>
Date: Sat, 10 Jul 2021 11:52:25 +0800
Subject: [PATCH] 待调试电机转动

---
 USR/SRC/pwm.c |   67 +++++++++++++++++++--------------
 1 files changed, 39 insertions(+), 28 deletions(-)

diff --git a/USR/SRC/pwm.c b/USR/SRC/pwm.c
index fc83065..e33156e 100644
--- a/USR/SRC/pwm.c
+++ b/USR/SRC/pwm.c
@@ -9,7 +9,7 @@
  * 
  */
 #include "pwm.h"
-
+static volatile uint32_t timer_outcnt = 0;
 /*****************************************************************************************************
 Timer2 is used to speed cal
 APB1 is 64M ,so Timer2 clock is 64M,after prescaler 480 is 0.1M(10us)
@@ -24,7 +24,7 @@
 
     timer_deinit(TIMER2);
 
-    timercontralcfg.prescaler = 6399; //64Mhz /6400 =0.01Mhz = 100us
+    timercontralcfg.prescaler = 4799; //48Mhz /4800 =0.01Mhz = 100us
     timercontralcfg.alignedmode = TIMER_COUNTER_EDGE;
     timercontralcfg.counterdirection = TIMER_COUNTER_UP;
     timercontralcfg.period = PERIOD_CAP; //100us*10000=1s
@@ -48,7 +48,7 @@
 *****************************************************************************************************/
 static void Timer0Init(void)
 {
-    uint16_t Duty = PERIOD_CMP + 1;
+    uint16_t Duty = PERIOD_CMP / 2;
     timer_parameter_struct timercontralcfg;
     timer_oc_parameter_struct timeroutcfg[3];
     timer_break_parameter_struct timer_breakpara;
@@ -63,22 +63,22 @@
 
     timeroutcfg[0].ocpolarity = TIMER_OC_POLARITY_HIGH; //channel output polarity is high
     timeroutcfg[0].ocnpolarity = TIMER_OCN_POLARITY_HIGH;
-    timeroutcfg[0].outputstate = TIMER_CCX_ENABLE;   //channel enable
-    timeroutcfg[0].outputnstate = TIMER_CCXN_ENABLE; //channel complementary enable
+    timeroutcfg[0].outputstate = TIMER_CCX_ENABLE;    //channel enable
+    timeroutcfg[0].outputnstate = TIMER_CCXN_DISABLE; //channel complementary DISABLE
     timeroutcfg[0].ocidlestate = TIMER_OC_IDLE_STATE_LOW;
     timeroutcfg[0].ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;
 
     timeroutcfg[1].ocpolarity = TIMER_OC_POLARITY_HIGH; //channel output polarity is high
     timeroutcfg[1].ocnpolarity = TIMER_OCN_POLARITY_HIGH;
-    timeroutcfg[1].outputstate = TIMER_CCX_ENABLE;   //channel enable
-    timeroutcfg[1].outputnstate = TIMER_CCXN_ENABLE; //channel complementary enable
+    timeroutcfg[1].outputstate = TIMER_CCX_ENABLE;    //channel enable
+    timeroutcfg[1].outputnstate = TIMER_CCXN_DISABLE; //channel complementary  DISABLE
     timeroutcfg[1].ocidlestate = TIMER_OC_IDLE_STATE_LOW;
     timeroutcfg[1].ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;
 
     timeroutcfg[2].ocpolarity = TIMER_OC_POLARITY_HIGH; //channel output polarity is high
     timeroutcfg[2].ocnpolarity = TIMER_OCN_POLARITY_HIGH;
-    timeroutcfg[2].outputstate = TIMER_CCX_ENABLE;   //channel enable
-    timeroutcfg[2].outputnstate = TIMER_CCXN_ENABLE; //channel complementary enable
+    timeroutcfg[2].outputstate = TIMER_CCX_ENABLE;    //channel enable
+    timeroutcfg[2].outputnstate = TIMER_CCXN_DISABLE; //channel complementary  DISABLE
     timeroutcfg[2].ocidlestate = TIMER_OC_IDLE_STATE_LOW;
     timeroutcfg[2].ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;
 
@@ -90,25 +90,25 @@
     timer_channel_output_pulse_value_config(TIMER0, TIMER_CH_1, Duty);
     timer_channel_output_pulse_value_config(TIMER0, TIMER_CH_2, Duty);
 
-    timer_channel_output_mode_config(TIMER0, TIMER_CH_0, TIMER_OC_MODE_PWM1); //set pwm1 mode first low then high
-    timer_channel_output_mode_config(TIMER0, TIMER_CH_1, TIMER_OC_MODE_PWM1);
-    timer_channel_output_mode_config(TIMER0, TIMER_CH_2, TIMER_OC_MODE_PWM1);
+    timer_channel_output_mode_config(TIMER0, TIMER_CH_0, TIMER_OC_MODE_PWM0); //set pwm1 mode first low then high
+    timer_channel_output_mode_config(TIMER0, TIMER_CH_1, TIMER_OC_MODE_PWM0);
+    timer_channel_output_mode_config(TIMER0, TIMER_CH_2, TIMER_OC_MODE_PWM0);
 
-    timer_channel_output_shadow_config(TIMER0, TIMER_CH_0, TIMER_OC_SHADOW_ENABLE); //shadow ENable
-    timer_channel_output_shadow_config(TIMER0, TIMER_CH_1, TIMER_OC_SHADOW_ENABLE);
-    timer_channel_output_shadow_config(TIMER0, TIMER_CH_2, TIMER_OC_SHADOW_ENABLE);
+    timer_channel_output_shadow_config(TIMER0, TIMER_CH_0, TIMER_OC_SHADOW_DISABLE); //shadow ENable
+    timer_channel_output_shadow_config(TIMER0, TIMER_CH_1, TIMER_OC_SHADOW_DISABLE);
+    timer_channel_output_shadow_config(TIMER0, TIMER_CH_2, TIMER_OC_SHADOW_DISABLE);
 
-    /* configure TIMER break function */
-    timer_break_struct_para_init(&timer_breakpara);
-    /* automatic output enable, break, dead time and lock configuration*/
-    timer_breakpara.runoffstate = TIMER_ROS_STATE_DISABLE;
-    timer_breakpara.ideloffstate = TIMER_IOS_STATE_DISABLE;
-    timer_breakpara.deadtime = 255;
-    timer_breakpara.breakpolarity = TIMER_BREAK_POLARITY_LOW;
-    timer_breakpara.outputautostate = TIMER_OUTAUTO_ENABLE;
-    timer_breakpara.protectmode = TIMER_CCHP_PROT_0;
-    timer_breakpara.breakstate = TIMER_BREAK_ENABLE;
-    timer_break_config(TIMER0, &timer_breakpara);
+    // /* configure TIMER break function */
+    // timer_break_struct_para_init(&timer_breakpara);
+    // /* automatic output enable, break, dead time and lock configuration*/
+    // timer_breakpara.runoffstate = TIMER_ROS_STATE_DISABLE;
+    // timer_breakpara.ideloffstate = TIMER_IOS_STATE_DISABLE;
+    // timer_breakpara.deadtime = 32;
+    // timer_breakpara.breakpolarity = TIMER_BREAK_POLARITY_LOW;
+    // timer_breakpara.outputautostate = TIMER_OUTAUTO_ENABLE;
+    // timer_breakpara.protectmode = TIMER_CCHP_PROT_0;
+    // timer_breakpara.breakstate = TIMER_BREAK_DISABLE;
+    // timer_break_config(TIMER0, &timer_breakpara);
 
     timer_primary_output_config(TIMER0, ENABLE);
     timer_auto_reload_shadow_enable(TIMER0);
@@ -116,7 +116,7 @@
 
 void TimerInit(void)
 {
-    // rcu_periph_clock_enable(RCU_TIMER2);
+    rcu_periph_clock_enable(RCU_TIMER2);
     rcu_periph_clock_enable(RCU_TIMER0);
     Timer0Init();
     Timer2Init();
@@ -125,7 +125,7 @@
 
 void SetPwmDuty(uint16_t ch, uint32_t duty)
 {
-    uint32_t duty_temp = (PERIOD_CMP >= duty) ? (PERIOD_CMP - duty) : 0;
+    // uint32_t duty_temp = (PERIOD_CMP >= duty) ? (PERIOD_CMP - duty) : 0;
     timer_channel_output_pulse_value_config(TIMER0, ch, duty);
 }
 
@@ -143,3 +143,14 @@
 {
     timer_disable(TIMER2);
 }
+extern void TIMER2_IRQHandler_CallBack(void)
+{
+    timer_outcnt++;
+}
+
+extern uint32_t GetSpeedTimerOutcnt(void)
+{
+    uint32_t rtn = timer_outcnt;
+    timer_outcnt = 0;
+    return rtn;
+}

--
Gitblit v1.8.0