![]() |
發(fā)布時間: 2018-10-5 17:26
正文摘要:從網(wǎng)上下載的程序,下載到板子試了,就一直這樣顯示不動,大佬幫忙看下程序問題出在哪 #include<reg52.h> #include <string.h> #define uint unsigned int #define uchar unsigned char uchar red=0,bl ... |
zhanghyg 發(fā)表于 2018-10-6 09:49 好的,謝謝哈 |
psuxaog 發(fā)表于 2018-10-5 22:01 好的,因為網(wǎng)上下載的,沒整理到 |
上面的程序有問題,用不到外中斷,以下程序僅供參考。 /******************************************* * 文件名: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; //顏色標志位(0:紅 1:綠 2:藍) //數(shù)組 uchar disp_buf[3]={0xff,0xff,0xff}; //臨時保存當前的色值 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ù)顏色標志位disp_p,讀取當前檢測 到的色值,并保存到disp_buf[] * 入口參數(shù): 無 * 出口參數(shù): 無 /********************************************/ void feed_buf() { //依次讀取紅、綠、藍色值 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服務程序 修改顏色標志disp_tc(0:紅 1:綠 2:藍) 設置S0 S1 S2 選擇濾波器 計算脈沖,讀取色值 * 入口參數(shù): 無 * 出口參數(shù): 無 /********************************************/ void c10ms_out() interrupt 1 { TR0=0; //關(guān)閉定時 TR1=0; //關(guān)閉計數(shù) disp_tc+=1;//實現(xià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; //選擇濾波器為藍色 } 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; } |
1. 你的程序很亂呀,要養(yǎng)成良好的風格 2. 主程序中有循環(huán)調(diào)用 DisplayZF,定時器3中斷中也調(diào)用了 DisplayZF,會有沖突 比如此時正在執(zhí)行主程序中的 DisplayZF,然后中斷來了,打破了原來的時序,所以你要 先整理一下程序再找原因 |
chen232001 發(fā)表于 2018-10-5 19:43 不是,增大延時也不行 |
是不是延遲太短,顯示太快,看不見。增大延時試試 |
Powered by 單片機教程網(wǎng)