Arduino UNO OLED 时钟

硬件基础

1. arduino UNO 开发板

2.128x64 OLED 7线 SPI 

3.DS3231时钟模块

Arduino UNO OLED 时钟_第1张图片
 

Arduino UNO OLED 时钟_第2张图片

Arduino UNO OLED 时钟_第3张图片

 Arduino 代码 PS:需要下载相应的库文件 

#include 
#include 
#include 
#include 
#include 

#define SCREEN_WIDTH 128 
#define SCREEN_HEIGHT 64 


#define OLED_MOSI  11
#define OLED_CLK   13
#define OLED_DC    9
#define OLED_CS    10
#define OLED_RESET 8
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT,
  OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);

#define NUMFLAKES     10 

#define LOGO_HEIGHT   16
#define LOGO_WIDTH    16

DS3231 Clock;
bool Century=false;
bool h12;
bool PM;
byte ADay, AHour, AMinute, ASecond, ABits;
bool ADy, A12h, Apm;

byte year, month, date, DoW, hour, minute, second;

static const unsigned char PROGMEM logo_bmp[] =
{ 0b00000000, 0b11000000,
  0b00000001, 0b11000000,
  0b00000001, 0b11000000,
  0b00000011, 0b11100000,
  0b11110011, 0b11100000,
  0b11111110, 0b11111000,
  0b01111110, 0b11111111,
  0b00110011, 0b10011111,
  0b00011111, 0b11111100,
  0b00001101, 0b01110000,
  0b00011011, 0b10100000,
  0b00111111, 0b11100000,
  0b00111111, 0b11110000,
  0b01111100, 0b11110000,
  0b01110000, 0b01110000,
  0b00000000, 0b00110000 };

void setup() {
  Wire.begin();

  if(!display.begin(SSD1306_SWITCHCAPVCC)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); 
  }

  display.display();
  delay(2000); 

  display.clearDisplay();

  display.drawPixel(10, 10, SSD1306_WHITE);

  display.display();
  delay(2000);

  Serial.begin(9600);
}

void ReadDS3231()
{
  int second,minute,hour,date,month,year,temperature; 
  second=Clock.getSecond();
  minute=Clock.getMinute();
  hour=Clock.getHour(h12, PM);
  date=Clock.getDate();
  month=Clock.getMonth(Century);
  year=Clock.getYear();
  
  temperature=Clock.getTemperature();
  
  Serial.print("20");
  Serial.print(year,DEC);
  Serial.print('-');
  Serial.print(month,DEC);
  Serial.print('-');
  Serial.print(date,DEC);
  Serial.print(' ');
  Serial.print(hour,DEC);
  Serial.print(':');
  Serial.print(minute,DEC);
  Serial.print(':');
  Serial.print(second,DEC);
  Serial.print('\n');
  Serial.print("Temperature=");
  Serial.print(temperature); 
  Serial.print('\n');

    display.setTextSize(1);             
    display.setTextColor(SSD1306_WHITE);
    display.clearDisplay();
    display.setCursor(15, 2);
    display.println("--CLOCK--");
    display.setCursor(0, 25);
    display.println("20");
    display.setCursor(12, 25);
    display.println(year);
    display.setCursor(22, 25);
    display.println(".");
    display.setCursor(29, 25);
    display.println(month);
    display.setCursor(35, 25);
    display.println(".");
    display.setCursor(42, 25);
    display.println(date);
    display.setCursor(64, 25);
    display.println(hour);
    display.setCursor(76, 25);
    display.println(":");
    display.setCursor(83, 25);
    display.println(minute);
    display.setCursor(94, 25);
    display.println(":");
    display.setCursor(99, 25);
    display.println(second);
    display.setCursor(0,50);
    display.println("Temperature=");
    display.setCursor(73,50);
    display.println(temperature);

    display.display(); 
}

void loop() {ReadDS3231();delay(1000);}

 使用DS3231时需要先初始化时间

你可能感兴趣的:(单片机,arduino)