|
给大家发次程序的目的是在于,便于大家学习用51大家来驱动TFT2.4彩屏
流程是由触发电路检测周期,fpga数周期和采样,数据发给单片机。单片机通过周期和数据确定横纵坐标,在tft上显示。
下载:
基于tft彩屏的数字示波器单片机编程部分.rar
(9.48 KB, 下载次数: 50)
单片机源程序:
- #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);
- }
- }
- }
复制代码
|
|