要求:
(1)使用Arduino控制OLED 显示屏SSD1306 ( proteus中搜索UG-2864HSWEG01),在屏幕上显示一个尽可 能大的时钟表盘,包含时针、分针、秒针。时钟能够正常走字计时。在屏幕上空余的地方显示日期。
(2)Arduino上连接几个按钮,按下按钮时可以调整日期和时间。
注:题目来自HBUE的硬件编程考题,代码电路仅供学习交流使用,转载使用时请注明来处。
/*
SOFTWARE: Proteus 8 Professional
CSDN: 空白之子
公众号:硬核开发(正在建设中)
*/
#include
#include
#include
#include
#define OLED_MOSI 9
#define OLED_CLK 10
#define OLED_DC 11
#define OLED_CS 12
#define OLED_RESET 13
Adafruit_SSD1306 display(OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
#define NUMFLAKES 10
#define XPOS 0
#define YPOS 1
#define DELTAY 2
#define LOGO16_GLCD_HEIGHT 16
#define LOGO16_GLCD_WIDTH 16
static const unsigned char PROGMEM logo16_glcd_bmp[] =
{ B00000000, B11000000,
B00000001, B11000000,
B00000001, B11000000,
B00000011, B11100000,
B11110011, B11100000,
B11111110, B11111000,
B01111110, B11111111,
B00110011, B10011111,
B00011111, B11111100,
B00001101, B01110000,
B00011011, B10100000,
B00111111, B11100000,
B00111111, B11110000,
B01111100, B11110000,
B01110000, B01110000,
B00000000, B00110000 };
#if (SSD1306_LCDHEIGHT != 64)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif
#define PI 3.1415926
int year = 2021, month = 6, day = 11;
int i, j, k, hour1 , hour2 , min1 , min2 , second1 , second2;
int temp[12]={31,28,31,30,31,30,31,31,30,31,30,31};
int loction[12][2]={{31,3}, {49,8},{58,18},{60,31},{56,44},{46,52},{33,55},{22,51},{12,43},{5,31},{7,18},{14,7}};
char clockNum[12][3] = { "12", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11"};
void setup()
{
Serial.begin(9600);
display.begin(SSD1306_SWITCHCAPVCC);
display.display();
display.clearDisplay();
for (int ii = 2; ii <= 8; ++ii)
pinMode(ii, INPUT);
}
void loop()
{
for (i = 0; i < 24; ++i)
{
for (j = 0; j < 60; ++j)
{
for (k = 0; k < 60; ++k)
{
display.setTextSize(1);
display.setTextColor(WHITE);
Adjust();
OutClock();
OutClockTime();
OutTime();
OutDate();
display.display();
display.clearDisplay();
delay(1000);
}
}
}
CorrectDate();
}
//钟表显示函数
void OutClock(void)
{
display.drawCircle(35, 32, 31, WHITE);
display.drawPixel(35, 32, WHITE);
display.setCursor(31,3);
display.println("12");
for (int jj = 0; jj < 12; ++jj)
{
display.setCursor(loction[jj][0], loction[jj][1]);
display.println( clockNum[jj]);
}
}
//钟表时间显示函数
void OutClockTime(void)
{
hour1 = 35 + sin(PI * (i / 6.0) ) * 8;
hour2 = 32 - cos(PI * (i / 6.0) ) * 8;
min1 = 35 + sin(PI * (j / 30.0) ) * 12;
min2 = 32 - cos(PI * (j / 30.0) ) * 12;
second1 = 35 + sin(PI * (k / 30.0) ) * 16;
second2 = 32 - cos(PI * (k / 30.0) ) * 16;
display.drawLine(35, 32, hour1, hour2, WHITE);
display.drawLine(35, 32, min1, min2, WHITE);
display.drawLine(35, 32, second1, second2, WHITE);
}
//日期显示函数
void OutDate(void)
{
display.setCursor(75,30);
display.write((year/1000)+'0');
display.setCursor(80,30);
display.write((year/100%10)+'0');
display.setCursor(85,30);
display.write((year%100/10)+'0');
display.setCursor(90,30);
display.write((year%10)+'0');
display.setCursor(95,30);
display.println("-");
display.setCursor(100,30);
display.write((month/10)+'0');
display.setCursor(105,30);
display.write((month%10)+'0');
display.setCursor(110,30);
display.println("-");
display.setCursor(115,30);
display.write((day/10)+'0');
display.setCursor(120,30);
display.write((day%10)+'0');
}
//时间显示函数
void OutTime(void)
{
display.setCursor(80,20);
display.write((i/10)+'0');
display.setCursor(85,20);
display.write((i%10)+'0');
display.setCursor(90,20);
display.println(":");
display.setCursor(95,20);
display.write((j/10)+'0');
display.setCursor(100,20);
display.write((j%10)+'0');
display.setCursor(105,20);
display.println(":");
display.setCursor(110,20);
display.write((k/10)+'0');
display.setCursor(115,20);
display.write((k%10)+'0');
}
//时间日期调节函数
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 (i >= 24 || i < 0)
i = 0;
if (j >= 60 || j < 0)
j = 0;
}
//日期更正函数
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++;
}
}