实验02:GPIO和NVIC

向下

实验02:GPIO和NVIC

帖子  Admin 于 周五 四月 23, 2010 11:09 am

按键使用 SELECTION,修改:
main.c
stm32f10x_it.c


使用ST的函数库程序如下(修改自:教材《基于MDK的STM32处理器开发应用》7.1.3例)
main.c :

/*******************************************************************************
* File Name : main.c
* Author : zhou_yinxiang@tom.com, CDUESTC
* Date First Issued : 04/20/2010
* Description : Main program body
使用ST的函数库程序,在STM32-SS上验证通过
(修改自:教材《基于MDK的STM32处理器开发应用》7.1.3例)
********************************************************************************/

/* Includes ------------------------------------------------------------------*/
#include "stm32f10x_lib.h"


/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
#define ADC1_DR_Address ((u32)0x4001244C)

unsigned short int ADC_ConvertedValue;
GPIO_InitTypeDef GPIO_InitStructure;
ADC_InitTypeDef ADC_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
ErrorStatus HSEStartUpStatus;
extern vu32 TimingDelay;
/* Private function prototypes -----------------------------------------------*/
void RCC_Configuration(void);
void NVIC_Configuration(void);
void GPIO_Configuration(void);
void Delay(vu32 nTime);
void SysTick_Configuration(void);
void SetupLED (void) ;
extern void SetupADC (void);

/* Private functions ---------------------------------------------------------*/

/*******************************************************************************
* Function Name : main
* Description : Main program.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
int main(void)
{
#ifdef DEBUG
debug();
#endif

/* Configure the system clocks */
RCC_Configuration();
SysTick_Configuration();

/* NVIC Configuration */
NVIC_Configuration();

/* Configure the GPIO ports */
GPIO_Configuration();

/* Connect EXTI Line11 to PD.11 */
GPIO_EXTILineConfig(GPIO_PortSourceGPIOD, GPIO_PinSource11);

/* Configure EXTI Line9 to generate an interrupt on falling edge */
EXTI_InitStructure.EXTI_Line = EXTI_Line11;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);

for(;Wink
{
GPIOC->ODR = 0xfffffc7f; //1111 1100 0111 1111 点亮LED1
Delay(100);
GPIOC->ODR = 0xfffffcbf; //1111 1100 1011 1111 点亮LED2
Delay(100);
GPIOC->ODR = 0xfffffd3f; //1111 1101 0011 1111 点亮LED3
Delay(100);
GPIOC->ODR = 0xfffffe3f; //1111 1110 0011 1111 点亮LED4
Delay(100);
}

}

/*******************************************************************************
* Function Name : SysTick_Configuration
* Description : Configures the SysTick to generate an interrupt each 1 millisecond.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void SysTick_Configuration(void)
{
/* Select AHB clock(HCLK) as SysTick clock source */
SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);

/* Set SysTick Priority to 3 */
NVIC_SystemHandlerPriorityConfig(SystemHandler_SysTick, 3, 0);

/* SysTick interrupt each 1ms with HCLK equal to 72MHz */
SysTick_SetReload(72000);

/* Enable the SysTick Interrupt */
SysTick_ITConfig(ENABLE);
}



/*******************************************************************************
* Function Name : RCC_Configuration
* Description : Configures the different system clocks.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void RCC_Configuration(void)
{
/* RCC system reset(for debug purpose) */
RCC_DeInit();

/* Enable HSE */
RCC_HSEConfig(RCC_HSE_ON);

/* Wait till HSE is ready */
HSEStartUpStatus = RCC_WaitForHSEStartUp();

if(HSEStartUpStatus == SUCCESS)
{
/* HCLK = SYSCLK */
RCC_HCLKConfig(RCC_SYSCLK_Div1);

/* PCLK2 = HCLK */
RCC_PCLK2Config(RCC_HCLK_Div1);

/* PCLK1 = HCLK/2 */
RCC_PCLK1Config(RCC_HCLK_Div2);

/* Flash 2 wait state */
FLASH_SetLatency(FLASH_Latency_2);
/* Enable Prefetch Buffer */
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

/* PLLCLK = 8MHz * 9 = 72 MHz */
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);

/* Enable PLL */
RCC_PLLCmd(ENABLE);

/* Wait till PLL is ready */
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
{
}

/* Select PLL as system clock source */
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

/* Wait till PLL is used as system clock source */
while(RCC_GetSYSCLKSource() != 0x08)
{
}
}

/* Enable GPIOB, GPIOC and AFIO clocks */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC |
RCC_APB2Periph_AFIO, ENABLE);
}

/*******************************************************************************
* Function Name : NVIC_Configuration
* Description : Configures Vector Table base location.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;

#ifdef VECT_TAB_RAM
/* Set the Vector Table base location at 0x20000000 */
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else /* VECT_TAB_FLASH */
/* Set the Vector Table base location at 0x08000000 */
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
#endif

/* Configure one bit for preemption priority */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);

/* Enable the EXTI9_5 Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQChannel;//EXTI9_5_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}


/*******************************************************************************
* Function Name : GPIO_Configuration
* Description : Configures the different GPIO ports.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;

/* Configure PC. as Output push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);

/* Configure PD11 as input floating (EXTI Line11) */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOD, &GPIO_InitStructure);
}


/*******************************************************************************
* Function Name : Delay
* Description : Inserts a delay time.
* Input : nTime: specifies the delay time length, in milliseconds.
* Output : None
* Return : None
*******************************************************************************/
void Delay(u32 nTime)
{
/* Enable the SysTick Counter */
SysTick_CounterCmd(SysTick_Counter_Enable);

TimingDelay = nTime;

while(TimingDelay != 0);

/* Disable the SysTick Counter */
SysTick_CounterCmd(SysTick_Counter_Disable);
/* Clear the SysTick Counter */
SysTick_CounterCmd(SysTick_Counter_Clear);
}

/*******************************************************************************
* Function Name : Delay
* Description : Inserts a delay time.
* Input : nCount: specifies the delay time length.
* Output : None
* Return : None
*******************************************************************************/
/*
void Delay(vu32 nCount)
{
for(; nCount != 0; nCount--);
}

*/





#ifdef DEBUG
/*******************************************************************************
* Function Name : assert_failed
* Description : Reports the name of the source file and the source line number
* where the assert error has occurred.
* Input : - file: pointer to the source file name
* - line: assert error line source number
* Output : None
* Return : None
*******************************************************************************/
void assert_failed(u8* file, u32 line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

/* Infinite loop */
while (1)
{
}
}
#endif

/******************* (C) COPYRIGHT 2007 STMicroelectronics *****END OF FILE****/









* File Name : stm32f10x_it.c

/*******************************************************************************
* Function Name : EXTI15_10_IRQHandler
* Description : This function handles External lines 15 to 10 interrupt request.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void EXTI15_10_IRQHandler(void)
{

int i;

if(EXTI_GetITStatus(EXTI_Line11) != RESET)
{
for(i=0;i<=8000000;i++)

GPIOC->ODR = 0xfffffc3f; //... 1111 1100 0011 1111 LED1 2 3 4 全部熄灭

for(i=0;i<=2000000;i++)

GPIOC->ODR = 0xffffffff; //... 1111 1111 1111 1111 LED1 2 3 4 全部点亮

for(i=0;i<=1000000;i++)


/* Clear the EXTI line 11 pending bit */
EXTI_ClearITPendingBit(EXTI_Line11);
}

Admin
Admin

帖子数 : 177
注册日期 : 10-03-21

http://cdmcu.forumotion.com

返回页首 向下

返回页首


 
您在这个论坛的权限:
不能在这个论坛回复主题