Lost !

-----hard working for the furture.

导航

统计

STM32 工控板-- 系统定时器测试

led.h

 

查看代码

#ifndef __LED_H
#define	__LED_H

#include "stm32f10x.h"

/* the macro definition to trigger the led on or off 
 * 1 - off
 - 0 - on
 */
#define ON  0
#define OFF 1

#define LED1(a)	if (a)	\
					GPIO_SetBits(GPIOB,GPIO_Pin_14);\
					else		\
					GPIO_ResetBits(GPIOB,GPIO_Pin_14)

#define LED2(a)	if (a)	\
					GPIO_SetBits(GPIOB,GPIO_Pin_12);\
					else		\
					GPIO_ResetBits(GPIOB,GPIO_Pin_12)

#define LED3(a)	if (a)	\
					GPIO_SetBits(GPIOB,GPIO_Pin_13);\
					else		\
					GPIO_ResetBits(GPIOB,GPIO_Pin_13)

void LED_GPIO_Config(void);

#endif /* __LED_H */

 

led.c

查看代码
 /******************** 鑫盛电子工作室 ********************
 * 文件名  :led.c
 * 描述    :led 应用函数库
 *
 * 实验平台:MINI STM32开发板 基于STM32F103VET6
 * 硬件连接:-----------------
 *          |   PB14 - LED1   |
 *          |   PB12 - LED2   |
 *          |   PB13 - LED3   |
 *           -----------------
 * 库版本  :ST3.0.0
 * 淘宝店:http://shop66177872.taobao.com
 *********************************************************/

#include "led.h"

/***************  配置LED用到的I/O口 *******************/
void LED_GPIO_Config(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); // 使能PB端口时钟
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOB, &GPIO_InitStructure);                        // 初始化PB端口
    GPIO_SetBits(GPIOB, GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14); // 关闭所有LED
}

 

SysTick.h

查看代码
 #ifndef __SYSTICK_H
#define __SYSTICK_H

#include "stm32f10x.h"

void SysTick_Init(void);
void Delay_us(__IO u32 nTime);

#endif /* __SYSTICK_H */

SysTick.c

查看代码
 /******************** 鑫盛电子工作室 ********************
 * 文件名  :SysTick.c
 * 描述    :SysTick 系统滴答时钟10us中断函数库,中断时间可自由配置,
 *           常用的有 1us 10us 1ms 中断。         
 * 实验平台:MINI STM32开发板 基于STM32F103VET6
 * 硬件连接:无
 * 库版本  :ST3.0.0  
 *淘宝店:http://shop66177872.taobao.com
*********************************************************/

#include "SysTick.h"

static __IO u32 TimingDelay;

/*初始化  SysTick*/
void SysTick_Init(void)
{
	/* SystemFrequency / 1000    1ms中断一次
	 * SystemFrequency / 100000	 10us中断一次
	 * SystemFrequency / 1000000 1us中断一次
	 */
	if (SysTick_Config(SystemFrequency / 100000))
  { 
    /* Capture error */ 
    while (1);
  }
}


/*us延时程序,10us为一个单位 */
void Delay_us(__IO u32 nTime)
{ 
  TimingDelay = nTime;

  while(TimingDelay != 0);
}


/* 获取节拍程序,在 SysTick 中断函数 SysTick_Handler()调用	 */  
void TimingDelay_Decrement(void)
{
  if (TimingDelay != 0x00)
  { 
    TimingDelay--;
  }
}

 

main.c

 

查看代码
 /******************** 鑫盛电子工作室 ********************
 * 文件名  :main.c
 * 描述    :3个LED在 SysTick 的控制下,以500ms的频率闪烁。
 * 实验平台:MINI STM32开发板 基于STM32F103VET6
 * 库版本  :ST3.0.0
 *淘宝店:http://shop66177872.taobao.com
 *********************************************************/
#include "SysTick.h"
#include "led.h"
#include "stm32f10x.h"

/*
 * 函数名:main
 * 描述  :主函数
 * 输入  :无
 * 输出  :无
 */
int main(void)
{

    SystemInit();      // 配置系统时钟为72M
    LED_GPIO_Config(); // LED 端口初始化

    /* 配置SysTick 为10us中断一次 */
    SysTick_Init();

    while (1)
    {
        LED1(0);
        LED2(0);
        LED3(0);
        Delay_us(50000); // 50000 * 10us = 500ms
        LED1(1);
        LED2(1);
        LED3(1);
        Delay_us(50000);
    }
}

posted on   失落''80  阅读(11)  评论(0编辑  收藏  举报

编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
点击右上角即可分享
微信分享提示