2740 views|8 replies

1131

Posts

17

Resources
The OP
 

[National Technology N32G457 Review] 4. PWM and breathing light test [Copy link]

 

My related posts:

[National Technology N32G457 Review] 1. Data Preparation

[National Technology N32G457 Review] 2. Unboxing + Lighting

[National Technology N32G457 Review] III. ADC and Serial Port Function Test


PWM initialization code

LED3 flashes at a frequency of 100HZ, with light and dark gradually changing to create a breathing effect.


void PwmInit(void)		//PWM定时器初始化	
{
	
	//开启T1时钟 + AFIO
    RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_TIM1|RCC_APB2_PERIPH_AFIO,ENABLE);
	
	//GPIO引脚复用
	//PA8/TIM1_CH1/LED1
    GPIO_InitType gpio;
    gpio.Pin=GPIO_PIN_8;       
    gpio.GPIO_Mode=GPIO_Mode_AF_PP;
    gpio.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_InitPeripheral(GPIOA,&gpio);
	//tim1部分引脚映射
	GPIO_ConfigPinRemap(GPIO_PART1_RMP_TIM1,ENABLE);

	
	//tim1定时器相关
	//144M/14400=10KHZ  / 10000 =1HZ
	//	/100=100hz
	TIM_TimeBaseInitType tim_base;
    tim_base.Prescaler = 14400;
    tim_base.CntMode   = TIM_CNT_MODE_UP;
    tim_base.Period    = 100;
    tim_base.ClkDiv    = 0;//TIM_CLK_DIV4;
    tim_base.RepetCnt  = 0;	
	TIM_InitTimeBase(TIM1, &tim_base);
	
	OCInitType tim;	
    tim.OcMode       = TIM_OCMODE_PWM1;
    tim.OutputState  = TIM_OUTPUT_STATE_ENABLE;
    tim.OutputNState = TIM_OUTPUT_NSTATE_ENABLE;
    tim.Pulse        = 5000;		//占空比
    tim.OcPolarity   = TIM_OC_POLARITY_LOW;
    tim.OcNPolarity  = TIM_OCN_POLARITY_HIGH;
    tim.OcIdleState  = TIM_OC_IDLE_STATE_SET;
    tim.OcNIdleState = TIM_OC_IDLE_STATE_RESET;
	
	TIM_InitOc1(TIM1,&tim);
	
	TIM_Enable(TIM1,ENABLE);
	
	TIM_EnableCtrlPwmOutputs(TIM1,ENABLE);
	
}

Main running code, calling this function once every 10ms, looping once every 1 second

void Task2(void){		//呼吸灯	10ms
	static uint16_t count=0;
	static int dir=0;
	
	TIM_SetCmp1(TIM1,count);
	if(dir==0){
		count++;
		if(count>100){
			dir=!dir;
		}
	}else{
		count--;
		if(count>65000){
			count=0;
			dir=!dir;
		}		
	}
}

Related references

#include "n32g45x.h"
#include "n32g45x_gpio.h"
#include "bsp.h"
#include <n32g45x_tim.h>

Disclaimer: All my posts are original and hand-typed by me, not something that can be done by just downloading a DEMO and running it.

Video effects:

Upload on the second floor

This post is from Domestic Chip Exchange

Latest reply

Breathing light is good I have used both op amps and 555. I think PWM might work better.   Details Published on 2022-1-27 13:54
 
 

1131

Posts

17

Resources
2
 


Operation effect

I am preparing to go back to my hometown, and will review the rest after the New Year.

This post is from Domestic Chip Exchange

Comments

Of course, you can bring it back to your hometown!  Details Published on 2022-1-25 13:31
 
 
 

6841

Posts

11

Resources
3
 
fxyc87 posted on 2022-1-25 12:16 Running effect I am preparing to go back to my hometown, and the rest will be evaluated after the new year.

Sure, you can bring it back to your hometown!

This post is from Domestic Chip Exchange
 
 
 

1412

Posts

3

Resources
4
 

It is better to test it after the New Year. Going home to spend time with your family is the right thing to do.

This post is from Domestic Chip Exchange
Personal signature

没有什么不可以,我就是我,不一样的烟火! 

 
 
 

2w

Posts

74

Resources
5
 

I appreciate the serious attitude of the host:)

This post is from Domestic Chip Exchange
Add and join groups EEWorld service account EEWorld subscription account Automotive development circle
Personal signature

加油!在电子行业默默贡献自己的力量!:)

 
 
 

2w

Posts

74

Resources
6
 

Happy New Year~~

This post is from Domestic Chip Exchange
Add and join groups EEWorld service account EEWorld subscription account Automotive development circle

Comments

Thanks.  Details Published on 2022-1-26 12:12
Personal signature

加油!在电子行业默默贡献自己的力量!:)

 
 
 

1131

Posts

17

Resources
7
 

Thanks.

This post is from Domestic Chip Exchange
 
 
 

1582

Posts

0

Resources
8
 

Breathing light is good

I have used both op amps and 555.

I think PWM might work better.

This post is from Domestic Chip Exchange

Comments

It is PWM. The PWM duty cycle is modified every 10mS.  Details Published on 2022-2-1 13:06
 
 
 

1131

Posts

17

Resources
9
 
se7ens posted on 2022-1-27 13:54 The breathing light is good. I have used op amps and 555 to adjust it. I feel that PWM may have a better effect

It is PWM. The PWM duty cycle is modified every 10mS.

This post is from Domestic Chip Exchange
 
 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews

Room 1530, Zhongguancun MOOC Times Building, Block B, 18 Zhongguancun Street, Haidian District, Beijing 100190, China Tel:(010)82350740 Postcode:100190

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list