2018年2月27日 星期二

用 Arduino MEGA 2560 SPI 介面使用 1.3吋 12864 點陣型 OLED 顯示器模組 (一)


使用點陣型OLED顯示器,最簡單的方式,就是使用現成的函式庫來顯示中英文字或畫圓/線/框 ⋯⋯ 等圖形。

在 Arduino 的系統上,u8g2 的函式庫應該是最好用的。

由於點陣型OLED顯示器需要從CPU傳送大量的點陣資料到驅動IC上,因此需要考慮

  1. 介面的速度:高速的SPI介面(最快 5MHz @SH1106)會比低速的IIC (最快400KHz~1MHz)介面好。IIC雖然接腳比較少,但是在很多應用上,因為速度太慢並不合適。
  2. 是否使用內建字型的OLED顯示器模組。(字型檔要使用很多記憶體)

以下為本文要使用的2種使用4-Wire SPI介面 OLED 顯示器模組

https://seller.shopee.tw/portal/product/928548884
https://seller.shopee.tw/portal/product/929270114
http://goods.ruten.com.tw/item/show?21808691433791
http://goods.ruten.com.tw/item/show?21808691338964.

在 Arduino 上要使用 u8g2, 要先安裝 u8g2 的函式庫,請到以下的網址去看如何安裝
https://github.com/olikraus/u8g2/wiki/u8g2install

安裝完成後,連接 Arduino MEGA 2560 到 OLED 顯示器模組

Arduino MEGA 2560   OLED 模組(含內建中英文字型)  OLED 模組(不含字型)
46                                  CS2 (字型檔晶片選擇)                    RES
47                                
48                                  CS1  (SH1106 晶片選擇)                 CS
49                                  DC   (資料/命令設定)                    DC
50 MISO                       FSO (字型IC資料輸出)                    -----
51 MOSI                       MOSI (輸出資料到LCD/字型IC)    MOSI
52 SCK                          CLK                                                  CLK
53 SS                              ------                                                  -----
3.3V                               3.3V                                                   3.3V
GND                              GND                                                  GND

連接完成後,在檢查一遍,確認沒有接錯後,就可以開始寫程式了。下面的連結有 u8g2 的範例程式,

https://github.com/olikraus/u8g2/wiki/setup_tutorial

範例程式的 CPU板子不是 Arduino MEGA 2560, 因此要修改 Constructor 的設定 (下面紅字的部分). 為達最快速度,使用硬體 SPI 來傳送資料,而不使用軟體 SPI 的方式。

//#define OledPanelWithFontBuiltIn

#include <Arduino.h>
#include <U8g2lib.h>
#include <U8x8lib.h>

#ifdef OledPanelWithFontBuiltIn
  #define SPI_CS2   46
  #define SPI_FSO   47
  #define SPI_CS1   48
  #define SPI_DC    49

  U8G2_SH1106_128X64_NONAME_1_4W_HW_SPI u8g2(U8G2_R0, SPI_CS1, SPI_DC);
#else
  #define SPI_RESET 46
  #define SPI_CS    48
  #define SPI_DC    49

  U8G2_SH1106_128X64_NONAME_1_4W_HW_SPI u8g2(U8G2_R0, SPI_CS, SPI_DC, SPI_RESET);
#endif

void setup(void) {
  u8g2.begin();
}

void loop(void) {

  u8g2.firstPage();
  do {
    u8g2.setFont(u8g2_font_ncenB14_tr);
#ifdef OledPanelWithFontBuiltIn  
    u8g2.drawStr(2,32,"Hello World1");
#else
    u8g2.drawStr(2,32,"Hello World2");
#endif
  } while ( u8g2.nextPage() );
}

如果在有內含字型檔的螢幕上顯示 "HelloWorld1" 如下圖,就表示一切都正常。

 或在沒有內含字型檔的螢幕上顯示 "HelloWorld2" 如下圖,就表示一切都正常。



下一篇:如何使用 Arduino MEGA 2560 SPI 介面和內含中英文字庫的 1.3吋 12864 點陣型 OLED 顯示器模組 (二)
https://www.blogger.com/blogger.g?blogID=7530656930048997875#editor/target=post;postID=8731948848737741146;onPublishedMenu=allposts;onClosedMenu=allposts;postNum=0;src=postname

2016年3月10日 星期四

Microchip MPLAB X IDE 開發程式流程 (for beginner)


之前都用 MPLAB IDE 在開發產品,最近開始使用新版的 MPLAB X IDE。一開始問題真的好多,在一陣 try & error 後,終於比較熟悉。以下是我自己的新案子開發流程

1. 按 File -> New Project





因為在 Code Configurator 內找不到我在用的 PIC18LF2550, 所以找了一顆接腳一樣的來用。


在完成新專案後,使用 Code Configurator 來產生週邊裝置的樣版程式。我很喜歡這個功能,所以就不用舊版的 IDE。



按 Code Configurator 後,產生下面的畫面,按下 System, 設定系統頻率。





選擇所要用的週邊裝置

按 pin Manager, 選 package.





最後按下 Generate Code,




系統自動產生以下的檔案,經過小修改,然後就可以寫主要的程式。




在開發過程中,常用的功能
1. 設定 include 目錄,在專案名稱 xxx 點 2 下,





點 XC8 compiler 就可設定 include 目錄。



2. 調整字體大小。在主畫面按 TOOL -> OPTION, 在選 Fonts & Colors





2016年3月2日 星期三

How to fix ESP8266 flash issues of invalid header of packet and flash over time



Bought a ESP8266-01 and a FT232 USB2TTL boards from Ruten (Taiwan's eBay).  Both of boards were made in the China.

I use old version of esp8266_flasher.exe and the latest flash program downloaded from bbs.ai-thinker.com. I got the following error messages during upgrading process to latest firmware of V.0.9.5.2:

1. invalid header of packet.
2. flash over time

I got some solutions from google search and it seems the only way to fix this issue is to flash ESP8266 from esptool.py which we can download from Github https://github.com/themadinventor/esptool and modify the following value in the esptool.py.  Without modification, I got same error message.

    # Maximum block sized for RAM and Flash writes, respectively.
    ESP_RAM_BLOCK   = 0x1800  
    ESP_FLASH_BLOCK = 0x40     (change from 400 to 40,  I tried many times, the terminal will get error message if this value > 40) I don't know why ??? I guess it's because of the flash maker.  The one I saw on my ESP8266-01 board is Berg Micro 25Q80A I can't even find its website and datasheet.

Now, it works great under my iMac and I got the following output from new firmware:

AT

OK
AT+GMR
AT version 0.21.0.0
SDK version 0.9.5

OK

If you are the first time to use Mac OS to upgrade ESP8266 firmware, you need to install pyserial library which you can download from https://github.com/themadinventor/esptool or your Mac OS can't run the program.


在露天買了一塊 ESP8266-01 和 FT232 USB轉TTL module, 在正常工作模式下正常。但是要燒錄成最新版本時,遇到了以下問題。

問題1. 使用 esp8266 flasher 燒錄程式
燒錄時間超時

問題2.
Invalid head of packet

問題已解,解決方法在上面