STM32F207下的实验(2)

F盘中workspace文件下的是配合STM32F207做的一些基本实验,其中Template是模板文件夹(也叫固件库)
包括
蜂鸣器Beep实验、
Can通信实验、
Pwm(占空比可变的脉冲波形)输出实验、
DMA传输数据实验、
Led灯点亮实验,
usart串口通信实验(通过串口线连接pc与开发板,在串口调试助手端输入指定指令,可控制开发板上的引脚)、
KEYS按键控制实验(开发板上有一些开关,通过控制开关,可控制led亮灭等实验)、
EXIT外部中断实验、
Icapture捕获实验(捕获按下按键的操作,可用来记录两次按键中间的时间间隔)、

TIME计时器实验

1. beep

beep.h

#include"stm32f2xx.h"

#ifndef __BEEP_H

#define __BEEP_H

void BEEP_Init(void);

void BEEP_on(void);

void BEEP_OFF(void);

void Delay(vu32 nCount);
	
#endif

beep.c

#include"beep.h"
#include"stm32f2xx.h"

void BEEP_Init(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF,ENABLE);
		
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
	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_DOWN;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	
	GPIO_Init(GPIOF,&GPIO_InitStructure);
	GPIO_SetBits(GPIOF,GPIO_Pin_6);
}

void BEEP_on(void)
{
  GPIOF-> BSRRH = GPIO_Pin_6; //BSRRH和BSRRL代表BSRR的高16位和低16位
}

void BEEP_OFF(void)
{
  GPIOF-> BSRRL = GPIO_Pin_6;
}

void Delay(vu32 nCount)
{
  for(; nCount != 0; nCount--);
}

main.c

#include"beep.h"
#include"stm32f2xx.h"

u8 count=0;

int main()
{
    BEEP_Init();
    //BEEP_on();
    //Delay(0x800000);

	
	while(1)
	{
	    BEEP_on();
	    Delay(0x800000);
	    BEEP_OFF();
	    Delay(0x800000);
	}
}

2. LED

led.h

#include"stm32f2xx.h"
#ifndef __LED_H

#define __LED_H

void LED_Init(void);

void Delay(vu32 nCount);

void Turn_On_LED(u8 LED_NUM);
	
#endif

led.c

#include"led.h"
#include"stm32f2xx.h"

void LED_Init(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);
		
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10;
	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);
}

void Delay(vu32 nCount)
{
  for(; nCount != 0; nCount--);
}

void Turn_On_LED(u8 LED_NUM)
{
  switch(LED_NUM)
	{
	  case 0:
			GPIO_ResetBits(GPIOF, GPIO_Pin_7);
		    break;
		case 1:
			GPIO_ResetBits(GPIOF, GPIO_Pin_8);
		    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);
		    break;
	}
}

main.c

#include"led.h"
#include"stm32f2xx.h"

u8 count=0;

int main()
{
    LED_Init();
    while(1)
    {
		GPIO_SetBits(GPIOF,GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10);
		Turn_On_LED(count%4);
		count++;
		Delay(0x2FFFFF);
        GPIO_ResetBits(GPIOF, GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10);
    }
}

3. keys

key.h

#include"stm32f2xx.h"
#ifndef __KEY_H

#define __KEY_H

void KEY_Init(void);

void LED_Init(void);

	
#endif

key.c

#include"key.h"
#include"stm32f2xx.h"

#define KEY1  GPIO_Pin_5
#define KEY2  GPIO_Pin_11
#define KEY3  GPIO_Pin_13
#define KEY4  GPIO_Pin_0

#define KEY1_press 1
#define KEY2_press 2
#define KEY3_press 3
#define KEY4_press 4

