找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1221|回复: 6
打印 上一主题 下一主题
收起左侧

一段单片机pwm调光里面的程序看不明白了

[复制链接]
跳转到指定楼层
楼主
ID:100982 发表于 2021-9-28 15:40 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
void InterruptTimer1() interrupt 3
{static bit dir = 0;
static unsigned char index = 0;
unsigned char code table[13] = { //占空比调整表5, 18, 30, 41, 51, 60, 68, 75, 81, 86, 90, 93, 95}

TH1 = T1RH; //重新加载 T1 重载值TL1 = T1RL;
AdjustDutyCycle(table[index]); //调整 PWM 的占空比if (dir == 0) //逐步增大占空比{
index++;
if (index >= 12)
{
dir = 1;
}
}
else //逐步减小占空比{
index--;
if (index == 0)
{
dir = 0;
}

}  


这个动态调整dc占空比的话,里面那个dir第一次进来的时候=0 ,第二次为1, dir又为0  ,这个占空比不就永远只在两个数之间循坏了?
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:123289 发表于 2021-9-28 16:11 | 只看该作者
if (dir == 0) 被你整到上一行去了!
回复

使用道具 举报

板凳
ID:100982 发表于 2021-9-28 16:48 | 只看该作者
yzwzfyz 发表于 2021-9-28 16:11
if (dir == 0) 被你整到上一行去了!

.......哈
回复

使用道具 举报

地板
ID:161164 发表于 2021-9-28 16:50 | 只看该作者
排版问题

  1. void InterruptTimer1() interrupt 3
  2. {   static bit dir = 0;
  3.     static unsigned char index = 0;
  4.     unsigned char code table[13] = { //占空比调整表
  5.         5, 18, 30, 41, 51, 60, 68, 75, 81, 86, 90, 93, 95
  6.     }

  7.     TH1 = T1RH; //重新加载 T1 重载值
  8.     TL1 = T1RL;
  9.     AdjustDutyCycle(table[index]); //调整 PWM 的占空比
  10.     if (dir == 0) //逐步增大占空比
  11.     {
  12.         index++;
  13.         if (index >= 12)
  14.         {
  15.             dir = 1;
  16.         }
  17.     }
  18.     else //逐步减小占空比
  19.     {
  20.         index--;
  21.         if (index == 0)
  22.         {
  23.             dir = 0;
  24.         }
  25.     }
  26. }
复制代码

T=0: dir=0;index=0;AdjustDutyCycle(5);
T=1: dir=0;index=1;AdjustDutyCycle(18);。。。
T=11: dir=0;index=11;AdjustDutyCycle(93);T=12: dir=0;index=12;AdjustDutyCycle(95);
T=13: dir=1;index=11;AdjustDutyCycle(93);T=14: dir=1;index=10;AdjustDutyCycle(90);
。。。



回复

使用道具 举报

5#
ID:303693 发表于 2021-9-28 17:28 | 只看该作者
dir是flag吧,table[index]才对应占空比
回复

使用道具 举报

6#
ID:382826 发表于 2021-9-30 15:50 | 只看该作者
我决定dir是PMW端口.
回复

使用道具 举报

7#
ID:517951 发表于 2021-10-6 08:13 | 只看该作者
这段代码应该是实现呼吸灯效果,亮度达到最大时dir反转为亮度逐渐减小,到了亮度最低值时,dir再反转为亮度逐渐增加,如此循环。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表