PWN模式下的蜂鸣/风扇/马达启动寄存器设置
PWN.h:
#ifndef __PWN_H__
#define __PWN_H__
#include "stm32mp1xx_rcc.h"
#include "stm32mp1xx_gpio.h"
#include "stm32mp1xx_tim.h"
//RCC章节初始化
void RCC_MP_AHB4ENSETR();
void RCC_MP_APB1ENSETR();
//GPIOB章节初始化
void GPIOB_MODER();
void GPIOB_AFRL();
//TIM4章节
void TIM4_CR1();
void TIM4_CCMR1();
void TIM4_CCER();
void TIM4_PSC();
void TIM4_ARR();
void TIM4_CCR1();
void all1();
void mada_init();
void fengshan_init();
#endif
PWN.C:
#include "pwn.h"
void all1()
{
RCC_MP_AHB4ENSETR();
RCC_MP_APB1ENSETR();
GPIOB_MODER();
GPIOB_AFRL();
TIM4_CR1();
TIM4_CCMR1();
TIM4_CCER();
TIM4_PSC();
TIM4_ARR();
TIM4_CCR1();
}
//RCC章节初始化
void RCC_MP_AHB4ENSETR()
{
RCC->MP_AHB4ENSETR |= (0x1 << 1);
}
void RCC_MP_APB1ENSETR()
{
RCC->MP_APB1ENSETR |= (0x1 << 2);
}
//GPIOB章节初始化
void GPIOB_MODER()
{
GPIOB->MODER &= (!(0x3 <<12));
GPIOB->MODER |= (0x1 << 13);
}
void GPIOB_AFRL()
{
GPIOB->AFRL &= (!(0xf <<24));
GPIOB->AFRL |= (0x1 << 25);
}
//TIM4章节
void TIM4_CR1()
{
TIM4->CR1 |= (0x1 << 0);
TIM4->CR1 &= (!(0xf <<4));
TIM4->CR1 |= (0x9 <<4);
}
void TIM4_CCMR1()
{
TIM4->CCMR1 &= (0x0 <<16);
TIM4->CCMR1 |= (0x7 <<4);
TIM4->CCMR1 &= (0x0 <<4);
TIM4->CCMR1 |=(0x1 <<3);
TIM4->CCMR1 &=(!(0x3 <<0));
}
void TIM4_CCER()
{
TIM4->CCER |= (0x1 <<3);
TIM4->CCER &= (0x0 <<1);
TIM4->CCER |= (0X1 <<0);
}
void TIM4_PSC()
{
TIM4->PSC =0xD0;
}
void TIM4_ARR()
{
TIM4->ARR =0x3E8;
}
void TIM4_CCR1()
{
TIM4->CCR1 = 0x12c;
}
/********************马达*************************/
void mada_init()
{ //1.RCC章节 初始地址:0x44007000 pf6 TIM16-CH1
RCC->MP_AHB4ENSETR |= (0x1 << 5);
RCC->MP_APB2ENSETR |= (0x1 << 3); //APB2总线 地址0xA08
//2.GPIOF章节
GPIOF->MODER &= (!(0x3 <<12));
GPIOF->MODER |= (0x1 << 13);
GPIOF->AFRL &= (!(0xf <<24));
GPIOF->AFRL |= (0x1 <<24 );
//3.TIM16章节
//PSC
TIM16->PSC =0xD0;
//ARR
TIM16->ARR =0x3E8;
//CCR1
TIM16->CCR1 = 0x12c;
//CR1
TIM16->CR1 |= (0x1 << 0); //0 1
TIM16->CR1 |= (0x1 << 7); //7 1
//CCMR1
TIM16->CCMR1 &= (!(0x1 <<16)); //16,6:4 0110
TIM16->CCMR1 &= (!(0x7 <<4));
TIM16->CCMR1 |= (0x3<<5);
TIM16->CCMR1 |= (0x1 <<3);
TIM16->CCMR1 &= (!(0x3 <<0));
//CCER
TIM16->CCER |= (0xf <<0);
//BDTR
TIM16->BDTR |= (0x1 << 15);
}
/****************风扇************************/
void fengfan_init()
{
/************RCC初始化***********/
RCC->MP_AHB4ENSETR |= (0x1 << 4);
RCC->MP_APB2ENSETR |= (0x1 << 0);
/***********GPIO初始化***********/
GPIOE->MODER &= (~(0x3 << 18));
GPIOE->MODER |= (0x1 << 19);
GPIOE->AFRH &= (~(0xf << 4));
GPIOE->AFRH |= (0x1 << 4);
/***********TIMI16_CH1初始化***********/
//PSC
TIM1->PSC = 0xD0;//设置分频器
//ARR
TIM1->ARR = 0x1F4;//设置重载计数器,会影响PWM方波的周期和频率
//CCR1
TIM1->CCR1 = 0x12C;//设置比较寄存器,占空比
//CR1
TIM1->CR1 |= (0x1 << 7);
TIM1->CR1 &= (~(0x3 << 5));
TIM1->CR1 |= (0x1 << 4);
TIM1->CR1 |= (0x1 << 0);
//设置为PWM1模式
TIM1->CCMR1 &= (~(0x1 << 16));
TIM1->CCMR1 &= (0x7 << 4);
TIM1->CCMR1 |= (0x3 << 5);
TIM1->CCMR1 |= (0x1 << 3);
TIM1->CCMR1 &= (~(0x3 << 0));
//CCER
TIM1->CCER |= (0x1 << 3);//设置为输出模式
TIM1->CCER &= (~(0x1 << 1));//设置起始电平为高
TIM1->CCER |= (0x1 << 0);//设置输出使能
//BDTR
TIM1->BDTR |= (0x1 << 15);
}
main.c:
#include "pwn.h"
extern void printf(const char *fmt, ...);
void delay_ms(int ms)
{
int i,j;
for(i = 0; i < ms;i++)
for (j = 0; j < 1800; j++);
}
int main(void)
{
// all1();
// mada_init();
fengshan_init();
while(1)
{
}
return 0;
}