打印
[51单片机]

新唐N76E003实现呼吸灯

[复制链接]
2502|4
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
空白丶|  楼主 | 2017-12-6 17:45 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
N76E003是有PWM模块的。新手只接触过没有PWM模块的。有模块的话怎么简化下面的代码呢

unsigned char HighRH = 0;
unsigned char HighRL = 0;
unsigned char LowRH = 0;
unsigned char LowRL = 0;
unsigned long PeriodCnt = 0;
unsigned char T1RH = 0;
unsigned char T1RL = 0;

void ConfigPWM(unsigned int fr,unsigned char dc);
void ConfigTimer1(unsigned int ms);
void ConfigTimer1(unsigned int ms);

void main ()
{
        EA = 1;
        P1 = 0X01;
        P1M1=0x00;
        P1M2=0x0f;
       
       
        ConfigPWM(100,10);
        ConfigTimer1(50);
        while(1);
               
}
void ConfigPWM(unsigned int fr,unsigned char dc)
{
        unsigned int  high,low;
       
        PeriodCnt = (16000000/12) / fr;
        high = (PeriodCnt*dc)/100;
        low = PeriodCnt - high;
        high = 65536 - high + 12;
        low = 65536 - low +12;
        HighRH = (unsigned char)(high >> 8);
        HighRL = (unsigned char)high;
        LowRH = (unsigned char)(low >> 8);
        LowRL = (unsigned char)low;
        TMOD &= 0XF0;
        TMOD |= 0X01;
        TH0 = HighRH;
        TL0 = HighRL;
        ET0 = 1;
        TR0 = 1;
        P0 = 0X01;
}

void ConfigTimer1(unsigned int ms)
{
        unsigned long tmp;
        tmp = 16000000/12;
        tmp = (tmp*ms)/1000;
        tmp = 65536 - tmp;
        tmp = tmp + 12;
        T1RH = (unsigned char)(tmp>>8);
        T1RL = (unsigned char) tmp;
        TMOD &= 0X0F;
        TMOD |= 0X10;
        TH1 = T1RH;
        TH1 = T1RL;
        ET1 = 1;
        TR1 = 1;
}
void AdjustDuyCycle(unsigned char dc)
{
        unsigned int high,low;
        high = (PeriodCnt*dc)/100;
        low = PeriodCnt - high;
        high = 65536 - high + 12;
        low = 65536 - low +12;
        HighRH = (unsigned char)(high>>8);
        HighRL = (unsigned char)high;
        LowRH = (unsigned char)(low>>8);
        LowRL = (unsigned char)low;
}
void InterruptTimer0() interrupt 1
{
        if(P1 == 1)
        {
                TH0 = LowRH;
                TL0 = LowRL;
                P1 = 0X00;
        }
        else
        {
                TH0 = HighRH;
                TL0 = HighRL;
                P1 = 0X01;
        }
}
void InterruptTimer1() interrupt 3
{
        static bit dir = 0;
        static unsigned char index = 0;
        unsigned char table[13]=
                {5,18,30,41,51,60,68,75,81,86,90,93,95};
       
        TH1 = T1RH;
        TL1 = T1RL;
        AdjustDuyCycle(table[index]);
        if (dir == 0)
        {
                index++;
                if(index >= 12)
                {
                        dir = 1;
                }
        }
                else
                {
                        index--;
                        if (index == 0)
                        {
                                dir = 0;
                        }
                }
        }

相关帖子

沙发
一路向北lm| | 2017-12-8 12:50 | 只看该作者
可以调试成功吗

使用特权

评论回复
板凳
sopc12| | 2017-12-11 14:39 | 只看该作者
加油,努力

使用特权

评论回复
地板
zqx1000| | 2018-4-10 23:36 | 只看该作者
kankan

使用特权

评论回复
5
Leven175| | 2018-12-14 14:14 | 只看该作者
楼主我也遇到这个问题,如果有PWM硬件模块,那么要怎么和定时器1的计数值作比较?

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1

主题

1

帖子

0

粉丝