风扇蜂鸣器
1.main.c
#include"led.h"
//封装延时函数
void delay(int ms)
{int i,j;for(i=0;i<ms;i++){for(j=0;j<2000;j++){}}
}
int main()
{//外设初始化all_pplout_init();while(1){BUZZER_ON();delay(2000);BUZZER_OFF();delay(2000);MOTOR_ON();delay(2000);MOTOR_OFF();delay(2000);FAN_ON();delay(2000);FAN_OFF();delay(2000);}return 0;
}
2.led.c
#include "led.h"void all_pplout_init()//led灯的初始化
{//GPIOE/GPIOF时钟使能RCC |= (0x3<<4);RCC |= (0x1<<2);//PE10设置为输出GPIOE.MODER &= (~(0X3<<12));GPIOE.MODER |= (0X1<<12);//PF10设置为输出GPIOE.MODER &= (~(0X3<<18));GPIOE.MODER |= (0X1<<18);//PE8设置为输出GPIOE.MODER &= (~(0X3<<12));GPIOE.MODER |= (0X1<<12);//PE10设置为推挽输出GPIOB.OTYPER &= (~(0X1<<6));//PF10设置为推挽输出GPIOE.OTYPER &= (~(0X1<<9));//PE8设置为推挽输出GPIOF.OTYPER &= (~(0X1<<6));//PE10设置为低速输出GPIOB.OSPEEDR &= (~(0X3<<12));//PF10设置为低速输出GPIOE.OSPEEDR &= (~(0X3<<18));//PE8设置为低速输出GPIOF.OSPEEDR &= (~(0X3<<12));//PE10无上拉下拉电阻GPIOB.OPUPDR &= (~(0X3<<12));//PF10无上拉下拉电阻GPIOE.OPUPDR &= (~(0X3<<18));//PE8无上拉下拉电阻GPIOF.OPUPDR &= (~(0X3<<12));//三盏灯默认输出低电平GPIOB.ODR &= (~(0X1<<6));GPIOE.ODR &= (~(0X1<<9));GPIOF.ODR &= (~(0X1<<6));
}//led1亮
void BUZZER_ON()
{GPIOB.ODR |= (0X1<<6);
}
//led2亮
void MOTOR_ON()
{GPIOE.ODR |= (0X1<<9);
}
//led1亮
void FAN_ON()
{GPIOF.ODR |= (0X1<<6);
}//LED1灭
void BUZZER_OFF()
{GPIOB.ODR &= (~(0X1<<6));
}
//LED2灭
void MOTOR_OFF()
{GPIOE.ODR &= (~(0X1<<9));
}
//LED3灭
void FAN_OFF()
{GPIOF.ODR &= (~(0X1<<6));
}
3.led.h
#ifndef __LED_H__
#define __LED_H__
typedef struct
{unsigned int MODER;unsigned int OTYPER;unsigned int OSPEEDR;unsigned int OPUPDR;unsigned int IDR;unsigned int ODR;
}gpio_t;#define RCC (*(unsigned int *)0X50000A28)
#define GPIOB (*(gpio_t *)0X50003000)
#define GPIOE (*(gpio_t *)0X50006000)
#define GPIOF (*(gpio_t *)0X50007000)void all_pplout_init();
void BUZZER_ON();
void FAN_ON();
void MOTOR_ON();
void BUZZER_OFF();
void FAN_OFF();
void MOTOR_OFF();#endif