我需要一个dsp28379D的关于0.9寸屏幕的代码
时间: 2024-03-12 16:45:14 浏览: 178
好的,针对您的需求我可以提供一份基于DSP28379D的0.9寸OLED屏幕驱动代码。代码使用的是SPI接口进行通信,您需要根据实际情况进行修改。以下是代码:
```c
#include "F2837xD_Device.h"
#include "F2837xD_Examples.h"
void Init_SPIC(void)
{
//将GPIO80-87设置为SPI端口
EALLOW;
GpioCtrlRegs.GPAPUD.bit.GPIO80 = 0; //使能GPIO80上拉
GpioCtrlRegs.GPAPUD.bit.GPIO81 = 0; //使能GPIO81上拉
GpioCtrlRegs.GPAPUD.bit.GPIO82 = 0; //使能GPIO82上拉
GpioCtrlRegs.GPAPUD.bit.GPIO83 = 0; //使能GPIO83上拉
GpioCtrlRegs.GPAPUD.bit.GPIO84 = 0; //使能GPIO84上拉
GpioCtrlRegs.GPAPUD.bit.GPIO85 = 0; //使能GPIO85上拉
GpioCtrlRegs.GPAPUD.bit.GPIO86 = 0; //使能GPIO86上拉
GpioCtrlRegs.GPAPUD.bit.GPIO87 = 0; //使能GPIO87上拉
GpioCtrlRegs.GPAQSEL2.bit.GPIO80 = 3; //将GPIO80的中断模式设置为异步
GpioCtrlRegs.GPAQSEL2.bit.GPIO81 = 3; //将GPIO81的中断模式设置为异步
GpioCtrlRegs.GPAQSEL2.bit.GPIO82 = 3; //将GPIO82的中断模式设置为异步
GpioCtrlRegs.GPAQSEL2.bit.GPIO83 = 3; //将GPIO83的中断模式设置为异步
GpioCtrlRegs.GPAQSEL2.bit.GPIO84 = 3; //将GPIO84的中断模式设置为异步
GpioCtrlRegs.GPAQSEL2.bit.GPIO85 = 3; //将GPIO85的中断模式设置为异步
GpioCtrlRegs.GPAQSEL2.bit.GPIO86 = 3; //将GPIO86的中断模式设置为异步
GpioCtrlRegs.GPAQSEL2.bit.GPIO87 = 3; //将GPIO87的中断模式设置为异步
GpioCtrlRegs.GPAMUX2.bit.GPIO80 = 1; //将GPIO80设置为SPI SIMO
GpioCtrlRegs.GPAMUX2.bit.GPIO81 = 1; //将GPIO81设置为SPI SOMI
GpioCtrlRegs.GPAMUX2.bit.GPIO82 = 1; //将GPIO82设置为SPI CLK
GpioCtrlRegs.GPAMUX2.bit.GPIO83 = 0; //将GPIO83设置为GPIO,这个引脚用于片选
GpioCtrlRegs.GPAMUX2.bit.GPIO84 = 0; //将GPIO84设置为GPIO,这个引脚用于复位
GpioCtrlRegs.GPAMUX2.bit.GPIO85 = 0; //将GPIO85设置为GPIO,这个引脚用于数据/命令选择
GpioCtrlRegs.GPAMUX2.bit.GPIO86 = 0; //将GPIO86设置为GPIO,这个引脚用于电源控制
GpioCtrlRegs.GPAMUX2.bit.GPIO87 = 0; //将GPIO87设置为GPIO,这个引脚用于切换屏幕方向
EDIS;
//初始化SPI模块
SpicRegs.SPICCR.bit.SPISWRESET = 0; //将SPI模块复位
SpicRegs.SPICCR.bit.SPICHAR = 7; //设置SPI数据位长度为8位
SpicRegs.SPICCR.bit.CLKPOLARITY = 0; //设置时钟极性为低电平有效
SpicRegs.SPICCR.bit.CLKPHASE = 0; //设置时钟相位为第一边沿采样
SpicRegs.SPICCR.bit.SPILBK = 0; //关闭SPI循环模式
SpicRegs.SPICCR.bit.SPICHAR = 7; //设置SPI数据位长度为8位
SpicRegs.SPICCR.bit.SPISWRESET = 1; //使能SPI模块
SpicRegs.SPICTL.bit.MASTER_SLAVE = 1; //设置SPI为主机模式
SpicRegs.SPICTL.bit.TALK = 1; //允许SPI发送数据
SpicRegs.SPICTL.bit.CLK_PHASE = 0; //设置时钟相位为第一边沿采样
SpicRegs.SPICTL.bit.OVERRUNINTENA = 0;//禁止SPI溢出中断
SpicRegs.SPICTL.bit.CLK_MODE = 1; //设置SPI为模式1
SpicRegs.SPICTL.bit.SPIINTENA = 0; //禁止SPI中断
SpicRegs.SPIBRR = 199; //设置SPI时钟为150kHz
SpicRegs.SPICCR.bit.SPISWRESET = 0; //将SPI模块复位
SpicRegs.SPICCR.bit.SPICHAR = 7; //设置SPI数据位长度为8位
SpicRegs.SPICCR.bit.CLKPOLARITY = 0; //设置时钟极性为低电平有效
SpicRegs.SPICCR.bit.CLKPHASE = 0; //设置时钟相位为第一边沿采样
SpicRegs.SPICCR.bit.SPILBK = 0; //关闭SPI循环模式
SpicRegs.SPICCR.bit.SPICHAR = 7; //设置SPI数据位长度为8位
SpicRegs.SPICCR.bit.SPISWRESET = 1; //使能SPI模块
}
void OLED_Init(void)
{
//将OLED屏幕复位
GpioDataRegs.GPASET.bit.GPIO84 = 1; //将GPIO84设置为高电平,复位信号为低电平有效
DELAY_US(1);
GpioDataRegs.GPACLEAR.bit.GPIO84 = 1; //将GPIO84设置为低电平
DELAY_US(10);
GpioDataRegs.GPASET.bit.GPIO84 = 1; //将GPIO84设置为高电平
DELAY_US(100);
//设置OLED屏幕显示方向
GpioDataRegs.GPACLEAR.bit.GPIO87 = 1; //将GPIO87设置为低电平,表示竖屏显示
DELAY_US(100);
//关闭OLED屏幕电源
GpioDataRegs.GPACLEAR.bit.GPIO86 = 1; //将GPIO86设置为低电平,关闭电源
DELAY_US(100);
GpioDataRegs.GPASET.bit.GPIO86 = 1; //将GPIO86设置为高电平,打开电源
DELAY_US(100);
//设置OLED屏幕预充电电压和电流
Send_Command(0x00); //设置低列地址
Send_Command(0x12); //设置高列地址
Send_Command(0x02); //设置预充电周期
Send_Command(0x03); //设置预充电电流
Send_Command(0x01); //设置列扫描映射
Send_Command(0x00); //设置显示模式
Send_Command(0x2c); //设置电荷泵电流
Send_Command(0x2e); //打开电荷泵
Send_Command(0x2f); //开启OLED屏幕
}
void Send_Command(Uint16 cmd)
{
GpioDataRegs.GPACLEAR.bit.GPIO85 = 1; //将GPIO85设置为低电平,表示发送指令
GpioDataRegs.GPACLEAR.bit.GPIO83 = 1; //将GPIO83设置为低电平,表示选中OLED屏幕
SpicRegs.SPITXBUF = cmd; //发送指令
while(SpicRegs.SPISTS.bit.INT_FLAG != 1);
SpicRegs.SPISTS.bit.INT_FLAG = 1; //清除中断标志位
GpioDataRegs.GPASET.bit.GPIO83 = 1; //将GPIO83设置为高电平,表示取消选中OLED屏幕
}
void Send_Data(Uint16 data)
{
GpioDataRegs.GPASET.bit.GPIO85 = 1; //将GPIO85设置为高电平,表示发送数据
GpioDataRegs.GPACLEAR.bit.GPIO83 = 1; //将GPIO83设置为低电平,表示选中OLED屏幕
SpicRegs.SPITXBUF = data; //发送数据
while(SpicRegs.SPISTS.bit.INT_FLAG != 1);
SpicRegs.SPISTS.bit.INT_FLAG = 1; //清除中断标志位
GpioDataRegs.GPASET.bit.GPIO83 = 1; //将GPIO83设置为高电平,表示取消选中OLED屏幕
}
void OLED_Clear(void)
{
Uint16 i,j;
for(i = 0; i < 8; i++)
{
Send_Command(0xb0 + i); //设置页地址
Send_Command(0x00); //设置低列地址
Send_Command(0x10); //设置高列地址
for(j = 0; j < 128; j++)
{
Send_Data(0x00);
}
}
}
void OLED_Display_Char(Uint16 x, Uint16 y, Uint16 chr)
{
Uint16 i, j;
chr = chr - 32;
if(x > 120)
{
x = 0;
y++;
}
Send_Command(0xb0 + y); //设置页地址
Send_Command(x & 0x0f); //设置低列地址
Send_Command((x >> 4) & 0x0f); //设置高列地址
for(i = 0; i < 6; i++)
{
Send_Data(F6x8[chr][i]);
}
}
void OLED_Display_String(Uint16 x, Uint16 y, Uint16 *str)
{
Uint16 i = 0;
while(str[i] != '\0')
{
OLED_Display_Char(x, y, str[i]);
x += 6;
if(x > 120)
{
x = 0;
y++;
}
i++;
}
}
void main(void)
{
Init_SPIC();
OLED_Init();
OLED_Clear();
OLED_Display_String(0, 0, "Hello,World!");
while(1);
}
```
请注意:上述代码仅供参考,具体的代码实现方式需要根据您使用的OLED屏幕型号和通信接口进行修改。
阅读全文