【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;  
  }
}

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

饒益一切有情。

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

樂一切有情。

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

法界種種功德。

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

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

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

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

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

敬、尊重讚歎聽受正法。

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

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

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

恒聞正法無間無斷。

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

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

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

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

菩薩摩訶薩。

相关推荐
春日见5 分钟前
决策规划控制面经汇总
人工智能·深度学习·算法·机器学习·自动驾驶
Full Stack Developme5 分钟前
Java DFA算法
java·python·算法
爱讲故事的6 分钟前
计算机网络第七章:无线与移动网络复习笔记
网络·笔记·计算机网络
fie888915 分钟前
LBP + HOG 特征检测与识别 MATLAB 实现
数据结构·算法·matlab
小陈phd16 分钟前
Text2SQL智能体学习笔记(一)——NL2SQL及执行流程介绍
笔记·学习
蛋白界小百灵16 分钟前
【生物技术科普】纳米抗体 vs 普通抗体:结构、特性与应用场景全面对比
经验分享·生物技术·实验
海天鹰17 分钟前
图片去黑边算法
qt·算法
问心无愧051318 分钟前
ctf sow web入门112
android·前端·笔记
稷下元歌19 分钟前
系统学习plc 基础指令上篇,官方资料课程笔记整 理
笔记·学习
yuan1999734 分钟前
CMS8S5880 电子锁程序(51单片机)
单片机·嵌入式硬件·51单片机