pwm.h内容
#ifndef _PWM_H
#define _PWM_H
#include "lpc17xx.h"
#define PWM_CYCLE 1200
#define PWM_OFFSET 200
#define MR0_INT 1 << 0
#define MR1_INT 1 << 1
#define MR2_INT 1 << 2
#define MR3_INT 1 << 3
#define MR4_INT 1 << 8
#define MR5_INT 1 << 9
#define MR6_INT 1 << 10
#define TCR_CNT_EN 0x00000001//TCR定时器控制寄存器使能
#define TCR_RESET 0x00000002 //TCR定时器控制寄存器复位
#define TCR_PWM_EN 0x00000008//TCR定时器控制寄存器使能PWM模式
//MCR--PWM匹配控制寄存器,PWM匹配控制寄存器用来控制在PWM匹配寄存器与PWM定时器计数器匹配时所执行的
//操作
#define PWMMR0I 1 << 0
#define PWMMR0R 1 << 1
#define PWMMR0S 1 << 2
#define PWMMR1I 1 << 3
#define PWMMR1R 1 << 4
#define PWMMR1S 1 << 5
#define PWMMR2I 1 << 6
#define PWMMR2R 1 << 7
#define PWMMR2S 1 << 8
#define PWMMR3I 1 << 9
#define PWMMR3R 1 << 10
#define PWMMR3S 1 << 11
#define PWMMR4I 1 << 12
#define PWMMR4R 1 << 13
#define PWMMR4S 1 << 14
#define PWMMR5I 1 << 15
#define PWMMR5R 1 << 16
#define PWMMR5S 1 << 17
#define PWMMR6I 1 << 18
#define PWMMR6R 1 << 19
#define PWMMR6S 1 << 20
//PCR----PWM控制寄存器,用来使能和选择每个PMW通道的类型
#define PWMSEL2 1 << 2
#define PWMSEL3 1 << 3
#define PWMSEL4 1 << 4
#define PWMSEL5 1 << 5
#define PWMSEL6 1 << 6
#define PWMENA1 1 << 9
#define PWMENA2 1 << 10
#define PWMENA3 1 << 11
#define PWMENA4 1 << 12
#define PWMENA5 1 << 13
#define PWMENA6 1 << 14
//LER---PWM锁存使能寄存器,当 PWM 匹配寄存器用来产生 PWM 时, PWM 锁存使能寄存器用来控制 PWM 匹配寄存器
//的更新。
#define LER0_EN 1 << 0
#define LER1_EN 1 << 1
#define LER2_EN 1 << 2
#define LER3_EN 1 << 3
#define LER4_EN 1 << 4
#define LER5_EN 1 << 5
#define LER6_EN 1 << 6
extern uint32_t PWM_Init(uint32_t ChannelNum,uint32_t cycle);
extern void PWM_Set(uint32_t ChannelNum,uint32_t cycle,uint32_t offset);
extern void PWM_Start (uint32_t ChannelNum);
extern void PWM_Stop(uint32_t ChannelNum);
extern void LED_Light(void);
#endif
pwm.c内容
#include "lpc17xx.h"
#include "pwm.h"
/*******************************************************************************
** uint32_t PWM_Init(uint32_t ChannelNum,uint32_t cycle)
**PWM初始化
**PWM号:uint32_t ChannelNum
**周期值: uint32_t cycle
**
********************************************************************************/
uint32_t PWM_Init(uint32_t ChannelNum,uint32_t cycle)
{
LPC_GPIO0->FIODIR = 0x00200000; // p0.21设置为输出
LPC_GPIO0->FIOPIN |= 0x00200000; // p0.21=1,蒙蔽锁存器对LED灯的影响
if(ChannelNum==1)
{
LPC_PINCON->PINSEL4=0x00000555;//设置p2.00~p2.05为pwm输出引脚
LPC_PWM1->TCR = TCR_RESET;
LPC_PWM1->PR = 0x00;//预分频寄存器设置清零
LPC_PWM1->MCR = PWMMR0R;//设置匹配可控制寄存器
LPC_PWM1->MR0 = cycle;//设置pwm匹配值
LPC_PWM1->MR1 = cycle*5/6;
LPC_PWM1->MR2 = cycle*4/6;
LPC_PWM1->MR3 = cycle*3/6;
LPC_PWM1->MR4 = cycle*2/6;
LPC_PWM1->MR5 = cycle*1/6;
LPC_PWM1->MR6 = 0;
//使能PWM
LPC_PWM1->LER = LER0_EN|LER1_EN | LER2_EN | LER3_EN | LER4_EN | LER5_EN | LER6_EN ;
}
else
{
return (0);//unknow channel number
}
return (1);
}
/**************************************************************
**PWM_Set(uint32_t ChannelNum,uint32_t cycle,uint32_t offset)
**设置pwm的周期值,匹配值,周期值
**匹配值:uint32_t cycle
**pwm号:uint32_t ChannelNum
**周期值:uint32_t offset
**************************************************************/
void PWM_Set(uint32_t ChannelNum,uint32_t cycle,uint32_t offset)
{
if(1==ChannelNum)
{
LPC_PWM1->MR0 = cycle;//设置pwm匹配寄存器的值
LPC_PWM1->MR1 = cycle *5/6 +offset;
LPC_PWM1->MR2 = cycle *4/6 +offset;
LPC_PWM1->MR3 = cycle *3/6 +offset;
LPC_PWM1->MR4 = cycle *2/6 +offset;
LPC_PWM1->MR5 = cycle *1/6 +offset;
LPC_PWM1->MR6 = offset;
LPC_PWM1->LER = LER0_EN|LER1_EN | LER2_EN | LER3_EN | LER4_EN | LER5_EN | LER6_EN ;
}
}
/*********************************************
**void PWM_Start(uint32_t ChannelNum)
**启动PWM号
**********************************************/
void PWM_Start (uint32_t ChannelNum)
{
if(ChannelNum==1)
{
LPC_PWM1->PCR = PWMENA1 | PWMENA2 | PWMENA3 | PWMENA4 | PWMENA5 | PWMENA6 ;//all single edge(边沿),all enable
LPC_PWM1->TCR =TCR_CNT_EN | TCR_PWM_EN;//counter enable,PWM enable
}
}
/*********************************************
**void PWM_Stop(uint32_t ChannelNum)
**停止PWM号
**********************************************/
void PWM_Stop(uint32_t ChannelNum)
{
if(ChannelNum==1)
{
LPC_PWM1->PCR = 0;
LPC_PWM1->TCR = 0x00;//stop all PWMs
}
return;
}
void LED_Light(void)
{
int led_fx=1;
int led_dt =0;
if(led_fx==1)
{
led_dt++;
}
else{
led_dt--;
}
if(led_dt>1200) led_fx = 0;
if(led_dt==0) led_fx=1;
LPC_PWM1->MR0 =led_dt;
LPC_PWM1->MR1 = led_dt;
LPC_PWM1->LER = LER0_EN|LER1_EN | LER2_EN | LER3_EN | LER4_EN | LER5_EN | LER6_EN ;
LPC_PWM1->PCR = PWMENA1 | PWMENA2 | PWMENA3 | PWMENA4 | PWMENA5 | PWMENA6 ;//all single edge(边沿),all enable
LPC_PWM1->TCR =TCR_CNT_EN | TCR_PWM_EN;//counter enable,PWM enable
}
//例程(main里面的例程)
// uint32_t cycle = PWM_CYCLE, offset = 0;
// LPC_GPIO0->FIODIR = 0x00200000;
// LPC_GPIO0->FIOPIN |= 0x00200000; /* 屏蔽锁存器对LED灯的影响 */
// LPC_GPIO2->FIODIR = 0x000000c0; //设置p2.6,2.7LEED为输出
// if ( PWM_Init( 1, 0 ) != 1 ) /* 屏蔽锁存器对LED灯的影响 */
// {
// while( 1 );
// }
// PWM_Set( 1, cycle, offset ); /*设置PWM的周期值,以及匹配值*/
// PWM_Start( 1 ); /* pwm输出 */
// while ( 1 )
// {}
/***********led灯亮度逐渐变化**********/
// uint32_t cycle = PWM_CYCLE,offset=0;
// uint32_t led_fx=1;
// uint32_t led_dt =0;
// uint32_t i=0,j=0;
// LPC_GPIO2->FIODIR = 0x000000c0;
// if(PWM_Init(1,0)!=1)//
// {
// while(1);
// }
// LPC_PWM1->MR0 =1200;/**************使能pwm前,匹配寄存器MR0的值一定要设置*********/
// while (1)
// {
// Delay(10);
// for(i=0;i<1200;i++)
// {
// LPC_PWM1->MR1=j;
// j++;
// Delay(15);
// LPC_PWM1->LER = LER0_EN|LER1_EN | LER2_EN | LER3_EN | LER4_EN | LER5_EN | LER6_EN ;
// LPC_PWM1->PCR = PWMENA1; //| PWMENA2 | PWMENA3 | PWMENA4 | PWMENA5 | PWMENA6 ;//all single edge(边沿),all enable
// LPC_PWM1->TCR =TCR_CNT_EN | TCR_PWM_EN;//counter enable,PWM enable
// }
// Delay(100);
// for(i=0;i<1200;i++)
// {
// LPC_PWM1->MR1=j;
// j--;
// Delay(15);
// LPC_PWM1->LER = LER0_EN|LER1_EN | LER2_EN | LER3_EN | LER4_EN | LER5_EN | LER6_EN ;
// LPC_PWM1->PCR = PWMENA1; //| PWMENA2 | PWMENA3 | PWMENA4 | PWMENA5 | PWMENA6 ;//all single edge(边沿),all enable
// LPC_PWM1->TCR =TCR_CNT_EN | TCR_PWM_EN;//counter enable,PWM enable
// }
// }
// }
main.c内容
#include "lpc17xx.h"
#include "pwm.h"
int main(void)
{
uint32_t cycle = PWM_CYCLE,offset=0;
uint32_t i=0,j=0;
SystemInit();
LPC_GPIO2->FIODIR = 0x000000c0;
if(PWM_Init(1,0)!=1)//蒙蔽锁存器对LED灯的影响并初始化PWM
{
while(1);
}
LPC_PWM1->MR0 =1200;//在使能pwm前要赋值匹配寄存器MR0
while (1)
{
Delay(10);
//LED逐渐变亮,然后逐渐变暗
for(i=0;i<1200;i++)
{
LPC_PWM1->MR1=j;
j++;
Delay(15);
LPC_PWM1->LER = LER0_EN|LER1_EN | LER2_EN | LER3_EN | LER4_EN | LER5_EN | LER6_EN ;
LPC_PWM1->PCR = PWMENA1| PWMENA2 ;//| PWMENA3 | PWMENA4 | PWMENA5 | PWMENA6 ;//all single edge enable
LPC_PWM1->TCR =TCR_CNT_EN | TCR_PWM_EN;//counter enable,PWM enable
}
Delay(100);
for(i=0;i<1200;i++)
{
LPC_PWM1->MR1=j;
j--;
Delay(15);
LPC_PWM1->LER = LER0_EN|LER1_EN | LER2_EN | LER3_EN | LER4_EN | LER5_EN | LER6_EN ;
LPC_PWM1->PCR = PWMENA1| PWMENA2 ;//| PWMENA3 | PWMENA4 | PWMENA5 | PWMENA6 ;//all single edge(±ßÑØ£©£¬all enable
LPC_PWM1->TCR =TCR_CNT_EN | TCR_PWM_EN;//counter enable,PWM enable
}
}
}