/***********按键控制函数******************/
#include "stm32f10x_lib.h"
//========================================
#define JTAG_SWD_DISABLE 0X02
#define SWD_ENABLE 0X01
#define JTAG_SWD_ENABLE 0X00
//========================================
ErrorStatus HSEStartUpStatus
//=======Private function prototypes======
void JTAG_Set(u8 mode);
u8 KEY_Scan(void);
void RCC_Configuration(void)
{
/* RCC system reset(for debug purpose) */
RCC_DeInit();
RCC_HSEConfig(RCC_HSE_ON); /* Enable HSE */
/* Wait till HSE is ready */
HSEStartUpStatus = RCC_WaitForHSEStartUp();
if(HSEStartUpStatus == SUCCESS)
{
/* Enable Prefetch Buffer */
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
/* Flash 2 wait state */
FLASH_SetLatency(FLASH_Latency_2);
/* HCLK = SYSCLK */
RCC_HCLKConfig(RCC_SYSCLK_Div1);
/* PCLK2 = HCLK */
RCC_PCLK2Config(RCC_HCLK_Div1);
/* PCLK1 = HCLK/2 */
RCC_PCLK1Config(RCC_HCLK_Div2);
/* PLLCLK = 8MHz * 9 = 72 MHz */
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_3);
/* 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)
{
}
}
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOD, ENABLE);
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_0;
GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_IPD;
GPIO_Init(GPIOA,&GPIO_InitStruct);
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_13|GPIO_Pin_15;
GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_IPU;
GPIO_Init(GPIOA,&GPIO_InitStruct);
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_8;
GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_Out_PP;;
GPIO_Init(GPIOA,&GPIO_InitStruct);
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_2;
GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_Out_PP;;
GPIO_Init(GPIOD,&GPIO_InitStruct);
}
void delay_nus(u32 n) //延时n us:n>=6,最小延时单位6us
{
u32 j;
while(n--) //外部晶振:8M;PLL:9; 8M X 9=72MHz
{
j=8; //微调参数,保证延时的精度
while(j--);
}
}
void delay_nms(u32 n)
{
while(n--) //外部晶振:8M;PLL:9; 8M X 9=72MHz
delay_nus(1100); //1ms延时补偿
}
//========================================
//========================================
int main(void)
{
#ifdef DEBUG
debug();
#endif
u8 t;
RCC_Configuration();
GPIO_Configuration();
while(1)
{
t=KEY_Scan();
if(t)
{
switch(t)
{
case 1:if(GPIO_ReadOutputDataBit(GPIOA,GPIO_Pin_8)==0)
GPIO_SetBits(GPIOA, GPIO_Pin_8);
else
GPIO_ResetBits(GPIOA, GPIO_Pin_8);
break;
case 2:if(GPIO_ReadOutputDataBit(GPIOD,GPIO_Pin_2)==0)
GPIO_SetBits(GPIOD, GPIO_Pin_2);
else
GPIO_ResetBits(GPIOD, GPIO_Pin_2);
break;
case 3:if(GPIO_ReadOutputDataBit(GPIOA,GPIO_Pin_8)==0)
GPIO_SetBits(GPIOA, GPIO_Pin_8);
else
GPIO_ResetBits(GPIOA, GPIO_Pin_8);
if(GPIO_ReadOutputDataBit(GPIOD,GPIO_Pin_2)==0)
GPIO_SetBits(GPIOD, GPIO_Pin_2);
else
GPIO_ResetBits(GPIOD, GPIO_Pin_2);
break;
}
}
}
}
void JTAG_Set(u8 mode)
{
u32 temp;
temp=mode;
temp<<=25;
RCC->APB2ENR|=1<<0;
//RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
AFIO->MAPR&=0XF8FFFFFF; //清除MARP的[26;24]
AFIO->MAPR|=temp; //设置JTAG模式
}
u8 KEY_Scan(void)
{
static u8 key_up=1; //按键松开标志
JTAG_Set(JTAG_SWD_DISABLE);
if(key_up&&((GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_13)==0)
||(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_15)==0)
||(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0)==1)))
{
delay_nms(10);
key_up=0;
if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_13)==0)
{
JTAG_Set(SWD_ENABLE);
return 1;
}
else
if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_15)==0)
{
JTAG_Set(SWD_ENABLE);
return 2;
}
else
if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0)==1)
{
JTAG_Set(SWD_ENABLE);
return 3;
}
}
else if((GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_13)==1)
&&(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_15)==1)
&&(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0)==0))
key_up=1;
JTAG_Set(SWD_ENABLE);
return 0; //无按键按下
}
#ifdef DEBUG
/*******************************************************************************
* Function Name : assert_failed
* Description : Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* Input : - file: pointer to the source file name
* - line: assert_param 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
出现下列错误: