【51单片机按键调节占空比3位数码管显示】2023-8-24

缘由51单片机PWM的占空比数码管显示-嵌入式-CSDN问答

51核心板上面遇到一个问题,打算将用key1,key2,控制led灯的功率,外加将占空比在数码管上输出,代码如下,但没办法正常运行,数码管闪烁,led灯不亮,究竟是什么问题,我找不到问题出在那里

复制代码
#include <reg52.h>
sbit led1=P2^4;
sbit BEEP=P1^0;
sbit SEL0=P2^0;
sbit SEL1=P2^1;
sbit SEL2=P2^2;
sbit SEL3=P2^3;
sbit key1=P3^2;
sbit key2=P3^3;
static arrshumagaun[]={0x03 , 0x9f , 0x25 , 0x0d , 0x99 , 0x49 , 0x41 , 0x1f , 0x01 , 0x09};
static int i=0,s=0,k=0,m;
static void delay(int nms);
 static unsigned int s_iDuty = 50; 
static void delay(int nms)
{
int i,j;
    for (i=0;i<nms;i++)
    {  
                for (j=0;j<123;j++){}
    }
}
static void InitInterrupt(void);
static void InitTimer0(void);
static void InitInterrupt()
{
    
  ET0 = 1;  
  EA  = 1;   
}
static void InitTimer0()
{
  TMOD = 0x02;
  TH0  = 0xFF;  
  TL0  = 0x9C; 
  TR0  = 1;   
}
void main()
{
  InitInterrupt();
  InitTimer0();     
  while (1)
  {
    if (key1==0)
    {
    s_iDuty+=10;
    }
    if (key2==0)
    {
    s_iDuty-=10;
    }
    
    P0=arrshumagaun[s_iDuty];
    SEL0=0;
    delay(3);
    SEL0=1;
    
    P0=arrshumagaun[s_iDuty];
    SEL1=0;
    delay(3);
    SEL1=1;
    
    P0=arrshumagaun[s_iDuty];
    SEL2=0;
    delay(3);
    SEL2=1;
    
  }
}
void Timer0_Handler () interrupt 1
{
  static unsigned int s_iCnt1 = 0;  
  s_iCnt1++;
  if(s_iCnt1 >= 100)      
  {
    s_iCnt1 = 0;         
  }
  if(s_iCnt1 <= s_iDuty)   
  {
    led1 = 1;   
  }
  else
  { 
    led1 = 0;  
  }
}
cpp 复制代码
#include "reg52.h"
sbit led1=P2^4;

sbit SEL0=P2^0;
sbit SEL1=P2^1;
sbit SEL2=P2^2;

sbit key1=P3^2;
sbit key2=P3^3;

unsigned char s_iDuty = 50;
unsigned char code arrshumagaun[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
void main()
{//缘由https://ask.csdn.net/questions/7980068/54304240
	unsigned char sy=0,my=0;
	TH0+=(65536-9216)/256;
	TL0+=(65536-9216)%256;
	TMOD=0X01;
	TR0=ET0=EA=1;
	while(1)
	{
		if (key1==0)
    {
    s_iDuty+=10;while(key1==0);
    }
    if (key2==0)
    {
    s_iDuty-=10;while(key2==0);
    }
		
//		if(s_iDuty<90)s_iDuty+=10;else s_iDuty=10;
		
		P1=arrshumagaun[s_iDuty/100];
    SEL0=0;
    while(++sy==0);
    SEL0=1;
    
    P1=arrshumagaun[s_iDuty/10%10];
    SEL1=0;
    while(++sy==0);
    SEL1=1;
    
    P1=arrshumagaun[s_iDuty%10];
    SEL2=0;
    while(++sy==0);
    SEL2=1;
	}
}
void Timer0_Handler () interrupt 1
{
  static unsigned int s_iCnt1 = 0;  
	TH0+=(65536-9216)/256;
	TL0+=(65536-9216)%256;
  s_iCnt1++;
  if(s_iCnt1 == 100)      
  {
    s_iCnt1 = 0;         
  }
  if(s_iCnt1 <= s_iDuty)   
  {
    led1 = 1;   
  }
  else
  { 
    led1 = 0;  
  }
}

「善現!是菩薩摩訶薩雖行一切法空,而愛樂正法不愛非法,恒願

饒益一切有情。

「善現!是菩薩摩訶薩雖行不可得空,而常稱讚三寶功德,利益安

樂一切有情。

「善現!是菩薩摩訶薩雖行諸法真如法界一味之相,而樂稱讚真如

法界種種功德。

「善現!是菩薩摩訶薩雖知諸法皆畢竟空,而愛善友不樂惡友。言

善友者謂佛、菩薩,若諸聲聞、獨覺乘等,能善教化安立有情,令

趣無上正等菩提,亦名善友。

「善現!是菩薩摩訶薩常樂親覲一切如來、應、正等覺,若聞如

來、應、正等覺在餘世界現說正法,即以願力往生彼界,供養恭

敬、尊重讚歎聽受正法。

「善現!是菩薩摩訶薩若晝若夜常不遠離念佛作意,常不遠離聞法

作意,由此因緣,隨諸國土有佛世尊現說正法,即乘願力往彼受

生,或乘神通往彼聽法。由是因緣,此諸菩薩生生之處常不離佛,

恒聞正法無間無斷。

「善現!是菩薩摩訶薩常為利樂諸有情故,雖能現起初靜慮定乃至

非想非非想處定,而巧方便起欲界心,教諸有情十善業道,亦隨願

力現生欲界有佛國土,供養恭敬、尊重讚歎諸佛世尊,聽聞正法修

諸勝行。善現!若菩薩摩訶薩成就如是諸行、狀、相,知是不退轉

菩薩摩訶薩。

相关推荐
PC2005-cloud1 天前
DeepSeek Harness 创建 skill 指南:结构、安装、使用
笔记·学习
依然鸣1 天前
PTA团体程序设计天梯赛L1真题讲解L1-077-080
开发语言·c++·算法·深度优先·pat考试·图论
库玛西1 天前
现代 C++ 智能指针全景指南:从 RAII 思想到工业级实践
c语言·开发语言·c++·笔记·面试
liulilittle1 天前
无锁并发容器的设计与实现原理
开发语言·c++·set·map·并发·无锁·lock-free
qeen871 天前
【数据结构】自平衡二叉搜索树各种旋转算法原理解析及AVL树的C++实现
数据结构·c++·算法
lucas_AI1 天前
1.2B 小模型赢过 235B 大模型:NaviDC-OCR 把文档解析卷明白了
人工智能·深度学习·算法
Augustzero1 天前
为什么线程不能说睡就睡?看懂等待与唤醒机制
c++·后端
程序员与背包客_CoderZ1 天前
Linux D-Bus通信协议详解:从入门到C/C++编码实战
linux·服务器·c语言·c++·嵌入式硬件·嵌入式软件·dbus
2501_918027311 天前
大模型量化后非英语能力为何退化 2-4 倍?LCD 语言条件反量化方法解析
经验分享
冻柠檬飞冰走茶1 天前
PTA基础编程题目集 7-35有理数均值(C++语言实现)
开发语言·数据结构·c++·算法·均值算法