[概要] [CircuitPythonで開発] [OLED編] 〔Seeeduino XIAOの使い方に戻る〕
-------------------------------------------------------------------------------- #include <U8g2lib.h> #include <PCF8563.h> // RTC(PCF8563)を使用する為のコンストラクタ PCF8563 rtc ; // SSD1306をグラフィックディスプレイとして使用する為のコンストラクタ // メモリバッファ(RAM)は1ページ分の128b分確保 // ローテーション(画面回転)なし、SCL=5番ピン SDA=4番ピン U8G2_SSD1306_128X64_NONAME_1_HW_I2C u8g2(U8G2_R0,5,4) ; #define PCF8563_address 0x51 // PCF8563のI2Cアドレス #define PCF8563_Weekdays 0x06 // 曜日をセットするレジスタアドレス値 // PCF8563に曜日を設定する(0:日曜〜6:土曜) void SetWeekdays(uint8_t wdays) { const uint8_t data = (((wdays/10) << 4)|(wdays%10)); Wire.beginTransmission(PCF8563_address); Wire.write(PCF8563_Weekdays); Wire.write(data); Wire.endTransmission(); } char WeekDayData[7][4] = {"日","月","火","水","木","金","土"} ; void setup() { u8g2.begin() ; // OLEDの初期化 u8g2.enableUTF8Print() ; // Arduino print()関数のUTF8サポートを有効にする rtc.init() ; // RTCの初期化 // 2021年1月23日 土曜日 15時24分00秒で初期設定 rtc.stopClock() ; // RTCのカウントを止める rtc.setYear(21) ; // 年の設定(00-99) rtc.setMonth(1) ; // 月の設定(01-12) rtc.setDay(23) ; // 日の設定(01-31) SetWeekdays(6) ; // 土曜日の設定(0-6) rtc.setHour(15) ; // 時の設定(00-23) rtc.setMinut(24) ;// 分の設定(00-59) rtc.setSecond(0) ;// 秒の設定(00-59) rtc.startClock() ;// RTCのカウントを開始する } void loop() { char buf[12] ; Time nowTime = rtc.getTime() ; // 現在の時刻を得る u8g2.setFont(u8g2_font_b16_t_japanese2) ; // 日本語フォント高さ16bit u8g2.firstPage() ; do { // 日付の表示 u8g2.setCursor(7, 16) ; sprintf(buf,"%d/%02d/%02d",nowTime.year+2000,nowTime.month,nowTime.day) ; u8g2.print(buf) ; // 曜日の表示 u8g2.setCursor(87, 16) ; sprintf(buf,"(%s)",WeekDayData[nowTime.weekday]) ; u8g2.print(buf) ; // 時刻の表示 u8g2.setCursor(30, 32) ; sprintf(buf,"%02d:%02d:%02d",nowTime.hour,nowTime.minute,nowTime.second) ; u8g2.print(buf) ; } while ( u8g2.nextPage() ) ; delay(500) ; } --------------------------------------------------------------------------------
-------------------------------------------------------------------------------- #include <U8g2lib.h> #include <PCF8563.h> #include <skPCF8563.h> char WeekDayData[7][4] = {"日","月","火","水","木","金","土"} ; // RTC(PCF8563)を使用する為のコンストラクタ PCF8563 rtc ; skPCF8563 skRTC ; // SSD1306をグラフィックディスプレイとして使用する為のコンストラクタ // メモリバッファ(RAM)は1ページ分の128b分確保 // ローテーション(画面回転)なし、SCL=5番ピン SDA=4番ピン U8G2_SSD1306_128X64_NONAME_1_HW_I2C u8g2(U8G2_R0,5,4) ; void setup() { u8g2.begin() ; // OLEDの初期化 u8g2.enableUTF8Print() ; // Arduino print()関数のUTF8サポートを有効にする u8g2.setFont(u8g2_font_b16_t_japanese2) ; // 日本語フォント高さ16bit rtc.init() ; // RTCの初期化 // 2021年1月23日 土曜日 15時24分00秒で初期設定 rtc.stopClock() ; // RTCのカウントを止める rtc.setYear(21) ; // 年の設定(00-99) rtc.setMonth(1) ; // 月の設定(01-12) rtc.setDay(23) ; // 日の設定(01-31) skRTC.SetWeekdays(6) ; // 土曜日の設定(0-6) rtc.setHour(15) ; // 時の設定(00-23) rtc.setMinut(24) ;// 分の設定(00-59) rtc.setSecond(0) ;// 秒の設定(00-59) rtc.startClock() ;// RTCのカウントを開始する // アラームの時間を設定し有効にする(毎25分に発生) skRTC.SetAlarmTime(25, 80, 80, 80) ; // タイマーのカウントダウンを開始する(3分間毎にカウントダウンが発生) skRTC.TimerCountdownStart(3,PCF8563_TimerF_1_60Hz) ; } void loop() { uint8_t almflg, timerflg ; char buf[12] ; // アラーム発生をチェック almflg = skRTC.AlarmMatch() ; // タイマーのカウントダウンをチェック timerflg = skRTC.TimerCountdownCheck() ; // 現在の時刻を得る Time nowTime = rtc.getTime() ; u8g2.firstPage() ; do { // タイマーのカウントダウンが発生したかチェックする if (timerflg == 1) { skRTC.TimerClearFlag() ; // アラームフラグをクリア u8g2.setCursor(10, 63) ; u8g2.print("カウントダウン") ; } // アラームが発生したかチェックする if (almflg == 1) { skRTC.AlarmClearFlag() ; // アラームフラグをクリア u8g2.setCursor(20, 63) ; u8g2.print("アラーム発生") ; } // 日付の表示 u8g2.setCursor(7, 16) ; sprintf(buf,"%d/%02d/%02d",nowTime.year+2000,nowTime.month,nowTime.day) ; u8g2.print(buf) ; // 曜日の表示 u8g2.setCursor(87, 16) ; sprintf(buf,"(%s)",WeekDayData[nowTime.weekday]) ; u8g2.print(buf) ; // 時刻の表示 u8g2.setCursor(30, 32) ; sprintf(buf,"%02d:%02d:%02d",nowTime.hour,nowTime.minute,nowTime.second) ; u8g2.print(buf) ; } while ( u8g2.nextPage() ) ; delay(500) ; } --------------------------------------------------------------------------------【skPCF8563ライブラリの使い方】
#include <PCF8563.h> #include <skPCF8563.h> PCF8563 rtc ; skPCF8563 skRTC ; void setup() { rtc.init() ; } こんな感じになります。・void skRTC.SetWeekdays(uint8_t wdays)
例)// 毎30分(1時間毎に1回発生)に起動させるなら skRTC.SetAlarmTime(30, 80, 80, 80) ; // 毎12時00分(1日毎に1回発生)に起動させるなら skRTC.SetAlarmTime(00, 12, 80, 80) ; // 毎月一日の12時00分(一ヶ月毎に1回発生)に起動させるなら skRTC.SetAlarmTime(00, 12, 1, 80) ; // 毎週日曜日の12時00分(毎週に1回発生)に起動させるなら skRTC.SetAlarmTime(00, 12, 80, 0) ;・uint8_t skRTC.AlarmMatch()
-------------------------------------------------------------------------------- #include <U8g2lib.h> #include <SD.h> #define bitmap_width 97 // 画像ビットマップの幅幅ビット数(13バイト) #define bitmap_height 51 // 画像ビットマップの高さビット数(51バイト) static const unsigned char bitmap_data_bits[] U8X8_PROGMEM = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3c, 0x80, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf8, 0x00, 0x00, 0x00, 0x3c, 0x80, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfe, 0x03, 0x00, 0x00, 0x3c, 0x80, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x03, 0x00, 0x00, 0x3c, 0x80, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0xcf, 0x07, 0x00, 0x00, 0x3c, 0x80, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x83, 0x07, 0x00, 0x00, 0x3c, 0x80, 0x07, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x81, 0x07, 0x00, 0x00, 0x3c, 0x80, 0x07, 0xfc, 0x03, 0x1c, 0x00, 0x3e, 0x1c, 0xc0, 0x03, 0x00, 0x00, 0x3c, 0x80, 0x07, 0xff, 0x07, 0x7f, 0x80, 0xff, 0x3f, 0xe0, 0x01, 0x00, 0x00, 0x3c, 0x80, 0x07, 0xff, 0x8f, 0xff, 0xc1, 0xff, 0x3f, 0xf0, 0x00, 0x00, 0x00, 0x3c, 0x80, 0x87, 0xff, 0xdf, 0xff, 0xc1, 0xc3, 0x07, 0x7c, 0x00, 0x00, 0x00, 0x3c, 0x80, 0x87, 0x0f, 0xfe, 0xff, 0xe3, 0x81, 0x03, 0x1e, 0x00, 0x00, 0x00, 0x3c, 0x80, 0xc7, 0x07, 0xfc, 0xe3, 0xe3, 0x81, 0x07, 0x0f, 0x00, 0x00, 0x00, 0x3c, 0x80, 0xc7, 0x07, 0xf8, 0xc1, 0xe7, 0x81, 0x87, 0xff, 0x07, 0x00, 0x00, 0x3c, 0x80, 0xc7, 0x03, 0xf0, 0x80, 0xe7, 0xc3, 0x87, 0xff, 0x07, 0x00, 0x00, 0x3c, 0x80, 0xc7, 0x03, 0x70, 0x80, 0xc7, 0xe7, 0x83, 0xff, 0x07, 0x00, 0x00, 0x3c, 0x80, 0xc7, 0x03, 0x78, 0x80, 0xc7, 0xff, 0x03, 0x00, 0x00, 0x00, 0x00, 0x3c, 0x80, 0xc7, 0x03, 0xf8, 0xc0, 0x87, 0xff, 0x01, 0x00, 0x00, 0x00, 0x00, 0x3c, 0x80, 0xc7, 0x07, 0xfc, 0xc1, 0xc7, 0x7f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3c, 0xc0, 0x87, 0x0f, 0xfe, 0xff, 0xe3, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x78, 0xc0, 0x83, 0xff, 0xdf, 0xff, 0xe3, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf8, 0xf1, 0x03, 0xff, 0x8f, 0xff, 0xe1, 0xff, 0x07, 0x00, 0x00, 0x00, 0x00, 0xf0, 0xff, 0x01, 0xfe, 0x0f, 0xff, 0xc0, 0xff, 0x0f, 0x00, 0x00, 0x00, 0x00, 0xe0, 0xff, 0x00, 0xfc, 0x03, 0x7c, 0xc0, 0xff, 0x1f, 0x00, 0x00, 0x00, 0x00, 0x80, 0x3f, 0x00, 0xf8, 0x01, 0x00, 0xe0, 0x01, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x00, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x00, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0xf0, 0xc7, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0xe0, 0xff, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0xc0, 0xff, 0x03, 0x00, 0x00, 0x00, 0x00, 0xfc, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf8, 0xff, 0xff, 0xff, 0xff, 0xff, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x24, 0x20, 0x00, 0x00, 0x08, 0x46, 0x02, 0x00, 0x80, 0xc0, 0x40, 0x00, 0x0c, 0x6e, 0x6a, 0xc0, 0xa4, 0x48, 0x04, 0xaa, 0xac, 0x8c, 0xaa, 0xac, 0x00, 0x6a, 0xa4, 0xaa, 0x20, 0xea, 0xa4, 0x64, 0x66, 0xaa, 0x46, 0x4a, 0x8a, 0x00, 0x4c, 0xa4, 0xaa, 0x20, 0xaa, 0xa2, 0x44, 0x2a, 0xaa, 0x28, 0xaa, 0x4c, 0x00, 0xe8, 0xa8, 0x6c, 0xc4, 0xa4, 0x42, 0xee, 0x2a, 0xcc, 0x26, 0x6c, 0xe8, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; // SSD1306をグラフィックディスプレイとして使用する為のコンストラクタ // メモリバッファ(RAM)は128X64バイト(画面フルサイズ分) // ローテーション(画面回転)なし、SCL=5番ピン SDA=4番ピン U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0,5,4) ; //////////////////////////////////////////////////////////////////////////////// // この関数では、XBMデータをSDカードに書き込みます。 void writeFile(const char *name, uint8_t w, uint8_t h, const uint8_t *bitmap) { uint8_t x; uint8_t y; File myFile = SD.open(name, FILE_WRITE); if (myFile) { myFile.write(w); // 最初のバイトはビットマップの幅です myFile.write(h); // 2番目のバイトはビットマップの高さです w = (w+7)/8; // 次のループでXBMデータを書き込みます for( y = 0; y < h; y++ ) { for( x = 0; x < w; x++ ) { myFile.write(u8x8_pgm_read(bitmap)); bitmap++; } } myFile.close(); Serial.println("write done."); } else { Serial.println("write error."); } } //////////////////////////////////////////////////////////////////////////////// // この関数では、指定された位置x、yにビットマップを描画します。 // ビットマップは、SDカードのファイル「ファイル名」から読み込みながら描画されます。 void drawFile(u8g2_int_t x, u8g2_int_t y, const char *filename) { uint8_t w; uint8_t h; uint8_t b; uint8_t mask; uint8_t len; u8g2_int_t xpos; File myFile = SD.open(filename); if (myFile) { // ビットマップの寸法を読み取る w = myFile.read(); // ビットマップの幅 h = myFile.read(); // ビットマップの高さ // ビットマップのすべての行を処理する while( h > 0 ) { xpos = x; // x位置の初期描画座標を設定 len = w; // lenには水平方向のバイト数が含まれます mask = 1; b = myFile.read(); // 最初の8ピクセルを"b"にロードします while(len > 0) { // 1本の線(1行)の全てのピクセルを描画します if ( b & mask ) { // 1ピクセルを確認してください u8g2.setDrawColor(1); u8g2.drawPixel(xpos,y); } else { u8g2.setDrawColor(0); u8g2.drawPixel(xpos,y); } xpos++; // ピクセルの次のx位置を計算します mask <<= 1; // マスクを更新する if ( mask == 0 ) { // 1バイト終了したかチェックします mask = 1; // マスクを元に戻し ... b = myFile.read(); // ... ファイルから次の8ピクセル値をロードします } len--; // 水平方向の幅を小さくします(残りのピクセル) } y++; // 次の行に移動 h--; // 残りの行数を減らす } myFile.close(); // 全て完了したので、ファイルを閉じます } u8g2.setDrawColor(1); // 色を復元する } //////////////////////////////////////////////////////////////////////////////// // メインの処理 void setup() { // シリアル通信を開く Serial.begin(9600); while (!Serial) { ; // シリアルポートが接続するのを待ちます(ネイティブUSBポートにのみ必要) } if (!SD.begin(2)) { // CSは2番ピン Serial.println("initialization failed!"); while (1); } Serial.println("initialization done."); // SDカードにサンプルデータを書き込みます writeFile("u8g2.bin", bitmap_width, bitmap_height, bitmap_data_bits); u8g2.begin(); u8g2.clearDisplay(); u8g2.setFont(u8g2_font_helvR10_tr); u8g2.firstPage(); do { u8g2.setCursor(0, 12); u8g2.print(F("Read from SD:")); // SDカードからXBMデータを読み込みディスプレイに描画します drawFile(0, 20, "u8g2.bin"); } while ( u8g2.nextPage() ); } void loop() { } --------------------------------------------------------------------------------
-------------------------------------------------------------------------------- #include <SD.h> #include <PCF8563.h> // RTC(PCF8563)を使用する為のコンストラクタ PCF8563 rtc ; //////////////////////////////////////////////////////////////////////////////// // RTCの現在の日付と時刻を返すコールバック関数 void dateTime(uint16_t *date, uint16_t *time) { Time nowTime = rtc.getTime() ; // PCF8563から現在の時刻を得る *date = FAT_DATE(nowTime.year+2000, nowTime.month, nowTime.day); *time = FAT_TIME(nowTime.hour, nowTime.minute, nowTime.second); } //////////////////////////////////////////////////////////////////////////////// // メインの処理 void setup() { File fds ; // シリアル通信の初期化 Serial.begin(9600) ; while (!Serial) { ; // シリアルポートが接続するのを待ちます(ネイティブUSBポートにのみ必要) } Serial.println("SD test start") ; rtc.init() ; // RTCの初期化 // RTCに現在時刻を設定する // バックアップバッテリ搭載して1回実行させれば良い // 2021年1月23日 土曜日 15時24分00秒で初期設定 rtc.stopClock() ; // RTCのカウントを止める rtc.setYear(21) ; // 年の設定(00-99) rtc.setMonth(1) ; // 月の設定(01-12) rtc.setDay(23) ; // 日の設定(01-31) // skRTC.SetWeekdays(6) ; // 土曜日の設定(0-6) rtc.setHour(15) ; // 時の設定(00-23) rtc.setMinut(24) ;// 分の設定(00-59) rtc.setSecond(0) ;// 秒の設定(00-59) rtc.startClock() ;// RTCのカウントを開始する // SDカードの初期化処理(フォーマットではないよ) if (!SD.begin(2)) { // CSは2番ピン // カードの初期化に失敗したか、またはSDが入っていない Serial.println("Card failed, or not present") ; while(1) ; } // SD用コールバック関数の定義(現在の日付と時刻を返す関数) SdFile::dateTimeCallback( &dateTime ) ; Serial.println("Complete initialization !") ;// 初期化完了 // ファイルの書込みオープン // test3.txtファイルが無い場合は作成されます、あればファイルの最後に追加されます。 fds = SD.open("test3.txt",FILE_WRITE) ; if (fds) { // 文字列を書込む\r\nは改行コード文字 fds.write("1234567890\r\n") ; Serial.write("1234567890\r\n") ; fds.write("abcdefg\r\n") ; Serial.write("abcdefg\r\n") ; // ファイルのクローズ fds.close() ; } else { // ファイルのオープンエラー Serial.println("error opening") ; } } void loop() { } --------------------------------------------------------------------------------
【きむ茶工房ガレージハウス】
Copyright (C) 2006-2021 Shigehiro Kimura All Rights Reserved.