目录:
1. DMA
2.Icapture
3. pwm
4. Time
1. DMA
led.h
#include"stm32f2xx.h"
#ifndef __LED_H
#define __LED_H
void LED_Init(void);
void Delay(vu32 nCount);
void CTL_LED(u8 LED_NUM, u8 OFF_ON);
#endif
led.c
#include"led.h"
#include"stm32f2xx.h"
void LED_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
//RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
//GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11|GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //GPIO_OType_PP表示推挽方式输出,GPIO_OType_OD表示开漏
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
//GPIO_Init(GPIOF, &GPIO_InitStructure);
//GPIO_SetBits(GPIOF,GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10);
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_SetBits(GPIOD,GPIO_Pin_11|GPIO_Pin_12);
}
void Delay(vu32 nCount)
{
for(; nCount != 0; nCount--);
}
void CTL_LED(u8 LED_NUM, u8 OFF_ON)
{
switch(LED_NUM)
{
case 0:
if(OFF_ON == 1)
{
GPIO_ResetBits(GPIOD, GPIO_Pin_11);
}
else
{
GPIO_SetBits(GPIOD, GPIO_Pin_11);
}
break;
case 1:
if(OFF_ON == 1)
{
GPIO_ResetBits(GPIOD, GPIO_Pin_12);
}
else
{
GPIO_SetBits(GPIOD, GPIO_Pin_12);
}
break;
//case 2:
//GPIO_ResetBits(GPIOF, GPIO_Pin_9);
// break;
//case 3:
//GPIO_ResetBits(GPIOF, GPIO_Pin_10);
//break;
default:
//GPIO_ResetBits(GPIOF,GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10);
GPIO_ResetBits(GPIOD,GPIO_Pin_11|GPIO_Pin_12);
break;
}
}
usart_2.h
#include"stm32f2xx.h"
#include "stdio.h"
#include "stm32f2xx_conf.h"
#include"led.h"
#ifndef __USART_2_H
#define __USART_2_H
void uart_init(u32 bound);
//void USART1_IRQHandler(void);
//void USART3_IRQHandler(void);
#define USART_REC_LEN 200 //最大接收字节数 200
//#define EN_USART1_RX 1 //使能(1)/禁止(0)串口1接收
#define EN_USART3_RX 1 //使能(1)/禁止(0)串口3接收
extern u8 USART_RX_BUF[USART_REC_LEN]; //接收缓冲,最大USART_REC_LEN个字节.末字节为换行符
extern u16 USART_RX_STA; //接收状态标记
void uart_init(u32 bound);
#endif
usart_2.c
#include"stm32f2xx.h"
#include"usart_2.h"
#include"led.h"
//初始化IO 串口3
//bound:波特率
void uart_init(u32 bound)
{
GPIO_InitTypeDef GPIO_InitStructure;//首先定义一个GPIO初始化结构体
USART_InitTypeDef USART_InitStructure;//定义一个串口初始化结构体
// NVIC_InitTypeDef NVIC_InitStructure; //定义中断初始化结构
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);//串口3是在APB1总线之下的,使能串口3的时钟,在rcc.h中
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); //GPIO时钟使能
GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_USART3); //引脚复用映射,在gpio.h文件,第三个参数表示映射到什么功能
GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_USART3);//表示将pinA9映射到串口3
//GPIO端口初始化
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10|GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
//端口初始化
USART_InitStructure.USART_BaudRate = bound;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //硬件流控制
USART_InitStructure.USART_Mode = USART_Mode_Rx|USART_Mode_Tx; //收发使能
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //8位字长
USART_Init(USART3, &USART_InitStructure);//串口初始化
USART_Cmd(USART3, ENABLE); //串口使能函数
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE); //中断使能,第二个参数表示使能哪一种中断,USART_IT_RXNE表示接收非空,即接收到数据,就要产生中断
}
dma.h
#include"stm32f2xx.h"
#ifndef __DAM_H_
#define __DAM_H_
void DMA_Config(DMA_Stream_TypeDef* DMA_Streamx);
void DMA_Enable(DMA_Stream_TypeDef* DMA_Streamx);
#endif