標題: 跪求大神修改下單片機程序 數(shù)碼管能顯示撥碼開關(guān)的動作 [打印本頁]

作者: tatata12128    時間: 2017-11-28 16:39
標題: 跪求大神修改下單片機程序 數(shù)碼管能顯示撥碼開關(guān)的動作
要求:數(shù)碼管能顯示撥碼開關(guān)的動作,但是我的程序只能分開控制,求修改
程序和仿真都有,求幫忙

數(shù)碼管顯示撥碼開關(guān)動作.zip

39.52 KB, 下載次數(shù): 27


作者: wulin    時間: 2017-11-28 19:44
沒有看到你的程序控制什么,只是用P1口的LED和數(shù)碼管顯示撥碼開關(guān)的狀態(tài)。
作者: wulin    時間: 2017-11-28 19:59
數(shù)組的數(shù)據(jù)類型設置錯誤,temp重復定義,for循環(huán)無意義。修改好了。

#include<reg51.h>
#include<intrins.h>
/********************************************************************************************************
**                         Marcos define
*********************************************************************************************************/
#define uint  unsigned int
#define uchar unsigned char
#define MotorTabNum 5

//IO設置
sbit QH    = P3^0;         //輸出端
sbit CLK   = P3^1;  //時鐘輸入端(上升沿有效)
sbit SPL   = P3^2;  //移位控制/置入控制(低電平有效)       
//數(shù)碼管
sbit SMG1 = P2^0;                                        //數(shù)碼管第一位定義
sbit SMG2 = P2^1;                                        //數(shù)碼管第二位定義
sbit SMG3 = P2^2;                                        //數(shù)碼管第三位定義
sbit SMG4 = P2^3;                                        //數(shù)碼管第四位定義
sbit SMG5 = P2^4;                                        //數(shù)碼管第五位定義
sbit SMG6 = P2^5;                                        //數(shù)碼管第六位定義
sbit SMG7 = P2^6;                                        //數(shù)碼管第七位定義
sbit SMG8 = P2^7;                                        //數(shù)碼管第八位定義
uchar  temp = 0;

uchar  table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};           //共陽極數(shù)碼管段值

void Display(void);
void delayms(xms);
void mDelay(unsigned int DelayTime);                //延時函數(shù)

/********************************************************************************************************
* Function Name  : read_int165
* Description    : 接收數(shù)據(jù)
* Input          : None
* Output         : None
* Return         : None
********************************************************************************************************/                     
uint read_int165(void)
{
  uchar i  = 0;
  uint read_data = 0;

  SPL           = 0;          //置數(shù),讀入并行輸入口數(shù)據(jù)        
  _nop_();
  SPL           = 1;                 //移位,并口輸入被封鎖,串行轉(zhuǎn)換開始
  _nop_();


  for(i=0;i<8;i++)        //8位數(shù)據(jù)
  {
     read_data <<= 1;
     if(QH)
         {
                 read_data|=QH;
         }
             CLK=0;           //下降沿      
             _nop_();
            CLK=1;           //上升沿      
             _nop_();
  }
  return read_data;
}
/********************************************************************************************************
* Function Name  : main
* Description    : 主函數(shù)
* Input          : None
* Output         : None
* Return         : None
********************************************************************************************************/
void main()
{
        //初始化
        //  unsigned char i;
       
//        uint  temp  = 0;
        CLK=0;
        while(1)
        {
                temp  = read_int165();
                //獲取高位,存放置temp
               
                P1=temp;  //接收到的字節(jié)顯示在P1 端口,顯示的值與撥碼開關(guān)對應
                //        for(i=0; i<10; i++)
                Display();
       
        }
}
void Display(void)
{
        unsigned char B1,B2,B3,B4,B5,B6,B7,B8;//定義數(shù)碼管的每一位
       
        B1=temp/10000000;                //取g_MotorNum的千萬位
        B2=temp%10000000/1000000;  //取g_MotorNum的百萬位
        B3=temp%1000000/100000;            //取g_MotorNum的十萬位       
        B4=temp%100000/10000;                    //取g_MotorNum的萬位
        B5=temp%10000/1000;                //取g_MotorNum的千位       
        B6=temp%1000/100;  //取g_MotorNum的百位       
        B7=temp%100/10;            //取g_MotorNum的十位       
        B8=temp%10;                    //取g_MotorNum的個位
       
        //顯示千萬位
        P0=table[B1];
        SMG1=1;                                                         
    delayms(2);
    SMG1=0;
       
        //顯示百萬位
        P0=table[B2];
        SMG2=1;                                                         
    delayms(5);
        SMG2=0;
        //顯示十萬位
       
        P0=table[B3];
        SMG3=1;                                                         
    delayms(2);
        SMG3=0;
        //顯示萬位
        P0=table[B4];
    SMG4=1;
    delayms(2);
    SMG4=0;
        //顯示千位
        P0=table[B5];
        SMG5=1;                                                         
    delayms(2);
    SMG5=0;
        //顯示百位
        P0=table[B6];
        SMG6=1;                                                         
    delayms(2);
        SMG6=0;
        //顯示十位
        P0=table[B7];
        SMG7=1;                                                         
    delayms(2);
        SMG7=0;
        //顯示個位
        P0=table[B8];
    SMG8=1;
    delayms(2);
    SMG8=0;
       
}
/********************************************************************************************************
* Function Name  : delayms
* Description    : 延時函數(shù)
* Input          : None
* Output         : None
* Return         : None
********************************************************************************************************/
void delayms(xms)
{
         unsigned int x,y;
         for(x=xms;x>0;x--)
                 for(y=110;y>0;y--);
}
作者: tatata12128    時間: 2017-11-28 20:40
謝謝啦,感恩。





歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1