ARDUINO板外面接線很麻煩,我用TFT屏模擬LED可以實現程序仿真,對初學者很有用
Sun Sep 22 19-03-11.jpg (172.97 KB, 下載次數: 100)
下載附件
2019-9-22 19:12 上傳
單片機源程序如下:
- #include <Adafruit_GFX.h>
- #include <MCUFRIEND_kbv.h>
- #include <TouchScreen.h>
- //#include<TimerOne.h>
- #include<TimerThree.h>
- #if defined(_GFXFONT_H_) //are we using the new library?
- #include <Fonts/FreeSans9pt7b.h>
- #define ADJ_BASELINE 11 //new fonts setCursor to bottom of letter
- #else
- #define ADJ_BASELINE 0 //legacy setCursor to top of letter
- #endif
- MCUFRIEND_kbv tft;
- #define YP A1 // must be an analog pin, use "An" notation!
- #define XM A2 // must be an analog pin, use "An" notation!
- #define YM 7 // can be a digital pin
- #define XP 6 // can be a digital pin
- #define LCD_CS A3
- #define LCD_CD A2
- #define LCD_WR A1
- #define LCD_RD A0
- #define LCD_RESET A4
- #define TS_MINX 150
- #define TS_MINY 120
- #define TS_MAXX 920
- #define TS_MAXY 940
- /*______End of Calibration______*/
- TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300); //300 is the sensitivity
- #define BLACK 0x0000
- #define BLUE 0x001F
- #define RED 0xF800
- #define GREEN 0x07E0
- #define CYAN 0x07FF
- #define MAGENTA 0xF81F
- #define YELLOW 0xFFE0
- #define WHITE 0xFFFF
- #define RGB(r, g, b) (((r&0xF8)<<8)|((g&0xFC)<<3)|(b>>3))
- #define GREY RGB(127, 127, 127)
- #define DARKGREY RGB(64, 64, 64)
- #define TURQUOISE RGB(0, 128, 128)
- #define PINK RGB(255, 128, 192)
- #define OLIVE RGB(128, 128, 0)
- #define PURPLE RGB(128, 0, 128)
- #define AZURE RGB(0, 128, 255)
- #define ORANGE RGB(255,128,64)
- /*************************************************************************************/
- #include <stdio.h>
- int ledPin1=21; int ledPin2=22; int ledPin3=23; int ledPin4=24;
- int ledPin5=25; int ledPin6=26; int ledPin7=27; int ledPin8=28;
- int DB[] = {21, 22, 23, 24, 25, 26, 27, 28};//使用數組來定義總線需要的管腳
- unsigned char m,j,p=0;
- #define ledx0 30
- #define ledy0 100
- #define ledd0 35
- #define ledw0 20
- #define ORT 1
- /*************************************************************************************/
- uint16_t ID;
- uint16_t LEDCOLOR=RED;
- int X,Y;
- byte action;
- boolean result = false;
- #define MINPRESSURE 10
- #define MAXPRESSURE 1000
- /*************************************************************************************/
- uint8_t conv2d(const char* p)
- {
- uint8_t v = 0;
- if ('0' <= *p && *p <= '9') v = *p - '0';
- return 10 * v + *++p - '0';
- }
- /*************************************************************************************/
- void setup(void)
- {
- Serial.begin(9600);
- tft.reset();
- ID = tft.readID();
- if (ID == 0xD3D3) ID = 0x9486; // write-only shield
- tft.begin(ID);
- tft.setRotation(1);
- tft.fillScreen(WHITE);
- Timer3.initialize(500000);
- Timer3.attachInterrupt(Timer1Int);
- #if defined(_GFXFONT_H_)
- tft.setFont(&FreeSans9pt7b);
- #endif
- pinMode(ledPin1,OUTPUT); pinMode(ledPin2,OUTPUT);
- pinMode(ledPin3,OUTPUT); pinMode(ledPin4,OUTPUT);
- pinMode(ledPin5,OUTPUT); pinMode(ledPin6,OUTPUT);
- pinMode(ledPin7,OUTPUT); pinMode(ledPin8,OUTPUT);
- DrawLed(ledx0+0*ledd0, ledy0, ledw0,ledw0,GREY,21,ORT);
- DrawLed(ledx0+1*ledd0, ledy0, ledw0,ledw0,GREY,22,ORT);
- DrawLed(ledx0+2*ledd0, ledy0, ledw0,ledw0,GREY,23,ORT);
- DrawLed(ledx0+3*ledd0, ledy0, ledw0,ledw0,GREY,24,ORT);
- DrawLed(ledx0+4*ledd0, ledy0, ledw0,ledw0,GREY,25,ORT);
- DrawLed(ledx0+5*ledd0, ledy0, ledw0,ledw0,GREY,26,ORT);
- DrawLed(ledx0+6*ledd0, ledy0, ledw0,ledw0,GREY,27,ORT);
- DrawLed(ledx0+7*ledd0, ledy0, ledw0,ledw0,GREY,28,ORT);
- m=0xfe;
-
- }
- /*************************************************************************************/
- TSPoint waitTouch()
- {
- TSPoint p;
- //p = ts.getPoint();
- if((p.z > MINPRESSURE )||(p.z < MAXPRESSURE))
- { p = ts.getPoint();
- pinMode(XM, OUTPUT);
- pinMode(YP, OUTPUT);
- p.x = map(p.x, TS_MINX, TS_MAXX, 0, 320);
- p.y = map(p.y, TS_MINY, TS_MAXY, 0, 240);
- }
-
- return p;
- }
- /*************************************************************************************/
- void DetectButtons()
- {
- TSPoint p = waitTouch();
- X = p.x; Y = p.y;
-
- if ((Y<200 && Y>160) &&(X<100 && X>60))
- result=!result;
-
- }
- /*************************************************************************************/
- void DrawLed(int x0, int y0, int h,int w ,int color,int IONum,byte ort)
- {
- if(ort==0)
- {tft.fillTriangle( x0, y0, x0, y0+h, x0+w, y0+h/2, color) ;
- tft.drawFastVLine(x0+w, y0, h, color);
- tft.drawFastHLine(x0-h, y0+w/2, h, color);
- tft.drawFastVLine(x0-h, y0+w/2-ledd0/2, ledd0, color);
-
- tft.drawFastHLine(x0+w, y0+h/2, w/2, color);
- tft.setCursor(x0+w+w/2,y0+h/2);
- }
- else if(ort==1)
- {tft.fillTriangle( x0, y0, x0+w, y0, x0+h/2, y0-w, color) ;
- tft.drawFastHLine(x0, y0-h, h, color);
- tft.drawFastVLine(x0+h/2, y0, h, color);
- tft.drawFastHLine(x0+w/2-ledd0/2, y0+h, ledd0, color);
-
- tft.drawFastVLine(x0+h/2, y0-w-w/2, w/2, color);
- tft.setCursor(x0+w/2,y0-h-h/2);
- }
-
- tft.setTextColor(BLACK);
- tft.setTextSize(1);
- tft.print(IONum);
- }
- /*************************************************************************************/
- void DetectLed(int IONum)
- {
- if(digitalRead(IONum))
- DrawLed( ledx0+(IONum%10-1)*ledd0, ledy0, ledw0,ledw0 , GREY,IONum, ORT);
- else
- DrawLed( ledx0+(IONum%10-1)*ledd0, ledy0, ledw0,ledw0 , LEDCOLOR,IONum, ORT);
- }
- /*************************************************************************************/
- /*************************************************************************************/
- void ledflash_8(int LEDCOLOR)
- { DetectLed(21); DetectLed(22);
- DetectLed(23); DetectLed(24);
- DetectLed(25); DetectLed(26);
- DetectLed(27); DetectLed(28);
- }
- /*************************************************************************************/
- void Timer1Int()
- {unsigned char i,k,n;
- static int x=1;
- // if(result)
- // {x=1-x;
- // digitalWrite(22,x);
- // }
- if(result)
- { k=m>>(8-p); n=m<<p;
- j=k|n;
- for (i=0; i<=7; i++) //總線賦值
- {
- digitalWrite(DB[i],j&0x01);
- j>>=1;
- }
- p=p+1;if(p==8) p=0;
- }
-
- }
- /*************************************************************************************/
-
- void loop(void)
- {
- static boolean cp=true;
- DetectButtons();
-
- if(result)
- {//DetectLed(22);
- ledflash_8(LEDCOLOR);
- if(!cp)
- {
- tft.drawChar(220,180,'S',WHITE,WHITE,1);
- tft.drawChar(232,180,'T',WHITE,WHITE,1);
- tft.drawChar(244,180,'A',WHITE,WHITE,1);
- tft.drawChar(256,180,'R',WHITE,WHITE,1);
- tft.drawChar(268,180,'T',WHITE,WHITE,1);
-
- tft.drawChar(220,180,'S',RED,WHITE,1);
- tft.drawChar(232,180,'T',RED,WHITE,1);
- tft.drawChar(244,180,'O',RED,WHITE,1);
- tft.drawChar(256,180,'P',RED,WHITE,1);
- cp=true;
- }
- }
- else
- { if(cp)
- {
- tft.drawChar(220,180,'S',WHITE,WHITE,1);
- tft.drawChar(232,180,'T',WHITE,WHITE,1);
- tft.drawChar(244,180,'O',WHITE,WHITE,1);
- tft.drawChar(256,180,'P',WHITE,WHITE,1);
-
- tft.drawChar(220,180,'S',BLUE,WHITE,1);
- tft.drawChar(232,180,'T',BLUE,WHITE,1);
- tft.drawChar(244,180,'A',BLUE,WHITE,1);
- tft.drawChar(256,180,'R',BLUE,WHITE,1);
- tft.drawChar(268,180,'T',BLUE,WHITE,1);
- cp=false;
- }
- }
- }
- /*************************************************************************************/
復制代碼
以上資料51hei提供下載:
LED_TFT閃爍 _1.rar
(2.45 KB, 下載次數: 11)
2019-9-22 19:13 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|