本来是要实现通过PWM的脉宽来实现LED灯的亮度,LED为PC7口,芯片为STM32f103VE,现在不知道为什么灯都不会亮,求高手帮忙解惑,谢谢!
#include<stm32f10x_lib.h>
#include"sys.h"
#include"delay.h"
#include"pwm.h"
int main(void)
{
u16 led2pwmval=0;
u8 dir=1; //亮灯方向
Stm32_Clock_Init(9);
delay_init(72);
PWM_Init(900,0);//pwm频率72000/900=8K
while(1)
{
delay_ms(10);
if(dir)
led2pwmval++;
else
led2pwmval--;
if(led2pwmval>300)
dir=0;
if(led2pwmval<=0)
dir=1;
LED2_PWM_VAL=led2pwmval;
}
}
void PWM_Init(u16 arr,u16 psc)
{
RCC->APB1ENR|=1<<1;
RCC->APB2ENR|=1<<4;//PC7
GPIOC->CRL&=0X0FFFFFFF;
GPIOC->CRL|=0XB0000000;
GPIOC->ODR|=1<<7;
TIM3->ARR=arr;
TIM3->PSC=psc;
TIM3->CCMR1|=7<<12;
TIM3->CCMR1|=1<<11;
TIM3->CCER|=1<<4;
TIM3->CR1=0X0080;
TIM3->CR1|=0X01;
}
#ifndef __PWM_H
#define __PWM_H
#include"sys.h"
#define LED2_PWM_VAL TIM3->CCR2
void PWM_Init(u16 arr,u16 psc);
#endif
|