用按鍵控制電機轉速 L298N模塊使用程序
STM32單片機源程序如下:
- #include "led.h"
- #include "delay.h"
- #include "key.h"
- #include "usart.h"
- #include "sys.h"
- #include "DCmotor.h"
- #include "exti.h"
- extern __IO uint32_t CaptureNumber;
- extern __IO uint16_t PWM_Duty;
- int main(void)
- {
- vu8 key=0;
- delay_init(); //延時函數初始化
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //設置NVIC中斷分組2:2位搶占優先級,2位響應優先級
- LED_Init(); //LED端口初始化
- KEY_Init(); //初始化與按鍵連接的硬件接口
- uart_init(115200);
- LED0=0; //先點亮紅燈
- EXTIX_Init(); //外部中斷初始化
-
- /* 初始化定時器PWM輸出 */
- DCMOTOR_BRUSH_TIMx_PWM_Init();
-
- /* 初始化編碼器捕獲,編碼器對應TIM3_CH3:PB0引腳 */
- TIMx_DCmotor_ENCODER_Init();
-
- DCMOTOR_25GA370_Contrl(1,1,PWM_Duty);
-
- printf("PWM_Duty = %d.\r\n",PWM_Duty);
-
- while(1)
- {
- key=KEY_Scan(0); //得到鍵值
- printf("freq:%d\n",CaptureNumber); // 打印編碼器捕獲值
- printf("\r\n電機轉速 = %f:\r\n",(float)CaptureNumber/34/11);
- CaptureNumber=0; // 清零,從零開始計數
- delay_ms(500);
-
- printf("IN while(1) PWM_Duty = %d.\r\n",PWM_Duty);
-
- // switch(key)
- // {
- // int num1 = 0, num2 = 0;
- //// printf("ENTER CASE\r\n");
- // case KEY2_PRES:
- // num1 += 1;
- // printf("KEY2 按了%d次\r\n",num1);
- // PWM_Duty += 50;
- // if(PWM_Duty > 1000)
- // {
- // PWM_Duty = 1000;
- // }
- // DCMOTOR_25GA370_Contrl(1,1,PWM_Duty);
- // printf("After KEY2 PWM_Duty = %d.\r\n",PWM_Duty);
- // break;
- //
- // case KEY1_PRES:
- // num2 += 1;
- // printf("KEY2 按了%d次\r\n",num2);
- // PWM_Duty -= 50;
- // if(PWM_Duty < 100)
- // {
- // PWM_Duty = 100;
- // }
- // DCMOTOR_25GA370_Contrl(1,1,PWM_Duty);
- // printf("After KEY1 PWM_Duty = %d.\r\n",PWM_Duty);
- // break;
- // default :break;
- // }
- }
- }
復制代碼
所有資料51hei提供下載:
L298N模塊使用demo(用按鍵控制電機轉速).rar
(303.71 KB, 下載次數: 54)
2018-12-7 04:23 上傳
點擊文件名下載附件
|