DSP28335LCD1602实时显示代码
时间: 2023-10-21 22:03:54 浏览: 185
LCD1602显示代码
以下是基于DSP28335和LCD1602的实时显示代码:
```c
#include "DSP2833x_Device.h"
#include "DSP2833x_Examples.h"
#include "DSP2833x_GlobalPrototypes.h"
#include "LCD1602.h"
void main(void)
{
InitSysCtrl();
InitGpio();
InitXintf();
Init_LCD1602(); // 初始化LCD1602
while(1)
{
// 在第一行第一列显示“Hello”
LCD1602_Write_String("Hello", 1, 1);
// 在第二行第一列显示当前时间
Uint16 hour = CpuTimer0Regs.TIM.all / 360000000;
Uint16 minute = (CpuTimer0Regs.TIM.all % 360000000) / 6000000;
Uint16 second = (CpuTimer0Regs.TIM.all % 6000000) / 100000;
char time_str[9];
sprintf(time_str, "%02d:%02d:%02d", hour, minute, second);
LCD1602_Write_String(time_str, 2, 1);
}
}
```
上述代码使用了一个名为LCD1602_Write_String的函数,该函数将字符串写入LCD1602。以下是LCD1602.h头文件和LCD1602.c源代码:
LCD1602.h:
```c
#ifndef __LCD1602_H__
#define __LCD1602_H__
#include "DSP2833x_Device.h"
void Init_LCD1602(void);
void LCD1602_Write_Command(Uint16 command);
void LCD1602_Write_Data(Uint16 data);
void LCD1602_Write_String(char *string, Uint16 line, Uint16 position);
#endif
```
LCD1602.c:
```c
#include "LCD1602.h"
void Init_LCD1602(void)
{
// 初始化GPIO为输出
EALLOW;
GpioCtrlRegs.GPBMUX1.bit.GPIO34 = 0;
GpioCtrlRegs.GPBDIR.bit.GPIO34 = 1;
GpioCtrlRegs.GPBMUX1.bit.GPIO35 = 0;
GpioCtrlRegs.GPBDIR.bit.GPIO35 = 1;
GpioCtrlRegs.GPBMUX1.bit.GPIO36 = 0;
GpioCtrlRegs.GPBDIR.bit.GPIO36 = 1;
GpioCtrlRegs.GPBMUX1.bit.GPIO37 = 0;
GpioCtrlRegs.GPBDIR.bit.GPIO37 = 1;
GpioCtrlRegs.GPBMUX1.bit.GPIO38 = 0;
GpioCtrlRegs.GPBDIR.bit.GPIO38 = 1;
GpioCtrlRegs.GPBMUX1.bit.GPIO39 = 0;
GpioCtrlRegs.GPBDIR.bit.GPIO39 = 1;
EDIS;
// 初始化LCD1602
DELAY_US(15000);
LCD1602_Write_Command(0x38);
DELAY_US(5000);
LCD1602_Write_Command(0x38);
DELAY_US(5000);
LCD1602_Write_Command(0x38);
DELAY_US(5000);
LCD1602_Write_Command(0x0C);
DELAY_US(5000);
LCD1602_Write_Command(0x01);
DELAY_US(5000);
LCD1602_Write_Command(0x06);
DELAY_US(5000);
}
void LCD1602_Write_Command(Uint16 command)
{
GpioDataRegs.GPBDAT.bit.GPIO34 = 0; // RS = 0
GpioDataRegs.GPBDAT.bit.GPIO35 = 0; // RW = 0
GpioDataRegs.GPBDAT.bit.GPIO36 = (command >> 4) & 0x1; // D4
GpioDataRegs.GPBDAT.bit.GPIO37 = (command >> 5) & 0x1; // D5
GpioDataRegs.GPBDAT.bit.GPIO38 = (command >> 6) & 0x1; // D6
GpioDataRegs.GPBDAT.bit.GPIO39 = (command >> 7) & 0x1; // D7
GpioDataRegs.GPBDAT.bit.GPIO34 = 1; // E = 1
DELAY_US(1);
GpioDataRegs.GPBDAT.bit.GPIO34 = 0; // E = 0
GpioDataRegs.GPBDAT.bit.GPIO36 = command & 0x1; // D4
GpioDataRegs.GPBDAT.bit.GPIO37 = (command >> 1) & 0x1; // D5
GpioDataRegs.GPBDAT.bit.GPIO38 = (command >> 2) & 0x1; // D6
GpioDataRegs.GPBDAT.bit.GPIO39 = (command >> 3) & 0x1; // D7
GpioDataRegs.GPBDAT.bit.GPIO34 = 1; // E = 1
DELAY_US(1);
GpioDataRegs.GPBDAT.bit.GPIO34 = 0; // E = 0
}
void LCD1602_Write_Data(Uint16 data)
{
GpioDataRegs.GPBDAT.bit.GPIO34 = 1; // RS = 1
GpioDataRegs.GPBDAT.bit.GPIO35 = 0; // RW = 0
GpioDataRegs.GPBDAT.bit.GPIO36 = (data >> 4) & 0x1; // D4
GpioDataRegs.GPBDAT.bit.GPIO37 = (data >> 5) & 0x1; // D5
GpioDataRegs.GPBDAT.bit.GPIO38 = (data >> 6) & 0x1; // D6
GpioDataRegs.GPBDAT.bit.GPIO39 = (data >> 7) & 0x1; // D7
GpioDataRegs.GPBDAT.bit.GPIO34 = 1; // E = 1
DELAY_US(1);
GpioDataRegs.GPBDAT.bit.GPIO34 = 0; // E = 0
GpioDataRegs.GPBDAT.bit.GPIO36 = data & 0x1; // D4
GpioDataRegs.GPBDAT.bit.GPIO37 = (data >> 1) & 0x1; // D5
GpioDataRegs.GPBDAT.bit.GPIO38 = (data >> 2) & 0x1; // D6
GpioDataRegs.GPBDAT.bit.GPIO39 = (data >> 3) & 0x1; // D7
GpioDataRegs.GPBDAT.bit.GPIO34 = 1; // E = 1
DELAY_US(1);
GpioDataRegs.GPBDAT.bit.GPIO34 = 0; // E = 0
}
void LCD1602_Write_String(char *string, Uint16 line, Uint16 position)
{
Uint16 address = (line == 1 ? 0x00 : 0x40) + position - 1;
LCD1602_Write_Command(0x80 | address);
while(*string)
{
LCD1602_Write_Data(*string++);
}
}
```
上述代码中,LCD1602_Write_Command函数和LCD1602_Write_Data函数分别用于向LCD1602写入指令和数据,并通过GPIO控制LCD1602。LCD1602_Write_String函数使用LCD1602_Write_Data函数将字符串写入LCD1602的指定位置。由于LCD1602的显示范围为2行16列,因此第一行的位置为1-16,第二行的位置为17-32。
阅读全文