ARM 灯亮 以及风扇吹,以及蜂鸣器响

 led.c

#include"led.h"
//延时函数
void delay_ms(int ms)
{
 int i,j;
 for(i = 0; i < ms;i++)
  for (j = 0; j < 1800; j++);
}
//led灯设置初始化 
 void all_led_init()
 {
    RCC |=(0x3<<4);
    //led1
    GPIOE->moder &= (~(0X3<<20));
    GPIOE->moder |= (0X1<<20);
    GPIOE->otyper &=(~(0X1<<10));
    GPIOE->ospeedr &= (~(0X3<<20));
    GPIOE->pupdr &= (~(0X3<<20));
     //led2
    GPIOF->moder &= (~(0X3<<20));
    GPIOF->moder |= (0X1<<20);
    GPIOF->otyper &=(~(0X1<<10));
    GPIOF->ospeedr &= (~(0X3<<20));
    GPIOF->pupdr &= (~(0X3<<20));
     //led3
    GPIOE->moder &= (~(0X3<<16));
    GPIOE->moder |= (0X1<<16);
    GPIOE->otyper &=(~(0X1<<8));
    GPIOE->ospeedr &= (~(0X3<<16));
    GPIOE->pupdr &= (~(0X3<<16));
 }

//风扇
void all_fengshan_init()
{
    RCC |=(0x9<<1);

    GPIOE->moder&= (~(0X3<<18));
    GPIOE->moder |= (0X1<<18);
    GPIOE->otyper &=(~(0X1<<9));
    GPIOE->ospeedr &= (~(0X3<<18));
    GPIOE->pupdr &= (~(0X3<<18));
  }

//蜂鸣器
void all_fengmingqi_init()
{
   
    RCC |=(0x9<<1);
    GPIOB->moder&= (~(0X3<<12));
    GPIOB->moder |= (0X1<<12);
    GPIOB->otyper &=(~(0X1<<6));
    GPIOB->ospeedr &= (~(0X3<<12));
    GPIOB->pupdr &= (~(0X3<<12));
   
}

void  all_led_flash()
{
    //LED1亮
    GPIOE->odr |=(0X1<<10);
    GPIOF->odr &= (~(0X1<<10));
    GPIOE->odr &= (~(0X1<<8));
    delay_ms(1000);
    //LED2亮
    GPIOE->odr &=(~(0X1<<10));
    GPIOF->odr |=(0X1<<10);
    GPIOE->odr &= (~(0X1<<8));
    delay_ms(1000);
    
    //LED3亮
    GPIOE->odr &=(~(0X1<<10));
    GPIOF->odr &= (~(0X1<<10));
    GPIOE->odr |= (0X1<<8);
    delay_ms(1000);

} 
void  all_fengshan_flash()
{
   //风扇
    GPIOE->odr |=(0X1<<9);
    GPIOB->odr &= (~(0X1<<6));
    delay_ms(5000);
}
void  all_fengmingqi_flash()
{
   //蜂鸣器
    GPIOB->odr |=(0X1<<6);
    GPIOE->odr &= (~(0X1<<9));
    delay_ms(5000);
}

led.h

#ifndef __LED_H__
#define __LED_H__

//定义寄存器组织结构体
typedef struct{
    unsigned int moder;
    unsigned int otyper;
    unsigned int ospeedr;
    unsigned int pupdr;
    unsigned int idr;
    unsigned int odr;
}gpio_t;
//定义GPIOE和GPIOF
#define GPIOE ((gpio_t *)0X50006000)
#define GPIOF ((gpio_t *)0X50007000)
#define GPIOB ((gpio_t *)0X50003000)
#define RCC (*(volatile unsigned int *)0X50000A28)
void delay_ms(int ms);
 void all_led_init();
 void  all_led_flash();
void all_fengshan_init();
void all_fengmingqi_init();
void all_fengshan_flash();
void all_fengmingqi_flash();


#endif

你可能感兴趣的:(c语言)