找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

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

看下程序哪里不對

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:213280 發(fā)表于 2018-10-5 17:26 | 只看該作者 |只看大圖 回帖獎勵 |倒序瀏覽 |閱讀模式
50黑幣
從網(wǎng)上下載的程序,下載到板子試了,就一直這樣顯示不動,大佬幫忙看下程序問題出在哪
#include<reg52.h>
#include <string.h>
#define uint unsigned int
#define uchar unsigned char
uchar red=0,blue=0,green=0,flag=0,i=0,j=0;
uchar dat[5]={0,0,0,0,0};
uint counter=0;
sbit TCS_OE=P1^0;
sbit TCS_S0=P1^1;
sbit TCS_S1=P1^2;
sbit TCS_S2=P1^3;
sbit TCS_S3=P1^4;
sbit TCS_OUT=P3^2;
sbit LCD_RS=P2^4;
sbit LCD_RW=P2^5;
sbit LCD_EN=P2^6;
sbit LCD_PSB=P2^1;
sbit LCD_RST=P2^3;
sbit LCD_BK=P2^7;
uchar table0[]="顏色識別:";
uchar table1[]="紅色:";
uchar table2[]="藍(lán)色:";
uchar table3[]="綠色:";
void delay(uint m)  //延時程序
{
unsigned int i,j;
  for(i=0;i<m;i++)
     for(j=0;j<10;j++);
}
void LCD_Busy(void)// 測試液晶是否忙
{  
   P0=0xff;    // 數(shù)據(jù)口先送 0xff
   LCD_RS=0;     // 指令
   LCD_RW=1;     // 讀出忙標(biāo)志(BF)及地址記數(shù)器(AC)
   LCD_EN=1;     // 使能
   while(P0&0x80); // 最高位為1則忙,再讀
   LCD_EN=0;
}
void LCD_cmd(char md,char dt)         //寫指令或數(shù)據(jù)  md=0命令 md=1數(shù)據(jù)
{
   LCD_Busy();    // 測試液晶是否忙
if(md) LCD_RS=1;  // 判斷是數(shù)據(jù)還是命令
   else LCD_RS=0;
   LCD_RW=0;    // 寫
   P0=dt;   // 送數(shù)據(jù)或命令
   delay(20);
   LCD_EN=1;     // 使能
   delay(20);
   LCD_EN=0;
}
void LCD_init(void)//液晶顯示器初始化
{  LCD_PSB=1;
   LCD_RST=0;
   delay(10);
   LCD_RST=1;
   LCD_cmd(0,0x34);  // 關(guān)閉圖形顯示 *
   delay(100);
   LCD_cmd(0,0x30);  // 基本指令動作   
   delay(100);
   LCD_cmd(0,0x06);    // 光標(biāo)的移動方向
   delay(100);
   LCD_cmd(0,0x0c);    // 開顯示,關(guān)光標(biāo)
   delay(100);
   LCD_cmd(0,0x01);  // 清屏,地址指針指向00H
   delay(100);
}
void DisplayZF(uchar m,char *p)
{    LCD_cmd(0,m);
     while((*p)!='\0')
     {                                      //顯示字符"         "
      LCD_cmd(1,*p++);
     }
}
void DisplayScreen()          
{   LCD_cmd(0,0x30);     // 基本指令動作
        //delay(100);
    LCD_cmd(0,0x0c);       // 開顯示,關(guān)光標(biāo)   
        //delay(100);
    DisplayZF(0x80,table0);
    //delay(100);
    DisplayZF(0x91,table1);
        //delay(100);
        DisplayZF(0x89,table2);
        //delay(100);
    DisplayZF(0x99,table3);
        //delay(100);
}
void INTER_init()
{   TCS_S0=1;
    TCS_S1=1;
        TCS_S2=0;
    TCS_S3=0;
        TCS_OE=1;
        TMOD=0x11;
        TH1=(65536-60000)/256;
        TL1=(65536-60000)%256;
        EA=1;//開總中斷
        ET0=0; //關(guān)定時器0中斷
        ET1=1; // 開定時器0中斷
        TR0=0; //關(guān)定時器0
        TR1=1; //啟動定時器1
        EX0=1;        //開外部中斷0
        EX1=0;        // 關(guān)外部中斷1
        IT0=1;         //外部中斷0跳變沿觸發(fā)
               
}
void timer0() interrupt 1
{
        counter++;       
}
void timer1() interrupt 3
{
        TH1=(65536-60000)/256;
        TL1=(65536-60000)%256;
    EA=0;//關(guān)總中斷
        EX0=0;
        TR1=0;       
        flag++;//實現(xiàn)先檢測紅色,再檢測藍(lán)色,然后檢測綠色,循環(huán)檢測
        if(flag==1)
        {                    
                 red=counter;
                 dat[0]=counter/1000+0x30;                 
                 counter=counter%1000;
                 dat[1]=counter/100+0x30;
                 counter=counter%100;
                 dat[2]=counter/10+0x30;
                 counter=counter%10;
                 dat[3]=counter+0x30;
                    DisplayZF(0x94,dat);                        
             TCS_S2=0;//下次檢測藍(lán)色(S2,S3=0,1)
                 TCS_S3=1;               
        }
        else if(flag==2)
        {
                    blue=counter;
                 dat[0]=counter/1000+0x30;                 
                 counter=counter%1000;
                 dat[1]=counter/100+0x30;
                 counter=counter%100;
                 dat[2]=counter/10+0x30;
                 counter=counter%10;
                 dat[3]=counter+0x30;               
                    DisplayZF(0x8c,dat);                                         
                TCS_S2=1;//下次檢測綠色(S2,S3=1,1)
                TCS_S3=1;
        }
    else if(flag==3)
        {
                    green=counter;
                 dat[0]=counter/1000+0x30;                 
                 counter=counter%1000;
                 dat[1]=counter/100+0x30;
                 counter=counter%100;
                 dat[2]=counter/10+0x30;
                 counter=counter%10;
                 dat[3]=counter+0x30;       
                    DisplayZF(0x9c,dat);                                                 
                TCS_S2=0;//下次檢測紅色(S2,S3=0,0)
                TCS_S3=0;
        }
        else if(flag==4)
        {      
             flag=0;                 
                 if((red+5)>(blue+green))                 
                     DisplayZF(0x85,"藍(lán)色");
                 else if((blue+8>red)&&(blue+2>green))
                     DisplayZF(0x85,"紅色");               
                 /*else if((green-3<red)&&(green-15>blue))
                     DisplayZF(0x85,"綠色");*/                 
                 else if((red>blue)&&(green>blue))
                      DisplayZF(0x85,"黃色");               
                 else
                     DisplayZF(0x85,"綠色");                 
        }
        counter = 0;//清零外中斷計數(shù)標(biāo)志               
    TR0=1;
        EX0=1;
        TR1=1;
        EA=1;
       
}       

