电调解锁方法
2ms高电平的pwm波,400hz,持续5s以上
1ms高电平的pwm波,持续2s
即可解锁,之后输入1-2ms范围的高电平的pwm波即可控制电机的转速
这里是主函数
#include "stdio.h"
#include <stdint.h>
#include <stdbool.h>
#include "hw_memmap.h"
#include "hw_types.h"
#include "hw_gpio.h"
#include "debug.h"
#include "fpu.h"
#include "gpio.h"
#include "pin_map.h"
#include "rom.h"
#include "sysctl.h"
#include "uart.h"
#include "uartstdio.h"
#include "SystickTime.h"
#include "wpwm.h"
#ifdef DEBUG
void
__error__(char *pcFilename, uint32_t ui32Line)
{
}
#endif
/*
*********************************************************************************************************
* 函 数 名: PrintfLogo
* 功能说明: 打印例程名称和例程发布日期, 接上串口线后,打开PC机的串口终端软件可以观察结果
* 形 参: 无
* 返 回 值: 无
*********************************************************************************************************
*/
void PrintfLogo(void)
{
printf("*************************************************************\n\r");
printf("* 例程名称 : %s\r\n", "基于滴答定时器的精准延时"); /* 打印例程名称 */
printf("* 例程版本 : %s\r\n", "V1.0"); /* 打印例程版本 */
printf("* 发布日期 : %s\r\n", "20200310"); /* 打印例程日期 */
printf("* 标准库版本 :TM4C123GH6PM\r\n");
printf("* \r\n"); /* 打印一行空格 */
printf("* QQ : 3138372165 \r\n");
printf("* Email : 3138372165@qq.com \r\n");
printf("* Copyright www.nameless.tech 无名创新\r\n");
printf("*************************************************************\n\r");
}
void ConfigureUART(void)
{
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);// Enable the GPIO Peripheral used by the UART.
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);// Enable UART0
ROM_GPIOPinConfigure(GPIO_PA0_U0RX);// Configure GPIO Pins for UART mode.
ROM_GPIOPinConfigure(GPIO_PA1_U0TX);
ROM_GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);
// Use the internal 16MHz oscillator as the UART clock source.
// Initialize the UART for console I/O.
UARTStdioConfig(0, 115200, 16000000);UARTClockSourceSet(UART0_BASE, UART_CLOCK_PIOSC);
}
int fputc(int ch, FILE *f){UARTCharPut(UART0_BASE,ch); return (ch);}
int fgetc(FILE *f) {int ch=UARTCharGet(UART0_BASE); return (ch);}
int main(void)
{
ROM_FPULazyStackingEnable();//使能FPU浮点运算
ROM_SysCtlClockSet(SYSCTL_SYSDIV_2_5 | SYSCTL_USE_PLL | SYSCTL_XTAL_16MHZ |
SYSCTL_OSC_MAIN);//配置系统时钟 80M
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC);//解锁PC口为普通IO
HWREG(GPIO_PORTC_BASE + GPIO_O_LOCK) = GPIO_LOCK_KEY;
HWREG(GPIO_PORTC_BASE + GPIO_O_CR) = 0x000000FF;
GPIOPinTypeGPIOOutput(GPIO_PORTC_BASE, GPIO_PIN_2);
GPIOPinWrite(GPIO_PORTC_BASE,GPIO_PIN_2,0);
initTime();//初始化滴答定时器
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
ROM_GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_2);//红色
ROM_GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_3);//绿色
ROM_GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_1);//蓝色
ConfigureUART();//初始化串口0
PrintfLogo();//串口打印版本信息
Init_PWM();//初始化pwm
PWM_Output(2000,2000,2000,2000);
delay_ms(5000);
PWM_Output(1000,1000,1000,1000);
delay_ms(2000);
PWM_Output(1200,1200,1200,1200);
while(1)
{
printf("%d\r\n",PWMGenPeriodGet(PWM0_BASE, PWM_GEN_0));
printf("%d\r\n",PWMGenPeriodGet(PWM0_BASE, PWM_GEN_1));
printf("%d\r\n",PWMGenPeriodGet(PWM0_BASE, PWM_GEN_2));
printf("%d\r\n",PWMGenPeriodGet(PWM0_BASE, PWM_GEN_3));
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, 0);//置低位点亮
delay_ms(100);
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, GPIO_PIN_2);//置高位熄灭
delay_ms(100);
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_3, 0);//置低位点亮
delay_ms(100);
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_3, GPIO_PIN_3);//置高位熄灭
delay_ms(100);
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1, 0);//置低位点亮
delay_ms(100);
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1, GPIO_PIN_1);//置高位熄灭
delay_ms(100);
}
}
pwm模块的函数(这一段程序来自于无名飞控
#include "wpwm.h"
//#define PWM_PERIOD_MAX 3125//2.5ms————400hz
#define PWM_PERIOD_MAX 3125//2.5ms————400hz
#define PWM_PERIOD_MAX_20MS (5000*8)
static uint16_t period;
/***********************************************************
@函数名:Init_PWM
@入口参数:无
@出口参数:无
@功能描述:PWM初始化
@作者:无名小哥
@日期:2019年01月27日
*************************************************************/
void Init_PWM(void)
{
SysCtlPWMClockSet(SYSCTL_PWMDIV_64); // Set divider to 80M/8=10M=0.1us
SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM0); // Enable PWM peripheral
SysCtlDelay(2); // Insert a few cycles after enabling the peripheral to allow the clock to be fully activated
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB); // Enable GPIOB peripheral
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC); // Enable GPIOC peripheral
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE); // Enable GPIOE peripheral
SysCtlDelay(2); // Insert a few cycles after enabling the peripheral to allow the clock to be fully activated
// Use alternate function
GPIOPinConfigure(GPIO_PB6_M0PWM0);
GPIOPinConfigure(GPIO_PB7_M0PWM1);
GPIOPinConfigure(GPIO_PB4_M0PWM2);
GPIOPinConfigure(GPIO_PB5_M0PWM3);
GPIOPinConfigure(GPIO_PE4_M0PWM4);
GPIOPinConfigure(GPIO_PE5_M0PWM5);
GPIOPinConfigure(GPIO_PC4_M0PWM6);
GPIOPinConfigure(GPIO_PC5_M0PWM7);
// Use pin with PWM peripheral
GPIOPinTypePWM(GPIO_PORTB_BASE, GPIO_PIN_6);//M0PWM0
GPIOPinTypePWM(GPIO_PORTB_BASE, GPIO_PIN_7);//M0PWM1
GPIOPinTypePWM(GPIO_PORTB_BASE, GPIO_PIN_4);//M0PWM2
GPIOPinTypePWM(GPIO_PORTB_BASE, GPIO_PIN_5);//M0PWM3
GPIOPinTypePWM(GPIO_PORTE_BASE, GPIO_PIN_4);//M0PWM4
GPIOPinTypePWM(GPIO_PORTE_BASE, GPIO_PIN_5);//M0PWM5
GPIOPinTypePWM(GPIO_PORTC_BASE, GPIO_PIN_4);//M0PWM6
GPIOPinTypePWM(GPIO_PORTC_BASE, GPIO_PIN_5);//M0PWM7
// Configure the PWM generator for count down mode with immediate updates to the parameters
PWMGenConfigure(PWM0_BASE, PWM_GEN_0, PWM_GEN_MODE_DOWN | PWM_GEN_MODE_NO_SYNC);
PWMGenConfigure(PWM0_BASE, PWM_GEN_1, PWM_GEN_MODE_DOWN | PWM_GEN_MODE_NO_SYNC);
PWMGenConfigure(PWM0_BASE, PWM_GEN_2, PWM_GEN_MODE_DOWN | PWM_GEN_MODE_NO_SYNC);
PWMGenConfigure(PWM0_BASE, PWM_GEN_3, PWM_GEN_MODE_DOWN | PWM_GEN_MODE_NO_SYNC);
// The period is set to 2.5ms (400 Hz)
period = PWM_PERIOD_MAX;
PWMGenPeriodSet(PWM0_BASE, PWM_GEN_0, period); // Set the period
PWMGenPeriodSet(PWM0_BASE, PWM_GEN_1, period);
PWMGenPeriodSet(PWM0_BASE, PWM_GEN_2, period); // Set the period
PWMGenPeriodSet(PWM0_BASE, PWM_GEN_3, period);
// Start the timers in generator 0 and 1
PWMGenEnable(PWM0_BASE, PWM_GEN_0);
PWMGenEnable(PWM0_BASE, PWM_GEN_1);
PWMGenEnable(PWM0_BASE, PWM_GEN_2);
PWMGenEnable(PWM0_BASE, PWM_GEN_3);
// Enable the outputs
PWMOutputState(PWM0_BASE, PWM_OUT_0_BIT | PWM_OUT_1_BIT | PWM_OUT_2_BIT | PWM_OUT_3_BIT
| PWM_OUT_4_BIT | PWM_OUT_5_BIT | PWM_OUT_6_BIT | PWM_OUT_7_BIT,
true);
PWM_Output(1000,1000,1000,1000);
}
/***********************************************************
@函数名:PWM_Output
@入口参数:uint16_t width1,uint16_t width2,uint16_t width3,
uint16_t width4,uint16_t width5,uint16_t width6,
uint16_t width7,uint16_t width8
@出口参数:无
@功能描述:PWM输出
@作者:无名小哥
@日期:2019年01月27日
*************************************************************/
void PWM_Output(uint16_t width1,uint16_t width2,uint16_t width3,uint16_t width4)
{
PWMPulseWidthSet(PWM0_BASE,PWM_OUT_6,1.25*width1);//PC4
PWMPulseWidthSet(PWM0_BASE,PWM_OUT_7,1.25*width2);//PC5
PWMPulseWidthSet(PWM0_BASE,PWM_OUT_1,1.25*width3);//PB7
PWMPulseWidthSet(PWM0_BASE,PWM_OUT_0,1.25*width4);//PB6
EPWM_Output(1.25*width1,1.25*width2,1.25*width3,1.25*width4);//外接PWM口与排针PWM口输出对应
//PWM1、PWM5同输出
//PWM2、PWM6同输出
//PWM3、PWM7同输出
//PWM4、PWM8同输出
}
void EPWM_Output(uint16_t width5,uint16_t width6,uint16_t width7,uint16_t width8)
{
PWMPulseWidthSet(PWM0_BASE,PWM_OUT_5,width5);//PE5
PWMPulseWidthSet(PWM0_BASE,PWM_OUT_4,width6);//PE4
PWMPulseWidthSet(PWM0_BASE,PWM_OUT_2,width7);//PB4
PWMPulseWidthSet(PWM0_BASE,PWM_OUT_3,width8);//PB5
}
强烈建议在实验室里面做,实验室有仪器,即使出现问题也可以更快地找出来。