初级会员

- 积分
- 159
- 金钱
- 159
- 注册时间
- 2015-10-12
- 在线时间
- 0 小时
|
5金钱
#include "stm32f10x.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_tim.h"
//#include"led.h"
u8 counter=0;
#define LED(x) GPIO_Write(GPIOC,x)
#define LED1_ON() GPIO_ResetBits(GPIOC,GPIO_Pin_0) //???
#define LED1_OFF() GPIO_SetBits(GPIOC, GPIO_Pin_0)
#define LED2_ON() GPIO_ResetBits(GPIOC,GPIO_Pin_1) //???
#define LED2_OFF() GPIO_SetBits(GPIOC, GPIO_Pin_1)
#define LED3_ON() GPIO_ResetBits(GPIOC,GPIO_Pin_2) //???
#define LED3_OFF() GPIO_SetBits(GPIOC, GPIO_Pin_2)
#define LED4_ON() GPIO_ResetBits(GPIOC,GPIO_Pin_3) //???
#define LED4_OFF() GPIO_SetBits(GPIOC, GPIO_Pin_3)
#define LED5_ON() GPIO_ResetBits(GPIOC,GPIO_Pin_4) //???
#define LED5_OFF() GPIO_SetBits(GPIOC, GPIO_Pin_4)
#define LED6_ON() GPIO_ResetBits(GPIOC,GPIO_Pin_5) //???
#define LED6_OFF() GPIO_SetBits(GPIOC, GPIO_Pin_5)
#define LED7_ON() GPIO_ResetBits(GPIOC,GPIO_Pin_6) //???
#define LED7_OFF() GPIO_SetBits(GPIOC, GPIO_Pin_6)
#define LED8_ON() GPIO_ResetBits(GPIOC,GPIO_Pin_7) //???
#define LED8_OFF() GPIO_SetBits(GPIOC, GPIO_Pin_7)
#define KEY0 GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_12)
#define KEY1 GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_13)
#define KEY2 GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_14)
#define KEY3 GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_15)
void GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);//开启外设时钟
/*输出端口初始化*/
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //配置端口为推挽输出模式
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
GPIO_Init(GPIOC, &GPIO_InitStructure);
}
void KEY_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);//开启外设时钟
GPIO_InitStructure.GPIO_Mode =GPIO_Mode_IPU ; //配置端口为推挽输出模式
GPIO_InitStructure.GPIO_Pin =GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
void Timer_Config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);//Timer3_Clock=APB1*2;
TIM_DeInit(TIM3); //复位定时器
TIM_TimeBaseStructure.TIM_Period=4999; //定时器初始值
TIM_TimeBaseStructure.TIM_Prescaler=(7200-1); //时钟预分频
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1; // 时钟分割
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; //向上计数模式
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure); //初始化定时器的值
TIM_ClearFlag(TIM3,TIM_FLAG_Update); //清除定时器中断标志
TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE); //使能中断
TIM_Cmd(TIM3,ENABLE); //开启时钟
}
void NVIC_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void TIM3_IRQHandler(void)//定时器中断函数
{
if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET) // 跟下面一句连在一起用的 判断是否中断有触发,出发后就清除
{
TIM_ClearITPendingBit(TIM3, TIM_IT_Update); //清除中断标志
counter++;
if(counter==10)
{counter=0;}
}
}
int main(void)
{
GPIO_Config();
Timer_Config();
NVIC_Config();
LED(0xff);
while(1)
{
}
}
|
|