gpio控制风扇、蜂鸣器、马达
CH1.h
#ifndef __CH1_H__
#define __CH1_H__
typedef struct
{
unsigned int MODER;
unsigned int OTYPER;
unsigned int OSPEEDR;
unsigned int OPUPDR ;
unsigned int IDR;
unsigned int ODR;
unsigned int BSRR;
}gpio_y;
#define GPIOB ((gpio_y *)0x50003000)
#define GPIOE ((gpio_y *)0x50006000)
#define GPIOF ((gpio_y *)0x50007000)
#define RCC (*(unsigned int *)0x50000A28)
void TIM1_init();
void TIM1_ON();
void TIM1_OFF();
void TIM4_init();
void TIM4_ON();
void TIM4_OFF();
void TIM16_init();
void TIM16_ON();
void TIM16_OFF();
#endif
CH1.c
#include "CH1.h"
//风扇的初始化
void TIM1_init()
{
//GPIOE时钟使能
RCC |= (0x3 << 4);
//PE9设置为输出
GPIOE->MODER &= (~(0X3 << 18));
GPIOE->MODER |= (0X1 << 18);
//PE9设置为推挽输出
GPIOE->OTYPER &= (~(0X1 << 9));
//PE9设置为低速输出
GPIOE->OSPEEDR &= (~(0X3 << 18));
//PE9无上拉下拉电阻
GPIOE->OPUPDR &= (~(0X3 << 18));
//默认输出低电平
GPIOE->ODR &= (~(0X1 << 9));
}
//蜂鸣器初始化
void TIM4_init()
{
//GPIOB时钟使能
RCC |= (0x3 << 4);
//PB6设置为输出
GPIOB->MODER &= (~(0X3 << 12));
GPIOB->MODER |= (0X1 << 12);
//PB6设置为推挽输出
GPIOB->OTYPER &= (~(0X1 << 6));
//PB6设置为低速输出
GPIOB->OSPEEDR &= (~(0X3 << 12));
//PB6无上拉下拉电阻
GPIOB->OPUPDR &= (~(0X3 << 12));
//默认输出低电平
GPIOB->ODR &= (~(0X1 << 6));
}
//马达初始化
void TIM16_init()
{
//GPIOF时钟使能
RCC |= (0x3 << 4);
//PF6设置为输出
GPIOF->MODER &= (~(0X3 << 12));
GPIOF->MODER |= (0X1 << 12);
//PF6设置为推挽输出
GPIOF->OTYPER &= (~(0X1 << 6));
//PF6设置为低速输出
GPIOF->OSPEEDR &= (~(0X3 << 12));
//PF6无上拉下拉电阻
GPIOF->OPUPDR &= (~(0X3 << 12));
//默认输出低电平
GPIOF->ODR &= (~(0X1 << 6));
}
// 风扇转动
void TIM1_ON()
{
GPIOE->ODR |= (0X1 << 9);
}
// 风扇关闭
void TIM1_OFF()
{
GPIOE->ODR |= (~(0X1 << 9));
}
// 蜂鸣器启动
void TIM4_ON()
{
GPIOB->ODR |= (0X1 << 6);
}
// 蜂鸣器关闭
void TIM4_OFF()
{
GPIOB->ODR |= (~(0X1 << 6));
}
// 马达启动
void TIME16_ON()
{
GPIOF->ODR |= (0X1 << 6);
}
// 马达关闭
void TIM16_OFF()
{
GPIOF->ODR |= (~(0X1 << 6));
}
main.c
#include "CH1.h"
// 延时函数
void delay(int ms)
{
int i,j;
for(i=0;i<ms;i++)
{
for(j=0;j<2000;j++)
{}
}
}
int main()
{
TIM1_init();
TIM4_init();
TIM16_init();
while (1)
{
TIM1_ON();
TIM4_ON();
TIM16_ON();
delay(1000);
TIM1_OFF();
TIM4_OFF();
TIM16_OFF();
delay(1000);
}
return 0;
}