|
給大家發次程序的目的是在于,便于大家學習用51大家來驅動TFT2.4彩屏
流程是由觸發電路檢測周期,fpga數周期和采樣,數據發給單片機。單片機通過周期和數據確定橫縱坐標,在tft上顯示。
0.png (67.5 KB, 下載次數: 88)
下載附件
2017-3-10 02:23 上傳
下載:
基于tft彩屏的數字示波器單片機編程部分.rar
(9.48 KB, 下載次數: 50)
2017-3-10 02:21 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
單片機源程序:
- #include"head.h"
- #include"intrins.h"
- sbit key1=P3^2; //水平掃描檔
- sbit key2=P3^3;
- sbit key3=P3^4;
- sbit key4=P3^5; //豎直檔
- sbit key5=P3^6;
- sbit key6=P3^7; //延遲
- uchar sdiv=1;//默認豎直檔
- float hdiv=0.000002;//默認水平掃描檔
- char xdata y[128]; //存放fpga給的每周期128個點
- uint i=0;
- float T;//周期
- float fu=0;//幅值
- sbit clk=P2^0;//單片機給fpga的時鐘
- sbit start=P2^2;//單片機給fpga的標志位
- uchar time[3];//存放的fpga給的周期信息
- long int t_long;//由周期信息移位得到的周期數(只是個數,需要轉化)
- long int freq;//頻率
- void receive()//單片機與fpga通訊部分,每次fpga發給單片機131個點,前3個點是和周期有關的信息,后128個點是每周期波形的數據點
- {
- uchar time[3];
- uchar i;
- start=1;
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- for(i=0;i<3;i++)
- {
- clk=1;
- delayms(10);
- _nop_();
- _nop_();
-
- clk=0; //clk下降沿fpga發數據
- _nop_();
- _nop_();
- clk=1;
- time[i]=P1; //高電平單片機收數據
- }
- t_long=((long int)time[2]*256*256+(int)time[1]*256+time[0])*2;//移位得到周期的數數
- for(i=0;i<128;i++)
- {
- clk=1;
- _nop_();
- _nop_();
-
- clk=0;
- _nop_();
- _nop_();
- clk=1;
-
- if((uchar)P1<128) //對AD而言128相當于0,所以相當于判斷正負
- y[i]=(uchar)P1+128;
- else
- y[i]=(uchar)P1-128;
- if(y[i]>fu)
- fu=y[i]; //接受最大值
- }
- _nop_();
- _nop_();
- start=0;
- clk=1;
- _nop_();
- _nop_();
-
- clk=0;
- _nop_();
- _nop_();
- clk=1;
-
- }
- void keyscan()//檢測按鍵改變div
- {
- if(key1==0)
- {
- delayms(10);
- if(key1==0)
- {
- hdiv=0.0000001;
- ClearScreen(0xffff);
- table();
- }
- while(!key1);
- }
- if(key2==0)
- {
- delayms(10);
- if(key2==0)
- {
- hdiv=0.000002;
- ClearScreen(0xffff);
- table();
- }
- while(!key2);
- }
- if(key3==0)
- {
- delayms(10);
- if(key3==0)
- {
- hdiv=0.02;
- ClearScreen(0xffff);
- table();
- }
- while(!key3);
- }
- if(key4==0)
- {
- delayms(10);
- if(key4==0)
- {
- sdiv=1;
- ClearScreen(0xffff);
- table();
- }
- while(!key4);
- }
- if(key5==0)
- {
- delayms(10);
- if(key5==0)
- {
- sdiv=5;
- ClearScreen(0xffff);
- table();
- }
- while(!key5);
- }
- }
-
- void main(void)
- {
- clk=1;
- start=0;
- ILI9325_Initial();//tft初始化
- while(1)//循環,每次都是先收到數據,然后顯示
- {
- ClearScreen(0xffff);//清屏
- table();//坐標
- fu=0;
- receive();
- fu=fu*5/128;
- freq=(long int)200000000/t_long;
- T=1.0/freq;
- keyscan();
- //畫圖函數
- for(i=0;i<320;i++) //i代表像素點位置,(T/hdiv*32)指的是顯示一個周期至少需要的像素點
- {
- if(i==320) break;
- LCD_SetPos(120+sdiv*y[(uint)(i%(uint)(T/hdiv*32)*127/(T/hdiv*32-1))],120+sdiv*y[(uint)(i%(uint)(T/hdiv*32)*127/(T/hdiv*32))-1],i,i);
- Write_Data_U16(0xf800);
- }
- LCD_PutString(30,20,"峰值:",0x001f,0xffff);
- LCD_PutChar8x16( 30, 56, (uchar)(fu*10)/10+48, 0x001f, 0xffff);
- LCD_PutChar8x16( 30, 62, '.', 0x001f, 0xffff);
- LCD_PutChar8x16( 30, 68, (uchar)(fu*10)%10+48, 0x001f, 0xffff);
- LCD_PutChar8x16( 30, 74, 'V', 0x001f, 0xffff);
- LCD_PutString(30,130,"頻率:",0x001f,0xffff);
- if(freq<1000){
- LCD_PutChar8x16(30,166,(int)freq/100+48,0x001f,0xffff);
- LCD_PutChar8x16(30,172,(int)freq/10%10+48,0x001f,0xffff);
- LCD_PutChar8x16(30,178,(int)freq%10+48,0x001f,0xffff);
- LCD_PutString(30,184,"Hz",0x001f,0xffff);
- }
- if(freq>=1000 && freq/1000<1000){
- LCD_PutChar8x16(30,166,(int)(freq/1000/100)+48,0x001f,0xffff);
- LCD_PutChar8x16(30,172,(int)(freq/1000/10%10)+48,0x001f,0xffff);
- LCD_PutChar8x16(30,178,(int)(freq/1000)%10+48,0x001f,0xffff);
- LCD_PutString(30,184,"KHz",0x001f,0xffff);
- }
- if(freq>=1000000 && freq/1000000<1000){
- LCD_PutChar8x16(30,166,(int)(freq/1000000/100)+48,0x001f,0xffff);
- LCD_PutChar8x16(30,172,(int)(freq/1000000/10%10)+48,0x001f,0xffff);
- LCD_PutChar8x16(30,178,(int)(freq/1000000%10)+48,0x001f,0xffff);
- LCD_PutString(30,184,"MHz",0x001f,0xffff);
- }
- LCD_PutString(30,220,"周期:",0x001f,0xffff);
- if(T>=1){
- LCD_PutChar8x16(30,256,(int)T+48,0x001f,0xffff);
- LCD_PutString(30,262,"s",0x001f,0xffff);
- }
- if(T<1 && 1000*T>=1){
- LCD_PutChar8x16(30,256,(int)(1000*T)/100+48,0x001f,0xffff);
- LCD_PutChar8x16(30,262,(int)(1000*T)/10%10+48,0x001f,0xffff);
- LCD_PutChar8x16(30,268,(int)(1000*T)%10+48,0x001f,0xffff);
- LCD_PutString(30,274,"ms",0x001f,0xffff);
- }
- if(1000*T<1 && 1000000*T>=1){
- LCD_PutChar8x16(30,256,(int)(1000000*T)/100+48,0x001f,0xffff);
- LCD_PutChar8x16(30,262,(int)(1000000*T)/10%10+48,0x001f,0xffff);
- LCD_PutChar8x16(30,268,(int)(1000000*T)%10+48,0x001f,0xffff);
- LCD_PutString(30,274,"us",0x001f,0xffff);
- }
-
- //是否延遲
- if(key6==0)
- {
- delayms(10);
- if(key6==0)
- delayms(10000);
- while(!key1);
- }
- }
- }
復制代碼
|
|