本人刚学会C51还不熟悉ADC这一块,求大神解答
掩码表是啥?下面的程序工作原理是啥?
1.为加速逻辑运算而设置的掩码表,这是以牺牲空间而换取时间的办法
电路原理图如下:
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;
- }
- }
- }
复制代码
|