找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
12
返回列表 发新帖
楼主: small2
打印 上一主题 下一主题
收起左侧

为何抄的单片机程序都不成?

  [复制链接]
41#
ID:995256 发表于 2021-12-21 16:06 | 只看该作者
用的系统不一样,有可能需要改一些数据呀
回复

使用道具 举报

42#
ID:995265 发表于 2021-12-21 16:08 | 只看该作者
这是根据博主代码的强壮性决定的吧
回复

使用道具 举报

43#
ID:995326 发表于 2021-12-21 17:48 | 只看该作者
因为抄了咋不懂,而且网上很多都是错误的
回复

使用道具 举报

44#
ID:471297 发表于 2021-12-21 20:22 | 只看该作者
确实太难了,我改的这个程序就有问题,哪位大神帮我看看吧,P0 P1 P2口接24个LED 循环亮,显示就是不正常
  1. #include <REG52.H>

  2. unsigned char RunMode;
  3. //**********************************System Fuction*************************************************
  4. void Delay1ms(unsigned int count)
  5. {
  6.         unsigned int i,j;
  7.         for(i=0;i<count;i++)
  8.         for(j=0;j<120;j++);
  9. }


  10. unsigned char GetKey(void)
  11. {
  12.         unsigned char KeyTemp,CheckValue,Key = 0x00;
  13.         CheckValue = P3&0x32;
  14.         if(CheckValue==0x32)
  15.                 return 0x00;
  16.         
  17.         Delay1ms(10);  //延迟1ms(10)
  18.         KeyTemp = P3&0x32;
  19.         if(KeyTemp==CheckValue)
  20.                 return 0x00;

  21.         if(!(CheckValue&0x02))
  22.                 Key|=0x01;
  23.         if(!(CheckValue&0x10))
  24.                 Key|=0x02;
  25.         if(!(CheckValue&0x20))
  26.                 Key|=0x04;
  27.         return Key;
  28. }

  29. unsigned int TimerCount,SystemSpeed,SystemSpeedIndex;
  30. void InitialTimer2(void)
  31. {
  32.         T2CON  = 0x00;                        //16 Bit Auto-Reload Mode
  33.         TH2 = RCAP2H = 0xFC;          //重装值,初始值        TL2 = RCAP2L = 0x18;
  34.         ET2=1;                                        //定时器 2 中断允许
  35.         TR2 = 1;                                //定时器 2 启动
  36.         EA=1;
  37. }

  38. unsigned int code SpeedCode[]={   1,   2,   3,   5,   8,  10,  14,  17,  20,  30,
  39.                                                              40,  50,  60,  70,  80,  90, 100, 120, 140, 160,
  40.                                                                 180, 200, 300, 400, 500, 600, 700, 800, 900,1000};//30
  41. void SetSpeed(unsigned char Speed)
  42. {
  43.         SystemSpeed =SpeedCode[Speed];
  44. }

  45. void LEDShow(unsigned long LEDStatus)    //修改处
  46. {
  47.         P0 = ~(LEDStatus&0x00FF);
  48.         P1 = ~((LEDStatus>>8)&0x00FF);
  49.         P2 = ~((LEDStatus>>16)&0x00FF);  //修改处
  50. }

  51. void InitialCPU(void)
  52. {
  53.         RunMode = 0x00;
  54.         TimerCount = 0;
  55.         SystemSpeedIndex = 10;

  56.         P0 = 0xFF;
  57.         P1 = 0xFF;
  58.         P2 = 0xFF;
  59.         P3 = 0xFF;
  60.         Delay1ms(500);
  61.         P0 = 0xFF;
  62.         P1 = 0xFF;
  63.         P2 = 0xFF;
  64.         P3 = 0xFF;
  65.         SetSpeed(SystemSpeedIndex);
  66.         
  67. }

  68. //Mode 0
  69. unsigned int LEDIndex = 0;
  70. bit LEDDirection = 1,LEDFlag = 1;

  71. void Mode_0(void)
  72. {
  73.         if(LEDDirection)
  74.                 LEDShow(0x000001<<LEDIndex);   //修改处
  75.         else
  76.                 LEDShow(0x800000>>LEDIndex);   //修改处
  77.         if(LEDIndex==23)                       //修改处                  
  78.                 LEDDirection = !LEDDirection;
  79.    LEDIndex = (LEDIndex+1)%24;                 //修改处              
  80. }

  81. void TimerEventRun(void)
  82. {
  83.         
  84.         if(RunMode ==0x00)
  85.         {
  86.                 Mode_0();
  87.         }

  88. }

  89. void Timer2(void) interrupt 5 using 3
  90. {
  91.         TF2 = 0;         //中断标志清除( Timer2 必须软件清标志!)
  92.         if(++TimerCount>=SystemSpeed)
  93.         {
  94.                 TimerCount = 0;
  95.                 TimerEventRun();
  96.            }
  97. }
  98. unsigned char MusicIndex = 0;
  99. void KeyDispose(unsigned char Key)
  100. {
  101.         
  102.         if(Key&0x02)
  103.         {
  104.                 if(SystemSpeedIndex>0)
  105.                 {
  106.                         --SystemSpeedIndex;
  107.                         SetSpeed(SystemSpeedIndex);
  108.                 }
  109.                 else
  110.                 {
  111.                         
  112.                 }
  113.         }
  114.         if(Key&0x04)
  115.         {
  116.                 if(SystemSpeedIndex<28)
  117.                 {
  118.                         ++SystemSpeedIndex;
  119.                         SetSpeed(SystemSpeedIndex);
  120.                 }
  121.                 else
  122.                 {
  123.                         
  124.                 }
  125.         }        
  126. }

  127. //***********************************************************************************
  128. main()
  129. {
  130.         unsigned char Key;
  131.         InitialCPU();
  132.         InitialTimer2();

  133.         while(1)
  134.         {
  135.                 Key = GetKey();
  136.                 if(Key!=0x00)
  137.                 {
  138.                         KeyDispose(Key);
  139.                 }
  140.         }
  141. }
复制代码
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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