STM32位带实现流水灯

led.c

#include"led.h"
#include"bitband.h"
void Led_Init(void)
{
	GPIO_InitTypeDef GPIO_VALUE; //???
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);//???
	GPIO_VALUE.GPIO_Mode=GPIO_Mode_Out_PP;//???? ????
	GPIO_VALUE.GPIO_Pin=GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3;//????
	GPIO_VALUE.GPIO_Speed=GPIO_Speed_50MHz;//????
	GPIO_Init(GPIOC,&GPIO_VALUE);//???	
}
void Led_On(int nu)
{
	switch(nu)
	{
		case 0:PCOut(1)=1;break;
		case 1:PCOut(2)=1;break;
		case 2:PCOut(3)=1;break;
	}
		
}
void Led_Off(int nu)
{
	switch(nu)
	{
		case 0:PCOut(1)=0;break;
		case 1:PCOut(2)=0;break;
		case 2:PCOut(3)=0;break;
	}	
}

led.h

#ifndef __LED_H
#define __LED_H
#include "stm32f10x_conf.h"

extern void Led_Init(void);
extern void Led_On(int opt);
extern void Led_Off(int opt);
#endif 

bitband.h

#ifndef __BITBAND_H
#define __BITBAND_H

#include "stm32f10x_conf.h"


#define BIT_BAND(ADDR,BITNUM) ((ADDR & 0xF0000000) + 0x2000000 + ((ADDR & 0xFFFFF) << 5) + (BITNUM << 2))

#define MEM_ADDR(ADDR) *(volatile unsigned int *)(ADDR)


#define BITBAND(ADDR,BITNUM) MEM_ADDR(BIT_BAND(ADDR,BITNUM))

#define GPIOA_IDR_ADDR (GPIOA_BASE + 0x08)
#define GPIOA_ODR_ADDR (GPIOA_BASE + 0x0C)

#define GPIOB_IDR_ADDR (GPIOB_BASE + 0x08)
#define GPIOB_ODR_ADDR (GPIOB_BASE + 0x0C)

#define GPIOC_IDR_ADDR (GPIOC_BASE + 0x08)
#define GPIOC_ODR_ADDR (GPIOC_BASE + 0x0C)

#define GPIOD_IDR_ADDR (GPIOD_BASE + 0x08)
#define GPIOD_ODR_ADDR (GPIOD_BASE + 0x0C)

#define PAOut(n) BITBAND(GPIOA_ODR_ADDR, n)
#define PAIn(n)  BITBAND(GPIOA_IDR_ADDR, n)

#define PBOut(n) BITBAND(GPIOB_ODR_ADDR, n)
#define PBIn(n)  BITBAND(GPIOB_IDR_ADDR, n)

#define PCOut(n) BITBAND(GPIOC_ODR_ADDR, n)
#define PCIn(n)  BITBAND(GPIOC_IDR_ADDR, n)

#define PDOut(n) BITBAND(GPIOD_ODR_ADDR, n)
#define PDIn(n)  BITBAND(GPIOD_IDR_ADDR, n)

#endif

main.c

#include "led.h"


int main(void)
{
	int i,j;
	Led_Init();
	while(1){
		Led_On(0);
	for(i=0;i<1000;i++)
	  for(j=0;j<1000;j++);
	Led_Off(0);
	
		Led_On(1);
	for(i=0;i<1000;i++)
	  for(j=0;j<1000;j++);
	Led_Off(1);
	
		Led_On(2);
	for(i=0;i<1000;i++)
	  for(j=0;j<1000;j++);
	Led_Off(2);
	}
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值