找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 1602|回復(fù): 1
打印 上一主題 下一主題
收起左側(cè)

SC52X+51單片機(jī)pcf8591光敏AD數(shù)碼管顯示

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:646540 發(fā)表于 2019-11-21 15:41 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式



源代碼:
#include <reg52.H>
#define uchar unsigned char
#define uint unsigned int
unsigned char dispcount;
sbit a=P1^0;//段控制
sbit b=P1^1;//位控制
sbit LeDen=P1^2;//LED燈控制端
sbit Line=P1^3;//點陣行控制端
sbit lcd_en=P1^7;//1602液晶使能端

sbit rst=P3^4;//DS1302復(fù)位端,低電平關(guān)閉  
sbit SDA=P3^2;
sbit SCL=P3^3;
unsigned char  Duanma[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
unsigned char  Weima[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
uchar temp[8];
uchar tempdata[8];
//bit ack;

///////延時函數(shù)//////
void Delay(uint z)
{
   uchar x;
    for(;z>0;z--)
      for(x=123;x>0;x--);
}

void Delayus()
{;;}
//////////啟動總線////////
void Start()
{
         SDA=1;
         SCL=1;
         Delayus();
         SDA=0;
         Delayus();
         SCL=0;
}
//////////結(jié)束總線///////////
void Stop()
{
         SDA=0;
         SCL=1;
         Delayus();
         SDA=1;
         Delayus();
}
//////非應(yīng)答函數(shù)/////////
void Noack()
{
         SDA=1;
         Delayus();
         SCL=1;
         Delayus();
         SCL=0;
}
void Ack()
{
         SDA=0;
         Delayus();
         SCL=1;
         Delayus();
         SCL=0;
}
/////////數(shù)據(jù)發(fā)送函數(shù)//////
void Send(uchar c)
{
         uchar bite;
         for(bite=0;bite<8;bite++)
           {
                  if((c<<bite)&0x80)SDA=1;
                  else SDA=0;
                  SCL=1;
                  Delayus();
                  SCL=0;  
           }
         SDA=1;
         Delayus();
         Delayus();
         Delayus();
}
///////接收函數(shù)///////////
uchar Rec()
{
         uchar byte,bite;
         SDA=1;
         for(bite=0;bite<8;bite++)
           {
                  SCL=0;
                  Delayus();
                  SCL=1;
                  Delayus();
                  byte<<=1;
                  if(SDA==1)byte+=1;
           }
         SCL=0;
         Delayus();
         return(byte);
}
//////////顯示函數(shù)//////////////
void Display(unsigned char FirstBit,unsigned char Num)
{
    static uchar i=0;

        a=1;
    P0=0;
        Delayus();
    a=0;

        b=1;  
    P0=Weima[i+FirstBit];
        Delayus();
    b=0;

    a=1;
    P0=tempdata[i];
        Delayus();
        a=0;

    i++;
    if(i==Num)
    i=0;
}
///////定時器初始化//////////
void Init_Timer0(void)
{
         TMOD |= 0x01;   //使用模式1,16位定時器,使用"|"符號可以在使用多個定時器時不受影響      
         EA=1;            //總中斷打開
         ET0=1;           //定時器中斷打開
         TR0=1;           //定時器開關(guān)打開
}

///////////讀取數(shù)值////////
uchar Read(uchar AI)
{
         uchar z;
         Start();
         Send(0x90);
         Ack();
         Send(0x40|AI);        //括號中的參數(shù)AI數(shù)值從0--3為四路AD的地址,通過改變該參數(shù)可以改變輸入通道
         Ack();
         Start();
         Send(0x91);
         Ack();
         z=Rec();
         Noack();
         Stop();
         return(z);
}
////////主函數(shù)////////////
void Main()
{
   uchar num=0;
   rst=0;//關(guān)閉1302時鐘
   lcd_en=0;//關(guān)閉1602液晶
   P0=0X00; //關(guān)閉點陣
   Line=0;
   P0=0XFF; //關(guān)閉LED燈
   LeDen=0;

   Init_Timer0();
   while(1)
         {
                  num=Read(0);//括號中的參數(shù)0--3為四路AD的地址,通過改變該參數(shù)可以改變輸入通道         
                                          
                  tempdata[0]=Duanma[num/1000];
                  tempdata[1]=Duanma[num/100];
                  tempdata[2]=Duanma[(num%100)/10];
                  tempdata[3]=Duanma[(num%100)%10];
                  Delay(100);
         }
}

void Timer0(void) interrupt 1
{
         TH0=(65536-2000)/256;    //重新賦值
         TL0=(65536-2000)%256;
         Display(0,8); //顯示函數(shù),使用中斷顯示

}


30、pcf8591一路AD數(shù)碼管顯示.rar

23.62 KB, 下載次數(shù): 16, 下載積分: 黑幣 -5

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報

沙發(fā)
ID:585455 發(fā)表于 2020-9-13 18:57 | 只看該作者
正在做這方面的測試,希望程式碼是可以使用的。
回復(fù)

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表