milanmetal

[RSDMK] TFT 1.41'' Demo

May 15th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.28 KB | None | 0 0
  1. #include <c8051f340.h>
  2. #include <math.h>
  3. #include <stdlib.h>        
  4. #include "time.h"
  5. #include "TFT.h"
  6.  
  7. void OSCILLATOR_Init (void);          
  8.  
  9. void main(void)
  10. {
  11.     int i;
  12.     byte buf[126];
  13.     int x, x2;
  14.     int y, y2;
  15.     int r;
  16.  
  17.     PCA0MD &= ~0x40;    // Watchdog tajmer iskljucen
  18.  
  19.     OSCILLATOR_Init();  // Inicijalizacija oscilatora (Fosc = 22.1184 MHz)
  20.     TIME_Init();        // Inicijalizacija sistemskog vremena (koristi se prekid tajmera 2)
  21.     TFT_Init();         // Inicijalizacija displeja
  22.  
  23.     while(1)
  24.     {
  25.        
  26.         TFT_clrScr();
  27.         TFT_setColor(255, 0, 0);
  28.         TFT_fillRect(0, 0, 127, 12);
  29.        
  30.         TFT_setColor(64, 64, 64);
  31.         TFT_fillRect(0,117,127,127);
  32.        
  33.         TFT_setColor(255, 255, 255);
  34.         TFT_setBackColor(255,0,0);
  35.         TFT_print("TFT 1.44\" DEMO", CENTER, 0, 0);
  36.        
  37.         TFT_setBackColor(64,64,64);
  38.         TFT_setColor(255,255,0);
  39.         TFT_print("C8051F340", CENTER, 116, 0);
  40.        
  41.         TFT_setColor(0,255,0);
  42.         TFT_drawRect(0,13,127,116);
  43.        
  44.        
  45.         // Draw crosshairs
  46.         TFT_setColor(0,0,255);
  47.         TFT_drawLine(63,14,63,115);
  48.         TFT_drawLine(1,63,126,63);
  49.         for (i=3; i<128; i+=10)
  50.         TFT_drawLine(i, 61, i, 65);
  51.         for (i=14; i<118; i+=10)
  52.         TFT_drawLine(61, i, 65, i);
  53.        
  54.        
  55.         // Draw sin-, cos- and tan-lines
  56.         TFT_setColor(0,255,255);
  57.         TFT_setBackColor(0,0,0);
  58.         TFT_print("Sin", 2, 14 ,0);
  59.        
  60.         for (i=1; i<126; i++)
  61.         {
  62.         TFT_drawPixel(i,63+(sin(((i*2.85)*3.14)/180)*45));
  63.         }
  64.        
  65.         TFT_setColor(255,0,0);
  66.         TFT_print("Cos", 2, 26, 0);
  67.         for (i=1; i<126; i++)
  68.         {
  69.         TFT_drawPixel(i,63+(cos(((i*2.85)*3.14)/180)*45));
  70.         }
  71.        
  72.         TFT_setColor(255,255,0);
  73.         TFT_print("Tan", 2, 38, 0);
  74.         for (i=1; i<126; i++)
  75.         {
  76.         TFT_drawPixel(i,63+(tan(((i*2.85)*3.14)/180)));
  77.         }
  78.        
  79.         delay_ms(2000);
  80.        
  81.         TFT_setColor(0,0,0);
  82.         TFT_fillRect(1,14,126,115);
  83.         TFT_setColor(0,0,255);
  84.         TFT_drawLine(63,14,63,115);
  85.         TFT_drawLine(1,63,126,63);
  86.        
  87.         // Draw a moving sinewave
  88.         x=1;
  89.         for (i=1; i<3654; i++)
  90.         {
  91.         x++;
  92.         if (x==127)
  93.           x=1;
  94.         if (i>127)
  95.         {
  96.           if ((x==63)||(buf[x-1]==63))
  97.             TFT_setColor(0,0,255);
  98.           else
  99.             TFT_setColor(0,0,0);
  100.           TFT_drawPixel(x,buf[x-1]);
  101.         }
  102.         TFT_setColor(0,255,255);
  103.         y=63+(sin(((i*1.3)*3.14)/180)*45);
  104.         TFT_drawPixel(x,y);
  105.         buf[x-1]=y;
  106.             delay_ms(3);
  107.         }
  108.        
  109.        
  110.         TFT_setColor(0,0,0);
  111.         TFT_fillRect(1,14,126,115);
  112.        
  113.         // Draw some filled rectangles
  114.         for (i=1; i<6; i++)
  115.         {
  116.         switch (i)
  117.         {
  118.           case 1:
  119.             TFT_setColor(255,0,255);
  120.             break;
  121.           case 2:
  122.             TFT_setColor(255,0,0);
  123.             break;
  124.           case 3:
  125.             TFT_setColor(0,255,0);
  126.             break;
  127.           case 4:
  128.             TFT_setColor(0,0,255);
  129.             break;
  130.           case 5:
  131.             TFT_setColor(255,255,0);
  132.             break;
  133.         }
  134.         TFT_fillRect(10+(i*10),10+(i*10), 60+(i*10), 60+(i*10));
  135.         }
  136.         delay_ms(2000);
  137.        
  138.         TFT_setColor(0,0,0);
  139.         TFT_fillRect(1,14,126,115);
  140.        
  141.         // Draw some filled, rounded rectangles
  142.         for (i=1; i<6; i++)
  143.         {
  144.         switch (i)
  145.         {
  146.           case 1:
  147.             TFT_setColor(255,0,255);
  148.             break;
  149.           case 2:
  150.             TFT_setColor(255,0,0);
  151.             break;
  152.           case 3:
  153.             TFT_setColor(0,255,0);
  154.             break;
  155.           case 4:
  156.             TFT_setColor(0,0,255);
  157.             break;
  158.           case 5:
  159.             TFT_setColor(255,255,0);
  160.             break;
  161.         }
  162.         TFT_fillRoundRect(70-(i*10),10+(i*10), 120-(i*10), 60+(i*10));
  163.         }
  164.         delay_ms(2000);
  165.        
  166.         TFT_setColor(0,0,0);
  167.         TFT_fillRect(1,14,126,115);
  168.        
  169.         // Draw some filled circles
  170.         for (i=1; i<6; i++)
  171.         {
  172.         switch (i)
  173.         {
  174.           case 1:
  175.             TFT_setColor(255,0,255);
  176.             break;
  177.           case 2:
  178.             TFT_setColor(255,0,0);
  179.             break;
  180.           case 3:
  181.             TFT_setColor(0,255,0);
  182.             break;
  183.           case 4:
  184.             TFT_setColor(0,0,255);
  185.             break;
  186.           case 5:
  187.             TFT_setColor(255,255,0);
  188.             break;
  189.         }
  190.         TFT_fillCircle(30+(i*10),35+(i*10), 25);
  191.         }
  192.        
  193.         delay_ms(2000);
  194.        
  195.         TFT_setColor(0,0,0);
  196.         TFT_fillRect(1,14,126,115);
  197.        
  198.         // Draw some lines in a pattern
  199.         TFT_setColor (255,0,0);
  200.         for (i=11; i<115; i+=3)
  201.         {
  202.         TFT_drawLine(1, i, i-10, 115);
  203.         }
  204.         TFT_setColor (255,0,0);
  205.         for (i=112; i>14; i-=3)
  206.         {
  207.         TFT_drawLine(126, i, i+14, 14);
  208.         }
  209.         TFT_setColor (0,255,255);
  210.         for (i=115; i>14; i-=3)
  211.         {
  212.         TFT_drawLine(1, i, 116-i, 14);
  213.         }
  214.         TFT_setColor (0,255,255);
  215.         for (i=14; i<115; i+=3)
  216.         {
  217.         TFT_drawLine(126, i, 140-i, 115);
  218.         }
  219.        
  220.         delay_ms(2000);
  221.        
  222.         TFT_setColor(0,0,0);
  223.         TFT_fillRect(1,14,126,115);
  224.        
  225.         // Draw some random circles
  226.         for (i=0; i<100; i++)
  227.         {
  228.         TFT_setColor(rand() % 255, rand() % 255, rand() % 255);
  229.         x=22+rand() % 85;
  230.         y=35+rand() % 59;
  231.         r=rand() % 20;
  232.         TFT_drawCircle(x, y, r);
  233.         }
  234.        
  235.         delay_ms(2000);
  236.        
  237.         TFT_setColor(0,0,0);
  238.         TFT_fillRect(1,14,126,115);
  239.        
  240.         // Draw some random rectangles
  241.         for (i=0; i<100; i++)
  242.         {
  243.         TFT_setColor(rand() % 255, rand() % 255, rand() % 255);
  244.         x=2+rand() % 124;
  245.         y=15+rand() % 101;
  246.         x2=2+rand() % 124;
  247.         y2=15+rand() % 101;
  248.         TFT_drawRect(x, y, x2, y2);
  249.         }
  250.        
  251.         delay_ms(2000);
  252.        
  253.         TFT_setColor(0,0,0);
  254.         TFT_fillRect(1,14,126,115);
  255.        
  256.         // Draw some random rounded rectangles
  257.         for (i=0; i<100; i++)
  258.         {
  259.         TFT_setColor(rand() % 255, rand() % 255, rand() % 255);
  260.         x=2+rand() % 124;
  261.         y=15+rand() % 101;
  262.         x2=2+rand() % 124;
  263.         y2=15+rand() % 101;
  264.         TFT_drawRoundRect(x, y, x2, y2);
  265.         }
  266.        
  267.         delay_ms(2000);
  268.        
  269.         TFT_setColor(0,0,0);
  270.         TFT_fillRect(1,14,126,115);
  271.        
  272.         // Draw some random lines
  273.         for (i=0; i<100; i++)
  274.         {
  275.         TFT_setColor(rand() % 255, rand() % 255, rand() % 255);
  276.         x=2+rand() % 124;
  277.         y=15+rand() % 101;
  278.         x2=2+rand() % 124;
  279.         y2=15+rand() % 101;
  280.         TFT_drawLine(x, y, x2, y2);
  281.         }
  282.        
  283.         delay_ms(2000);
  284.        
  285.         TFT_setColor(0,0,0);
  286.         TFT_fillRect(1,14,126,115);
  287.        
  288.         // Draw some random pixels
  289.         for (i=0; i<5000; i++)
  290.         {
  291.         TFT_setColor(rand() % 255, rand() % 255, rand() % 255);
  292.         TFT_drawPixel(2+rand() % 124, 15+rand() % 101);
  293.         }
  294.         delay_ms(2000);
  295.        
  296.         //Set up the "Finished"-screen
  297.         TFT_fillScr(0,0,255);
  298.         TFT_setColor(255,0,0);
  299.         TFT_fillRoundRect(2, 40, 125, 88);
  300.        
  301.         TFT_setColor(255,255,255);
  302.         TFT_setBackColor(255,0,0);
  303.         TFT_print("That's it!", CENTER, 46, 0);
  304.         TFT_print("Restarting in a", CENTER, 66, 0);
  305.         TFT_print("few seconds...", CENTER, 76, 0);
  306.        
  307.         TFT_setColor(0,0,0);
  308.         TFT_setBackColor(0,0,255);
  309.         TFT_print("Runtime: (msecs)", CENTER, 108, 0);
  310.         TFT_printNumI(millis(), CENTER, 118, 0, ' ');
  311.    
  312.         delay_ms(2000);
  313.     }
  314. }
Add Comment
Please, Sign In to add comment