这是我学习stm32半个月的心得,给其他人借鉴
(一)串口通讯 源码
#include "stm32f10x.h"
#include "string.h"
int sum=0;
/***********************************************************************
外设时钟使能
************************************************************************/
void RCC_Configuration(void)
{
/* 使能外设时钟 */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA| RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC |RCC_APB2Periph_GPIOE| RCC_APB2Periph_AFIO , ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2|RCC_APB1Periph_TIM3, ENABLE);
}
/***********************************************************************
GPIO口配置
************************************************************************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;//声明GPIO结构变量 GPIO_InitStructure
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;//管脚的选择 PB12灯亮配置
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//速度的选择
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP ; //推挽输出
GPIO_Init(GPIOB, &GPIO_InitStructure); //GPIOB初始化
//USART1
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // 推挽复用输出
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; // 推挽复用输出
GPIO_Init(GPIOA, &GPIO_InitStructure);
//GPIO
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; // 推挽复用输出
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
/********************************************************************
中断优先级配置
*********************************************************************/
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
// NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);//
// NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn;//
// NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;//抢占优先级别
// NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;//响应优先级别
// NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//使能
// NVIC_Init(&NVIC_InitStructure);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;//抢占优先级别
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;//响应优先级别
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//使能
NVIC_Init(&NVIC_InitStructure);
}
/*****************************************************************
USART1 初始化 baud 波特率
*****************************************************************/
void USART1_Configuration(unsigned int baud)
{
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate = baud;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_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_Init(USART1, &USART_InitStructure);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_Cmd(USART1, ENABLE);
}
/*******************************************************************************
初始化时钟晶振 72MHZ
*******************************************************************************/
void SysClock_Init(void)
{
ErrorStatus HSEStartUpStatus;
RCC_DeInit();
RCC_HSEConfig(RCC_HSE_ON);
HSEStartUpStatus = RCC_WaitForHSEStartUp();
if(HSEStartUpStatus == SUCCESS){
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
FLASH_SetLatency(FLASH_Latency_2);
RCC_HCLKConfig(RCC_SYSCLK_Div1);
RCC_PCLK2Config(RCC_HCLK_Div1);
RCC_PCLK1Config(RCC_HCLK_Div2);
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
RCC_PLLCmd(ENABLE);
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET){
;
}
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
while(RCC_GetSYSCLKSource() != 0x08){
;
}
}
}
/*******************************************************
MAIN 函数
*******************************************************/
int main(void)
{
SysClock_Init();
RCC_Configuration();
GPIO_Configuration();
NVIC_Configuration();
USART1_Configuration(9600);
// GPIO_SetBits(GPIOB, GPIO_Pin_12);
while(1)
{
;
}
}
/*******************************************************************************
USART1_IRQHandler
*******************************************************************************/
void USART1_IRQHandler(void)
{
int i,j=0,k,index=0;
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) // 如果是串口接收中断
{
if((USART_ReceiveData(USART1))!=' ') //
{
sum=sum*10+(USART_ReceiveData(USART1)-48);
}
if((USART_ReceiveData(USART1))==' ')
{
char str[5];
if(sum<10000)
{
j=10000-sum;
k=j;
for(i=0;k>0;i++) /*index保存i的位数*/
{
k = k / 10;
index++;
}
for(i=0;i<index;i++)
{
str[index-i-1]=j%10+48; /*记得加'0'*/
j=j/ 10;
}
for(i=0;i<index;i++)
{
USART_SendData(USART1, str[i]);
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
}
sum=0;
}
if(sum>10000)
{
USART_SendData(USART1,'-');
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
j=sum-10000;
k=j;
for(i=0;k>0;i++) /*index保存i的位数*/
{
k = k / 10;
index++;
}
for(i=0;i<index;i++)
{
str[index-i-1]=sum%10+48; /*记得加'0'*/
j=j/ 10;
}
for(i=0;i<index;i++)
{
USART_SendData(USART1, str[i]);
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
}
sum=0;
}
}
}
}
/*******************************************************************************
TIM3_IRQHandler
*******************************************************************************/
void TIM3_IRQHandler(void)