void main(void)
{
    LCD_init();
        INTER_init();
        while(1)
        {       
        DisplayScreen();
    delay(100);
        }  
}

IMG_20181005_171931.jpg (5.11 MB, 下載次數(shù): 46)

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

使用道具 舉報

沙發(fā)
ID:405662 發(fā)表于 2018-10-5 19:43 | 只看該作者
是不是延遲太短,顯示太快,看不見。增大延時試試
回復(fù)

使用道具 舉報

板凳
ID:213280 發(fā)表于 2018-10-5 21:48 | 只看該作者
chen232001 發(fā)表于 2018-10-5 19:43
是不是延遲太短,顯示太快,看不見。增大延時試試

不是,增大延時也不行
回復(fù)

使用道具 舉報

地板
ID:405708 發(fā)表于 2018-10-5 22:01 | 只看該作者
1. 你的程序很亂呀,要養(yǎng)成良好的風(fēng)格
2. 主程序中有循環(huán)調(diào)用 DisplayZF,定時器3中斷中也調(diào)用了 DisplayZF,會有沖突
    比如此時正在執(zhí)行主程序中的 DisplayZF,然后中斷來了,打破了原來的時序,所以你要
   先整理一下程序再找原因
回復(fù)

使用道具 舉報

5#
ID:330198 發(fā)表于 2018-10-6 09:49 | 只看該作者
上面的程序有問題,用不到外中斷,以下程序僅供參考。
/*******************************************
* 文件名:TCS230.c
* 功能: 讀取RGB值
* 說明:
* 時間:
/********************************************/
#include <reg51.h>
#include <intrins.h>

#define uchar unsigned char
#define uint unsigned int

/**引腳定義**/
//sbit oe=P1^0;   
//sbit s0=P1^2;
//sbit s1=P1^3;
sbit s2=P1^5;
sbit s3=P1^4;

//變量、常量定義

long int color,red,green,blue;
long int disp_tc;   
long int buf_cor;    //顏色值

uint rp=3,gp=6,bp=3;    //定義比例因子

uchar disp_p;    //顏色標(biāo)志位(0:紅 1:綠 2:藍(lán))


//數(shù)組

uchar disp_buf[3]={0xff,0xff,0xff};    //臨時保存當(dāng)前的色值

uchar code table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x08,0x42,0x00};/*數(shù)碼管花數(shù)*/
uchar code scan[4]={0x8f,0x4f,0x2f,0x0f};    /*數(shù)碼管位選*/

/*******************************************
* 函數(shù)名稱: t0_init()
* 函數(shù)功能: 定時器0初始化
* 入口參數(shù): 無
* 出口參數(shù): 無
/********************************************/
void t0_init()
{
     TMOD=0x51;        //T1計數(shù) T0定時 工作方式1

     TH0=0x3C;        //定時50ms

     TL0=0xB0;
     TH1=0xFF;        //計數(shù)初值

     TL1=0x9C;
     EA=1;            //開中斷

     ET0=1;        
     TR0=1;            //啟動

     TR1=1;
}

