diff --git a/HARDWARE/LED/led.c b/HARDWARE/LED/led.c index ab4a3fc..9ad8d61 100644 --- a/HARDWARE/LED/led.c +++ b/HARDWARE/LED/led.c @@ -33,6 +33,62 @@ void LED_Init(void) } +void gpio_Init(void) +{ + GPIO_InitTypeDef GPIO_InitStructure; + + // spi1 chip-select + RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);//使能GPIOF时钟 + + GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//普通输出模式 + GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出 + GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz + GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉 + GPIO_Init(GPIOC, &GPIO_InitStructure);//初始化 + + // spi2 chip-select + RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);//使能GPIOF时钟 + + GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;//普通输出模式 +// GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出 + GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz + GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉 + GPIO_Init(GPIOC, &GPIO_InitStructure);//初始化 + + + // micro_move switch + RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);//使能GPIOF时钟 + + GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1; + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;//普通输出模式 + // GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出 + GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz + GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉 + GPIO_Init(GPIOE, &GPIO_InitStructure);//初始化 + + + // position_move sensor + RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);//使能GPIOF时钟 + + GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1; + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;//普通输出模式 + // GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出 + GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz + GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉 + GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化 + + RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);//使能GPIOF时钟 + + GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;//普通输出模式 + // GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出 + GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz + GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉 + GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化 + +} diff --git a/HARDWARE/SPI/spi.c b/HARDWARE/SPI/spi.c index a4c93a1..475c117 100644 --- a/HARDWARE/SPI/spi.c +++ b/HARDWARE/SPI/spi.c @@ -10,7 +10,7 @@ //以下是SPI模块的初始化代码,配置成主机模式 //SPI口初始化 //这里针是对SPI1的初始化 -void SPI1_Init(void) +void motor_spi1_init(void) { GPIO_InitTypeDef GPIO_InitStructure; SPI_InitTypeDef SPI_InitStructure; @@ -19,16 +19,16 @@ void SPI1_Init(void) RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);//使能SPI1时钟 //GPIOFB3,4,5初始化设置 - GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5;//PB3~5复用功能输出 + GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;//PB3~5复用功能输出 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//复用功能 GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉 - GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化 + GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化 - GPIO_PinAFConfig(GPIOB,GPIO_PinSource3,GPIO_AF_SPI1); //PB3复用为 SPI1 - GPIO_PinAFConfig(GPIOB,GPIO_PinSource4,GPIO_AF_SPI1); //PB4复用为 SPI1 - GPIO_PinAFConfig(GPIOB,GPIO_PinSource5,GPIO_AF_SPI1); //PB5复用为 SPI1 + GPIO_PinAFConfig(GPIOA,GPIO_PinSource5,GPIO_AF_SPI1); //PB3复用为 SPI1 + GPIO_PinAFConfig(GPIOA,GPIO_PinSource6,GPIO_AF_SPI1); //PB4复用为 SPI1 + GPIO_PinAFConfig(GPIOA,GPIO_PinSource7,GPIO_AF_SPI1); //PB5复用为 SPI1 //这里只针对SPI口初始化 RCC_APB2PeriphResetCmd(RCC_APB2Periph_SPI1,ENABLE);//复位SPI1 @@ -83,6 +83,88 @@ u8 spi1_read_write_byte(u8 TxData) +//以下是SPI模块的初始化代码,配置成主机模式 +//SPI口初始化 +//这里针是对SPI2的初始化 +void motor_spi2_init(void) +{ + GPIO_InitTypeDef GPIO_InitStructure; + SPI_InitTypeDef SPI_InitStructure; + + RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);//使能GPIOA时钟 + RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);//使能GPIOA时钟 + + RCC_APB2PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);//使能SPI1时钟 + + GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3; + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//复用功能 + GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出 + GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz + GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉 + GPIO_Init(GPIOC, &GPIO_InitStructure);//初始化 + + GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//复用功能 + GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出 + GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz + GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉 + GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化 + + GPIO_PinAFConfig(GPIOC,GPIO_PinSource2,GPIO_AF_SPI2); //PB3复用为 SPI1 + GPIO_PinAFConfig(GPIOC,GPIO_PinSource3,GPIO_AF_SPI2); //PB4复用为 SPI1 + GPIO_PinAFConfig(GPIOB,GPIO_PinSource10,GPIO_AF_SPI2); //PB5复用为 SPI1 + + //这里只针对SPI口初始化 + RCC_APB2PeriphResetCmd(RCC_APB1Periph_SPI2,ENABLE);//复位SPI1 + RCC_APB2PeriphResetCmd(RCC_APB1Periph_SPI2,DISABLE);//停止复位SPI1 + + SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; //设置SPI单向或者双向的数据模式:SPI设置为双线双向全双工 + SPI_InitStructure.SPI_Mode = SPI_Mode_Master; //设置SPI工作模式:设置为主SPI + SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; //设置SPI的数据大小:SPI发送接收8位帧结构 + SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; //串行同步时钟的空闲状态为高电平 + SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; //串行同步时钟的第二个跳变沿(上升或下降)数据被采样 + SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; //NSS信号由硬件(NSS管脚)还是软件(使用SSI位)管理:内部NSS信号有SSI位控制 + SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256; //定义波特率预分频的值:波特率预分频值为256 + SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; //指定数据传输从MSB位还是LSB位开始:数据传输从MSB位开始 + SPI_InitStructure.SPI_CRCPolynomial = 7; //CRC值计算的多项式 + SPI_Init(SPI2, &SPI_InitStructure); //根据SPI_InitStruct中指定的参数初始化外设SPIx寄存器 + + SPI_Cmd(SPI2, ENABLE); //使能SPI外设 + + SPI1_ReadWriteByte(0xff);//启动传输 +} +//SPI1速度设置函数 +//SPI速度=fAPB2/分频系数 +//@ref SPI_BaudRate_Prescaler:SPI_BaudRatePrescaler_2~SPI_BaudRatePrescaler_256 +//fAPB2时钟一般为84Mhz: +void SPI2_SetSpeed(u8 SPI_BaudRatePrescaler) +{ + assert_param(IS_SPI_BAUDRATE_PRESCALER(SPI_BaudRatePrescaler));//判断有效性 + SPI2->CR1&=0XFFC7;//位3-5清零,用来设置波特率 + SPI2->CR1|=SPI_BaudRatePrescaler; //设置SPI1速度 + SPI_Cmd(SPI2,ENABLE); //使能SPI1 +} +//SPI1 读写一个字节 +//TxData:要写入的字节 +//返回值:读取到的字节 +u8 SPI2_ReadWriteByte(u8 TxData) +{ + + while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET){}//等待发送区空 + + SPI_I2S_SendData(SPI2, TxData); //通过外设SPIx发送一个byte 数据 + + while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET){} //等待接收完一个byte + + return SPI_I2S_ReceiveData(SPI2); //返回通过SPIx最近接收的数据 + +} + +u8 spi2_read_write_byte(u8 TxData) +{ + SPI2_ReadWriteByte(TxData); +} + diff --git a/HARDWARE/TIMER/timer.c b/HARDWARE/TIMER/timer.c index 97edaa2..0d912cb 100644 --- a/HARDWARE/TIMER/timer.c +++ b/HARDWARE/TIMER/timer.c @@ -13,6 +13,55 @@ //All rights reserved ////////////////////////////////////////////////////////////////////////////////// +void TIM1_PWM_Init(u16 arr,u16 psc) +{ + GPIO_InitTypeDef GPIO_InitStructure; + TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure; + TIM_OCInitTypeDef TIM_OCInitStructure; + + + RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE); ///使能TIM1时钟 + RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE,ENABLE); + + GPIO_PinAFConfig(GPIOE,GPIO_PinSource9,GPIO_AF_TIM1); + + GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; + GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; + GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; + GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; + GPIO_Init(GPIOE,&GPIO_InitStructure); + + GPIO_ResetBits(GPIOE, GPIO_Pin_9); + + TIM_TimeBaseInitStructure.TIM_Period = arr; //自动重装载值 + TIM_TimeBaseInitStructure.TIM_Prescaler=psc; //定时器分频 + TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up; //向上计数模式 + TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1; + + TIM_TimeBaseInit(TIM1,&TIM_TimeBaseInitStructure);//初始化TIM3 + + TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; + TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; + TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; + TIM_OCInitStructure.TIM_Pulse = 100; + + TIM_OC1Init(TIM1, &TIM_OCInitStructure); + + TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable); + + TIM_ARRPreloadConfig(TIM1,ENABLE); + + //TIM_Cmd(TIM3, ENABLE); + +} + +//定时器3中断服务函数 +void TIM1_IRQHandler(void) +{ +} + + void TIM2_Int_Init(u16 arr,u16 psc) { @@ -115,6 +164,49 @@ void TIM2_IRQHandler(void) } +void TIM2_PWM_Init(u16 arr,u16 psc) +{ + GPIO_InitTypeDef GPIO_InitStructure; + TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure; + TIM_OCInitTypeDef TIM_OCInitStructure; + + + RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE); ///使能TIM1时钟 + RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE); + + GPIO_PinAFConfig(GPIOB,GPIO_PinSource3,GPIO_AF_TIM1); + + GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; + GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; + GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; + GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; + GPIO_Init(GPIOB,&GPIO_InitStructure); + + GPIO_ResetBits(GPIOB, GPIO_Pin_9); + + TIM_TimeBaseInitStructure.TIM_Period = arr; //自动重装载值 + TIM_TimeBaseInitStructure.TIM_Prescaler=psc; //定时器分频 + TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up; //向上计数模式 + TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1; + + TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStructure);//初始化TIM3 + + TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; + TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; + TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; + TIM_OCInitStructure.TIM_Pulse = 100; + + TIM_OC1Init(TIM2, &TIM_OCInitStructure); + + TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Enable); + + TIM_ARRPreloadConfig(TIM2,ENABLE); + + //TIM_Cmd(TIM3, ENABLE); + +} + //通用定时器3中断初始化 @@ -170,3 +262,52 @@ void TIM3_PWM_Init(u16 arr,u16 psc) void TIM3_IRQHandler(void) { } + +void TIM4_PWM_Init(u16 arr,u16 psc) +{ + GPIO_InitTypeDef GPIO_InitStructure; + TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure; + TIM_OCInitTypeDef TIM_OCInitStructure; + + + RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE); ///使能TIM3时钟 + RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD,ENABLE); + + GPIO_PinAFConfig(GPIOD,GPIO_PinSource15,GPIO_AF_TIM3); + + GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15; + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; + GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; + GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; + GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; + GPIO_Init(GPIOD,&GPIO_InitStructure); + + GPIO_ResetBits(GPIOD, GPIO_Pin_15); + + TIM_TimeBaseInitStructure.TIM_Period = arr; //自动重装载值 + TIM_TimeBaseInitStructure.TIM_Prescaler=psc; //定时器分频 + TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up; //向上计数模式 + TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1; + + TIM_TimeBaseInit(TIM4,&TIM_TimeBaseInitStructure);//初始化TIM3 + + TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; + TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; + TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; + TIM_OCInitStructure.TIM_Pulse = 100; + + TIM_OC1Init(TIM4, &TIM_OCInitStructure); + + TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable); + + TIM_ARRPreloadConfig(TIM4,ENABLE); + + //TIM_Cmd(TIM3, ENABLE); + +} + +//定时器3中断服务函数 +void TIM4_IRQHandler(void) +{ +} + diff --git a/OBJ/USART.axf b/OBJ/USART.axf index fc31afa..b70baa6 100644 Binary files a/OBJ/USART.axf and b/OBJ/USART.axf differ diff --git a/OBJ/USART.build_log.htm b/OBJ/USART.build_log.htm index fe50764..a8a334d 100644 --- a/OBJ/USART.build_log.htm +++ b/OBJ/USART.build_log.htm @@ -27,11 +27,9 @@ Project File Date: 02/09/2026