ek-tm4c123gxl的OLED代码
时间: 2024-06-17 10:07:20 浏览: 247
EK-TM4C123GXL是德州仪器(TI)推出的一款基于ARM Cortex-M4内核的微控制器开发板,而OLED则是一种显示屏,常用于显示文字和图像。在使用EK-TM4C123GXL开发板进行OLED显示时,需要使用相应的驱动程序。以下是一个简单的OLED显示代码示例:
```
#include "inc/hw_memmap.h"
#include "driverlib/sysctl.h"
#include "driverlib/gpio.h"
#include "driverlib/ssi.h"
#define SSI0_BASE GPIO_PORTA_BASE
#define SSI0_CLK GPIO_PIN_2
#define SSI0_FSS GPIO_PIN_3
#define SSI0_RX GPIO_PIN_4
#define SSI0_TX GPIO_PIN_5
void OLED_Init(void)
{
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
while(!SysCtlPeripheralReady(SYSCTL_PERIPH_GPIOA));
GPIOPinConfigure(GPIO_PA2_SSI0CLK);
GPIOPinConfigure(GPIO_PA3_SSI0FSS);
GPIOPinConfigure(GPIO_PA4_SSI0RX);
GPIOPinConfigure(GPIO_PA5_SSI0TX);
GPIOPinTypeSSI(GPIO_PORTA_BASE, SSI0_CLK | SSI0_FSS | SSI0_RX | SSI0_TX);
SysCtlPeripheralEnable(SYSCTL_PERIPH_SSI0);
while(!SysCtlPeripheralReady(SYSCTL_PERIPH_SSI0));
SSIConfigSetExpClk(SSI0_BASE, SysCtlClockGet(), SSI_FRF_MOTO_MODE_0, SSI_MODE_MASTER, 1000000, 8);
SSIEnable(SSI0_BASE);
}
void OLED_WriteCmd(uint8_t cmd)
{
GPIOPinWrite(GPIO_PORTA_BASE, SSI0_FSS, 0);
SSIDataPut(SSI0_BASE, 0x00);
SSIDataPut(SSI0_BASE, cmd);
while(SSIBusy(SSI0_BASE));
GPIOPinWrite(GPIO_PORTA_BASE, SSI0_FSS, SSI0_FSS);
}
void OLED_WriteDat(uint8_t dat)
{
GPIOPinWrite(GPIO_PORTA_BASE, SSI0_FSS, 0);
SSIDataPut(SSI0_BASE, 0x40);
SSIDataPut(SSI0_BASE, dat);
while(SSIBusy(SSI0_BASE));
GPIOPinWrite(GPIO_PORTA_BASE, SSI0_FSS, SSI0_FSS);
}
void OLED_Clear(void)
{
uint8_t i,j;
for(i=0;i<8;i++)
{
OLED_WriteCmd(0xb0+i);
OLED_WriteCmd(0x00);
OLED_WriteCmd(0x10);
for(j=0;j<128;j++)
OLED_WriteDat(0x00);
}
}
void OLED_ShowChar(uint8_t x,uint8_t y,uint8_t chr,uint8_t size,uint8_t mode)
{
uint8_t i,m,temp,size1;
size1 = (size / 8 + ((size % 8) ? 1 : 0)) * (size / 2); //计算字符所占的字节数
chr = chr - ' '; //获取偏移后的值
for(i=0;i<size1;i++)
{
switch(size)
{
case 12:
temp = asc2_1206[chr][i];
break;
case 16:
temp = asc2_1608[chr][i];
break;
default:
return;
}
for(m=0;m<8;m++)
{
if(temp & 0x80)
OLED_DrawPoint(x+m,y+i,mode);
temp <<= 1;
}
}
}
void OLED_DrawPoint(uint8_t x,uint8_t y,uint8_t mode)
{
uint8_t page = y/8;
uint8_t column = x;
OLED_WriteCmd(0xb0+page);
OLED_WriteCmd(column&0x0f);
OLED_WriteCmd(((column&0xf0)>>4)|0x10);
if(mode == 1)
OLED_WriteDat(0xff);
else
OLED_WriteDat(0x00);
}
void OLED_ShowString(uint8_t x,uint8_t y,char *chr,uint8_t size,uint8_t mode)
{
while((*chr)!='\0')
{
if(x > 120)
{
x = 0;
y += size;
}
if(y > 56)
{
y = x = 0;
OLED_Clear();
}
OLED_ShowChar(x,y,*chr,size,mode);
x += size / 2;
chr++;
}
}
int main(void)
{
SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ); //设置系统时钟为16MHz
OLED_Init(); //初始化OLED
OLED_Clear(); //清屏
OLED_ShowString(10,10,"Hello World!",16,1); //显示字符串
while(1);
}
```
该代码中包含了OLED初始化函数、清屏函数、显示字符函数、绘制点函数和显示字符串函数等,可以根据自己的需求进行相应的修改。
阅读全文