| 首先,建立ASF工程,然后选SAMD21开发板。然后加入OLED模块: 
 
 
 然后加入以下代码:
 
 
 其管脚定义在config里。#include <asf.h>
int main(void)
{
        //! the page address to write to
        uint8_t page_address;
        //! the column address, or the X pixel.
        uint8_t column_address;
        //! store the LCD controller start draw line
        uint8_t start_line_address = 0;
        system_init();
        delay_init();
        // Initialize SPI and SSD1306 controller
        ssd1306_init();
        // set addresses at beginning of display
        ssd1306_set_page_address(0);
        ssd1306_set_column_address(0);
        // fill display with lines
        for (page_address = 0; page_address <= 8; page_address++) {
                ssd1306_set_page_address(page_address);
                for (column_address = 0; column_address < 128; column_address++) {
                        ssd1306_set_column_address(column_address);
                        /* fill every other pixel in the display. This will produce
                        horizontal lines on the display. */
                        ssd1306_write_data(0x0f);
                }
        }
        // scroll the display using hardware support in the LCD controller
        while (true) {
                ssd1306_set_display_start_line_address(start_line_address++);
                delay_ms(250);
        }
}
 
 运行效果如下:#ifndef CONF_SSD1306_H_INCLUDED
#define CONF_SSD1306_H_INCLUDED
#include <board.h>
// Interface configuration for SAM D21 Xplained Pro
#  define SSD1306_SPI                 EXT3_SPI_MODULE
#  define CONFIG_SSD1306_FRAMEBUFFER
#  define SSD1306_DC_PIN              EXT3_PIN_5
#  define SSD1306_RES_PIN             EXT3_PIN_10
#  define SSD1306_CS_PIN              EXT3_PIN_15
#  define SSD1306_SPI_PINMUX_SETTING  EXT3_SPI_SERCOM_MUX_SETTING
#  define SSD1306_SPI_PINMUX_PAD0     EXT3_SPI_SERCOM_PINMUX_PAD0
#  define SSD1306_SPI_PINMUX_PAD1     PINMUX_UNUSED
#  define SSD1306_SPI_PINMUX_PAD2     EXT3_SPI_SERCOM_PINMUX_PAD2
#  define SSD1306_SPI_PINMUX_PAD3     EXT3_SPI_SERCOM_PINMUX_PAD3
// Minimum clock period is 50ns[url=home.php?mod=space&uid=517595]@3.3V[/url] -> max frequency is 20MHz
#define SSD1306_CLOCK_SPEED           1000000UL
#define SSD1306_DISPLAY_CONTRAST_MAX  40
#define SSD1306_DISPLAY_CONTRAST_MIN  30
#endif /* CONF_SSD1306_H_INCLUDED */
 
 
 |