/***********************************************************************
火牛开发板基础实验
跑马灯
************************************************************************/
#include "stm32f10x.h"
/***********************************************************************
外设时钟使能
************************************************************************/
void RCC_Configuration(void)
{
/* 使能外设时钟 */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 |
RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO, ENABLE); //使能或者失能APB2外设时钟,
//RCC_APB2Periph_USART1//USART1时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); //RCC_APB2Periph_GPIOA//GPIOB时钟
}
/*******************************************************************************
GPIO的配置
*******************************************************************************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;//定义一个结构体变量
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;//选择GPIO口的值
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;//推挽输出//推挽输出:可以输出高,低电平,连接数字器件;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//输出速率
GPIO_Init(GPIOB, &GPIO_InitStructure);//把设置写入寄存器,GPIOB口初始化完成
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;//选择GPIO口的值
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//上拉输入
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//输入速率
GPIO_Init(GPIOA, &GPIO_InitStructure);//把设置写入寄存器,GPIOA口初始化完成
}
/*******************************************************************************
中断优先级初始化
*******************************************************************************/
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;//NXIC初始化结构体定义
#ifdef VECT_TAB_RAM
NVIC_SetVectorTable(NVIC_VectTab_RAM,0x0);//设置向量表的位置和偏移//向量表在RAM,偏移量为0
#else
NVIC_SetVectorTable(NVIC_VectTab_FLASH,0x0);//向量表在FLASH,偏移量为0
#endif
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);//优先级分组,先占优先级1位,从优先级3位
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn; //指定外部中断0为IRQ通道,新库的版本为IRQn,旧库版本为IRQChannel
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;//先占优先级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;//从优先级级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//使能
NVIC_Init(&NVIC_InitStructure);//初始化完成
//
//// NVIC_InitStructure.NVIC_IRQChannel = EXTI1_IRQChannel;//
//// NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;//
//// NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;//
//// NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//
//// NVIC_Init(&NVIC_InitStructure);//
}
/*******************************************************************************
外部中断初始化
*******************************************************************************/
void EXTI_Configuration(void)//PA0,1作为EXTI中断线0,1的中断源输入
{
EXTI_InitTypeDef EXTI_InitStructure; //EXTI初始化结构定义
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource0);//选择用作外部中断源的GPIO端口为GPIOA_0
EXTI_InitStructure.EXTI_Line = EXTI_Line0;//外部中断线
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;//线路为外部中断请求
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;//下降沿触发
EXTI_InitStructure.EXTI_LineCmd = ENABLE;//使能
EXTI_Init(&EXTI_InitStructure);//初始化完成
//
//// GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource1);
//// EXTI_InitStructure.EXTI_Line = EXTI_Line1;
//// EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
//// EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
//// EXTI_InitStructure.EXTI_LineCmd = ENABLE;
//// EXTI_Init(&EXTI_InitStructure);
}
/*******************************************************************************
串口初始化
*******************************************************************************/
//void USART_Configuration(void)
//{
// USART_InitTypeDef USART_InitStructure;
// USART_InitStructure.USART_BaudRate = 9600;//波特率的设置
// USART_InitStructure.USART_WordLength = USART_WordLength_8b;//一帧传送或接收到的数据位
// 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_Tx | USART_Mode_Rx;//发送,接收使能
// USART_InitStructure.USART_Clock = USART_Clock_Disable;//USART时钟低电平使能,技能发送又能接收
// USART_InitStructure.USART_CPOL = USART_CPOL_High;//SCLK时钟输出极性是高电平
// USART_InitStructure.USART_CPHA = USART_CPHA_1Edge;//时钟数据第一个边沿进行捕获
// USART_InitStructure.USART_LastBit = USART_LastBit_Disable;//最后一位数据的时钟脉冲不从SCLK输出
// USART_Init(USART1, &USART_InitStructure);
//}
/*******************************************************************************
初始化时钟晶振 72MHZ
*******************************************************************************/
void SysClock_Init(void)
{
ErrorStatus HSEStartUpStatus;//定义一个枚举体变量//SUCCESS:HSE晶振稳定且就绪,ERROR:HSE晶振未就绪
RCC_DeInit();//将外设RCC寄存器重设为缺省值
RCC_HSEConfig(RCC_HSE_ON);//设置外部高速晶振(HSE)
HSEStartUpStatus = RCC_WaitForHSEStartUp();//等待HSE起振,返回一个ErrorStatus枚举值,该函数将等待直到HSE就绪,或者在超时的情况下退出
if(HSEStartUpStatus == SUCCESS){
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);//使能或者失能预取指缓存,FLASH_PrefetchBuffer_Enable//预取指缓存使能
FLASH_SetLatency(FLASH_Latency_2);//设置代码延时值,FLASH_Latency_2//2延时周期
RCC_HCLKConfig(RCC_SYSCLK_Div1);//设置AHB时钟(HCLK),RCC_SYSCLK_Div1//AHB时钟 = 系统时钟
RCC_PCLK2Config(RCC_HCLK_Div1);//设置高速AHB时钟(PCLK2),RCC_HCLK_Div1//APB2时钟 = HCLK
RCC_PCLK1Config(RCC_HCLK_Div2);//设置低速AHB时钟(PCLK1),RCC_HCLK_Div2//APB1时钟 = HCLK / 2
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);//设置PLL时钟源及倍频系数,RCC_PLLSource//PLL的输入时钟源,RCC_PLLMul//PLL倍频系数,使PLL输出时钟频率不超过72 MHz
RCC_PLLCmd(ENABLE);//使能或者失能PLL,PLL新状态,这个参数可以取:ENABLE或者DISABLE,如果PLL被用于系统时钟,,那么它不能被失能
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET){
;
}//RCC_GetFlagStatus(RCC_FLAG_PLLRDY)//检查指定的RCC标志位设置与否,RCC_FLAG_PLLRDY//PLL就绪
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);//设置系统时钟(SYSCLK),RCC_SYSCLKSource: 用作系统时钟的时钟源,
while(RCC_GetSYSCLKSource() != 0x08){
;
} //RCC_GetSYSCLKSource()//返回用作系统时钟的时钟源
}
}
/******************************************
*
* 延时程序 ms
*
****************************************/
void delay_ms(u16 time)
{
u16 i=0;
while(time--)
{
i=12000; //自己定义
while(i--) ;
}
}
/*******************************************************
LED 函数
*******************************************************/
//void LED()
//{
// if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0)==0)
// {
//// delay_ms(5);
//// if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0)==0)
//// {
// GPIO_ResetBits(GPIOB,GPIO_Pin_12);
// delay_ms(500);
//// GPIO_SetBits(GPIOB,GPIO_Pin_12);
//// delay_ms(500);
//// }
// }
// else
// GPIO_ResetBits(GPIOB,GPIO_Pin_12);
//// delay_ms(1000);
//}
/*******************************************************
MAIN 函数
*******************************************************/
int main(void)
{
SysClock_Init();
RCC_Configuration();
GPIO_Configuration();
NVIC_Configuration();
EXTI_Configuration();
GPIO_SetBits(GPIOB,GPIO_Pin_12);//灭
while(1)
{
// LED();
;
}
}
/*********************************************************
中断服务程序
*********************************************************/
void EXTI0_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line0) == SET)//检查指定的EXTI线路触发请求发生与否,返回一个EXTI_Line新状态
{
EXTI_ClearITPendingBit(EXTI_Line0);//清除线路挂起位
EXTI_ClearFlag(EXTI_Line0);//清除中断挂起位
GPIO_ResetBits(GPIOB,GPIO_Pin_12);//点亮LED
delay_ms(5000);//延时
// GPIO_SetBits(GPIOB,GPIO_Pin_12);//灭
}
}
////void EXTI1_IRQHandler(void)
////{
//// if(EXTI_GetITStatus(EXTI_Line1) == SET)
//// {
//// EXTI_ClearITPendingBit(EXTI_Line1);
//// EXTI_ClearFlag( EXTI_Line1);
//// GPIO_SetBits(GPIOB,GPIO_Pin_12);
//// delay(500);
//// GPIO_ResetBits(GPIOB,GPIO_Pin_12);
//// }
////}
/**********************************************************************
***********************************************************************/
此帖出自
小平头技术问答
谢谢啊 ,再问一下,下面这个程序怎样设置优先级呢?怎样就能用一根中断线配置两个中断口,中断服务程序怎么写啊, 求大神指导啊,
************************************************************************/
#include "stm32f10x.h"
#include "string.h"
/***********************************************************************
外设时钟使能
************************************************************************/
void RCC_Configuration(void)
{
/* 使能外设时钟 */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_AFIO |
RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC |
RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
}
/*******************************************************************************
全部用到的引脚将在在配置
*******************************************************************************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
// /* 配置串口1引脚 */
// 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_InitStructure.GPIO_Pin = GPIO_Pin_13;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOE, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOE, &GPIO_InitStructure);
/*LED*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
/*******************************************************************************
全部中断在此配置
*******************************************************************************/
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Configure the NVIC Preemption Priority Bits */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
/* Enable the USART1 Interrupt */
// NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
// NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
// NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
// NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn; // S4中断入口
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
// NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn; // S3中断入口
// NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
// NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
// NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
/*******************************************************************************
外部中断在此配置
*******************************************************************************/
void EXTI_Configuration(void)
{
EXTI_InitTypeDef EXTI_InitStructure;
GPIO_EXTILineConfig(GPIO_PortSourceGPIOD, GPIO_PinSource13); // S4中断
EXTI_InitStructure.EXTI_Line = EXTI_Line13;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOE, GPIO_PinSource13); // S3中断
EXTI_InitStructure.EXTI_Line = EXTI_Line13;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOE, GPIO_PinSource5); // S3中断
EXTI_InitStructure.EXTI_Line = EXTI_Line5;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
//EXTI_GenerateSWInterrupt(EXTI_Line13);
}
/*******************************************************************************
初始化时钟晶振 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){
;
}
}
}
/*****************************************************************
USART1 初始化 baud 波特率
*****************************************************************/
void USART1_Init(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);
}
/*****************************************************************
从 USART1 发送一个字节
*****************************************************************/
void USART1_SendByte(unsigned char temp)
{
USART_SendData(USART1, temp);
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
}
/*****************************************************************
从 USART1 发送字符串
*****************************************************************/
void USART1_Printf(char *pch)
{
while(*pch != ' '){
USART1_SendByte(*(unsigned char *)pch);
pch++;
}
}
/******************************************
*
* 延时程序 ms
*
****************************************/
void Delay(unsigned short time)
{
unsigned short i, j;
for(; time > 0; time--){
for(j = 0; j < 10; j++){
for(i = 0; i < 1000; i++);
}
}
}
/*******************************************************
MAIN 函数
*******************************************************/
int main(void)
{
SysClock_Init(); // 初始化系统时钟 72MHZ
RCC_Configuration(); // 使能外设
GPIO_Configuration(); // 配置引脚
NVIC_Configuration(); // 配置中断
EXTI_Configuration();
// USART1_Init(9600); // 配置串口1,波特率9600
//
// USART1_Printf("火牛开发板基础实验");
while(1){
;
}
// return(0);
}
谢谢啊 ,再问一下,下面这个程序怎样设置优先级呢?怎样就能用一根中断线配置两个中断口,中断服务程序怎么写啊, 求大神指导啊,
************************************************************************/
#include "stm32f10x.h"
#include "string.h"
/***********************************************************************
外设时钟使能
************************************************************************/
void RCC_Configuration(void)
{
/* 使能外设时钟 */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_AFIO |
RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC |
RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
}
/*******************************************************************************
全部用到的引脚将在在配置
*******************************************************************************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
// /* 配置串口1引脚 */
// 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_InitStructure.GPIO_Pin = GPIO_Pin_13;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOE, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOE, &GPIO_InitStructure);
/*LED*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
/*******************************************************************************
全部中断在此配置
*******************************************************************************/
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Configure the NVIC Preemption Priority Bits */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
/* Enable the USART1 Interrupt */
// NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
// NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
// NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
// NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn; // S4中断入口
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
// NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn; // S3中断入口
// NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
// NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
// NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
/*******************************************************************************
外部中断在此配置
*******************************************************************************/
void EXTI_Configuration(void)
{
EXTI_InitTypeDef EXTI_InitStructure;
GPIO_EXTILineConfig(GPIO_PortSourceGPIOD, GPIO_PinSource13); // S4中断
EXTI_InitStructure.EXTI_Line = EXTI_Line13;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOE, GPIO_PinSource13); // S3中断
EXTI_InitStructure.EXTI_Line = EXTI_Line13;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOE, GPIO_PinSource5); // S3中断
EXTI_InitStructure.EXTI_Line = EXTI_Line5;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
//EXTI_GenerateSWInterrupt(EXTI_Line13);
}
/*******************************************************************************
初始化时钟晶振 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){
;
}
}
}
/*****************************************************************
USART1 初始化 baud 波特率
*****************************************************************/
void USART1_Init(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);
}
/*****************************************************************
从 USART1 发送一个字节
*****************************************************************/
void USART1_SendByte(unsigned char temp)
{
USART_SendData(USART1, temp);
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
}
/*****************************************************************
从 USART1 发送字符串
*****************************************************************/
void USART1_Printf(char *pch)
{
while(*pch != ' '){
USART1_SendByte(*(unsigned char *)pch);
pch++;
}
}
/******************************************
*
* 延时程序 ms
*
****************************************/
void Delay(unsigned short time)
{
unsigned short i, j;
for(; time > 0; time--){
for(j = 0; j < 10; j++){
for(i = 0; i < 1000; i++);
}
}
}
/*******************************************************
MAIN 函数
*******************************************************/
int main(void)
{
SysClock_Init(); // 初始化系统时钟 72MHZ
RCC_Configuration(); // 使能外设
GPIO_Configuration(); // 配置引脚
NVIC_Configuration(); // 配置中断
EXTI_Configuration();
// USART1_Init(9600); // 配置串口1,波特率9600
//
// USART1_Printf("火牛开发板基础实验");
while(1){
;
}
// return(0);
}
一周热门 更多>