本例程根据自己画的开发板进行编写驱动,Demo例程主要包括
1、systick定时器的初始化和中断
2、板载LED灯GPIO驱动
3、板载KEY按键GPIO中断
4、USART串口1作为调试串口
本Demo实现的功能是,按键中断修改LED亮灭的时间频率,然后串口输出显示对应的Speed
程序代码如下:
main主函数
#include <stdio.h>
#include "stm32f0xx.h"
#include "stm32f0xx_it.h"
#include "m0_systick.h"
#include "led.h"
#include "debug.h"
#include "key.h"
void key_switch(void);
extern uint8_t key_num;
int main(void)
{
SystemInit();
M0_Systick_Config();
LED_Init();
Debug_Config();
Key_Init();
ToggleLED();
Debug_Info("hello\r\n");
Delay_ms(500);
while(1)
{
key_switch();
}
}
void key_switch(void)
{
if(key_num==0)
{
key_num=key_num+1;
}
else if(key_num==1)
{
ToggleLED();
Delay_ms(100);
Debug_Info("Speed-1\r\n");
}
else if(key_num==2)
{
key_num=key_num+1;
}
else if(key_num==3)
{
ToggleLED();
Delay_ms(200);
Debug_Info("Speed-2\r\n");
}
else if(key_num==4)
{
key_num=key_num+1;
}
else if(key_num==5)
{
ToggleLED();
Delay_ms(300);
Debug_Info("Speed-3\r\n");
}
else if(key_num==6)
{
key_num=key_num+1;
}
else if(key_num==7)
{
ToggleLED();
Delay_ms(400);
Debug_Info("Speed-4\r\n");
}
else if(key_num==8)
{
key_num=key_num+1;
}
else if(key_num==9)
{
ToggleLED();
Delay_ms(500);
Debug_Info("Speed-5\r\n");
}
else if(key_num==10)
{
key_num=key_num+1;
}
else if(key_num==11)
{
ToggleLED();
Delay_ms(600);
Debug_Info("Speed-6\r\n");
}
else if(key_num==12)
{
key_num=key_num+1;
}
else if(key_num==13)
{
ToggleLED();
Delay_ms(1000);
Debug_Info("Speed-7\r\n");
}
}
key.c
#include "key.h"
#include "debug.h"
//°′?ü---->PA0
uint8_t KEY_NUM = 0;
void Key_Init()
{
GPIO_InitTypeDef GPIO_InitStruct;
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA,ENABLE);
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStruct.GPIO_OType = GPIO_OType_OD;
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_Level_3;
GPIO_Init(KEY_PORT,&GPIO_InitStruct);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA, EXTI_PinSource0);
EXTI_InitStructure.EXTI_Line = EXTI_Line0;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 0x00; //ó??è??±è′??ú?óê??D????
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void EXTI0_1_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line0) != RESET)
{
KEY_NUM = KEY_WKUP;
printf("hello\r\n");
EXTI_ClearITPendingBit(EXTI_Line0);
}
}
key.h
#ifndef __KEY_H__
#define __KEY_H__
#include "stm32f0xx.h"
#define KEY_PORT GPIOA
#define KEY_PIN GPIO_Pin_0
#define KEY_CLK RCC_AHBPeriph_GPIOA
#define KEY_WKUP 1
#define NO_KEY 0
#define WKUP GPIO_ReadInputDataBit(KEY_PORT,KEY_PIN)
extern uint8_t KEY_NUM;
void Key_Init(void);
#endif
led.c
#include "led.h"
void LED_Init(void)
{
GPIO_InitTypeDef GPIO_InitTypeStruct;
RCC_AHBPeriphClockCmd(LED_CLK,ENABLE);
GPIO_InitTypeStruct.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitTypeStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitTypeStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitTypeStruct.GPIO_Speed = GPIO_Speed_Level_3;
GPIO_InitTypeStruct.GPIO_Pin = LED_PIN;
GPIO_Init(LED_PORT,&GPIO_InitTypeStruct);
}
void ToggleLED(void)
{
LED_PORT->ODR ^= LED_PIN;
}
led.h
#ifndef __LED_H
#define __LED_H
#include "stm32f0xx.h"
#define LED_PORT GPIOF
#define LED_PIN GPIO_Pin_0
#define LED_CLK RCC_AHBPeriph_GPIOF
#define LED_ON GPIO_ResetBits(LED_PORT,LED_PIN)
#define LED_OFF GPIO_SetBits(LED_PORT,LED_PIN)
void LED_Init(void);
void ToggleLED(void);
#endif
debug.c
#include "debug.h"
#include "stm32f0xx.h"
void Debug_Config(void)
{
GPIO_InitTypeDef GPIO_Debug;
USART_InitTypeDef USART_Debug;
USART_CLK_CMD(USART_CLK,ENABLE);
GPIO_CLK_CMD(GPIO_TX_CLK|GPIO_RX_CLK,ENABLE);
GPIO_Debug.GPIO_Mode = GPIO_Mode_AF;
GPIO_Debug.GPIO_OType = GPIO_OType_PP;
GPIO_Debug.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Debug.GPIO_Speed = GPIO_Speed_Level_3;
GPIO_Debug.GPIO_Pin = GPIO_TX_PIN;
GPIO_Init(GPIO_TX_PORT,&GPIO_Debug);
GPIO_Debug.GPIO_Pin = GPIO_RX_PIN;
GPIO_Init(GPIO_RX_PORT,&GPIO_Debug);
GPIO_PinAFConfig(GPIO_TX_PORT,GPIO_PinSource2,GPIO_AF_1);
GPIO_PinAFConfig(GPIO_RX_PORT,GPIO_PinSource3,GPIO_AF_1);
USART_Debug.USART_BaudRate = 115200;
USART_Debug.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_Debug.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Debug.USART_Parity = USART_Parity_No;
USART_Debug.USART_StopBits = USART_StopBits_1;
USART_Debug.USART_WordLength = USART_WordLength_8b;
USART_Init(USART_DEBUG,&USART_Debug);
USART_Cmd(USART_DEBUG,ENABLE);
}
int fputc(int ch ,FILE *f)
{
USART_SendData(USART_DEBUG,(uint8_t)ch);
while(USART_GetFlagStatus(USART_DEBUG,USART_FLAG_TXE) == RESET);
return (ch);
}
int fgetc(FILE *f)
{
while (USART_GetFlagStatus(USART_DEBUG, USART_FLAG_RXNE) == RESET);
return (int)USART_ReceiveData(USART_DEBUG);
}
void Usart_SendByte(USART_TypeDef *cUSARTx,uint8_t ch)
{
USART_SendData(cUSARTx,ch);
while(USART_GetFlagStatus(cUSARTx,USART_FLAG_TXE)==RESET);
}
void Usart_SendArray( USART_TypeDef * pUSARTx, uint8_t *array, uint16_t num)
{
uint8_t i;
for(i=0;i<num;i++)
{
Usart_SendByte(pUSARTx,array[i]);
}
while(USART_GetFlagStatus(pUSARTx,USART_FLAG_TC)==RESET);
}
void Usart_SendString( USART_TypeDef * pUSARTx, char *str)
{
unsigned int k=0;
do
{
Usart_SendByte( pUSARTx, *(str + k) );
k++;
} while(*(str + k)!='\0');
while(USART_GetFlagStatus(pUSARTx,USART_FLAG_TC)==RESET)
{}
}
void Usart_SendHalfWord( USART_TypeDef * pUSARTx, uint16_t ch)
{
uint8_t temp_h, temp_l;
temp_h = (ch&0XFF00)>>8;
temp_l = ch&0XFF;
USART_SendData(pUSARTx,temp_h);
while (USART_GetFlagStatus(pUSARTx, USART_FLAG_TXE) == RESET);
USART_SendData(pUSARTx,temp_l);
while (USART_GetFlagStatus(pUSARTx, USART_FLAG_TXE) == RESET);
}
void ErrorCallBack(char *mess)
{
Debug_Info(mess);
while(1);
}
debug.h
#ifndef __DEBUG_H
#define __DEBUG_H
#include "stm32f0xx.h"
#include <stdio.h>
#define __DEBUG__
#ifdef __DEBUG__
#define Debug_Info(fmt,arg...) printf(fmt,##arg)
#else
#define Debug_Info(fmt,arg...)
#endif
#define USART_DEBUG USART1
#define USART_CLK_CMD RCC_APB2PeriphClockCmd
#define USART_CLK RCC_APB2Periph_USART1
#define GPIO_CLK_CMD RCC_AHBPeriphClockCmd
#define GPIO_TX_CLK RCC_AHBPeriph_GPIOA
#define GPIO_TX_PORT GPIOA
#define GPIO_TX_PIN GPIO_Pin_9
#define GPIO_RX_CLK RCC_AHBPeriph_GPIOA
#define GPIO_RX_PORT GPIOA
#define GPIO_RX_PIN GPIO_Pin_10
void Debug_Config(void);
void Usart_SendByte(USART_TypeDef *cUSARTx,uint8_t ch);
void Usart_SendArray( USART_TypeDef * pUSARTx, uint8_t *array, uint16_t num);
void Usart_SendString( USART_TypeDef * pUSARTx, char *str);
void Usart_SendHalfWord( USART_TypeDef * pUSARTx, uint16_t ch);
void ErrorCallBack(char *mess);
#endif
stm32f0xx_it.c
#include "stm32f0xx_it.h"
#include "m0_systick.h"
#include "debug.h"
#include "key.h"
#include "led.h"
uint32_t sysTickUptime = 0;
static volatile uint32_t closeTime = 0;
uint8_t key_num=0; //°′?üμ¥?÷è???±?á?
unsigned long nowTimeCnt1[1];
unsigned long nowTimeCnt2[1];
uint8_t keyPressSta = 0;
uint8_t keyLongPressSta = 0;
uint8_t ledTwinklingCnt = 7;
uint8_t _1sSta = 0;
/** @addtogroup Template_Project
* @{
*/
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/******************************************************************************/
/* Cortex-M0 Processor Exceptions Handlers */
/******************************************************************************/
/**
* @brief This function handles NMI exception.
* @param None
* @retval None
*/
void NMI_Handler(void)
{
}
//__asm void wait()
//{
// BX lr
//}
/**
* @brief This function handles Hard Fault exception.
* @param None
* @retval None
*/
void HardFault_Handler(void)
{
/* Go to infinite loop when Hard Fault exception occurs */
// while(1);
}
/**
* @brief This function handles SVCall exception.
* @param None
* @retval None
*/
void SVC_Handler(void)
{
}
/**
* @brief This function handles PendSVC exception.
* @param None
* @retval None
*/
void PendSV_Handler(void)
{
}
/**
* @brief This function handles SysTick Handler.
* @param None
* @retval None
*/
void SysTick_Handler(void)
{
tida();
sysTickUptime++;
TimeStamp_Increment();
if(sysTickUptime % 1000 == 0)
{
_1sSta = 1;
}
if(KEY_NUM == KEY_WKUP)
{
KEY_NUM = NO_KEY;
keyPressSta = 1;
closeTime = 0;
Debug_Info("key_short\r\n");
key_num++;
Debug_Info("key_num:%d\r\n",key_num);
if(key_num>=14)
key_num=0;
}
else if(!WKUP && !keyPressSta)
{
keyPressSta = 1;
}
if(keyPressSta)
{
if(WKUP == 0)
{
closeTime++;
if(closeTime >= 1000)
{
keyPressSta = 0;
keyLongPressSta = 1;
get_tick_count(nowTimeCnt2);
}
}
}
if(keyLongPressSta)
{
if(sysTickUptime - nowTimeCnt2[0] > 200)
{
ToggleLED();
ledTwinklingCnt--;
if(ledTwinklingCnt == 0)
{
Debug_Info("turn off\r\n");
}
get_tick_count(nowTimeCnt2);
}
}
}
/******************************************************************************/
/* STM32F0xx Peripherals Interrupt Handlers */
/* Add here the Interrupt Handler for the used peripheral(s) (PPP), for the */
/* available peripheral interrupt handler's name please refer to the startup */
/* file (startup_stm32f0xx.s). */
/******************************************************************************/
/**
* @brief This function handles PPP interrupt request.
* @param None
* @retval None
*/
/*void PPP_IRQHandler(void)
{
}*/
/**
* @}
*/
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
以上是Demo的基础功能,后续模块的添加会依据此Demo进行修改。
|