求高手,改程序,我的旋转led就快成功了!

2020-01-26 12:56发布

仿制了个旋转led时钟,源程序在下面,单片机 STC89C52 LQFP封装  22.1184M晶振

我用stc89c51rc      12M晶振  求高手给改下程序适应我的硬件!

不改的话,程序运行到一定时间就自动复位






/*********************************旋转LED字体 时钟程序**************************************************
硬件设计:07电气工程 张国荣
程序编写:07电气工程 傅春平
由于我们水平有限 提供的程序都是初级水平的 望大家多多指教!
需要改进的地方还很多,我们在后续中也会不断升级!

功能简介:
单片机 STC89C52 LQFP封装  22.1184M晶振
字体和数字钟程序!
红外遥控调时间和切换模式!
板上自带DS1302时钟芯片!有掉电保护!
自适应旋转速度 在电机有点转速不均匀的问题得到很好的解决 使得稳定显示
因成本很高就提供了10套的材料!
套件出售地址:http://item.taobao.com/auction/i ... m_num_id=7746062100

采用51电子的无线供电方法!电路稍加改造!提供的是PCB的成品
无需自己调试!旋转的时候很安静!基本无噪声,使用寿命长!
交流群:120069344  有什么问题欢迎大家的讨论!
QQ:77554971
祝大家学习进步!
******************************************************************************************************/
//
#include "reg51.h"
#include "intrins.h"
#define uchar unsigned char
#define uint unsigned int    //宏定义

//手动添加定时器2寄存器定义
sfr T2CON  = 0xC8;
sfr TL2 = 0xCC;
sfr TH2 = 0xCD;
sfr RCAP2L = 0xCA;
sfr RCAP2H = 0xCB;
sbit TF2   = T2CON^7;
sbit EXF2  = T2CON^6;
sbit RCLK  = T2CON^5;
sbit TCLK  = T2CON^4;
sbit EXEN2 = T2CON^3;
sbit TR2   = T2CON^2;
sbit C_T2  = T2CON^1;
sbit CP_RL2= T2CON^0;
sbit ET2  =0xAD;

sbit KEY=P3^2;
sbit led1=P1^1;

sbit ACC0=ACC^0;           //1bit数据存储位
sbit ACC7=ACC^7;

sbit T_CLK=P3^5;                    //DS1302引脚连接
sbit T_IO =P3^6;
sbit T_RST=P3^7;

uchar irtime;//红外用全局变量

uint TimeCount=0;
uint         DelayN=80,                //延时临时计数
                DelayTime=80;        //每次延时的周期个数
bit irokk,flag;
uchar IRcord[2];
uchar   irdata[17];
uint j,k;

uchar miao,Cent,Hour,Month,Day,Week,count,id,mode;
uchar starts_time[7]={0x00,0x10,0x22,0x22,0x09,0x03,0x10};         //初始化后设置
void Set_id(uchar sel,uchar sel_1);
void display(void);
unsigned char code zimo[] = {
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,/*" ",0*/

0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,/*" ",1*/

0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,/*" ",2*/

0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,/*" ",3*/

0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,/*" ",4*/

0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,/*" ",5*/

0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,/*" ",6*/

0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,/*" ",7*/

0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,/*" ",8*/

0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x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B,0xFA,0xFB,0xF6,0xFB,0xEE,0xFB,0x9E,0xFC,0x7C,0xFF,0xFF,/*"2",2*/

0xFF,0xFF,0xFC,0xF9,0xFB,0xFE,0xFB,0xBE,0xFB,0xBE,0xFB,0x5D,0xFC,0xE3,0xFF,0xFF,/*"3",3*/

0xFF,0xFF,0xFF,0xC7,0xFF,0x37,0xFE,0xF6,0xFD,0xF6,0xF8,0x00,0xFF,0xF6,0xFF,0xFF,/*"4",4*/

0xFF,0xFF,0xF8,0x19,0xFB,0xDE,0xFB,0xBE,0xFB,0xBE,0xFB,0xDD,0xFB,0xE3,0xFF,0xFF,/*"5",5*/

0xFF,0xFF,0xFE,0x03,0xFD,0xDD,0xFB,0xBE,0xFB,0xBE,0xF9,0xDD,0xFF,0xE3,0xFF,0xFF,/*"6",6*/

0xFF,0xFF,0xF8,0xFF,0xFB,0xFF,0xFB,0xC0,0xFB,0x3F,0xF8,0xFF,0xFB,0xFF,0xFF,0xFF,/*"7",7*/

0xFF,0xFF,0xFC,0x71,0xFB,0xAE,0xFB,0xDE,0xFB,0xDE,0xFB,0xAE,0xFC,0x71,0xFF,0xFF,/*"8",8*/

0xFF,0xFF,0xFE,0x3F,0xFD,0xDC,0xFB,0xEE,0xFB,0xEE,0xFD,0xDD,0xFE,0x03,0xFF,0xFF,/*"9",9*/

0xFF,0xFF,0xFF,0xDF,0xFF,0xDF,0xFF,0xDF,0xFF,0xDF,0xFF,0xDF,0xFF,0xDF,0xFF,0xFF,/*"-",10*/
};
/******************************************************************************************/

