vl:超值型产品,stm32f100系列
xl:超高密度产品,stm32f101/103系列
ld:低密度产品,FLASH小于64K
md:中等密度产品,FLASH=64 or 128
hd:高密度产品,FLASH大于128
//#include "stm32f10x_adc.h"
//#include "stm32f10x_bkp.h"
//#include "stm32f10x_can.h"
//#include "stm32f10x_cec.h"
//#include "stm32f10x_crc.h"
//#include "stm32f10x_dac.h"
//#include "stm32f10x_dbgmcu.h"
//#include "stm32f10x_dma.h"
//#include "stm32f10x_exti.h"
#include "stm32f10x_flash.h"
//#include "stm32f10x_fsmc.h"
#include "stm32f10x_gpio.h"
//#include "stm32f10x_i2c.h"
//#include "stm32f10x_iwdg.h"
//#include "stm32f10x_pwr.h"
#include "stm32f10x_rcc.h"
//#include "stm32f10x_rtc.h"
//#include "stm32f10x_sdio.h"
//#include "stm32f10x_spi.h"
//#include "stm32f10x_tim.h"
#include "stm32f10x_usart.h"
//#include "stm32f10x_wwdg.h"
#include "misc.h"
static void RCC_Config(void)
{
static volatile ErrorStatus HSEStartUpStatus = SUCCESS;
RCC_DeInit(); //默认配置SYSCLK, HCLK, PCLK2, PCLK1, 复位后就是该配置
RCC_HSEConfig(RCC_HSE_ON); //使能外部高速晶振
HSEStartUpStatus = RCC_WaitForHSEStartUp();//等待外部高速稳定
if(HSEStartUpStatus == SUCCESS)
{
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);//使能flash预读取缓冲区
FLASH_SetLatency(FLASH_Latency_2); //令Flash处于等待状态,2是针对高频时钟的
RCC_HCLKConfig(RCC_SYSCLK_Div1); //HCLK = SYSCLK 设置高速总线时钟=系统时钟
RCC_PCLK2Config(RCC_HCLK_Div1); //PCLK2 = HCLK 设置低速总线2时钟=高速总线时钟
RCC_PCLK1Config(RCC_HCLK_Div2); //PCLK1 = HCLK/2 设置低速总线1的时钟=高速时钟的二分频
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9); //PLLCLK = 8MHz * 9 = 72 MHz 利用锁相环讲外部8Mhz晶振9倍频到72Mhz
RCC_PLLCmd(ENABLE); //使能PLL锁相环
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET){} //等待锁相环输出稳定
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); //将锁相环输出设置为系统时钟
while(RCC_GetSYSCLKSource() != 0x08){} //等待校验成功
}
//使能GPIO口所使用的时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOD|RCC_APB2Periph_GPIOE|RCC_APB2Periph_GPIOF|RCC_APB2Periph_GPIOG, ENABLE);
}
static void USART1_Config(u32 baudRate)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(COM1_RCC, ENABLE); //使能 USART1 时钟
RCC_APB2PeriphClockCmd(COM1_GPIO_RCC, ENABLE); //使能串口1引脚时钟
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //配置 USART1 的Tx 引脚类型为推挽式的
GPIO_InitStructure.GPIO_Pin = COM1_TX_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(COM1_GPIO_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//配置 USART1 的Rx 为输入悬空
GPIO_InitStructure.GPIO_Pin = COM1_RX_PIN;
GPIO_Init(COM1_GPIO_PORT, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = baudRate;//设置波特率为baudRate
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//设置数据位为8位
USART_InitStructure.USART_StopBits = USART_StopBits_1;//设置停止位为1位
USART_InitStructure.USART_Parity = USART_Parity_No; //无奇偶校验
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //没有硬件流控
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;//发送与接收
USART_ITConfig(COM1, USART_IT_RXNE, ENABLE);//接收中断使能
USART_Init(COM1,&USART_InitStructure); //串口1相关寄存器的配置
USART_Cmd(COM1,ENABLE); //使能串口1
}
#ifdef __GNUC__
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif
PUTCHAR_PROTOTYPE
{
USART_SendData(USART1, (uint8_t) ch);
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET){}
return ch;
}
static void NVIC_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0); //设置中断向量表的基地址为0x08000000
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);//设置优先级分组:先占优先级0位,从优先级4位
/*使能USART1中断*/
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; //通道设置为串口1中断
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;//中断占优先级0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //打开中断
NVIC_Init(&NVIC_InitStructure);
}
static void LED_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(LED1_GPIOX_CLK, ENABLE);//使能LED对应引脚端口的时钟
GPIO_InitStructure.GPIO_Pin = LED1_Pin ; //配置LED1为推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(LED1_GPIOX, &GPIO_InitStructure);
RCC_APB2PeriphClockCmd(LED2_GPIOX_CLK, ENABLE);
GPIO_InitStructure.GPIO_Pin = LED2_Pin ; //配置LED2为推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(LED2_GPIOX, &GPIO_InitStructure);
RCC_APB2PeriphClockCmd(LED2_GPIOX_CLK, ENABLE);
GPIO_InitStructure.GPIO_Pin = LED3_Pin ; //配置LED3为推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(LED3_GPIOX, &GPIO_InitStructure);
RCC_APB2PeriphClockCmd(LED4_GPIOX_CLK, ENABLE);
GPIO_InitStructure.GPIO_Pin = LED4_Pin ; //配置LED1为推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(LED4_GPIOX, &GPIO_InitStructure);
LED1_OFF();
LED2_OFF();
LED3_OFF();
LED4_OFF();
}
void Delay_us(u32 nus)
{
SysTick->LOAD=nus*9; //时间加载
SysTick->CTRL|=0x01; //开始倒数
while(!(SysTick->CTRL&(1<<16)));//等待时间到达
SysTick->CTRL=0X00000000; //关闭计数器
SysTick->VAL=0X00000000; //清空计数器
}
void Delay_ms(u16 nms)
{
SysTick->LOAD=(u32)nms*9000;//给重装载寄存器赋值,9000时,将产生1ms的时基
SysTick->CTRL|=0x01; //开始倒数
while(!(SysTick->CTRL&(1<<16))); //等待时间到达
SysTick->CTRL=0X00000000; //关闭计数器
SysTick->VAL=0X00000000; //清空计数器
}
void BSP_Init(void)
{
RCC_Config();
USART1_Config(115200);
LED_Config();
NVIC_Config();
}
#ifndef __BSP_H__
#define __BSP_H__
#include "stm32f10x.h"
#include
/*----------printf调试选项----------*/
#define _DEBUG 1
#if _DEBUG
#define PRINTF(fmt, ...) printf(fmt, ## __VA_ARGS__)
#else
#define PRINTF(fmt, ...)
#endif
/*----------串口1相关定义----------*/
#define COM1 USART1
#define COM1_RCC RCC_APB2Periph_USART1
#define COM1_GPIO_RCC RCC_APB2Periph_GPIOA
#define COM1_GPIO_PORT GPIOA
#define COM1_TX_PIN GPIO_Pin_9
#define COM1_RX_PIN GPIO_Pin_10
/*----------串口2相关定义----------*/
#define COM2_USART USART2
#define COM2_RCC RCC_APB1Periph_USART2
#define COM2_GPIO_RCC RCC_APB2Periph_GPIOA
#define COM2_GPIO_PORT GPIOA
#define COM2_TX_PIN GPIO_Pin_2
#define COM2_RX_PIN GPIO_Pin_3
/*----------串口3相关定义----------*/
#define COM3 USART3
#define COM3_RCC RCC_APB1Periph_USART3
#define COM3_GPIO_RCC RCC_APB2Periph_GPIOA
#define COM3_GPIO_PORT GPIOB
#define COM3_TX_PIN GPIO_Pin_10
#define COM3_RX_PIN GPIO_Pin_11
/*----------LED1相关定义----------*/
#define LED1_Pin GPIO_Pin_6
#define LED1_GPIOX GPIOF
#define LED1_GPIOX_CLK RCC_APB2Periph_GPIOF
#define LED1_ON() {LED1_GPIOX->BRR = LED1_Pin;}
#define LED1_OFF() {LED1_GPIOX->BSRR = LED1_Pin;}
#define LED1_Toggle() {LED1_GPIOX->ODR = (LED1_GPIOX->ODR)^LED1_Pin;}
/*----------LED2相关定义----------*/
#define LED2_Pin GPIO_Pin_7
#define LED2_GPIOX GPIOF
#define LED2_GPIOX_CLK RCC_APB2Periph_GPIOF
#define LED2_ON() {LED2_GPIOX->BRR = LED2_Pin;}
#define LED2_OFF() {LED2_GPIOX->BSRR = LED2_Pin;}
#define LED2_Toggle() {LED2_GPIOX->ODR = (LED2_GPIOX->ODR)^LED2_Pin;}
/*----------LED3相关定义----------*/
#define LED3_Pin GPIO_Pin_8
#define LED3_GPIOX GPIOF
#define LED3_GPIOX_CLK RCC_APB2Periph_GPIOF
#define LED3_ON() {LED3_GPIOX->BRR = LED3_Pin;}
#define LED3_OFF() {LED3_GPIOX->BSRR = LED3_Pin;}
#define LED3_Toggle() {LED3_GPIOX->ODR = (LED3_GPIOX->ODR)^LED3_Pin;}
/*----------LED4相关定义---------*/
#define LED4_Pin GPIO_Pin_9
#define LED4_GPIOX GPIOF
#define LED4_GPIOX_CLK RCC_APB2Periph_GPIOF
#define LED4_ON() {LED4_GPIOX->BRR = LED4_Pin;}
#define LED4_OFF() {LED4_GPIOX->BSRR = LED4_Pin;}
#define LED4_Toggle() {LED4_GPIOX->ODR = (LED4_GPIOX->ODR)^LED4_Pin;}
void BSP_Init(void);
void Delay_us(u32 nus);
void Delay_ms(u16 nms);
#endif
#include "stm32f10x_it.h"
#include
void HardFault_Handler(void)
{
printf("Hard error!!!\r\n");
NVIC_SystemReset(); //软件复位
}
void USART1_IRQHandler(void)
{
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
USART_SendData(USART1,USART_ReceiveData(USART1));//发送收到的字节
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);//等待发送完成
}
}
#include "stm32f10x.h"
#include "BSP.h"
int main(void)
{
BSP_Init();
PRINTF("\nmain() is running!\r\n");
while(1)
{
LED1_Toggle();
Delay_ms(1000);
}
}
上一篇:STM32 IO口模拟串口通讯
下一篇:STM32 SHT10温湿度传感器的信号采集
推荐阅读最新更新时间:2024-03-16 15:14