​Atmel SAM4S开发套件体验2之OLED板

作者: renegade
上传时间为: 2014-12-01 10:45 PM
2014-12-01
阅读:

这一篇文章主要讲Atmel开发板套件上OLED的使用。

先介绍一下OLED的特性:OLED显示技术具有自发光的特性,采用非常薄的有机材料涂层和玻璃基板,当有电流通过时,这些有机材料就会发光,而且OLED显示屏幕可视角度大,并且能够节省电能,从2003年开始这种显示设备在MP3播放器上得到了应用。三星手机的屏幕就是用的OLED屏哦。

我们先按照官方用户指南的顺序看看这块板子

这是Xplained Pro 的身份牌,嘿嘿,就是通过这个,你可以通过MCU识别出这块配套板子的基础信息。

第一个是厂家名称,然后依次是产品名,版本号,产品序列号,最小电压,最大电压,以及最大电流值。

下面这个是这块板子的引脚接口,第一个是Id,也就是上面说的身份牌,第二个gnd接地端口,然后是两路ADC接口,然后是两个GPIO口,然后是一个Pwm输出口,9是可以作为中断口,10号可以用作SPI串行通信,11号作为数据线,12号作为时钟线,13,14分别是串口的Tx,rx引脚,15~18是用SPI的,19,20分别是vcc,gnd

然后我们只需要按照顺序操作这些引脚即可。

我们上一段代码分析分析:

#ifndef CONF_SSD1306_H_INCLUDED
#define CONF_SSD1306_H_INCLUDED
一开始是一段宏定义,帮助大家跟好的理解这段代码的。
// Interface configuration for SAM4S Xplained Pro
#define SSD1306_SPI_INTERFACE
#define SSD1306_SPI SPI

#define SSD1306_DC_PIN       UG_2832HSWEG04_DATA_CMD_GPIO
#define SSD1306_RES_PIN      UG_2832HSWEG04_RESET_GPIO
#define SSD1306_CS_PIN       UG_2832HSWEG04_SS

// Minimum clock period is 50ns@3.3V -> max frequency is 20MHz
#define SSD1306_CLOCK_SPEED          UG_2832HSWEG04_BAUDRATE
#define SSD1306_DISPLAY_CONTRAST_MAX 40
#define SSD1306_DISPLAY_CONTRAST_MIN 30

#endif /* CONF_SSD1306_H_INCLUDED */
#include "ssd1306.h"
#include "font.h"

/**
 * \internal
 * \brief Initialize the hardware interface
 *
 * Depending on what interface used for interfacing the OLED controller this
 * function will initialize the necessary hardware.
 */这一段主要是调用的官方的底层库函数写的,方面简洁,主要是配置SPI
static void ssd1306_interface_init(void)
{
#ifdef SSD1306_SERIAL_INTERFACE
	spi_flags_t spi_flags = SPI_MODE_0;
	board_spi_select_id_t spi_select_id = 0;
#endif

#if defined(SSD1306_USART_SPI_INTERFACE)
	struct usart_spi_device device = {
		.id = SSD1306_CS_PIN,
	};
	usart_spi_init(SSD1306_USART_SPI);
	usart_spi_setup_device(SSD1306_USART_SPI, &device, spi_flags,
			SSD1306_CLOCK_SPEED, spi_select_id);
#elif defined(SSD1306_SPI_INTERFACE)
	struct spi_device device = {
		.id = SSD1306_CS_PIN,
	};
	spi_master_init(SSD1306_SPI);
	spi_master_setup_device(SSD1306_SPI, &device, spi_flags,
			SSD1306_CLOCK_SPEED, spi_select_id);
#ifdef SAM
	spi_enable(SSD1306_SPI);
#endif
#endif
}

/**
 * \brief Initialize the OLED controller
 *
 * Call this function to initialize the hardware interface and the OLED
 * controller. When initialization is done the display is turned on and ready
 * to receive data.
 */下面这一段是对oled屏幕的初始化设置
void ssd1306_init(void)
{
	// Do a hard reset of the OLED display controller
	ssd1306_hard_reset();

	// Initialize the interface
	ssd1306_interface_init();

	// 1/32 Duty (0x0F~0x3F)
	ssd1306_write_command(SSD1306_CMD_SET_MULTIPLEX_RATIO);
	ssd1306_write_command(0x1F);

	// Shift Mapping RAM Counter (0x00~0x3F)
	ssd1306_write_command(SSD1306_CMD_SET_DISPLAY_OFFSET);
	ssd1306_write_command(0x00);

	// Set Mapping RAM Display Start Line (0x00~0x3F)
	ssd1306_write_command(SSD1306_CMD_SET_START_LINE(0x00));

	// Set Column Address 0 Mapped to SEG0
	ssd1306_write_command(SSD1306_CMD_SET_SEGMENT_RE_MAP_COL127_SEG0);

	// Set COM/Row Scan Scan from COM63 to 0
	ssd1306_write_command(SSD1306_CMD_SET_COM_OUTPUT_SCAN_DOWN);

	// Set COM Pins hardware configuration
	ssd1306_write_command(SSD1306_CMD_SET_COM_PINS);
	ssd1306_write_command(0x02);

	ssd1306_set_contrast(0x8F);

	// Disable Entire display On
	ssd1306_write_command(SSD1306_CMD_ENTIRE_DISPLAY_AND_GDDRAM_ON);

	ssd1306_display_invert_disable();

	// Set Display Clock Divide Ratio / Oscillator Frequency (Default => 0x80)
	ssd1306_write_command(SSD1306_CMD_SET_DISPLAY_CLOCK_DIVIDE_RATIO);
	ssd1306_write_command(0x80);

	// Enable charge pump regulator
	ssd1306_write_command(SSD1306_CMD_SET_CHARGE_PUMP_SETTING);
	ssd1306_write_command(0x14);

	// Set VCOMH Deselect Level
	ssd1306_write_command(SSD1306_CMD_SET_VCOMH_DESELECT_LEVEL);
	ssd1306_write_command(0x40); // Default => 0x20 (0.77*VCC)

	// Set Pre-Charge as 15 Clocks & Discharge as 1 Clock
	ssd1306_write_command(SSD1306_CMD_SET_PRE_CHARGE_PERIOD);
	ssd1306_write_command(0xF1);

	ssd1306_display_on();
}

/**
 * \brief Display text on OLED screen.
 * \param string String to display.
 */
void ssd1306_write_text(const char *string)
{
	uint8_t *char_ptr;
	uint8_t i;

	while (*string != 0) {
		if (*string < 0x7F) {
			char_ptr = font_table[*string - 32];
			for (i = 1; i <= char_ptr[0]; i++) {
				ssd1306_write_data(char_ptr[i]);
			}
			ssd1306_write_data(0x00);
		}
			string++;
	}
}
配置完了,可以调用这里的函数对oled进行操作了,文章开头就是用oled显示内存卡容量以及内容的哦。
全部评论 ()
条评论
写评论

创建讨论帖子

登录 后参与评论
系统提示