/*********延时子程序**********/
void DelayUs(uchar n)
{
        for(;n>0;n--){
                DelayN=DelayTime;
                while (DelayN);
        }
}
/**************************************************/
void tim1_isr (void) interrupt 3 using 3//定时器1中断服务函数
{
  irtime++;
}
//外部中断解码程序
void ex1_isr (void) interrupt 2 using 2//外部中断1服务函数
{
  static unsigned char  i,w,z;
  static bit startflag;

if(startflag)
{
  
    if(irtime<126&&irtime>=69)//引导码 TC9012的头码                 63     33
           { i=0;
                  
                   w=1;
                   z=0;
                  
                        irdata=irtime;
                        irtime=0;
                        }
       z++;
          if(z>=18)
           {
      irdata[w]=irtime;
      irtime=0;
      w++;
       if(w==17)
         {
       irokk=1;
     w=0;
      }          
              }
  else
    irtime=0;         
               
                  }

   
    else
  {irtime=0;startflag=1;}

}
/************************************************/
void Ircordpro(void)//红外码值处理函数
{
  unsigned char i, j, k;
  unsigned char cord,value;

  k=1;
  for(i=0;i<2;i++)//处理2个字节
     {
      for(j=1;j<=8;j++) //处理1个字节8位
         {
          cord=irdata[k];
          if(cord>10)//大于某值为1         7
      {
                
             value=value|0x80;
   }
          else
      {
             value=value;
   }
          if(j<8)
      {
    value=value>>1;
   }
           k++;
         }
     IRcord=value;
     value=0;   
     }
   
}
/****************************************************************/
//定时器0
void t2(void) interrupt 5             //定时器2中断子程序
{   
   TF2=0;
   count++;
   if(count>10)
     {
      count=0;
      flag=~flag;
         }
         }
/**************************************************************/
void timer0() interrupt 1  using 1                //定时器0中断子程序
{
        TimeCount++;
        if (DelayN!=0)
        DelayN--;
}
/*******************DS1302读写程序**********************************
/********************************************************************
函 数 名:RTInputByte()
功    能:实时时钟写入一字节
说    明:往DS1302写入1Byte数据 (内部函数)
入口参数:d 写入的数据
返 回 值:无  
***********************************************************************/
void RTInputByte(uchar d)
{
    uchar h;
    ACC = d;
    for(h=8; h>0; h--)
    {
        T_IO = ACC0;           /*相当于汇编中的 RRC */
        T_CLK = 1;
        T_CLK = 0;
        ACC = ACC >> 1;
    }
}
/********************************************************************
函 数 名:RTOutputByte()
功    能:实时时钟读取一字节
说    明:从DS1302读取1Byte数据 (内部函数)
入口参数:无  
返 回 值:ACC
设    计:zhaojunjie           日    期:2002-03-19
修    改:                     日    期:
***********************************************************************/
uchar RTOutputByte(void)
{
    uchar h;
    for(h=8; h>0; h--)
    {
        ACC = ACC >>1;         /*相当于汇编中的 RRC */
        ACC7 = T_IO;
        T_CLK = 1;
        T_CLK = 0;
    }
    return(ACC);
}
/********************************************************************
函 数 名:W1302()
功    能:往DS1302写入数据
说    明:先写地址,后写命令/数据 (内部函数)
调    用:RTInputByte() , RTOutputByte()
入口参数:ucAddr: DS1302地址, ucData: 要写的数据
返 回 值:无
***********************************************************************/
void W1302(uchar ucAddr, uchar ucDa)
{
    T_RST = 0;
    T_CLK = 0;
    T_RST = 1;
    RTInputByte(ucAddr);       /* 地址,命令 */
    RTInputByte(ucDa);       /* 写1Byte数据*/
    T_CLK = 1;
    T_RST = 0;
}
/********************************************************************
函 数 名:R1302()
功    能:读取DS1302某地址的数据
说    明:先写地址,后读命令/数据 (内部函数)
调    用:RTInputByte() , RTOutputByte()
入口参数:ucAddr: DS1302地址
返 回 值:ucData :读取的数据
***********************************************************************/
uchar R1302(uchar ucAddr)
{
    uchar ucData;
    T_RST = 0;
    T_CLK = 0;
    T_RST = 1;
    RTInputByte(ucAddr);             /* 地址,命令 */
    ucData = RTOutputByte();         /* 读1Byte数据 */
    T_CLK = 1;
    T_RST = 0;
    return(ucData);
}

