#include "stm32f10x.h"                  // Device header
#include "usart.h"
#include <string.h>
#include <stdio.h>
#include "misc.h"
#include "stm32f10x_spi.h"
#include "stm32f10x_tim.h"
#include "ww_pwm.h"
#include "ww_spi.h"
#define  SMG_CSL()    (GPIOA->ODR &= ~(1<<4))
#define  SMG_CSH()    (GPIOA->ODR |= 1<<4)
u8 Key_Val=0,Key_Up=0,Key_Down=0,Key_Old=0;
u8 Key_SlowDown,SMG_SlowDown;
u8 Usart_Send_Flag=0;
u8 Motor_Fault_Flag,Led_Flag;
u8 const DISP_TAB[] = { 0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x00};
u8 Led_Buf[]={0,0,0,0,0,0,0,0};//高电平点亮
volatile u8 Speed=0,Led_Pos=0;
u8 Spaning_Mode,Draction_Mode;//旋转模式：0匀速旋转；1加速旋转；2减速旋转    旋转方向：1正向旋转；2反向旋转
volatile uint16_t SpeedUp_SlowDown_Delay;
uint16_t SpeedUp_SlowDown_Con=200;volatile uint16_t Led_Compare=0;
void Motor_Con(u8 Draction);
void TIM2_IRQHandler(void)
{
	TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
	if(++Led_Compare>=(500-Speed*5))
	{
		Led_Flag^=1;
		Led_Compare=0;
	}
if(++Key_SlowDown==20)Key_SlowDown=0;
	if(++SMG_SlowDown==200)SMG_SlowDown=0;
	if(++Led_Pos==8)Led_Pos=0;
	if(Spaning_Mode==1 && ++SpeedUp_SlowDown_Delay%SpeedUp_SlowDown_Con==0)
	{
		
		if(Speed<=90)
			Speed+=5;
		else
		{
			Speed=80;
			Motor_Fault_Flag=1;
			Spaning_Mode=0;
		}
		TIM1->CCR1=TIM1->CCR4=Speed;
	}
	else if(Spaning_Mode==2&& ++SpeedUp_SlowDown_Delay%SpeedUp_SlowDown_Con==0)
	{
		if(Speed>=30)
			Speed-=5;
		else
		{
			Speed=25;
			Spaning_Mode=0;
		}
		TIM1->CCR1=TIM1->CCR4=Speed;
	}
	//GPIO_WriteBit(GPIOC,GPIO_Pin_8,Bit_RESET);
		//GPIO_Write(GPIOC,~(Led_Buf[Led_Pos]<<(8+Led_Pos)) & GPIO_ReadOutputData(GPIOC));
}

void Timer2_init(void)
{
	TIM_TimeBaseInitTypeDef timerInitStructure;
	NVIC_InitTypeDef nvic;
// 启用定时器2的时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

// 配置定时器2的预分频器和自动重载寄存器

timerInitStructure.TIM_Prescaler = 71; // 预分频器值，实现 72MHz / (71 + 1) = 1MHz 的计数频率
timerInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
timerInitStructure.TIM_Period = 999; // 自动重载寄存器值，实现 1MHz / (999 + 1) = 1kHz 的中断频率
timerInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
timerInitStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM2, &timerInitStructure);

// 配置定时器2的中断
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);

// 配置定时器2的中断优先级

nvic.NVIC_IRQChannel = TIM2_IRQn;
nvic.NVIC_IRQChannelPreemptionPriority = 0;
nvic.NVIC_IRQChannelSubPriority = 0;
nvic.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&nvic);

// 启动定时器2
TIM_Cmd(TIM2, ENABLE);
}
//配置矢量中断，矢量的意思就是有顺序，有先后的意思。

