stm32标准库例程
时间: 2023-08-05 14:06:49 浏览: 259
引用[1]:在STM32标准库中,有一些例程和工程模板可以供我们参考和使用。在文件目录STM32F10x_StdPeriph_Lib_V3.5.0\Project\STM32F10x_StdPeriph_Template下,存放了官方的一个库工程模板。这个模板包含了一些文件,如stm32f10x_it.c、stm32f10x_it.h、stm32f10x_conf.h和system_stm32f10x.c。其中,stm32f10x_it.c是用来编写中断服务函数的,system_stm32f10x.c包含了STM32芯片上电后初始化系统时钟、扩展外部存储器用的函数,stm32f10x_conf.h被包含进stm32f10x.h文件中。[1]
引用[3]:此外,STM32标准库还提供了一些驱动库的源代码及启动文件,可以从官网的Libraries文件夹中获取。这些源代码和启动文件非常重要,我们可以在学习和开发过程中参考这些例程,了解外设的功能和使用方法。同时,还有一个库帮助文档stm32f10x_stdperiph_lib_um.chm,其中包含了每个外设的函数说明,非常详细。[3]
综上所述,STM32标准库提供了一些例程和工程模板,以及驱动库的源代码和启动文件,可以帮助我们学习和开发STM32芯片的外设功能。
相关问题
stm32标准库sdram例程
以下是使用STM32标准库的SDRAM例程:
```c
#include "stm32f4xx.h"
#define SDRAM_BANK_ADDR ((uint32_t)0xC0000000)
void SDRAM_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
FMC_SDRAM_InitTypeDef SDRAM_InitStruct;
FMC_SDRAM_TimingTypeDef SDRAM_TimingStruct;
uint32_t tmpr = 0;
/* Enable FMC clock */
RCC_AHB3PeriphClockCmd(RCC_AHB3Periph_FMC, ENABLE);
/* Enable GPIOs clock */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_GPIOD | RCC_AHB1Periph_GPIOE |
RCC_AHB1Periph_GPIOF | RCC_AHB1Periph_GPIOG | RCC_AHB1Periph_GPIOH |
RCC_AHB1Periph_GPIOI, ENABLE);
/* Configure PC0, PC2, PC3, PC4, PC5, PC6, PC7, PC8, PC9, PC10, PC11, PC12, PD0, PD1, PD3, PD6, PD7, PD8, PD9, PD10, PD11, PD12, PD13, PD14, PD15, PD0, PD1, PD3, PD4, PD5, PD6, PD7, PD8, PD9, PD10, PD11, PD12, PD13, PD14, PD15 as alternate function */
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOC, &GPIO_InitStruct);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_3 | GPIO_Pin_6 | GPIO_Pin_7 | 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_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOD, &GPIO_InitStruct);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_7 | 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_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOE, &GPIO_InitStruct);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOF, &GPIO_InitStruct);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_10;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOG, &GPIO_InitStruct);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOH, &GPIO_InitStruct);
GPIO_InitStruct.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_Pin_9 | GPIO_Pin_10;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOI, &GPIO_InitStruct);
/* Connect TIMCLK to FMC Alternate function */
GPIO_PinAFConfig(GPIOC, GPIO_PinSource3, GPIO_AF_FMC);
/* Connect SDCLK to FMC Alternate function */
GPIO_PinAFConfig(GPIOC, GPIO_PinSource0, GPIO_AF_FMC);
/* Connect A0, A1, A2, A3, A4, A5, BA0 and BA1 to FMC Alternate function */
GPIO_PinAFConfig(GPIOF, GPIO_PinSource0, GPIO_AF_FMC);
GPIO_PinAFConfig(GPIOF, GPIO_PinSource1, GPIO_AF_FMC);
GPIO_PinAFConfig(GPIOF, GPIO_PinSource2, GPIO_AF_FMC);
GPIO_PinAFConfig(GPIOF, GPIO_PinSource3, GPIO_AF_FMC);
GPIO_PinAFConfig(GPIOF, GPIO_PinSource4, GPIO_AF_FMC);
GPIO_PinAFConfig(GPIOF, GPIO_PinSource5, GPIO_AF_FMC);
GPIO_PinAFConfig(GPIOG, GPIO_PinSource4, GPIO_AF_FMC);
GPIO_PinAFConfig(GPIOG, GPIO_PinSource5, GPIO_AF_FMC);
/* Connect D0, D1, D2, D3, D4, D5, D6, D7, D8, D9, D10, D11, D12, D13, D14 and D15 to FMC Alternate function */
GPIO_PinAFConfig(GPIOD, GPIO_PinSource14, GPIO_AF_FMC);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource15, GPIO_AF_FMC);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource0, GPIO_AF_FMC);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource1, GPIO_AF_FMC);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource8, GPIO_AF_FMC);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource9, GPIO_AF_FMC);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource10, GPIO_AF_FMC);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource11, GPIO_AF_FMC);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource12, GPIO_AF_FMC);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource13, GPIO_AF_FMC);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource4, GPIO_AF_FMC);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource5, GPIO_AF_FMC);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource7, GPIO_AF_FMC);
/* Connect NBL0 and NBL1 to FMC Alternate function */
GPIO_PinAFConfig(GPIOE, GPIO_PinSource0, GPIO_AF_FMC);
GPIO_PinAFConfig(GPIOE, GPIO_PinSource1, GPIO_AF_FMC);
/* SDRAM GPIO configuration */
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15 | GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOB, &GPIO_InitStruct);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOF, &GPIO_InitStruct);
/* Configure a clock configuration enable command */
FMC_SDRAMCommandTypeDef SDRAMCommandStruct;
SDRAMCommandStruct.FMC_CommandMode = FMC_CommandMode_CLK_Enabled;
SDRAMCommandStruct.FMC_CommandTarget = FMC_CommandTarget_SDRAM;
SDRAMCommandStruct.FMC_AutoRefreshNumber = 1;
SDRAMCommandStruct.FMC_ModeRegisterDefinition = 0;
FMC_SDRAMCmdConfig(&SDRAMCommandStruct);
/* Configure a PALL (precharge all) command */
SDRAMCommandStruct.FMC_CommandMode = FMC_CommandMode_PALL;
SDRAMCommandStruct.FMC_CommandTarget = FMC_CommandTarget_SDRAM;
SDRAMCommandStruct.FMC_AutoRefreshNumber = 1;
SDRAMCommandStruct.FMC_ModeRegisterDefinition = 0;
FMC_SDRAMCmdConfig(&SDRAMCommandStruct);
/* Configure an Auto Refresh command */
SDRAMCommandStruct.FMC_CommandMode = FMC_CommandMode_AutoRefresh;
SDRAMCommandStruct.FMC_CommandTarget = FMC_CommandTarget_SDRAM;
SDRAMCommandStruct.FMC_AutoRefreshNumber = 8;
SDRAMCommandStruct.FMC_ModeRegisterDefinition = 0;
FMC_SDRAMCmdConfig(&SDRAMCommandStruct);
/* Configure a load Mode register command */
SDRAMCommandStruct.FMC_CommandMode = FMC_CommandMode_LoadMode;
SDRAMCommandStruct.FMC_CommandTarget = FMC_CommandTarget_SDRAM;
SDRAMCommandStruct.FMC_AutoRefreshNumber = 1;
SDRAMCommandStruct.FMC_ModeRegisterDefinition = (uint32_t)SDRAM_MODEREG_BURST_LENGTH_1 |
SDRAM_MODEREG_BURST_TYPE_SEQUENTIAL |
SDRAM_MODEREG_CAS_LATENCY_2 |
SDRAM_MODEREG_OPERATING_MODE_STANDARD |
SDRAM_MODEREG_WRITEBURST_MODE_SINGLE;
FMC_SDRAMCmdConfig(&SDRAMCommandStruct);
/* Configure SDRAM timing */
SDRAM_TimingStruct.FMC_LoadToActiveDelay = 2;
SDRAM_TimingStruct.FMC_ExitSelfRefreshDelay = 7;
SDRAM_TimingStruct.FMC_SelfRefreshTime = 4;
SDRAM_TimingStruct.FMC_RowCycleDelay = 7;
SDRAM_TimingStruct.FMC_WriteRecoveryTime = 2;
SDRAM_TimingStruct.FMC_RPDelay = 2;
SDRAM_TimingStruct.FMC_RCDDelay = 2;
FMC_SDRAMInitStructure.FMC_SDRAMTimingStruct = &SDRAM_TimingStruct;
/* Configure SDRAM control configuration */
SDRAM_InitTypeDef SDRAM_InitStruct;
SDRAM_InitStruct.FMC_Bank = FMC_Bank2_SDRAM;
SDRAM_InitStruct.FMC_ColumnBitsNumber = FMC_ColumnBits_Number_8b;
SDRAM_InitStruct.FMC_InternalBankNumber = FMC_InternalBank_Number_4;
SDRAM_InitStruct.FMC_RowBitsNumber = FMC_RowBits_Number_12b;
SDRAM_InitStruct.FMC_SDMemoryDataWidth = FMC_SDMemory_Width_16b;
SDRAM_InitStruct.FMC_CASLatency = FMC_CAS_Latency_2;
SDRAM_InitStruct.FMC_WriteProtection = FMC_Write_Protection_Disable;
SDRAM_InitStruct.FMC_SDClockPeriod = FMC_SDClock_Period_2;
SDRAM_InitStruct.FMC_ReadBurst = FMC_Read_Burst_Enable;
SDRAM_InitStruct.FMC_ReadPipeDelay = FMC_ReadPipe_Delay_1;
SDRAM_InitStruct.FMC_SDClockInitiation = FMC_SDClock_Initiation_Delay_2;
FMC_SDRAMInitStructure.FMC_SDRAMStruct = &SDRAM_InitStruct;
FMC_SDRAMInit(&FMC_SDRAMInitStructure);
/* SDRAM initialization sequence */
tmpr = *(__IO uint32_t *)(SDRAM_BANK_ADDR | (0x33 << 1));
tmpr = *(__IO uint32_t *)(SDRAM_BANK_ADDR | (0x33 << 1));
tmpr = *(__IO uint32_t *)(SDRAM_BANK_ADDR | (0x33 << 1));
tmpr = *(__IO uint32_t *)(SDRAM_BANK_ADDR | (0x33 << 1));
tmpr = *(__IO uint32_t *)(SDRAM_BANK_ADDR | (0x33 << 1));
tmpr = *(__IO uint32_t *)(SDRAM_BANK_ADDR | (0x33 << 1));
tmpr = *(__IO uint32_t *)(SDRAM_BANK_ADDR | (0x33 << 1));
tmpr = *(__IO uint32_t *)(SDRAM_BANK_ADDR | (0x33 << 1));
tmpr = *(__IO uint32_t *)(SDRAM_BANK_ADDR | (0x33 << 1));
tmpr = *(__IO uint32_t *)(SDRAM_BANK_ADDR | (0x33 << 1));
tmpr = *(__IO uint32_t *)(SDRAM_BANK_ADDR | (0x33 << 1));
tmpr = *(__IO uint32_t *)(SDRAM_BANK_ADDR | (0x33 << 1));
/* Configure a load Mode register command */
SDRAMCommandStruct.FMC_CommandMode = FMC_CommandMode_LoadMode;
SDRAMCommandStruct.FMC_CommandTarget = FMC_CommandTarget_SDRAM;
SDRAMCommandStruct.FMC_AutoRefreshNumber = 1;
SDRAMCommandStruct.FMC_ModeRegisterDefinition = (uint32_t)SDRAM_MODEREG_BURST_LENGTH_1 |
SDRAM_MODEREG_BURST_TYPE_SEQUENTIAL |
SDRAM_MODEREG_CAS_LATENCY_2 |
SDRAM_MODEREG_OPERATING_MODE_STANDARD |
SDRAM_MODEREG_WRITEBURST_MODE_SINGLE;
FMC_SDRAMCmdConfig(&SDRAMCommandStruct);
}
int main(void)
{
/* Initialize SDRAM */
SDRAM_Init();
/* SDRAM test */
uint32_t *sdram_ptr = (uint32_t *)SDRAM_BANK_ADDR;
for (uint32_t i = 0; i < 0x100000; i++)
{
sdram_ptr[i] = i;
}
while (1)
{
}
}
```
这个例程初始化了SDRAM并进行了一次写入测试。你需要根据你的具体硬件配置做出一些修改,比如引脚配置和时序配置。
stm32f103标准库例程
STM32F103标准库例程是一套使用STMicroelectronics公司提供的STM32F103系列微控制器的软件示例。这些例程是为了帮助开发者快速上手并进行基本功能开发而设计的。
STM32F103标准库例程提供了各种各样的功能,例如GPIO(通用输入输出)、定时器、串口通信、SPI(串行外设接口)、I2C(串行外设接口)等。这些功能可以通过简单的配置和函数调用实现。开发者只需要了解相应的函数及其参数即可使用这些功能,无需从头编写底层驱动程序。
例如,如果想要使用GPIO控制外部设备,可以通过配置相应的引脚模式、输出电平等属性来实现。相应的示例提供了函数用于这些引脚的初始化、设置输入输出状态等。
此外,STM32F103标准库例程还提供了许多其他常用功能的示例,如ADC(模数转换器)、PWM(脉宽调制)、DMA(直接内存访问)等。这些功能可以广泛应用于不同的应用场景,例如传感器采集、电机驱动、通信接口等。
与其他类似的开发工具相比,STM32F103标准库例程的优势在于其丰富的功能和易于使用的界面。开发者只需了解相应的函数和例程文档,即可轻松实现所需功能。此外,STMicroelectronics公司还提供了丰富的技术文档和支持资源,以帮助开发者更好地理解和使用这些例程。
总的来说,STM32F103标准库例程是一套功能强大、易于使用的示例代码,适用于STM32F103系列微控制器的开发。它提供了丰富的功能和简化的开发流程,帮助开发者快速构建应用程序,提高开发效率。
阅读全文