void KEY_Init(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	
	//首先对按键1进行初始化,按键1是A型的
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
		
	GPIO_InitStructure.GPIO_Pin = KEY1;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; 
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
	GPIO_Init(GPIOA,&GPIO_InitStructure);
	
	//对按键2进行初始化,按键2是F型的
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF,ENABLE);
		
	GPIO_InitStructure.GPIO_Pin = KEY2;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; 
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
	GPIO_Init(GPIOF,&GPIO_InitStructure);
	
	//对按键3进行初始化,按键3是C型的
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC,ENABLE);
		
	GPIO_InitStructure.GPIO_Pin = KEY3;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; 
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
	GPIO_Init(GPIOC,&GPIO_InitStructure);
	
	//对按键4进行初始化,按键4是A型的
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
		
	GPIO_InitStructure.GPIO_Pin = KEY4;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; 
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
	GPIO_Init(GPIOA,&GPIO_InitStructure);
	
	GPIO_SetBits(GPIOF,GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10);
}

void LED_Init(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF,ENABLE);
		
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10;
	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);
}

main.c

#include"key.h"
#include"stm32f2xx.h"

#define KEY1  GPIO_Pin_5
#define KEY2  GPIO_Pin_11
#define KEY3  GPIO_Pin_13
#define KEY4  GPIO_Pin_0

#define KEY1_press 1
#define KEY2_press 2
#define KEY3_press 3
#define KEY4_press 4

int main()
{
	int Key;
	KEY_Init();
  LED_Init();
	
  while(1)
  {
		//GPIO_SetBits(GPIOA, GPIO_Pin_5|GPIO_Pin_0);
		//GPIO_SetBits(GPIOF, GPIO_Pin_11);
		//GPIO_SetBits(GPIOC, GPIO_Pin_13);
		
	  GPIO_SetBits(GPIOA, KEY1|KEY4);
		GPIO_SetBits(GPIOF, KEY2);
		GPIO_SetBits(GPIOC, KEY3);
		GPIO_SetBits(GPIOF, GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10);
		
		switch(Key)
		{
		  case 1:
				GPIO_ResetBits(GPIOF, GPIO_Pin_10);
			  break;
			case 2:
				GPIO_ResetBits(GPIOF, GPIO_Pin_9);
			  break;
			case 3:
				GPIO_ResetBits(GPIOF, GPIO_Pin_8);
			  break;
			case 4:
				GPIO_ResetBits(GPIOF, GPIO_Pin_7);
			  break;
			default:
				break;	
		}
		
    //GPIO_ResetBits(GPIOF,GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10);
  }
}

4. usart_1

usart_1.h

#include"key.h"
#include"stm32f2xx.h"

#define KEY1  GPIO_Pin_5
#define KEY2  GPIO_Pin_11
#define KEY3  GPIO_Pin_13
#define KEY4  GPIO_Pin_0

#define KEY1_press 1
#define KEY2_press 2
#define KEY3_press 3
#define KEY4_press 4

int main()
{
	int Key;
	KEY_Init();
  LED_Init();
	
  while(1)
  {
		//GPIO_SetBits(GPIOA, GPIO_Pin_5|GPIO_Pin_0);
		//GPIO_SetBits(GPIOF, GPIO_Pin_11);
		//GPIO_SetBits(GPIOC, GPIO_Pin_13);
		
	  GPIO_SetBits(GPIOA, KEY1|KEY4);
		GPIO_SetBits(GPIOF, KEY2);
		GPIO_SetBits(GPIOC, KEY3);
		GPIO_SetBits(GPIOF, GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10);
		
		switch(Key)
		{
		  case 1:
				GPIO_ResetBits(GPIOF, GPIO_Pin_10);
			  break;
			case 2:
				GPIO_ResetBits(GPIOF, GPIO_Pin_9);
			  break;
			case 3:
				GPIO_ResetBits(GPIOF, GPIO_Pin_8);
			  break;
			case 4:
				GPIO_ResetBits(GPIOF, GPIO_Pin_7);
			  break;
			default:
				break;	
		}
		
    //GPIO_ResetBits(GPIOF,GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10);
  }
}

usart1_t.c

#include"stm32f2xx.h"
#include"usart_1.h"