/********************************************************************
函 数 名:Set1302()
功    能:设置初始时间
说    明:先写地址,后读命令/数据(寄存器多字节方式)
调    用:W1302()
入口参数:pClock: 设置时钟数据地址 格式为: 秒 分 时 日 月 星期 年
                               7Byte (BCD码)1B 1B 1B 1B 1B  1B  1B
返 回 值:无
***********************************************************************/
void Set1302(uchar *pClock)
{
    uchar h;
    uchar ucAddr = 0x80;
    W1302(0x8e,0x00);           /* 控制命令,WP=0,写操作?*/
    for(h =7; h>0; h--)
    {
        W1302(ucAddr,*pClock);  /* 秒 分 时 日 月 星期 年 */
        pClock++;
        ucAddr +=2;
    }
        W1302(0xc0,0x01);
        W1302(0XC2,0X03);                        //初始自动转换显示时间为3秒。
    W1302(0x8e,0x80);           /* 控制命令,WP=1,写保护?*/

}
/******************读取DS1302中的时间****************/
void du1302()
{
      miao=R1302(0x81);  //对取 秒 分 时 日 月 星期 年
          Cent=R1302(0x83);          
          Hour=R1302(0x85);
          Day=R1302(0x87);
          Month=R1302(0x89);
          Week=R1302(0x8b);

         
}
/*****************************************************/
//根据选择调整相应项目
void Set_id(uchar sel,uchar sel_1)
{
  signed char time;
  uchar max,min,address;

if(sel==1)  {address=0x84; max=23;min=0;}    //小时
if(sel==2)  {address=0x82; max=59;min=0;}    //分钟
if(sel==3)  {address=0x88; max=12;min=1;}    //月
if(sel==4)  {address=0x86; max=31;min=1;}    //日
if(sel==5)  {address=0x8a; max=7;min=1;}    //星期
  time=R1302(address+1)/16*10+R1302(address+1)%16;
  if (sel_1==0) time++;  else time--;
  if(time>max) time=min;   
  if(time<min) time=max;
           
  W1302(0x8e,0x00);
  W1302(address,time/10*16+time%10);
  W1302(0x8e,0x80);  
}
/*********************************************************/
//加减键与显示
void id_case1_key(void)     
{
    if (IRcord[0]==0x0A)
       {
             
           IRcord[0]=0x00;          
        Set_id(id,0);   
       }
    if (IRcord[0]==0x09)
       {
                   
           IRcord[0]=0x00;          
       Set_id(id,1);   
       }
   
}
/*****************************************************/
//按键扫描 显示时间及闪烁位选择
void Scan_Key(void)   
{
if(IRcord[0]==0x0E)     
  {
   
   IRcord[0]=0x00;   
   id++;
   if(id>5)
    id=0;   
  }
  id_case1_key();
}
/***********************************************************/
  void display(void)       
  {       
    uchar i;
/******************显示时分秒数据**************************/             
  if((flag==1)&&(id==1))    //实现调时 的闪烁位 (以下同)
  {
   for(i=0;i<8;i++)           //这里用16个LED实现时间显示(以下同)
        {         
         P2=0xFF;        //时的十位不显示(亮灭的结合就实现了闪烁)
         P0=0XFF;         
         DelayUs(1);                
         }         
          P0=0XFF;
         P2=0XFF;
  for(i=0;i<8;i++)
        {
          P0=0XFF;
          P2=0XFF;                //时的个位不显示                
         DelayUs(1);                
         }
         P0=0XFF;
         P2=0XFF;
  }
else
   {
  for(i=0;i<8;i++)                         //这里用8个LED实现时间显示
        {         
         P0=tab[Hour/16][i*2];        //时的十位
         P2=tab[Hour/16][i*2+1];                          
         DelayUs(1);                
         }         
         P0=0XFF;
         P2=0XFF;
  for(i=0;i<8;i++)
        {
         P0=tab[Hour%16][i*2];                //时的个位
         P2=tab[Hour%16][i*2+1];                  
         DelayUs(1);                
         }       
         P0=0XFF;
         P2=0XFF;
         }
  for(i=0;i<8;i++)
        {
         P0=tab[10][i*2];                        //显示两点
         P2=tab[10][i*2+1];         
         DelayUs(1);         
         }
          P0=0XFF;
          P2=0XFF;
/******************************************************/
  if((flag==1)&&(id==2))      
  {
  for(i=0;i<8;i++)
        {
          P0=0XFF;
          P2=0XFF;                 //分的十位不显示       
         DelayUs(1);
         }
          P0=0XFF;
          P2=0XFF;
  for(i=0;i<8;i++)
        {
         P0=0XFF;
         P2=0XFF;                //分的个位不显示                                                                          
         DelayUs(1);         
         }
         P0=0XFF;
         P2=0XFF;
  }
else
   {       
  for(i=0;i<8;i++)
        {
         P0=tab[Cent/16][i*2];                 //分的十位
         P2=tab[Cent/16][i*2+1];       
         DelayUs(1);                
         }
         P2=0XFF;
         P0=0XFF;
  for(i=0;i<8;i++)
        {
         P0=tab[Cent%16][i*2];                //分的个位
         P2=tab[Cent%16][i*2+1];                                                                        
         DelayUs(1);         
         }
         P0=0XFF;
         P2=0XFF;
         }
  for(i=0;i<8;i++)                  //显示两点
        {
         P0=tab[10][i*2];
         P2=tab[10][i*2+1];       
         DelayUs(1);         
         }
         P0=0XFF;
         P2=0XFF;
/***********************************************/
        for(i=0;i<8;i++)
        {
         P0=tab[miao/16][i*2];                 //秒的十位
         P2=tab[miao/16][i*2+1];       
         DelayUs(1);                  
         }
         P0=0XFF;
         P2=0XFF;
        for(i=0;i<8;i++)
        {
         P0=tab[miao%16][i*2];           //秒的个位
         P2=tab[miao%16][i*2+1];         
         DelayUs(1);                  
        }
         P0=0XFF;
         P2=0XFF;
         
/*********************显示月日数据***************************/
   DelayUs(25);         //这里延时的作用是 把日期与时间拉开距离
/******************显示星期数据******************************/

        if((flag==1)&&(id==5))
        {
  for(i=8;i>0;i--)
        {
         P0=0xFF;           //星期值不显示
         P2=0xFF;
         DelayUs(1);         
        }
        P0=0xFF;           //星期值不显示
        P2=0xFF;
        }
        else
        {
  for(i=8;i>0;i--)
        {
         P2=tab1[Week][i*2];           //星期值
         P0=tab1[Week][i*2+1];         
         DelayUs(1);         
        }

        }
        P0=0xFF;
        P2=0XFF;
/****************************************************/
         for(i=7;i>0;i--)  
        {
         P2=tab1[10][i*2];                                   //-字符
         P0=tab1[10][i*2+1];       
         DelayUs(1);         
        }
      P0=0XFF;
          P2=0XFF;
/****************************************************/
if((flag==1)&&(id==4))
{
for(i=8;i>0;i--)
        {
         P0=0XFF;
         P2=0XFF;                //日期个位不显示         
         DelayUs(1);         
        }
         P0=0XFF;
         P2=0XFF;
for(i=8;i>0;i--)
        {
         P0=0XFF;                //日期十位不显示
         P2=0XFF;
         DelayUs(1);                 
        }
        P0=0XFF;
        P2=0XFF;
}
        else
        {
        for(i=8;i>0;i--)
        {
         P2=tab1[Day%16][i*2];                                 //日期个位
         P0=tab1[Day%16][i*2+1];
         DelayUs(1);         
        }
          P0=0XFF;
          P2=0XFF;
        for(i=8;i>0;i--)
        {
         P2=tab1[Day/16][i*2];                                //日期十位
         P0=tab1[Day/16][i*2+1];               
         DelayUs(1);         
        }
      

        }
        P0=0XFF;
        P2=0XFF;
/******************************************/
for(i=7;i>0;i--)
        {
         P2=tab1[10][i*2];                                        //-字符
         P0=tab1[10][i*2+1];                 
         DelayUs(1);                  
        }
        P0=0XFF;
        P2=0XFF;

if((flag==1)&&(id==3))      
  {
  for(i=8;i>0;i--)
        {
         P0=0XFF;
         P2=0XFF;                   //月的个位不显示         
         DelayUs(1);         
        }
         P0=0XFF;
         P2=0XFF;
for(i=8;i>0;i--)
        {
         P0=0XFF;
         P2=0XFF;                         //月的十位不显示       
         DelayUs(1);                  
        }
         P0=0XFF;
         P2=0XFF;
  }
else
{

    for(i=8;i>0;i--)
        {
         P2=tab1[Month%16][i*2];                         //月的个位
         P0=tab1[Month%16][i*2+1];
         DelayUs(1);                  
        }
        P0=0XFF;
        P2=0XFF;
        }

for(i=8;i>0;i--)
        {
         P2=tab1[Month/16][i*2];                   //月的十位
         P0=tab1[Month/16][i*2+1];
         DelayUs(1);                 
        }
         P0=0XFF;
         P2=0XFF;


       


  }