/*******************************************
* 函數(shù)名稱: delay_1ms(uint w)
* 函數(shù)功能: 延時1mx
* 入口參數(shù): 無符號整形W
* 出口參數(shù): 無
/********************************************/
void delay_1ms(uint w)   
{
     uint y,z;
     for(z=w;z>0;z--)
        for(y=100;y>0;y--);
}

/*******************************************
* 函數(shù)名稱: feed_buf()
* 函數(shù)功能: 根據(jù)顏色標(biāo)志位disp_p,讀取當(dāng)前檢測
            到的色值,并保存到disp_buf[]
* 入口參數(shù): 無
* 出口參數(shù): 無
/********************************************/
void feed_buf()
{
    //依次讀取紅、綠、藍(lán)色值

     if(disp_p==0)
        {red=buf_cor/rp;color=red;}    //讀取紅色值         

     else if(disp_p==1)
        {green=buf_cor/gp;color=green;}
     else if(disp_p==2)
        {blue=buf_cor/bp;color=blue;}
            
     disp_buf[0]=color/100;
     disp_buf[1]=(color-100*disp_buf[0])/10;
     disp_buf[2]=color%10;
}

/*******************************************
* 函數(shù)名稱: display()
* 函數(shù)功能: 數(shù)碼管顯示
* 入口參數(shù): 無
* 出口參數(shù): 無
/********************************************/
void display()
{
   uchar i;
   for(i=0;i<3;i++)
   {
        if(disp_p==0&&i==0)
            {P0=table[10];P2=scan[0];delay_1ms(2);P2=0xff;}
        else if(disp_p==1&&i==0)
            {P0=table[11];P2=scan[0];delay_1ms(2);P2=0xff;}
        else if(disp_p==2&&i==0)
            {P0=table[12];P2=scan[0];delay_1ms(2);P2=0xff;}
        else if(i<=3)
            {P0=table[disp_buf[i-1]];P2=scan[i];delay_1ms(2);P2=0xff;}
   }
}

/*******************************************
* 函數(shù)名稱: main()
/********************************************/
void main()
{
     oe=0;        //使能TCS230

     s0=1; s1=1;    //選擇輸出比例因子100%

     t0_init();        //定時計數(shù)初使化

     while(1)
     {
        display();
     }
}

/*******************************************
* 函數(shù)名稱: c10ms_out()
* 函數(shù)功能: 定時中斷0服務(wù)程序
            修改顏色標(biāo)志disp_tc(0:紅 1:綠 2:藍(lán))
            設(shè)置S0 S1 S2 選擇濾波器
            計算脈沖,讀取色值
* 入口參數(shù): 無
* 出口參數(shù): 無
/********************************************/
void c10ms_out() interrupt 1
{
   TR0=0;    //關(guān)閉定時

   TR1=0;    //關(guān)閉計數(shù)


   disp_tc+=1;//實現(xiàn)先檢測綠色,再檢測藍(lán)色,然后檢測紅色,循環(huán)檢測

   
   if(disp_tc==100)
   {
        disp_tc=0;
        if(disp_p==0)
        {
            disp_p+=1;   
            s2=1;s3=1;    //選擇濾波器為綠色            

        }
        else if(disp_p==1)
        {            
            disp_p+=1;
            s2=1;s3=0;    //選擇濾波器為藍(lán)色

        }
        else if(disp_p==2)
        {            
            disp_p=0;
            s2=0;s3=0;    //選擇濾波器為紅色

        }
    }
   
    if(disp_tc%10==0)
    {
        buf_cor=TH1*256+TL1;    //計算這段時間內(nèi) TCS230 的輸出脈沖數(shù)

        feed_buf();
    }
   
    //重賦初值

     TH0=0xB0;
     TL0=0x3C;
     TL1=0x00;
     TH1=0x00;
     TR0=1;
     TR1=1;
}
回復(fù)

使用道具 舉報

6#
ID:213280 發(fā)表于 2018-10-6 10:33 | 只看該作者
psuxaog 發(fā)表于 2018-10-5 22:01
1. 你的程序很亂呀,要養(yǎng)成良好的風(fēng)格
2. 主程序中有循環(huán)調(diào)用 DisplayZF,定時器3中斷中也調(diào)用了 DisplayZ ...

好的,因為網(wǎng)上下載的,沒整理到
回復(fù)

使用道具 舉報

7#
ID:213280 發(fā)表于 2018-10-6 14:45 | 只看該作者
zhanghyg 發(fā)表于 2018-10-6 09:49
上面的程序有問題,用不到外中斷,以下程序僅供參考。
/*******************************************
*  ...

好的,謝謝哈
回復(fù)

使用道具 舉報

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

本版積分規(guī)則

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

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

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