void My_USART1_Init(void)
{
	GPIO_InitTypeDef  GPIO_InitStructure;//首先定义一个GPIO初始化结构体
	USART_InitTypeDef USART_InitStructure;//定义一个串口初始化结构体
	NVIC_InitTypeDef NVIC_InitStructure; //定义中断初始化结构
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);//串口1是在APB2总线之下的,使能串口1的时钟,在rcc.h中
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); //GPIO时钟使能
	
	GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1); //引脚复用映射,在gpio.h文件,第三个参数表示映射到什么功能
	GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1);//表示将pinA9映射到串口1
	
	//GPIO端口初始化
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9|GPIO_Pin_10;
    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(GPIOA, &GPIO_InitStructure);
	
	//端口初始化
	USART_InitStructure.USART_BaudRate = 115200;
	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(USART1, &USART_InitStructure);//串口初始化
	USART_Cmd(USART1, ENABLE); //串口使能函数
	
	USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //中断使能,第二个参数表示使能哪一种中断,USART_IT_RXNE表示接收非空,即接收到数据,就要产生中断
	
	//初始化中断函数
	NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; //设置中断通道,串口1中断
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;   //使能
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;  //设置响应优先级
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;  //设置抢占优先级
	NVIC_Init(&NVIC_InitStructure); //根据上面制定的参数初始化NVIC寄存器,在FWLIB的misc.c中
	
}

//中断服务函数,在启动文件startup_stm32。。。无返回值,无参数
void USART1_IRQHandler(void)            
{
	u8 res;
	//先判断相应的状态位,这里判断串口1是否为上面的接收中断,若是,返回1,if结果为真
	if(USART_GetITStatus(USART1,USART_IT_RXNE)) //表示串口1已经接收到了数据
	{	
		res = USART_ReceiveData(USART1); //串口的接收函数
		USART_SendData(USART1, res);  //把接收到的数据发送出去,把res发送
	}
}

main.c

#include"usart_1.h"
#include"stm32f2xx.h"

int main(void)
{
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置中断优先级分组
	My_USART1_Init();  //在主函数中调用初始化函数,以初始化串口1
	while(1); //等待其他设备给STM32发送数据
	
}

5. usart_2

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"


//下面是printf输出函数

#if 1
#pragma import(__use_no_semihosting)             
//标准库需要的支持函数                 
struct __FILE 
{ 
	int handle; 
}; 

FILE __stdout;       
//定义_sys_exit()以避免使用半主机模式    
_sys_exit(int x) 
{ 
	x = x; 
} 
//重定义fputc函数 
int fputc(int ch, FILE *f)
{ 	
	//while((USART1->SR&0X40)==0);//循环发送,直到发送完毕   
	//USART1->DR = (u8) ch;     
	while((USART3->SR&0X40)==0);//循环发送,直到发送完毕   
	USART3->DR = (u8) ch;  
	return ch;
}
#endif


#if EN_USART3_RX  
u8 USART_RX_BUF[USART_REC_LEN];     //接收缓冲,最大USART_REC_LEN个字节.
//接收状态
//bit15,	接收完成标志
//bit14,	接收到0x0d
//bit13~0,	接收到的有效字节数目
u16 USART_RX_STA = 0;       //接收状态标志,16位变量


//初始化IO 串口1 
//bound:波特率