/***********************************************************/

/*****主函数*****/
void main(void)
{
   TMOD=0x22;//定时器0工作方式2  定时器1工作方式2,TH0是重装值,TL0是初值
   TH0=0x80;        //设定定时器0初值
   TL0=0x80;
   TR0=1;
   ET0=1;

   TH1=0x00;//reload value
   TL1=0x00;//initial value
   ET1=1;//开中断
   TR1=1;
   RCAP2H = 0x3C;        //定时器2 定时50MS
   RCAP2L = 0x0B0;
   ET2 = 1;
   TR2=1;
//外部中断
IT1 = 1;   // Configure interrupt 0 for falling edge on /INT0 (P3.3)
EX1 = 1;   // Enable EX1 Interrupt

EA = 1;  //开总中断
led1=0;
mode=2;
             
while(1)    //无限循环
{
  if(irokk==1)
   {  
    Ircordpro();
     irokk=0;
   }
   if(IRcord[0]==0x1A)//红外遥控的按键值 不同的遥控器不同的按键值不同 根据实际改 以下按键功能相同
          {
            Set1302(starts_time);    //初始化
            W1302(0x8e,0x00);            //控制命令,WP=0,写操作               
            W1302(0x90,0xa5);            //打开充电二级管  一个二级管串联一个2K电阻
          }
   if(IRcord[0]==0x10)       
        {
           IRcord[0]=0x00;
           mode=1;
        }
           if(IRcord[0]==0x14)       
        {
           IRcord[0]=0x00;
           mode=2;
        }         
if(KEY==0)          //这里就不用中断0了
  {
  DelayTime=TimeCount/160;       
        TimeCount=0;        //延时周期计数清零
if(mode==1)
{
  du1302(); //读取 秒 分 时 日 月 星 期 年
  Scan_Key();
  display();
  }
  if(mode==2)
  {
             j++;            
            if(j>432)
            {
                j=0;
            }            
            for(k=j;k<128+j;k++)
            {                              
                P0=zimo[k*2];
                P2=zimo[k*2+1];
                DelayUs(1);
                       
                P0=0XFF;
                P2=0XFF;
               
            }
  }
  }
}
}
/*****END*****/


友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。