跑马灯实验(正点原子战舰版——stm32F103ZET6)

8 篇文章 0 订阅

库函数版本

一,总体过程

  • 使能IO口时钟。调用函数RCC_APB2PeriphColckCmd();
    (不同的IO组,调用的时钟使能函数不一样。)
  • 初始化IO口模式。调用函数GPIO_Init();
  • 操作IO口,输出高低电平.
    GPIO_SetBits();
    GPIO_ResetBits();

二,工程文件创建

1,在新建工程模板里新建“HAREWARE”文件夹,在里面创建LED文件夹,在里面新建led.c和led.h文件,添加到HAREWARE工程里去:
在这里插入图片描述
2,将LED文件夹添加到头文件库中去:
在这里插入图片描述
在这里插入图片描述

三,工程代码编写

分别编写led.c,led.h,main.c三个文件,这些代码其实都是根据最初说到的步骤,一步步参考固件库里面编写的。编写完后代码如下:

//led.h
#ifndef __LED_H
#define __LED_H

void LED_Init(void);

#endif

//led.c
#include "LED.h"
#include "stm32f10x.h"

void LED_Init(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE, ENABLE);

	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5;
	GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(GPIOB, &GPIO_InitStructure);
	GPIO_SetBits(GPIOB, GPIO_Pin_5);
	
	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5;
	GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(GPIOE, &GPIO_InitStructure);
	GPIO_SetBits(GPIOE, GPIO_Pin_5);
}
//main.c
#include "stm32f10x.h"
#include "LED.h"
#include "delay.h"

int main(void)
 {	
	delay_init();
	LED_Init();
	
	while(1)
	{
		GPIO_SetBits(GPIOB, GPIO_Pin_5);
		GPIO_ResetBits(GPIOB, GPIO_Pin_5);
		delay_ms(400);
		
		GPIO_SetBits(GPIOB, GPIO_Pin_5);
		GPIO_ResetBits(GPIOB, GPIO_Pin_5);
		delay_ms(400);
	}
 }

四,生成hex文件,烧写到stm32中去

在这里插入图片描述

寄存器版本

一,总体过程

  • 使能IO口时钟。配置寄存器RCC_ APB2ENR;
  • 初始化IO口模式。配置寄存器GPIOx_CRH/CRL;
  • 操作IO口,输出高低电平。配置寄存器;
    GPIOX ODR或者BSRR/BRR。

二、四过程和库函数版本一样,接下来介绍:三,代码编写

//led.h
#ifndef __LED_H
#define __LED_H

void LED_Init(void);

#endif

//led.c
#include "led.h"
#include "stm32f10x.h"
#include "delay.h"

void LED_Init(void)
{
	//先使能时钟,将RCC_APB2ENR寄存器的第三位和第六位置1:
	RCC ->APB2ENR|=1<<3;
	RCC ->APB2ENR|=1<<6;
	
	//GPIOB.5设置为通用推挽输出,50MHz,通过配置低寄存器CRL来实现:
	GPIOB->CRL&=0xFF0FFFFF;//清零
	GPIOB->CRL|=0x00300000;//置1
	//让GPIOB输出高电平:
	GPIOB->ODR|=1<<5;
	
	
	//GPIOE.5
	GPIOE->CRL&=0xFF0FFFFF;//清零
	GPIOE->CRL|=0x00300000;//置1
	//让GPIOE输出高电平:
	GPIOE->ODR|=1<<5;
}

//main.c
#include "led.h"
#include "stm32f10x.h"
#include "delay.h"

int main(void)
{
	delay_init();
	LED_Init();
	
	while(1)
	{
		GPIOB->ODR|=1<<5;
		GPIOE->ODR|=1<<5;
		delay_ms(400);
		
		GPIOB->ODR|=~(1<<5);
		GPIOE->ODR|=~(1<<5);
		delay_ms(400);
	}
}

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值