找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 2980|回復: 4
收起左側

為什么1602和ADC會有干擾?

[復制鏈接]
ID:272089 發(fā)表于 2018-10-17 10:13 | 顯示全部樓層 |閱讀模式
20黑幣
#include<regx52.h>
#include<absacc.h>

#define ADCPORT XBYTE[0x1eff]

#define uchar unsigned char
#define uint  unsigned int

//void delay(uint i){while(i--);}

sbit k1=P2^4; sbit k2=P2^5;
sbit k3=P2^6; sbit k4=P2^7;
sbit EOC=P3^4;

sbit RS=P2^3; //1=數(shù)據(jù);0=命令
sbit RW=P2^4; //1=讀;0=寫
sbit E=P2^5;  //1=使能;0=禁止
#define Dat1602 P0 /** 數(shù)據(jù)端口 **/
sbit BF=Dat1602^7; /** 忙信號線 **/

uint s=0,min=0,c=0,tc=250,co=20,q=0;

/** LCM忙檢測 **/
bit busy1602(){
        bit busy=0;           //暫存標志
        Dat1602=0xff;  //口線置1,防止干擾
        RS=0; RW=1;           //置“命令、讀”模式
        E=1;  E=1;     //使能,寫兩次做短延時
        busy=BF;E=0;   //暫存標志,關閉使能
        return busy;   //返回忙信號,1=忙
}
/** 寫命令 參數(shù):comDat為要發(fā)送的命令 **/
void WriteComm(uchar comDat){  
    while (busy1602());     //忙,則等待
        RS=0;RW=0;                                //置“命令、寫”模式
    Dat1602=comDat;E=1;E=0;        //送出命令,并使之有效
}
/** 寫數(shù)據(jù) 參數(shù):dat為要發(fā)送的數(shù)據(jù) **/
void WriteData(uchar dat){  
    while (busy1602());  //忙,則等待
        RS=1;RW=0;                         //置“數(shù)據(jù)、寫”模式
    Dat1602=dat;E=1;E=0; //送出數(shù)據(jù),并使之有效
}

/** 初始化 **/
void Init1602(){
        WriteComm(0x38);//8位,兩行,5*7點陣
        WriteComm(0x0c);//顯示開,光標關
        WriteComm(0x06);//默認,光標右移
        WriteComm(1);        //清顯示
}

/** 顯示字符串 **/
void showString(uchar row,uchar col,uchar code *s){
        uchar i=0;
        row%=2;col%=40;                                 //防止越界
        WriteComm(0x80+row*0x40+col);//光標定位
        for(;col<40&&s[i]!=0;i++,col++){WriteData(s[i]);}
}
void ADC()
{
   ADCPORT=0x1e;
   while(!EOC);
   P0=ADCPORT;
   co=P1;
}

void main()
{

        TMOD=0x01;
        EA=ET0=1;
        TR0=1;
    Init1602();
        showString(0,0,"Time:12:0");
        showString(1,0,"T:");
        showString(1,6,"C");
        showString(1,8,"Co2:");
        showString(1,14,"%");
                       
        while(1)
        {
            
                //co=co*500./200;
                //co=co/100;
                WriteComm(0x80+9);             //光標定位
            WriteData(min/100+'0');               //時間顯示
                WriteData(':');                  
            WriteData(s/10%10+'0');         
            WriteData(s%10+'0');

                WriteComm(0xc0+2);                                //溫度設定
                WriteData(tc/100+'0');
                WriteData(tc/10%10+'0');
                WriteData('.');
                WriteData(tc%10+'0');

                WriteComm(0xc0+12);
                   WriteData(co/10%10+'0');                //二氧化碳濃度設定
                WriteData(co%10+'0');
               

                while(q==1)
            {       
                                    WriteComm(0xc0+5);
                                    WriteComm(0x0f);
                                        if(k1==0)
                            {
                               while(k1==0);
                                   tc+=5;
                                           break;
                            }
                            if(k2==0)
                            {
                               while(k2==0);
                                   tc-=5;
                                           break;
                            }
                                    
            }
                while(q==2)
            {       
                                    WriteComm(0xc0+13);
                                    WriteComm(0x0f);
                                        if(k1==0)
                            {
                               while(k1==0);
                                   co++;
                                           break;
                            }
                            if(k2==0)
                            {
                               while(k2==0);
                                   co--;
                                           break;
                            }
                                       
            }         
        }
       
}

void time0() interrupt 1
{
    TH0=0xfc;
        TL0=0x17;
        ADC();
        if(++c==1000)
        {
            c=0;
            s++;
                if(s>59)
                {
                   s=0;
                   min++;
                }
        }
        if(k3==0)
                {
                    while(k3==0);
                    q=(q+1)%3;
                }
        if(k4==0)
        {
           while(k4==0);
           q=0;
        }               
}

回復

使用道具 舉報

ID:290170 發(fā)表于 2018-10-17 11:11 | 顯示全部樓層
你看看數(shù)據(jù)腳的定義 2.5 2.6 2.7你用在屏幕刪 又用在按鍵上 同時用怎么可能不出問題
回復

使用道具 舉報

ID:277550 發(fā)表于 2018-10-17 12:08 | 顯示全部樓層
出現(xiàn)的狀況是怎樣的,說出來別人也容易幫忙分析
~~~~~~~~~~~~~
回復

使用道具 舉報

ID:272089 發(fā)表于 2018-10-17 18:22 | 顯示全部樓層
999994 發(fā)表于 2018-10-17 11:11
你看看數(shù)據(jù)腳的定義 2.5 2.6 2.7你用在屏幕刪 又用在按鍵上 同時用怎么可能不出問題

按鍵刪了還是一樣,就顯示一部分數(shù)值,其余的都是亂碼。
回復

使用道具 舉報

ID:111634 發(fā)表于 2018-10-17 22:39 | 顯示全部樓層
不會有干擾!是你的電路或控制程序有問題。!
回復

使用道具 舉報

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

本版積分規(guī)則

手機版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

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

快速回復 返回頂部 返回列表