#include <stdint.h>
#include <stdbool.h>
#include <time.h>
#include "utils/ustdlib.h"
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "driverlib/sysctl.h"
#include "driverlib/systick.h"
#include "driverlib/rom.h"
#include "driverlib/gpio.h"
#include "driverlib/fpu.h"
#include "driverlib/pin_map.h"
#include "driverlib/pwm.h"
#include "grlib/grlib.h"
#include "driverlib/qei.h"
#include "inc/hw_qei.h"
#include "inc/tm4c123gh6pm.h"
#define uchar unsigned char
#define PERIOD_TIME SysCtlClockGet()/1000
#define DIO GPIO_PIN_1
#define RCLK GPIO_PIN_2
#define SCLK GPIO_PIN_3
int anjianzhi=1;
uchar LED[8];
uchar LED1[8];
uchar judge=0;
uchar jianceTime=0;
float zhankongbi,PulseWidth;
unsigned long speed_signal=0;
uint32_t speed=8;
int speedDirectionGet=0;
/*****************************************************************
* **************************PWM初始化******************************
*****************************************************************/
void Init_PWM()
{
zhankongbi=50;
PulseWidth=PERIOD_TIME*0.5;
// 设置PWM时钟和系统时钟一致
SysCtlPWMClockSet(SYSCTL_PWMDIV_1);
// 使能PWM外设
SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM0);
// 使能外设端口
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
//设置对应管脚的PWM信号功能
GPIOPinConfigure(GPIO_PB4_M0PWM2);
GPIOPinConfigure(GPIO_PB7_M0PWM1);
//设置PWM信号端口
GPIOPinTypePWM(GPIO_PORTB_BASE, GPIO_PIN_4);
GPIOPinTypePWM(GPIO_PORTB_BASE, GPIO_PIN_7);
//PWM生成器配置
PWMGenConfigure(PWM0_BASE, PWM_GEN_1, PWM_GEN_MODE_DOWN | PWM_GEN_MODE_NO_SYNC);
PWMGenConfigure(PWM0_BASE, PWM_GEN_0, PWM_GEN_MODE_DOWN | PWM_GEN_MODE_NO_SYNC);
//设置PWM信号周期
PWMGenPeriodSet(PWM0_BASE, PWM_GEN_1, PERIOD_TIME);
PWMGenPeriodSet(PWM0_BASE, PWM_GEN_0, PERIOD_TIME);
//设置PWM信号占空比
PWMPulseWidthSet(PWM0_BASE, PWM_OUT_2,PulseWidth);
PWMPulseWidthSet(PWM0_BASE, PWM_OUT_1,PulseWidth);
// 使能PWM输出端口
PWMOutputState(PWM0_BASE, PWM_OUT_2_BIT, false);
PWMOutputState(PWM0_BASE, PWM_OUT_1_BIT, false);
// 使能PWM生成器
PWMGenEnable(PWM0_BASE, PWM_GEN_1);
PWMGenEnable(PWM0_BASE, PWM_GEN_0);
// 使能PWm生成器模块的及时功能.
PWMSyncTimeBase(PWM0_BASE, PWM_GEN_1);
PWMSyncTimeBase(PWM0_BASE, PWM_GEN_0);
}
/*************************************************************************************
**********************************电机控制********************************************
*************************************************************************************/
void govern_speed(void)
{
PulseWidth=zhankongbi/100*PERIOD_TIME;
PWMPulseWidthSet(PWM0_BASE, PWM_OUT_2, PulseWidth);
PWMPulseWidthSet(PWM0_BASE, PWM_OUT_1, PulseWidth);
}
void forward(void)
{
PWMOutputState(PWM0_BASE, PWM_OUT_2_BIT, true);
PWMOutputState(PWM0_BASE, PWM_OUT_1_BIT, false);
judge=1;
}
void reverse(void)
{
judge=0;
PWMOutputState(PWM0_BASE, PWM_OUT_1_BIT, true);
PWMOutputState(PWM0_BASE, PWM_OUT_2_BIT, false);
}
void stop(void)
{
PWMOutputState(PWM0_BASE, PWM_OUT_2_BIT, false);
PWMOutputState(PWM0_BASE, PWM_OUT_1_BIT, false);
judge=0;
return;
}
/*****************************************************************
*************************** 数码管显示*******************************
****************************************************************/
unsigned char LED_0F[]=
{ //0 1 2 3 4 5 6 7 8 9
0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0xFF,0xbf
};
void LED_OUT(uchar X)
{
uchar i;
for(i=8;i>=1;i–)
{
if (X&0x80)
GPIOPinWrite(GPIO_PORTF_BASE, DIO|RCLK|SCLK, DIO);
else
GPIOPinWrite(GPIO_PORTF_BASE,DIO,0);
X<<=1;
GPIOPinWrite(GPIO_PORTF_BASE,SCLK, 0);
GPIOPinWrite(GPIO_PORTF_BASE,DIO|RCLK|SCLK, SCLK);
}
}
void LED4_Display (void)
{
uchar i;
//显示第一位
i = LED_0F[LED[0]];
LED_OUT(i);
LED_OUT(0x01);
GPIOPinWrite(GPIO_PORTF_BASE, RCLK, 0);
GPIOPinWrite(GPIO_PORTF_BASE, DIO|RCLK|SCLK, RCLK);
//显示第二位
i = LED_0F[LED[1]];
LED_OUT(i);
LED_OUT(0x02);
GPIOPinWrite(GPIO_PORTF_BASE, RCLK, 0);
GPIOPinWrite(GPIO_PORTF_BASE, DIO|RCLK|SCLK, RCLK);
//显示第三位
i = LED_0F[LED[2]];
LED_OUT(i);
LED_OUT(0x04);
GPIOPinWrite(GPIO_PORTF_BASE, RCLK, 0);
GPIOPinWrite(GPIO_PORTF_BASE, DIO|RCLK|SCLK, RCLK);
//显示第四位
i = LED_0F[LED[3]];
LED_OUT(i);
LED_OUT(0x08);
GPIOPinWrite(GPIO_PORTF_BASE, RCLK,0);
GPIOPinWrite(GPIO_PORTF_BASE, DIO|RCLK|SCLK, RCLK);
}
void Init_LED(void)
{
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, DIO|RCLK|SCLK);
}
void SpeedDisplay(void)
{
int a=speed;
LED[3]=a/1000;
a=a%1000;
LED[2]=a/100;
a=a%100;
LED[1]=a/10;
a=a%10;
LED[0]=a;
LED4_Display();
}
void LED_Display()
{
int i;
if(jianceTime==0)
{
/*for(i=0;i<=1000;i++){
LED[0]=1-speedDirectionGet;
LED[1]=LED[2]=LED[3]=11;
LED4_Display();
}*/
for(i=0;i<=1000;i++){
SpeedDisplay();
}
}else if(jianceTime==0xFF){
for(i=0;i<1000;i++){
LED4_Display();
}
jianceTime=0;
}else{
LED4_Display();
}
}
/*****************************************************************
**************************按键程序*********************************
****************************************************************/
void Init_anjian(void)//按键初始化
{
//使能GPIO口
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
GPIOPinTypeGPIOOutput(GPIO_PORTA_BASE,GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4);
GPIOPinTypeGPIOInput(GPIO_PORTB_BASE,GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3);
GPIOPadConfigSet(GPIO_PORTB_BASE,GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3,GPIO_STRENGTH_2MA,GPIO_PIN_TYPE_STD_WPU);
GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3,0xFF);
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4,0xFF);
}
void judge_forward_reverse(void)
{
if(judge==1){
LED[0]=0;LED[1]=10;LED[2]=10;LED[3]=10;
reverse();
return;
}
if(judge==0){
forward();
LED[0]=1;LED[1]=10;LED[2]=10;LED[3]=10;
return;
}
}
void shaomiao(i)
{
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4,0xFF);
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_2,0);
if(!GPIOPinRead(GPIO_PORTB_BASE,GPIO_PIN_0))
LED1[i]=1;
if(!GPIOPinRead(GPIO_PORTB_BASE,GPIO_PIN_1))
LED1[i]=4;
if(!GPIOPinRead(GPIO_PORTB_BASE,GPIO_PIN_2))
LED1[i]=7;
if(!GPIOPinRead(GPIO_PORTB_BASE,GPIO_PIN_3)){
jianceTime=0xFF;
judge_forward_reverse();
return;
}
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_2,GPIO_PIN_2);
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_3,0);
if(!GPIOPinRead(GPIO_PORTB_BASE,GPIO_PIN_0))
LED1[i]=2;
if(!GPIOPinRead(GPIO_PORTB_BASE,GPIO_PIN_1))
LED1[i]=5;
if(!GPIOPinRead(GPIO_PORTB_BASE,GPIO_PIN_2))
LED1[i]=8;
if(!GPIOPinRead(GPIO_PORTB_BASE,GPIO_PIN_3))
LED1[i]=0;
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_3,GPIO_PIN_3);
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_4,0);
if(!GPIOPinRead(GPIO_PORTB_BASE,GPIO_PIN_0))
LED1[i]=3;
if(!GPIOPinRead(GPIO_PORTB_BASE,GPIO_PIN_1))
LED1[i]=6;
if(!GPIOPinRead(GPIO_PORTB_BASE,GPIO_PIN_2))
LED1[i]=9;
if(!GPIOPinRead(GPIO_PORTB_BASE,GPIO_PIN_3)){
LED[0]=11;LED[1]=11;LED[2]=11;LED[3]=11;
jianceTime=0xFF;
stop();
return;
}
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_4,GPIO_PIN_4);
//按键值显示
LED[0]=LED1[i];
LED[1]=10;
LED[2]=10;
LED[3]=10;
jianceTime++;
}
void key(void)//按键主函数
{
int x,y;
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4,0);
if(!(GPIOPinRead(GPIO_PORTB_BASE,GPIO_PIN_0)&&GPIOPinRead(GPIO_PORTB_BASE,GPIO_PIN_1)&&GPIOPinRead(GPIO_PORTB_BASE,GPIO_PIN_2)&&GPIOPinRead(GPIO_PORTB_BASE,GPIO_PIN_3)))
{
SysCtlDelay(SysCtlClockGet()/100);
if(!(GPIOPinRead(GPIO_PORTB_BASE,GPIO_PIN_0)&&GPIOPinRead(GPIO_PORTB_BASE,GPIO_PIN_1)&&GPIOPinRead(GPIO_PORTB_BASE,GPIO_PIN_2)&&GPIOPinRead(GPIO_PORTB_BASE,GPIO_PIN_3)))
{
shaomiao(2-jianceTime);
}
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4,0);
while(!(GPIOPinRead(GPIO_PORTB_BASE,GPIO_PIN_0)&&GPIOPinRead(GPIO_PORTB_BASE,GPIO_PIN_1)&&GPIOPinRead(GPIO_PORTB_BASE,GPIO_PIN_2)&&GPIOPinRead(GPIO_PORTB_BASE,GPIO_PIN_3)))
{
}
}
if(jianceTime==3){
jianceTime=0xFF;
for(x=0;x<=2;x++){
LED[x]=LED1[x];
}
y=LED1[2]*100+LED1[1]*10+LED1[0];
if(y>100){
LED[0]=LED[1]=LED[2]=11;LED[3]=10;
}else if(y!=0){
zhankongbi=y;
}else{
stop();
}
}
}
/******************************************************
************************速度计算************************
*****************************************************/
void SpeedCompute(void)
{
//QEIIntDisable(QEI1_BASE,QEI_INTTIMER);
speed_signal=QEIVelocityGet(QEI1_BASE);
speed=(speed_signal*100*60)/(4*334);
speedDirectionGet=QEIDirectionGet(QEI1_BASE);
//speedDirectionGet=QEIErrorGet(QEI1_BASE);
}
/******************************************************
******************正交编码初始化*************************
*****************************************************/
void Init_QEI(void)
{
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC);
SysCtlPeripheralEnable(SYSCTL_PERIPH_QEI1);
GPIOPinTypeQEI(GPIO_PORTC_BASE,GPIO_PIN_5|GPIO_PIN_6);
GPIOPadConfigSet(GPIO_PORTC_BASE,GPIO_PIN_5|GPIO_PIN_6,GPIO_STRENGTH_2MA,GPIO_PIN_TYPE_STD_WPU);
QEIConfigure(QEI1_BASE, (QEI_CONFIG_CAPTURE_A_B|QEI_CONFIG_NO_RESET|QEI_CONFIG_QUADRATURE|QEI_CONFIG_NO_SWAP),0);
QEIVelocityConfigure(QEI1_BASE,QEI_VELDIV_1,SysCtlClockGet()/100);
QEIEnable(QEI1_BASE);
QEIVelocityEnable(QEI1_BASE);
}
/***************************************************
* *******************主函数****************************
******************************************************/
int main(void)
{
FPUEnable();
//FPULazyStackingEnable();
SysCtlClockSet(SYSCTL_SYSDIV_4|SYSCTL_USE_PLL|SYSCTL_XTAL_16MHZ|SYSCTL_OSC_MAIN);
Init_LED();
Init_anjian();
Init_PWM();
Init_QEI();
while(1)
{
key();
govern_speed();
LED_Display();
SpeedCompute();
}
}
user5748748:
回复 kaipeng chang:
正好碰到了这类问题,看了你的代码,解决了我的困惑,非常感谢