标题:基于C51单片机的电机堵转试验控制程序,要实现LED数码管计数工作
只看楼主
xm3613
Rank: 1
等 级:新手上路
帖 子:2
专家分:1
注 册:2013-8-27
 问题点数:0 回复次数:0 
基于C51单片机的电机堵转试验控制程序,要实现LED数码管计数工作
程序如下,由于我才学习这个,所以啥都不太清楚,还请大侠帮忙写下,这个是公司给的部分程序
程序是用于电机堵转试验控制程序,现在就是要实现LED数码管计数工作,好像是只要让数码管实现从0到99计数就可以了
#include <c8051f340.h>
#include <INTRINS.H>
#include <function declare.h>

unsigned char code_table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};

unsigned char TM0_flag;
unsigned char Uart0_Rcv_data;           //Uart0 recive operate code and configure data from PC,then sending to SBS by Uart1
unsigned char Uart0_data_counter;            //The length of operate code
unsigned char data_counter=0;
unsigned char Start_flag;
unsigned int  Counter;
unsigned char Seconds,Times;
unsigned char shiwei;
unsigned char gewei;
unsigned char TimeH,TimeM,TimeL;
unsigned int  AD_result=0;
unsigned int  AD_result1=0;
unsigned int  AD_value=0;

void Delay_us( unsigned char t)   //延时
{
    unsigned char i,j;
    for(i=0;i<1;i++)
       {
             for(j=0;j<t;j++)
                 {
              _nop_();    // 调用NOP,延时1us
          _nop_();
          _nop_();
          _nop_();
          _nop_();
             }
           }
}

void Delay_ms( unsigned int t)   //延时
{
    unsigned char i;
        unsigned int  j;
    for(i=0;i<250;i++)
       {
             for(j=0;j<t;j++)
                 {
              _nop_();    // 调用NOP,延时1us
          _nop_();
          _nop_();
          _nop_();
          _nop_();
          _nop_();
          _nop_();        
          _nop_();        
          _nop_();  
                  _nop_();    // 调用NOP,延时1us
             }
           }
}
/*
void Delay_s(unsigned char t)
{
    unsigned char i;
        unsigned int  j;
    for(i=0;i<50;i++)
       {
             for(j=0;j<t;j++)
                 {
               Delay_ms(50);
             }
           }

}*/
/********************AD converter process************************/
void ADC0_sample(void)
{
   unsigned char i;
   unsigned char Result_H,Result_L;
   for(i=0;i<5;i++)
    {
         ADC0H=0x00;
     ADC0L=0x00;
     AD0BUSY= 1;
         if(AD0INT==1)
          {
           AD0INT = 0;                              // Clear the interrupt flag
           AD0BUSY= 0;
       Delay_ms(2);                      //延时保证AD正常工作
       AD_result1=((unsigned int)ADC0L|((unsigned int)ADC0H)<<8);
       AD_result=AD_result+AD_result1;
      }
        }
   AD_result=AD_result/5;
   Result_H = ((unsigned char) (AD_result>>8));
   Result_L = ((unsigned char) (AD_result));
   Uart0_Send_A_Byte(Result_H);    //High 8bits
   Uart0_Send_A_Byte(Result_L);          //Low  8bits
   AD_result=AD_value;
   AD_result=0;
}

  
void DA_MAX5304(unsigned int ibyte)
{
  unsigned int N_value;
  unsigned char i;
   N_value = ibyte<<3;
   DA_CLK= 0;
   Delay_ms(5);
   DA_CS = 0;
   Delay_ms(5);
   for(i=0;i<16;i++)
     {
       if(N_value&0x8000)
          DA_DIN = 1;
       else
          DA_DIN = 0;
       DA_CLK = 1;
       Delay_ms(2);
       DA_CLK = 0;
       Delay_ms(1);
       N_value=(N_value<<1);   
     }
   DA_CS = 1;
}




void main()
{  

   System_Init();
   Uart0_Send_A_Byte(0x15);
   Uart0_Send_A_Byte(0x55);
   Uart0_Send_A_Byte(0x37);
   Uart0_Send_A_Byte(0x28);
   Uart0_Send_A_Byte(0xa4);
   Delay_ms(250);
   Delay_ms(250);
//   ES0 = 1;
//   EIE2 |= 0x02;  

   TR0 = 0;                    //T0定时器停止
   CS0 = 1;
   CS1 = 1;
   CS2 = 1;
   CS3 = 1;
   CS4 = 1;
//   P2 = 0x80;
//   CS0 = 0;
   while(1)
    {
          if(Start_flag==1)
          {
             Start_flag=0;
                   TR0 = 1;
          }
          if(TM0_flag==1)                                  //每1秒钟进入该程序执行
            {
                  TM0_flag = 0;
                  shiwei=Seconds/10;
                  gewei =Seconds%10;
                  TimeH        =Times/100;
                  TimeM        =(Times/10)%10;
                  TimeL        =Times%10;
                  Seconds++;
                  
                  if((Seconds%6)==0)                  //每6秒钟执行该程序执行
                           {
                         Seconds=0;
                         Times++;
                         ADC0_sample();
                        }
          if(Times==180)
                    {
                         Times =0;
                        }
                }
          SEG_Leddisplay();  
    }

        
}

void SEG_Leddisplay(void)
{
           CS0 = 1;
           CS1 = 1;
           CS2 = 1;
           CS3 = 1;
           CS4 = 1;
           P2 = code_table[gewei];
           P1 = code_table[TimeL];
           CS0 = 0;
           CS2 = 0;
           Delay_us(5);
           CS0 = 1;
           CS2 = 1;
           Delay_us(5);
           P2 = code_table[shiwei];
           P1 = code_table[TimeM];
           CS1 = 0;
           CS3 = 0;
           Delay_us(5);
           CS1 = 1;
           CS3 = 1;
           Delay_us(5);
           P1 = code_table[TimeH];
           CS4 = 0;
           Delay_us(5);
           CS4 = 1;
}
搜索更多相关主题的帖子: LED数码管 function include operate 单片机 
2013-08-27 21:59



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-419735-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.230034 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved