找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 5987|回复: 4
打印 上一主题 下一主题
收起左侧

基于TFT彩屏的数字示波器单片机部分代码(TFT的单片机驱动)

[复制链接]
跳转到指定楼层
楼主

给大家发次程序的目的是在于,便于大家学习用51大家来驱动TFT2.4彩屏

流程是由触发电路检测周期,fpga数周期和采样,数据发给单片机。单片机通过周期和数据确定横纵坐标,在tft上显示。



下载:
基于tft彩屏的数字示波器单片机编程部分.rar (9.48 KB, 下载次数: 50)


单片机源程序:
  1. #include"head.h"
  2. #include"intrins.h"

  3. sbit key1=P3^2;        //水平扫描档
  4. sbit key2=P3^3;
  5. sbit key3=P3^4;

  6. sbit key4=P3^5;        //竖直档
  7. sbit key5=P3^6;
  8. sbit key6=P3^7; //延迟

  9. uchar sdiv=1;//默认竖直档
  10. float hdiv=0.000002;//默认水平扫描档
  11. char xdata y[128]; //存放fpga给的每周期128个点           
  12. uint i=0;
  13. float T;//周期
  14. float fu=0;//幅值

  15. sbit clk=P2^0;//单片机给fpga的时钟
  16. sbit start=P2^2;//单片机给fpga的标志位
  17. uchar time[3];//存放的fpga给的周期信息
  18. long int t_long;//由周期信息移位得到的周期数(只是个数,需要转化)
  19. long int freq;//频率



  20. void receive()//单片机与fpga通讯部分,每次fpga发给单片机131个点,前3个点是和周期有关的信息,后128个点是每周期波形的数据点
  21. {
  22.   uchar time[3];
  23.   uchar i;
  24.   start=1;
  25.   _nop_();
  26.   _nop_();
  27.   _nop_();
  28.   _nop_();
  29.   _nop_();
  30.   _nop_();
  31.   _nop_();
  32.   _nop_();
  33.   _nop_();
  34.   _nop_();
  35.   _nop_();
  36.   _nop_();

  37.    for(i=0;i<3;i++)
  38.     {
  39.           clk=1;
  40.           delayms(10);
  41.           _nop_();
  42.           _nop_();

  43.        
  44.           clk=0;                //clk下降沿fpga发数据
  45.           _nop_();
  46.           _nop_();

  47.           clk=1;

  48.           time[i]=P1;        //高电平单片机收数据
  49.         }
  50.         t_long=((long int)time[2]*256*256+(int)time[1]*256+time[0])*2;//移位得到周期的数数

  51.    for(i=0;i<128;i++)
  52.     {
  53.           clk=1;
  54.           _nop_();
  55.           _nop_();
  56.        
  57.           clk=0;
  58.           _nop_();
  59.           _nop_();
  60.           clk=1;
  61.        
  62.           if((uchar)P1<128)          //对AD而言128相当于0,所以相当于判断正负
  63.           y[i]=(uchar)P1+128;
  64.           else
  65.           y[i]=(uchar)P1-128;


  66.         if(y[i]>fu)
  67.                 fu=y[i];  //接受最大值
  68.         }

  69.         _nop_();
  70.         _nop_();


  71.       start=0;
  72.           clk=1;
  73.           _nop_();
  74.           _nop_();
  75.        
  76.           clk=0;
  77.           _nop_();
  78.           _nop_();
  79.           clk=1;
  80.                           
  81. }




  82. void keyscan()//检测按键改变div
  83. {
  84.         if(key1==0)
  85.         {
  86.                 delayms(10);
  87.                 if(key1==0)
  88.                 {
  89.                         hdiv=0.0000001;
  90.                         ClearScreen(0xffff);
  91.                         table();
  92.                 }
  93.                 while(!key1);
  94.         }

  95.         if(key2==0)
  96.         {
  97.                 delayms(10);
  98.                 if(key2==0)
  99.                 {                                                                  
  100.                         hdiv=0.000002;
  101.                         ClearScreen(0xffff);
  102.                         table();
  103.                 }
  104.                 while(!key2);
  105.         }

  106.         if(key3==0)
  107.         {
  108.                 delayms(10);
  109.                 if(key3==0)
  110.                 {
  111.                         hdiv=0.02;
  112.                         ClearScreen(0xffff);
  113.                         table();
  114.                 }
  115.                 while(!key3);
  116.         }

  117.         if(key4==0)
  118.         {
  119.                 delayms(10);
  120.                 if(key4==0)
  121.                 {
  122.                         sdiv=1;
  123.                         ClearScreen(0xffff);
  124.                         table();
  125.                 }
  126.                 while(!key4);
  127.         }

  128.         if(key5==0)
  129.         {
  130.                 delayms(10);
  131.                 if(key5==0)
  132.                 {
  133.                         sdiv=5;
  134.                         ClearScreen(0xffff);
  135.                         table();
  136.                 }
  137.                 while(!key5);
  138.         }
  139. }
  140.        
  141. void  main(void)
  142. {   
  143.         clk=1;
  144.         start=0;
  145.         ILI9325_Initial();//tft初始化

  146.         while(1)//循环,每次都是先收到数据,然后显示
  147.                 {
  148.                         ClearScreen(0xffff);//清屏
  149.                         table();//坐标
  150.                         fu=0;
  151.                         receive();
  152.                         fu=fu*5/128;
  153.                         freq=(long int)200000000/t_long;
  154.                         T=1.0/freq;
  155.                         keyscan();

  156.                        //画图函数
  157.                     for(i=0;i<320;i++)        //i代表像素点位置,(T/hdiv*32)指的是显示一个周期至少需要的像素点
  158.                         {
  159.                             if(i==320) break;
  160.                                 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);
  161.                                 Write_Data_U16(0xf800);

  162.                     }
  163.                         LCD_PutString(30,20,"峰值:",0x001f,0xffff);
  164.                         LCD_PutChar8x16( 30, 56, (uchar)(fu*10)/10+48, 0x001f, 0xffff);
  165.                         LCD_PutChar8x16( 30, 62, '.', 0x001f, 0xffff);
  166.                         LCD_PutChar8x16( 30, 68, (uchar)(fu*10)%10+48, 0x001f, 0xffff);
  167.                         LCD_PutChar8x16( 30, 74, 'V', 0x001f, 0xffff);

  168.                         LCD_PutString(30,130,"频率:",0x001f,0xffff);
  169.                         if(freq<1000){
  170.                                 LCD_PutChar8x16(30,166,(int)freq/100+48,0x001f,0xffff);
  171.                             LCD_PutChar8x16(30,172,(int)freq/10%10+48,0x001f,0xffff);
  172.                                 LCD_PutChar8x16(30,178,(int)freq%10+48,0x001f,0xffff);
  173.                                 LCD_PutString(30,184,"Hz",0x001f,0xffff);
  174.                                 }
  175.                         if(freq>=1000 && freq/1000<1000){
  176.                                 LCD_PutChar8x16(30,166,(int)(freq/1000/100)+48,0x001f,0xffff);
  177.                             LCD_PutChar8x16(30,172,(int)(freq/1000/10%10)+48,0x001f,0xffff);
  178.                                 LCD_PutChar8x16(30,178,(int)(freq/1000)%10+48,0x001f,0xffff);
  179.                                 LCD_PutString(30,184,"KHz",0x001f,0xffff);
  180.                                 }
  181.                         if(freq>=1000000 && freq/1000000<1000){
  182.                                 LCD_PutChar8x16(30,166,(int)(freq/1000000/100)+48,0x001f,0xffff);
  183.                             LCD_PutChar8x16(30,172,(int)(freq/1000000/10%10)+48,0x001f,0xffff);
  184.                                 LCD_PutChar8x16(30,178,(int)(freq/1000000%10)+48,0x001f,0xffff);
  185.                                 LCD_PutString(30,184,"MHz",0x001f,0xffff);
  186.                                 }                       
  187.                         LCD_PutString(30,220,"周期:",0x001f,0xffff);
  188.                         if(T>=1){       
  189.                                 LCD_PutChar8x16(30,256,(int)T+48,0x001f,0xffff);
  190.                                 LCD_PutString(30,262,"s",0x001f,0xffff);
  191.                                 }
  192.                         if(T<1 && 1000*T>=1){
  193.                                 LCD_PutChar8x16(30,256,(int)(1000*T)/100+48,0x001f,0xffff);
  194.                                 LCD_PutChar8x16(30,262,(int)(1000*T)/10%10+48,0x001f,0xffff);
  195.                                 LCD_PutChar8x16(30,268,(int)(1000*T)%10+48,0x001f,0xffff);
  196.                             LCD_PutString(30,274,"ms",0x001f,0xffff);
  197.                                 }
  198.                         if(1000*T<1 && 1000000*T>=1){
  199.                                 LCD_PutChar8x16(30,256,(int)(1000000*T)/100+48,0x001f,0xffff);
  200.                                 LCD_PutChar8x16(30,262,(int)(1000000*T)/10%10+48,0x001f,0xffff);
  201.                                 LCD_PutChar8x16(30,268,(int)(1000000*T)%10+48,0x001f,0xffff);
  202.                             LCD_PutString(30,274,"us",0x001f,0xffff);
  203.                                 }

  204.                        
  205.                         //是否延迟
  206.                         if(key6==0)
  207.                       {
  208.                                 delayms(10);
  209.                                 if(key6==0)
  210.                                 delayms(10000);
  211.                                 while(!key1);
  212.                         }       

  213.                 }

  214. }
复制代码



分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏5 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:140725 发表于 2017-3-22 03:56 | 只看该作者
好资料,51黑有你更精彩
回复

使用道具 举报

板凳
ID:149167 发表于 2017-3-22 08:40 | 只看该作者
51黑有你真精彩
回复

使用道具 举报

地板
ID:302712 发表于 2018-4-19 11:29 | 只看该作者
不错的好资料,谢谢分享
回复

使用道具 举报

5#
ID:443340 发表于 2018-12-10 15:21 | 只看该作者
振羡慕你那么厉害
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表