初级会员

- 积分
- 159
- 金钱
- 159
- 注册时间
- 2015-10-12
- 在线时间
- 0 小时
|
5金钱
#include "stm32f10x.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_tim.h"
#include "stm32f10x_usart.h"
//#include"led.h"
u8 counter=0;
#define LED(x) GPIO_Write(GPIOC,x)//定义LED(x) 为后面的函数 向指定的GPIO数据端口写入数据
#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=5000; //定时器初始值
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 USART_config(u32 bound)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA,ENABLE);
USART_DeInit(USART1);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode =GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode =GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = bound;
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_Cmd(USART1, 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)//定时器中断函数
{
// static u8 i=1;
if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET) // 跟下面一句连在一起用的 判断是否中断有触发,出发后就清除
{
TIM_ClearITPendingBit(TIM3, TIM_IT_Update); //清除中断标志
counter++;
// LED(~i); // 这个放在这里 就是八盏灯都亮了 然后下面有一个定时的
// if(counter >1) //这里>1就是1秒 >=或者>0都是等于0.5秒 初始值设置的5000 等于 5毫秒 0.5秒 // 这里如果是 >=1 第八盏灯还是不亮 其实是亮的 只是没有看见的
// {
// LED(~i); // 这个放在这里 就七盏灯循环亮 其实第八盏是亮的 但是速度太快 肉眼看不见而已 因为下面有一个if( == )两个LED灯都亮啦
// if(i ==0xff)
// {
// i = 0;
// LED(~i);
// }
// i=((i<<1)+1);
// counter=0; //这里不清0 就会一直累计加 就会满足条件
// }
}
}
int main(void)
{
GPIO_Config();
Timer_Config();
NVIC_Config();
KEY_Config();
USART_config(9600);
LED(0xff); //这里就是全灭,这个里面你可以从0x00~0xff取值,屏蔽中断里,把这句注释掉,1是灭,0是亮
// 这里初始化的 如果要一开始是全亮的话 就0;
while(1)
{
// if(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_15)== 0)
// LED(~1);
// if(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_14)== 0)
// LED(~2);
// if(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_13)== 0)
// LED(~4);
// if(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_12)== 0)
// LED(~8);
}
}
|
|