ili9341显示屏–时钟
要求:
- 使用Arduino控制LCD显示屏ili9341,在屏幕上显示一个尽可能大的时钟表盘,包含时针、分针、秒针。时钟能够正常走字计时。在屏幕上空余的地方显示日期。
- Arduino上连接几个按钮,按下按钮时可以调整日期和时间。
注:题目来自HBUE的硬件编程考题,本代码仍有缺陷,显示器闪动严重,且不够简洁,代码电路仅供学习交流使用,转载使用时请注明来处。
代码
/*
SOFTWARE: Proteus 8 Professional
CSDN: 空白之子
*/
#include <stdint.h>
#include <TFTv2.h>
#include <SPI.h>
#define PI 3.1415926
int i, j, k, m;
int hour1, hour2, min1, min2, second1, second2;
int year = 2021, month = 6, day = 18;
int temp[12]={31,28,31,30,31,30,31,31,30,31,30,31};
char clockNum[12][3] = { "12", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11"};
char num[12][3] = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
void setup()
{
Serial.begin(9600);
SPSR |= (1 << SPI2X); // 2x SPI speed
TFT_BL_ON; // turn on the background light
Tft.TFTinit(); //init TFT library
for (int ii = 2; ii <= 8; ++ii)
pinMode(ii, INPUT);
OutClock();
}
//显示器240*320
void loop()
{
for (i = 0; i < 24; ++i)
{
for (j = 0; j < 60; ++j)
{
for (k = 0; k < 60; ++k)
{
Adjust();
OutTime();
Clear();
}
}
}
CorrectDate();
}
//显示裱框
void OutClock(void)
{
int m, x, y;
Tft.drawCircle(120, 110, 90,WHITE);
for ( m = 0; m < 12; ++m)
{
x = 120 + sin(PI * (m / 6.0) ) * 85;
y = 110 - cos(PI * (m / 6.0) ) * 85;
Tft.drawString(clockNum[m], x, y, 2, WHITE);
}
}
//显示时间日期表针
void OutTime(void)
{
//时针、分针、秒针
if (j == 59 || i == 0)
{
Tft.drawLine(120,110,hour1,hour2,BLACK);
}
if (i == 59 || i == 0)
{
Tft.drawLine(120,110,min1,min2,BLACK);
}
Tft.drawLine(120,110, second1, second2, BLACK);
hour1 = 120 + sin(PI * (i / 6.0) ) * 35;
hour2 = 110 - cos(PI * (i / 6.0) ) * 35;
min1 = 120 + sin(PI * (j / 30.0) ) * 55;
min2 = 110 - cos(PI * (j / 30.0) ) * 55;
second1 = 120 + sin(PI * (k / 30.0) ) * 70;
second2 = 110 - cos(PI * (k / 30.0) ) * 70;
Tft.drawLine(120,110, second1, second2, WHITE);
Tft.drawLine(120,110,min1,min2,GREEN);
Tft.drawLine(120,110,hour1,hour2,RED);
//数字时间日期
Tft.drawString(num[year/1000], 45, 260, 2, WHITE);
Tft.drawString(num[year/100%10], 60, 260, 2, WHITE);
Tft.drawString(num[year%100/10], 75, 260, 2, WHITE);
Tft.drawString(num[year%10], 90, 260, 2, WHITE);
Tft.drawString("-", 105, 260, 2, WHITE);
Tft.drawString(num[month/10], 120, 260, 2, WHITE);
Tft.drawString(num[month%10], 135, 260, 2, WHITE);
Tft.drawString("-", 150, 260, 2, WHITE);
Tft.drawString(num[day/10], 165, 260, 2, WHITE);
Tft.drawString(num[day%10], 180, 260, 2, WHITE);
Tft.drawString(num[i/10], 60, 230, 2, WHITE);
Tft.drawString(num[i%10], 75, 230, 2, WHITE);
Tft.drawString(":", 90, 230, 2, WHITE);
Tft.drawString(num[ j/10], 105, 230, 2, WHITE);
Tft.drawString(num[j%10], 120, 230, 2, WHITE);
Tft.drawString(":", 135, 230, 2, WHITE);
Tft.drawString(num[k/10], 150, 230, 2, WHITE);
Tft.drawString(num[k%10], 165, 230, 2, WHITE);
delay(1000);
}
//更正日期
void CorrectDate(void)
{
day++;
if ( (year % 4 == 0 && year %100 != 0) || (year %400 == 0) )
temp[1] = 29;
if (day > temp[month - 1] )
{
day = 1;
month++;
}
if (month > 12)
{
month = 1;
year++;
}
}
//调整时间日期
void Adjust(void)
{
if (digitalRead(2) == HIGH)
{
if (digitalRead(7) == HIGH)
year++;
if (digitalRead(8) == HIGH)
year--;
}
if (digitalRead(3) == HIGH)
{
if (digitalRead(7) == HIGH)
month++;
if (digitalRead(8) == HIGH)
month--;
}
if (digitalRead(4) == HIGH)
{
if (digitalRead(7) == HIGH)
day++;
if (digitalRead(8) == HIGH)
day--;
}
if (digitalRead(5) == HIGH)
{
if (digitalRead(7) == HIGH)
i++;
if (digitalRead(8) == HIGH)
i--;
}
if (digitalRead(6) == HIGH)
{
if (digitalRead(7) == HIGH)
j++;
if (digitalRead(8) == HIGH)
j--;
}
if (k < 0 || k >= 60)
k = 0;
if (j < 0 || j >= 60)
j = 0;
if (i < 0 || i >= 24)
i = 0;
}
//部分清屏
void Clear(void)
{
Tft.drawLine(120,110,hour1,hour2,BLACK);
Tft.drawLine(120,110,min1,min2,BLACK);
Tft.drawLine(120,110, second1, second2, BLACK);
Tft.drawString(num[year/1000], 45, 260, 2, BLACK);
Tft.drawString(num[year/100%10], 60, 260, 2, BLACK);
Tft.drawString(num[year%100/10], 75, 260, 2, BLACK);
Tft.drawString(num[year%10], 90, 260, 2, BLACK);
Tft.drawString("-", 105, 260, 2, BLACK);
Tft.drawString(num[month/10], 120, 260, 2, BLACK);
Tft.drawString(num[month%10], 135, 260, 2, BLACK);
Tft.drawString("-", 150, 260, 2, BLACK);
Tft.drawString(num[day/10], 165, 260, 2, BLACK);
Tft.drawString(num[day%10], 180, 260, 2, BLACK);
Tft.drawString(num[i/10], 60, 230, 2, BLACK);
Tft.drawString(num[i%10], 75, 230, 2, BLACK);
Tft.drawString(":", 90, 230, 2, BLACK);
Tft.drawString(num[ j/10], 105, 230, 2, BLACK);
Tft.drawString(num[j%10], 120, 230, 2, BLACK);
Tft.drawString(":", 135, 230, 2, BLACK);
Tft.drawString(num[k/10], 150, 230, 2, BLACK);
Tft.drawString(num[k%10], 165, 230, 2, BLACK);
}