void SMG_Write(u8 Speed )
{
	SMG_CSL();
	SPI1_ReadWrite(DISP_TAB[Speed%10]);
	if(Speed<10)
	{
		SPI1_ReadWrite(DISP_TAB[16]);
		SPI1_ReadWrite(DISP_TAB[16]);	
	}
	else if(Speed<100&&Speed>9)
	{
		SPI1_ReadWrite(DISP_TAB[Speed/10%10]);
		SPI1_ReadWrite(DISP_TAB[16]);	
	}
	else if(Speed>99)
	{
		SPI1_ReadWrite(DISP_TAB[Speed/10%10]);
		SPI1_ReadWrite(DISP_TAB[Speed/100%10]);
	}
	SPI1_ReadWrite(DISP_TAB[16]);
	SMG_CSH();
}
void NVIC_Configuration(void)
{
  NVIC_InitTypeDef NVIC_InitStructure;	//定义数据结构体
  
  NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0000);//将中断矢量放到Flash的0地址

  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);//设置优先级配置的模式，详情请阅读原材料中的文章

  //使能串口中断，并设置优先级
  NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;  
  NVIC_Init(&NVIC_InitStructure);	//将结构体丢到配置函数，即写入到对应寄存器中
}
u8 Key_Read()
{
	u8 Key=0,i;
	for(i=0;i<8;i++)
	{

		if(!(GPIOC->IDR & (0x0001)<<i))
		{
			Key=i+1;
		}
		if(Key)break;
	}
	return Key;
}
void Key_Proc()
{	
	if(Key_SlowDown )return ;
	Key_SlowDown=1;
	Key_Val = Key_Read();//实时读取键码值
	Key_Down = Key_Val & (Key_Old ^ Key_Val);//捕捉按键下降沿
	Key_Up = ~Key_Val & (Key_Old ^ Key_Val);//捕捉按键上降沿
	Key_Old = Key_Val;//辅助扫描变量
		switch(Key_Down)
		{
			case 1:
				SpeedUp_SlowDown_Delay=0;
			if(Speed==0)
				Speed=25;
			Motor_Con(1);
			Spaning_Mode=1;
			TIM1->CCR1=TIM1->CCR4= Speed;
			printf("姓名；孟朝阳\t学号：202111200601\t当前模式：加速正转\t当前加速度：%d\r\n",(1000- SpeedUp_SlowDown_Con)/100+2);
				break;
			case 2:
			Spaning_Mode=0;
			if(Draction_Mode!=0)
			{
				Draction_Mode=3;
				printf("姓名；孟朝阳\t学号：202111200601\t当前模式：匀速正转\t当前加速度：0\r\n");
			}
				break;
			case 3:
				SpeedUp_SlowDown_Delay=0;
			if(Speed==0)
				Speed=25;
			Motor_Con(1);
			Spaning_Mode=2;
			TIM1->CCR1=TIM1->CCR4= Speed;
			printf("姓名；孟朝阳\t学号：202111200601\t当前模式：减速正转\t当前加速度：%d\r\n",(1000- SpeedUp_SlowDown_Con)/100+2);
				break;
			case 4:
				SpeedUp_SlowDown_Delay=0;
			if(Speed==0)
				Speed =25;
			Motor_Con(2);
			Spaning_Mode=1;
			TIM1->CCR1= TIM1->CCR4=Speed;
			printf("姓名；孟朝阳\t学号：202111200601\t当前模式：加速反转\t当前加速度：%d\r\n",(1000- SpeedUp_SlowDown_Con)/100+2);
			break;
			case 5:
				SpeedUp_SlowDown_Delay=0;
			if(Speed==0)
			Speed=25;
			Motor_Con(2);
			Spaning_Mode=2;
			TIM1->CCR1= TIM1->CCR4=Speed;
			printf("姓名；孟朝阳\t学号：202111200601\t当前模式：减速反转\r\n当前加速度：%d\r\n",(1000- SpeedUp_SlowDown_Con)/100+2);
				break;
			case 6:
				Spaning_Mode=0;
			Speed=0;
			Motor_Con(0);
			Motor_Fault_Flag=0;
			Usart_Send_Flag=0;
			printf("停止旋转\r\n");
			break;
			case 7:
				if(SpeedUp_SlowDown_Con<1000)
					SpeedUp_SlowDown_Con+=100;
				else
					SpeedUp_SlowDown_Con=1000;
				printf("当前加速度：%d",(1000- SpeedUp_SlowDown_Con)/100+2 );
				break;
			case 8:
				if(SpeedUp_SlowDown_Con>200)
					SpeedUp_SlowDown_Con-=100;
				else
					SpeedUp_SlowDown_Con=200;
				printf("当前加速度：%d",(1000- SpeedUp_SlowDown_Con)/100+2 );
				break;
			default:break;
		}
}
void SMG_Proc()
{
	if(SMG_SlowDown)return;
	SMG_SlowDown=1;
	SMG_Write(Speed );
	if(!Usart_Send_Flag&&Motor_Fault_Flag)
	{
		printf("转速过高，将调整转速为：80");
		Usart_Send_Flag=1;
	}
}
void LED_Proc()
{
	if(Draction_Mode==0)
	{
		GPIO_WriteBit(GPIOC,GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10,Bit_SET);
//		GPIO_WriteBit(GPIOC,GPIO_Pin_9,Bit_SET);
//		GPIO_WriteBit(GPIOC,GPIO_Pin_10,Bit_SET);
	}
	else if(Draction_Mode==1)
	{
		GPIO_WriteBit(GPIOC,GPIO_Pin_8,Led_Flag? Bit_SET:Bit_RESET);;
		GPIO_WriteBit(GPIOC,GPIO_Pin_10 | GPIO_Pin_9,Bit_SET);
	}
	else if(Draction_Mode==2)
	{
		GPIO_WriteBit(GPIOC,GPIO_Pin_10,Led_Flag? Bit_SET:Bit_RESET);;
		GPIO_WriteBit(GPIOC,GPIO_Pin_8 | GPIO_Pin_9,Bit_SET);

	}
  else
	{
		GPIO_WriteBit(GPIOC,GPIO_Pin_9,Led_Flag? Bit_SET:Bit_RESET);;
		GPIO_WriteBit(GPIOC,GPIO_Pin_8 | GPIO_Pin_10,Bit_SET);
	}
	GPIO_WriteBit(GPIOC,GPIO_Pin_11,Motor_Fault_Flag?Bit_RESET:Bit_SET);
	
}
void Motor_Con(u8 Draction)
{
	
	if(Draction ==0)//停止
	{
		Draction_Mode=Draction;
		GPIO_ResetBits(GPIOB , GPIO_Pin_0);   
	}
	else if(Draction ==1 )//正转
	{
		Draction_Mode=Draction;
		PWM_GPIO_Configuration();
		GPIO_SetBits(GPIOB , GPIO_Pin_0); //EN1=1;
	GPIO_ResetBits(GPIOA , GPIO_Pin_11);	//IN2=0; IN1有PA8 PWM控制
	}
	else if(Draction ==2)//反转
	{
		Draction_Mode=Draction;
		PWM_GPIO_Configuration();
		GPIO_SetBits(GPIOB , GPIO_Pin_0); //EN1=1;
	GPIO_ResetBits(GPIOA , GPIO_Pin_8);	//IN1=0; IN2有PA11 PWM控制
	}
		
}
void KeyLED_GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;

// 启用GPIOC的时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);

// 配置PC0-PC7为上拉输入模式
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);

// 配置PC8-PC15为推挽输出模式
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
}

int main(void)
{
	SystemInit();
	KeyLED_GPIO_Configuration();
	
	PWM_GPIO_Configuration();
	usart_Configuration();
	PWM_TIM_Configuration();
	SPI_Configuration();
	SPI_GPIO_Configuration();
	//PWM_TIM_Configuration();
	//GPIO_SetBits(GPIOB , GPIO_Pin_0); //EN1=1;
	//GPIO_ResetBits(GPIOB , GPIO_Pin_1);	//IN2=0; IN1有PA8 PWM控制
	Timer2_init();
	GPIO_Write(GPIOC , (0xff00|GPIO_ReadInputData(GPIOC)));
	//SMG_Write(Speed );
	//SMG_Write(Speed );
	while(1)
	{
		Key_Proc();
		SMG_Proc();
		LED_Proc();
	}
}

请优化上述代码