void uart_init(u32 bound)
{
	GPIO_InitTypeDef  GPIO_InitStructure;//首先定义一个GPIO初始化结构体
	USART_InitTypeDef USART_InitStructure;//定义一个串口初始化结构体
	NVIC_InitTypeDef NVIC_InitStructure; //定义中断初始化结构
	
	//RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);//串口1是在APB2总线之下的,使能串口1的时钟,在rcc.h中
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);//串口3是在APB1总线之下的,使能串口3的时钟,在rcc.h中
	//RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); //GPIO时钟使能
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); //GPIO时钟使能

	
	//GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1); //引脚复用映射,在gpio.h文件,第三个参数表示映射到什么功能
	//GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1);//表示将pinA9映射到串口1
    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(GPIOA, &GPIO_InitStructure);
    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(USART1, &USART_InitStructure);//串口初始化
	//USART_Cmd(USART1, ENABLE); //串口使能函数
	USART_Init(USART3, &USART_InitStructure);//串口初始化
	USART_Cmd(USART3, ENABLE); //串口使能函数
	
	//USART_ITConfig(USART1, USART_IT_RXNE,ENABLE); //中断使能,第二个参数表示使能哪一种中断,USART_IT_RXNE表示接收非空,即接收到数据,就要产生中断
	USART_ITConfig(USART3, USART_IT_RXNE, ENABLE); //中断使能,第二个参数表示使能哪一种中断,USART_IT_RXNE表示接收非空,即接收到数据,就要产生中断

	//初始化中断函数
	//NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; //设置中断通道,串口1中断
	NVIC_InitStructure.NVIC_IRQChannel= USART3_IRQn; //设置中断通道,串口3中断
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;   //使能
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;  //设置响应优先级
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;  //设置抢占优先级
	NVIC_Init(&NVIC_InitStructure); //根据上面制定的参数初始化NVIC寄存器,在FWLIB的misc.c中
	
}

//中断服务函数,在启动文件startup_stm32。。。无返回值,无参数
//void USART1_IRQHandler(void)                	//串口1中断服务程序
void USART3_IRQHandler(void)                    //串口3中断服务程?

{
	u16 Res;
	
	if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET)  //接收中断(接收到的数据必须是0x0d 0x0a结尾)
	{
	    Res = USART_ReceiveData(USART3);//读取接收到的数
		if((USART_RX_STA&0x8000) == 0)//0表示接收未完成
		{
			
			if(USART_RX_STA&0x4000)//接收到了0x0d,判断上一次接收的是否为0x0d
			{
				if(Res!=0x0a)
				    USART_RX_STA = 0;//接收错误,重新开始,上一次接收为0x0d,判断这一次接收的是否为0x0a
				else USART_RX_STA|=0x8000;	//接收完成了 ,若这次接收的是0x0a,则将最高位标记为1,表示接收完成
			}
			else //还没收到0X0D,上次没有接收到0x0d,判断这次是否接收到
			{	
				if(Res == 0x0d)
				    USART_RX_STA|=0x4000; //若这次接收到0x0d,再判断是否接收到0x0a
				else  //若两次都没有接收到0x0d,则说明接收到的是有效数据,将其保存到buf中
				{
					USART_RX_BUF[USART_RX_STA&0X3FFF] = Res ;
					USART_RX_STA++; //有效数据个数加1
					if(USART_RX_STA > (USART_REC_LEN-1))
					    USART_RX_STA = 0;//接收数据错误,重新开始接收	  
				}		 
			}
		}  
		else
		{
			//CTL_LED(1, 1);
		}
  } 
}
#endif	

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;
	}
}

main.c

#include"usart_2.h"
#include"stm32f2xx.h"
#include"led.h"
#include"delay.h"

int main(void)
{ 
 
	u8 t;
	u8 len;	
	u16 times = 0;  
	//u8 count = 0;
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置系统中断优先级分组2
	Delay(168);		    //延时初始化 
	uart_init(115200);	//串口初始化波特率为115200
	LED_Init();		  	//初始化与LED连接的硬件接口

	while(1)
	{
	
		if(USART_RX_STA&0x8000) //不停判断最高位数据是否为1
		{					   
			len = USART_RX_STA&0x3fff;//得到此次接收到的数据长度
			
			printf("\r\n发送的信息为:\r\n");
			for(t = 0; t < len; t++)
			{
				USART_SendData(USART3, USART_RX_BUF[t]);         //向串口3发送数据
				while(USART_GetFlagStatus(USART3, USART_FLAG_TC) != SET);//等待发送结束
			}
			printf("\r\n\r\n");//插入换行
			USART_RX_STA = 0;  //发送结束后,将sta设为0,方便下次接收
		}
		else  //若接收未完成,则不断打印一些提示信息
		{
	
			times++;
			
			if(times%5000 == 0)
			{
				printf("\r\n serial port\r\n");
			}
			if(times%200 == 0)
			{
			    printf("input data, press enter end!\r\n");  
			}
			if(times%30 == 0)
			{
				CTL_LED(0, 1);  //点亮LED灯0,提示系统正在运行  
			}
			
			//delay_ms(10); 
			Delay(168000);
	
		
		}
	  
	}
}

