在很多的单片机方案应用中,很多都会用到到指示灯,比如流水灯,RGB七彩灯,对了还有呼吸灯.现在用PMS150C做个demo,稍作修改就可以用在pms152,pms132B灯应广其他系列的单片机中.不多说,上代码.一切尽在代码中.
#include "extern.h"
/*定时时间是否到了cinit*/
bit FLAG_NMS;
/*计数值cinit*/
byte count;
/*定时器初始化cinit*/
word T16COUNTER;
byte ucMsCnt;
byte ucPwmDt;
bit ucPwmInc;
BIT PWM :PA.3;
BIT TEST :PA.4;
/*定时器初始化*/
void PwmTmr2Init ( void )
{
tm2ct = 0x0;/*计数值*/
//pwm duty
tm2b = 0;//START_PWM_CNT;
//8 位 pwm, 预分频 = 64, 分频 = 2,
tm2s = 0b0_11_00000;
/*0010=HIRC输出,0100=ILRC;
,01= PB2,10=PA3,11=PB4,
PWM模式,禁用极性反转*/
tm2c = 0b0010_10_1_0;
}
/***************************************/
void TIME16_Init(void)
{
/*关中断*/
// INTRQ = 0;
/*停止定时器*/
// T16M.5 =0;
/*计数值清零*/
T16COUNTER =488;
STT16 T16COUNTER;
/*ms标记reset*/
FLAG_NMS =0;
/*计算方法16M/*/
$ T16M IHRC,/1,BIT11;
$ INTEN T16;
}
void FPPA0 (void)
{
.ADJUST_IC SYSCLK=IHRC/2 // SYSCLK=IHRC/2
TIME16_Init();
$ PWM out,low;
$ TEST out,low;
PwmTmr2Init();
ucPwmInc=1;
ucPwmDt=0;
engint;
while (1)
{
if(FLAG_NMS)
{
ucMsCnt++;
if(ucMsCnt>10)
{
ucMsCnt=0;
/*增加*/
if(ucPwmInc)
{
if(ucPwmDt<255)
{
/*更新*/
ucPwmDt++;
tm2b=ucPwmDt;
/*切换*/
if(ucPwmDt==255)
{
ucPwmInc=0;
}
}
}
/*减少*/
else
{
/*更新条件*/
if(ucPwmDt)
{
/*更新*/
ucPwmDt--;
tm2b=ucPwmDt;
/*切换条件*/
if(!ucPwmDt)
{
ucPwmInc=1;
}
}
}
}
FLAG_NMS=0;
}
}
}
void Interrupt ( void )
{
pushaf;
if ( Intrq.T16 )
{
Intrq.T16 = 0;
STT16 T16COUNTER;
if ( count>0 )
{
count--;
}
else
{
count = 9;
/*1ms*/
FLAG_NMS= 1;
$ TEST toggle;
}
}
popaf;
} QQ3261-56566
转载:https://blog.csdn.net/fanhaidz/article/details/111242463