PWM硬件是任何现代微控制器的另一个基本要求。我们可以将PWM用于许多应用,例如电机控制,照明控制,开关模式电源(SMPS)等。借助PWM,我们还可以模拟数模转换器(DAC)。幸运的是,N76E003带有一个独立的PWM模块,该模块不属于任何内部定时器。它具有您可以想象的所有功能。它可用于生成简单的独立6个单通道PWM。它还可以用于生成具有死区时间功能的互补和相互依赖的PWM。
#include "N76E003.h"
#include "SFR_Macro.h"
#include "Function_define.h"
#include "Common.h"
#include "Delay.h"
#include "soft_delay.h"
unsigned int R_value[10] = {20, 150, 250, 360, 440, 560, 680, 820, 900, 1020};
unsigned int G_value[10] = {440, 560, 680, 820, 900, 1020, 20, 150, 250, 360};
unsigned int B_value[10] = {900, 1020, 20, 150, 250, 360, 440, 560, 680, 820};
void set_PWM_period(unsigned int value);
void set_PWM0(unsigned int value);
void set_PWM1(unsigned int value);
void set_PWM2(unsigned int value);
void set_PWM3(unsigned int value);
void set_PWM4(unsigned int value);
void set_PWM5(unsigned int value);
void main(void)
{
signed int i = 0;
signed char j = 0;
P01_PushPull_Mode;
P10_PushPull_Mode;
P11_PushPull_Mode;
PWM1_P11_OUTPUT_ENABLE;
PWM2_P10_OUTPUT_ENABLE;
PWM4_P01_OUTPUT_ENABLE;
PWM_IMDEPENDENT_MODE;
PWM_EDGE_TYPE;
set_CLRPWM;
PWM_CLOCK_FSYS;
PWM_CLOCK_DIV_64;
PWM_OUTPUT_ALL_NORMAL;
set_PWM_period(1023);
set_PWMRUN;
while(1)
{
for(i = 0; i < 1024; i += 10)
{
set_PWM1(i);
delay_ms(20);
}
for(i = 1023; i > 0; i -= 10)
{
set_PWM1(i);
delay_ms(20);
}
for(i = 0; i < 1024; i += 10)
{
set_PWM2(i);
delay_ms(20);
}
for(i = 1023; i > 0; i -= 10)
{
set_PWM2(i);
delay_ms(20);
}
for(i = 0; i < 1024; i += 10)
{
set_PWM4(i);
delay_ms(20);
}
for(i = 1023; i > 0; i -= 10)
{
set_PWM4(i);
delay_ms(20);
}
delay_ms(600);
for(i = 0; i <=9; i++)
{
for(j = 0; j <= 9; j++)
{
set_PWM4(R_value[j]);
set_PWM1(G_value[j]);
set_PWM2(B_value[j]);
delay_ms(200);
}
for(j = 9; j >= 0; j--)
{
set_PWM4(R_value[j]);
set_PWM1(G_value[j]);
set_PWM2(B_value[j]);
delay_ms(200);
}
}
delay_ms(600);
}
}
void set_PWM_period(unsigned int value)
{
PWMPL = (value & 0x00FF);
PWMPH = ((value & 0xFF00) >> 8);
}
void set_PWM0(unsigned int value)
{
PWM0L = (value & 0x00FF);
PWM0H = ((value & 0xFF00) >> 8);
set_LOAD;
}
void set_PWM1(unsigned int value)
{
PWM1L = (value & 0x00FF);
PWM1H = ((value & 0xFF00) >> 8);
set_LOAD;
}
void set_PWM2(unsigned int value)
{
PWM2L = (value & 0x00FF);
PWM2H = ((value & 0xFF00) >> 8);
set_LOAD;
}
void set_PWM3(unsigned int value)
{
PWM3L = (value & 0x00FF);
PWM3H = ((value & 0xFF00) >> 8);
set_LOAD;
}
void set_PWM4(unsigned int value)
{
set_SFRPAGE;
PWM4L = (value & 0x00FF);
PWM4H = ((value & 0xFF00) >> 8);
clr_SFRPAGE;
set_LOAD;
}
void set_PWM5(unsigned int value)
{
set_SFRPAGE;
PWM5L = (value & 0x00FF);
PWM5H = ((value & 0xFF00) >> 8);
clr_SFRPAGE;
set_LOAD;
}
说明[size=15.2015px]N76E003的PWM硬件生成的PWM基于比较匹配原理,这在其框图中很明显。有6个PWM通道,它们可以独立使用或作为相互依存的组使用,以形成互补的PWM。N76E003生成的PWM可以根据用户要求为边沿对齐或居中对齐的PWM。PWM模块可以由系统时钟或定时器1溢出直接提供时钟。此外,还有一个预分频器单元可减少输入时钟源。 [size=15.2015px]为了演示N76E003的PWM功能,我使用了RGB LED。使用PWM通道1,2和4生成了三个独立的PWM。让我们研究一下设置PWM通道和PWM硬件的过程。由于PWM是GPIO引脚的输出功能,因此必须首先将PWM GPIO设置为推挽式GPIO。我们还必须启用我们要使用的PWM输出通道。
|