6. Exti

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_GPIOD, ENABLE);
	
	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(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; 
			default: 
				GPIO_ResetBits(GPIOD,GPIO_Pin_11|GPIO_Pin_12);
		    break;
		}
}

exti.h

#include"stm32f2xx.h"

#ifndef __EXTI_H

#define __EXTI_H

void EXTIX_Init(void);

void EXTI0_IRQHandler(void);

void EXTI1_IRQHandler(void);

#endif

exti.c

#include"stm32f2xx.h"
#include"EXTI.h"
#include"key.h"
#include"led.h"

void EXTIX_Init(void)
{
    EXTI_InitTypeDef EXTI_InitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;  //中断分组结构
    
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);  //使能syscfg时钟,为什么用SYSCFG时钟
    
    KEY_Init();

    SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOE, EXTI_PinSource0); //设置IO口与中断线的映射关系,PE0连接到中断线0
	SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOE, EXTI_PinSource1);  //PE1连接到中断线1
	
	//对外部中断的中断线进行初始化
    EXTI_InitStructure.EXTI_Line = EXTI_Line0|EXTI_Line1;   //中断线0和1
    EXTI_InitStructure.EXTI_LineCmd = ENABLE;      //是否使能
    EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;         //模式,中断模式,event是事件
    EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling;    //触发方式,下降沿方式表示需要下拉,下拉时电平拉低
    //EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
	EXTI_Init(&EXTI_InitStructure);  //初始化线上中断,设置触发条件等

	NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;  //0,1,2,3
    NVIC_Init(&NVIC_InitStructure);   //配置中断分组,并使能中断
		
    NVIC_InitStructure.NVIC_IRQChannel = EXTI1_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //0,1,2,3
    NVIC_Init(&NVIC_InitStructure);   //配置中断分组,并使能中断
}

void EXTI0_IRQHandler(void) //中断服务函数0
{
	Delay(10);  //消抖
	if(KEY0 == 0)
	{
		CTL_LED(0, 1);  //这里打开开关6时,led0灯0亮
	}

	EXTI_ClearITPendingBit(EXTI_Line0);  //清除中断标志位
}

void EXTI1_IRQHandler(void) //中断服务函数1
{
	Delay(10);
	if(KEY1 == 0)
	{
		CTL_LED(0, 0);  //这里打开开关5时,led0灯灭
		//CTL_LED(1, 1);  //这里打开开关5时,led1灯亮
	}

	EXTI_ClearITPendingBit(EXTI_Line1);  //清除LINE1上的中断标志位
}

key.h

#include"stm32f2xx.h"

#ifndef __KEY_H

#define __KEY_H

void KEY_Init(void);

#define KEY0  GPIO_ReadInputDataBit(GPIOE, GPIO_Pin_0)
#define KEY1  GPIO_ReadInputDataBit(GPIOE, GPIO_Pin_1)

#endif

key.c

#include"key.h"
#include"stm32f2xx.h"

void KEY_Init(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	
 	//对pe0~pe5进行初始化
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
		
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; 
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
	GPIO_Init(GPIOE, &GPIO_InitStructure);
	
	//GPIO_SetBits(GPIOE, GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5);
}

main.c

#include"key.h"
#include"stm32f2xx.h"
#include"led.h"
#include"EXTI.h"

int main()
{
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	Delay(168);
	KEY_Init();
	LED_Init();
	EXTIX_Init();

	while(1)
	{
		//GPIO_SetBits(GPIOE, GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6);
		//Delay(168);
	}
}

猜你喜欢

转载自blog.csdn.net/juliarjuliar/article/details/79785019