本人剛學會C51還不熟悉ADC這一塊,求大神解答
掩碼表是啥?下面的程序工作原理是啥?
1.為加速邏輯運算而設置的掩碼表,這是以犧牲空間而換取時間的辦法
電路原理圖如下:
QQ截圖20181122235343.png (30.03 KB, 下載次數: 67)
下載附件
2018-11-22 23:53 上傳
code unsigned int LcdMaskTab[]={0x0001,0x0002,0x0004,0x0008,0x0010,0x0020,0x0040,0x0080,0x0100,0x0200,0x0400,0x0800,0x1000,0x2000,0x4000,0x8000};
2.disp_0到disp_hz分別是干啥的?
3.里面的over是啥意思
4.ADC采集是怎么實現的?代碼里哪里有體現?
以下是全部代碼,成功測試過
- #include <STC12c5a.H>
- #include<intrins.h>
- #include<math.h>
- sbit RS=P0^0; //并行的指令/數據選擇信號, H數據, L命令
- sbit RW=P0^1; //并行讀寫選擇信號, H讀, L寫
- sbit E=P0^2; //并行使能端, H有效, L無效
- sbit jiakey=P3^0;
- sbit jiankey=P3^1;
- sbit res=P0^3;
- sbit PSB=P0^4;
- #define LcdData P2
- unsigned char dati=0;
- unsigned char dat[100];
- unsigned char over=0;
- unsigned int temp=0;
- unsigned char mode=0;
- unsigned int delnop=0;
- //////////////////////////////////////
- unsigned char Lcd_CheckBusy(void)
- {
- unsigned char Busy;
- LcdData=0xff;
- RS=0;
- RW=1;
- E=1;
- _nop_();
- Busy=LcdData&0x80;
- E=0;
- return Busy;
- }
- /*********************************
- 向LCD寫入字節數據
- **********************************/
- void Lcd_WriteData(unsigned char Data)
- {
- while(Lcd_CheckBusy());
- RS=1;
- RW=0;
- E=0;
- _nop_();
- _nop_();
- LcdData=Data;
- E=1;
- _nop_();
- _nop_();
- E=0;
- }
- /***********************************
- 從LCD中讀出數據
- ************************************/
- unsigned char Lcd_ReadData(void)
- {
- unsigned char Temp;
- while(Lcd_CheckBusy());
- LcdData=0xff;
- RS=1;
- RW=1;
- E=1;
- _nop_();
- Temp=LcdData;
- E=0;
- return Temp;
- }
- /*************************************
- 想LCD中寫入指令代碼
- **************************************/
- void Lcd_WriteCmd(unsigned char CmdCode)
- {
- while(Lcd_CheckBusy());
- RS=0;
- RW=0;
- E=0;
- _nop_();
- _nop_();
- LcdData=CmdCode;
- _nop_();
- _nop_();
- E=1;
- _nop_();
- _nop_();
- E=0;
- }
- /**************************************
- 為加速邏輯運算而設置的掩碼表,這是以犧牲空間而換取時間的辦法
- ***************************************/
- code unsigned int LcdMaskTab[]={0x0001,0x0002,0x0004,0x0008,0x0010,0x0020,0x0040,0x0080,0x0100,0x0200,0x0400,0x0800,0x1000,0x2000,0x4000,0x8000};
- /***************************************
- 向LCD指定坐標寫入一個象素,象素顏色有兩種,0代表白(無顯示),1代表黑(有顯示)
- ****************************************/
- void Lcd_PutPixel(unsigned char x,unsigned char y,unsigned char Color)
- {
- unsigned char z,w;
- unsigned int Temp;
- if(x>=128||y>=64)
- return;
- Color=Color%2;
- w=15-x%16;//確定對這個字的第多少位進行操作
- x=x/16;//確定為一行上的第幾字
- if(y<32) //如果為上頁
- z=0x80;
- else //否則如果為下頁
- z=0x88;
- y=y%32;
- //EA=0;
- Lcd_WriteCmd(0x36);
- Lcd_WriteCmd(y+0x80); //行地址
- Lcd_WriteCmd(x+z); //列地址
- Temp=Lcd_ReadData();//先空讀一次
- Temp=(unsigned int)Lcd_ReadData()<<8;//再讀出高8位
- Temp|=(unsigned int)Lcd_ReadData();//再讀出低8位
- //EA=1;
- if(Color==1) //如果寫入顏色為1
- Temp|=LcdMaskTab[w];//在此處查表實現加速
- else //如果寫入顏色為0
- Temp&=~LcdMaskTab[w];//在此處查表實現加速
- //EA=0;
- Lcd_WriteCmd(y+0x80); //行地址
- Lcd_WriteCmd(x+z); //列地址
- Lcd_WriteData(Temp>>8);//先寫入高8位,再寫入低8位
- Lcd_WriteData(Temp&0x00ff);
- Lcd_WriteCmd(0x30);
- //EA=1;
- }
- /*****************************************
- 清除Lcd全屏,如果清除模式Mode為0,則為全屏清除為顏色0(無任何顯示)
- 否則為全屏清除為顏色1(全屏填充顯示)
- ******************************************/
- void Lcd_Clear(unsigned char Mode)
- {
- unsigned char x,y,ii;
- unsigned char Temp;
- if(Mode%2==0)
- Temp=0x00;
- else
- Temp=0xff;
- Lcd_WriteCmd(0x36);//擴充指令 繪圖顯示
- for(ii=0;ii<9;ii+=8)
- for(y=0;y<0x20;y++)
- for(x=0;x<8;x++)
- {
- //EA=0;
- Lcd_WriteCmd(y+0x80); //行地址
- Lcd_WriteCmd(x+0x80+ii); //列地址
- Lcd_WriteData(Temp); //寫數據 D15-D8
- Lcd_WriteData(Temp); //寫數據 D7-D0
- //EA=1;
- }
- Lcd_WriteCmd(0x30);
- }
- /****************************************
- LCD初始化
- *****************************************/
- void Lcd_Reset()
- {
- Lcd_WriteCmd(0x30); //選擇基本指令集
- Lcd_WriteCmd(0x0c); //開顯示(無游標、不反白)
- Lcd_WriteCmd(0x01); //清除顯示,并且設定地址指針為00H
- Lcd_WriteCmd(0x06); //指定在資料的讀取及寫入時,設定游標的移動方向及指定顯示的移位
- }
- //////////////////////////////////////
- void InitADC()
- {
- P1ASF=0X80;
- ADC_RES=0;
- ADC_CONTR=0xef;
- EADC=1;
- }
- void adc_isr() interrupt 5 using 1
- {
- ADC_CONTR=0xef;
- if(over==0)
- {
- temp=delnop;
- while(temp)
- {
- temp--;
- }
- dat[dati]=ADC_RES;
- dati++;
- if(dati>100)
- {
- dati=0;
- over=1;
- }
- }
- }
- //////////////////////////////////////
- void disp_0(unsigned char x,unsigned char y)
- {
- Lcd_PutPixel(x+1,y+1,1);
- Lcd_PutPixel(x+1,y+2,1);
- Lcd_PutPixel(x+1,y+3,1);
- Lcd_PutPixel(x+2,y+0,1);
- Lcd_PutPixel(x+2,y+4,1);
- Lcd_PutPixel(x+3,y+1,1);
- Lcd_PutPixel(x+3,y+2,1);
- Lcd_PutPixel(x+3,y+3,1);
- }
- void disp_1(unsigned char x,unsigned char y)
- {
- Lcd_PutPixel(x+1,y+1,1);
- Lcd_PutPixel(x+1,y+4,1);
- Lcd_PutPixel(x+2,y+0,1);
- Lcd_PutPixel(x+2,y+1,1);
- Lcd_PutPixel(x+2,y+2,1);
- Lcd_PutPixel(x+2,y+3,1);
- Lcd_PutPixel(x+2,y+4,1);
- Lcd_PutPixel(x+3,y+4,1);
- }
- void disp_2(unsigned char x,unsigned char y)
- {
- Lcd_PutPixel(x+1,y+0,1);
- Lcd_PutPixel(x+1,y+2,1);
- Lcd_PutPixel(x+1,y+3,1);
- Lcd_PutPixel(x+1,y+4,1);
- Lcd_PutPixel(x+2,y+0,1);
- Lcd_PutPixel(x+2,y+2,1);
- Lcd_PutPixel(x+2,y+4,1);
- Lcd_PutPixel(x+3,y+0,1);
- Lcd_PutPixel(x+3,y+1,1);
- Lcd_PutPixel(x+3,y+2,1);
- Lcd_PutPixel(x+3,y+4,1);
- }
- void disp_3(unsigned char x,unsigned char y)
- {
- Lcd_PutPixel(x+1,y+0,1);
- Lcd_PutPixel(x+1,y+2,1);
- Lcd_PutPixel(x+1,y+4,1);
- Lcd_PutPixel(x+2,y+0,1);
- Lcd_PutPixel(x+2,y+2,1);
- Lcd_PutPixel(x+2,y+4,1);
- Lcd_PutPixel(x+3,y+0,1);
- Lcd_PutPixel(x+3,y+1,1);
- Lcd_PutPixel(x+3,y+2,1);
- Lcd_PutPixel(x+3,y+3,1);
- Lcd_PutPixel(x+3,y+4,1);
- }
- void disp_4(unsigned char x,unsigned char y)
- {
- Lcd_PutPixel(x+1,y+0,1);
- Lcd_PutPixel(x+1,y+1,1);
- Lcd_PutPixel(x+1,y+2,1);
- Lcd_PutPixel(x+2,y+2,1);
- Lcd_PutPixel(x+3,y+0,1);
- Lcd_PutPixel(x+3,y+1,1);
- Lcd_PutPixel(x+3,y+2,1);
- Lcd_PutPixel(x+3,y+3,1);
- Lcd_PutPixel(x+3,y+4,1);
- }
- void disp_5(unsigned char x,unsigned char y)
- {
- Lcd_PutPixel(x+1,y+0,1);
- Lcd_PutPixel(x+1,y+1,1);
- Lcd_PutPixel(x+1,y+2,1);
- Lcd_PutPixel(x+1,y+4,1);
- Lcd_PutPixel(x+2,y+0,1);
- Lcd_PutPixel(x+2,y+2,1);
- Lcd_PutPixel(x+2,y+4,1);
- Lcd_PutPixel(x+3,y+0,1);
- Lcd_PutPixel(x+3,y+2,1);
- Lcd_PutPixel(x+3,y+3,1);
- Lcd_PutPixel(x+3,y+4,1);
- }
- void disp_p(unsigned char x,unsigned char y)
- {
- Lcd_PutPixel(x+0,y+0,1);
- Lcd_PutPixel(x+1,y+0,1);
- Lcd_PutPixel(x+1,y+1,1);
- Lcd_PutPixel(x+2,y+0,1);
- Lcd_PutPixel(x+2,y+1,1);
- Lcd_PutPixel(x+2,y+2,1);
- Lcd_PutPixel(x+3,y+0,1);
- Lcd_PutPixel(x+3,y+1,1);
- Lcd_PutPixel(x+4,y+0,1);
- }
- void disp_k(unsigned char x,unsigned char y)
- {
- Lcd_PutPixel(x+0,y+0,1);
- Lcd_PutPixel(x+0,y+1,1);
- Lcd_PutPixel(x+0,y+2,1);
- Lcd_PutPixel(x+0,y+3,1);
- Lcd_PutPixel(x+0,y+4,1);
- Lcd_PutPixel(x+1,y+2,1);
- Lcd_PutPixel(x+2,y+1,1);
- Lcd_PutPixel(x+2,y+3,1);
- Lcd_PutPixel(x+3,y+0,1);
- Lcd_PutPixel(x+3,y+4,1);
- }
- void disp_hz(unsigned char x,unsigned char y)
- {
- Lcd_PutPixel(x+0,y+0,1);
- Lcd_PutPixel(x+0,y+1,1);
- Lcd_PutPixel(x+0,y+2,1);
- Lcd_PutPixel(x+0,y+3,1);
- Lcd_PutPixel(x+0,y+4,1);
- Lcd_PutPixel(x+1,y+2,1);
- Lcd_PutPixel(x+2,y+0,1);
- Lcd_PutPixel(x+2,y+1,1);
- Lcd_PutPixel(x+2,y+2,1);
- Lcd_PutPixel(x+2,y+3,1);
- Lcd_PutPixel(x+2,y+4,1);
- Lcd_PutPixel(x+4,y+1,1);
- Lcd_PutPixel(x+4,y+3,1);
- Lcd_PutPixel(x+4,y+4,1);
- Lcd_PutPixel(x+5,y+1,1);
- Lcd_PutPixel(x+5,y+2,1);
- Lcd_PutPixel(x+5,y+4,1);
- }
- void clr(unsigned char starx,unsigned char stary,unsigned char endx,unsigned char endy)
- {
- char x=0;
- char y=0;
- for(x=starx;x<endx;x++)
- {
- for(y=stary;y<endy;y++)
- {
- Lcd_PutPixel(x,y,0);
- }
- }
- }
- void disp_bj(void)
- {
- unsigned char x=0;
- unsigned char y=0;
- for(x=13;x<114;x++)
- {
- Lcd_PutPixel(x,52,1);
- }
- for(y=0;y<52;y++)
- {
- Lcd_PutPixel(13,y,1);
- }
- for(y=0;y<52;y++)
- {
- Lcd_PutPixel(114,y,1);
- }
- Lcd_PutPixel(13,51,0);
- Lcd_PutPixel(13,41,0);
- Lcd_PutPixel(13,31,0);
- Lcd_PutPixel(13,21,0);
- Lcd_PutPixel(13,11,0);
- Lcd_PutPixel(13,1,0);
- Lcd_PutPixel(114,51,0);
- Lcd_PutPixel(114,41,0);
- Lcd_PutPixel(114,31,0);
- Lcd_PutPixel(114,21,0);
- Lcd_PutPixel(114,11,0);
- Lcd_PutPixel(114,1,0);
- disp_0(5,50);
- disp_1(5,40);
- disp_2(5,30);
- disp_3(5,20);
- disp_4(5,10);
- disp_5(5,0);
- disp_0(117,50);
- disp_1(117,40);
- disp_2(117,30);
- disp_3(117,20);
- disp_4(117,10);
- disp_5(117,0);
- disp_2(13,58);
- disp_hz(18,58);
- disp_2(38,58);
- disp_0(43,58);
- disp_hz(48,58);
- disp_2(63,58);
- disp_0(68,58);
- disp_0(73,58);
- disp_hz(78,58);
- disp_2(88,58);
- disp_k(93,58);
- disp_hz(98,58);
- }
- void line(unsigned char x0,unsigned char y0,unsigned char x1,unsigned char y1)
- {
- int i,dx,dy,e,x,y;
- Lcd_PutPixel(x0,y0,1);
- Lcd_PutPixel(x1,y1,1);
- dx=x1-x0;
- dy=y1-y0;
- x=x0;
- y=y0;
- if(dx>0&&dy>0)
- {
- if(dx>dy)
- {
- e=-dx;
- for(i=0;i<dx;i++)
- {
- Lcd_PutPixel(x,y,1);
- x++;
- e=e+2*dy;
- if(e>=0)
- {
- y++;
- e=e-2*dx;
- }
- }
- }
- else
- {
- e=-dy;
- x=x0;
- y=y0;
- for(i=0;i<dy;i++)
- {
- Lcd_PutPixel(x,y,1);
- y++;
- e=e+2*dx;
- if(e>=0)
- {
- x++;
- e=e-2*dy;
- }
- }
- }
- }
- if(dx<0&&dy<0)
- {
- dx=x0-x1;
- dy=y0-y1;
- if(dx>dy)
- {
- e=-dx;
- for(i=0;i<dx;i++)
- {
- Lcd_PutPixel(x,y,1);
- x--;
- e=e+2*dy;
- if(e>=0)
- {
- y--;
- e=e-2*dx;
- }
- }
- }
- else
- {
- e=-dy;
- for(i=0;i<dy;i++)
- {
- Lcd_PutPixel(x,y,1);
- y--;
- e=e+2*dx;
- if(e>=0)
- {
- x--;
- e=e-2*dy;
- }
- }
- }
- }
- if(dx>0&&dy<0)
- {
- dy=y0-y1;
- if(dx>dy)
- {
- e=-dx;
- for(i=0;i<dx;i++)
- {
- Lcd_PutPixel(x,y,1);
- x++;
- e=e+2*dy;
- if(e>=0)
- {
- y--;
- e=e-2*dx;
- }
- }
- }
- else
- {
- e=-dy;
- for(i=0;i<dy;i++)
- {
- Lcd_PutPixel(x,y,1);
- y--;
- e=e+2*dx;
- if(e>=0)
- {
- x++;
- e=e-2*dy;
- }
- }
- }
- }
- if(dx<0&&dy>0)
- {
- dx=x0-x1;
- if(dx>dy)
- {
- e=-dx;
- for(i=0;i<dx;i++)
- {
- Lcd_PutPixel(x,y,1);
- x--;
- e=e+2*dy;
- if(e>=0)
- {
- y++;
- e=e-2*dx;
- }
- }
- }
- else
- {
- e=-dy;
- for(i=0;i<dy;i++)
- {
- Lcd_PutPixel(x,y,1);
- y++;
- e=e+2*dx;
- if(e>=0)
- {
- x--;
- e=e-2*dy;
- }
- }
- }
- }
- if(dx!=0&&dy==0)
- {
- if(dx>0)
- {
- for(i=0;i<dx;i++)
- {
- Lcd_PutPixel(x,y,1);
- x++;
- }
- }
- else
- {
- dx=x0-x1;
- for(i=0;i<dx;i++)
- {
- Lcd_PutPixel(x,y,1);
- x--;
- }
- }
- }
- if(dx==0&&dy!=0)
- {
- if(dy>0)
- {
- for(i=0;i<dy;i++)
- {
- Lcd_PutPixel(x,y,1);
- y++;
- }
- }
- else
- {
- dy=y0-y1;
- for(i=0;i<dy;i++)
- {
- Lcd_PutPixel(x,y,1);
- y--;
- }
- }
- }
- }
- void disp_ware()
- {
- unsigned char x=0;
- unsigned char y=0;
- clr(14,0,15,52);
- for(x=1;x<100;x++)
- {
- clr(x+14,0,x+15,52);
- line(x+13,51-(dat[x-1]/5),x+14,51-(dat[x]/5));
- }
- }
- //////////////////////////////////////
- main()
- {
- Lcd_Reset();
- Lcd_Clear(0);
- InitADC();
- disp_bj();
- EA=1;
- while(1)
- {
- if(over)
- {
- disp_ware();
- if(jiakey==0)
- {
- if(mode<3)
- {
- mode++;
- }
- }
- if(jiankey==0)
- {
- if(mode>0)
- {
- mode--;
- }
- }
- switch(mode)
- {
- case 0:
- delnop=1;
- disp_p(91,54);
- clr(66,54,71,57);
- clr(41,54,46,57);
- clr(16,54,21,57);
- break;
- case 1:
- delnop=40;
- disp_p(66,54);
- clr(91,54,96,57);
- clr(41,54,46,57);
- clr(16,54,21,57);
- break;
- case 2:
- delnop=440;
- disp_p(41,54);
- clr(91,54,96,57);
- clr(66,54,71,57);
- clr(16,54,21,57);
- break;
- case 3:
- delnop=4440;
- disp_p(16,54);
- clr(91,54,96,57);
- clr(41,54,46,57);
- clr(66,54,71,57);
- break;
- default:
- break;
- }
- over=0;
- }
- }
- }
復制代碼
|