神农鼎 发表于 2023-4-3 23:04:08

我不是研发十多年,已多年装不懂技术了,只是凭感觉帮你找些专业对口的资料


下面是 STC8H实验箱上的 LCD1602的演示程序

/*---------------------------------------------------------------------*/
/* --- STC MCU Limited ------------------------------------------------*/
/* --- STC 1T Series MCU Demo Programme -------------------------------*/
/* --- Mobile: (86)13922805190 ----------------------------------------*/
/* --- Fax: 86-0513-55012956,55012947,55012969 ------------------------*/
/* --- Tel: 86-0513-55012928,55012929,55012966 ------------------------*/
/* --- Web: www.STCMCU.com --------------------------------------------*/
/* --- Web: www.STCMCUDATA.com---------------------------------------*/
/* --- QQ:800003751 -------------------------------------------------*/
/* 如果要在程序中使用此代码,请在程序中注明使用了STC的资料及程序      */
/*---------------------------------------------------------------------*/


/*************功能说明    **************

本例程基于STC8H8K64U为主控芯片的实验箱9进行编写测试,STC8G、STC8H系列芯片可通用参考.

驱动LCD1602字符屏.

显示效果为: LCD显示时间.

第一行显示 ---Clock demo---
第二行显示   12-00-00

下载时, 选择时钟 24MHz (用户可自行修改频率).

******************************************/


#include    "reg51.h"       //包含此头文件后,里面声明的寄存器不需要再手动输入,避免重复定义
#include    "intrins.h"

#define   MAIN_Fosc       24000000L   //定义主时钟

typedef   unsigned char   u8;
typedef   unsigned int    u16;
typedef   unsigned long   u32;

//手动输入声明"reg51.h"头文件里面没有定义的寄存器
sfr TH2= 0xD6;
sfr TL2= 0xD7;
sfr IE2   = 0xAF;
sfr INT_CLKO = 0x8F;
sfr AUXR = 0x8E;
sfr AUXR1 = 0xA2;
sfr P_SW1 = 0xA2;
sfr P_SW2 = 0xBA;
sfr S2CON = 0x9A;
sfr S2BUF = 0x9B;

sfr P4   = 0xC0;
sfr P5   = 0xC8;
sfr P6   = 0xE8;
sfr P7   = 0xF8;
sfr P1M1 = 0x91;    //PxM1.n,PxM0.n   =00--->Standard,    01--->push-pull
sfr P1M0 = 0x92;    //                  =10--->pure input,11--->open drain
sfr P0M1 = 0x93;
sfr P0M0 = 0x94;
sfr P2M1 = 0x95;
sfr P2M0 = 0x96;
sfr P3M1 = 0xB1;
sfr P3M0 = 0xB2;
sfr P4M1 = 0xB3;
sfr P4M0 = 0xB4;
sfr P5M1 = 0xC9;
sfr P5M0 = 0xCA;
sfr P6M1 = 0xCB;
sfr P6M0 = 0xCC;
sfr P7M1 = 0xE1;
sfr P7M0 = 0xE2;

sbit P00 = P0^0;
sbit P01 = P0^1;
sbit P02 = P0^2;
sbit P03 = P0^3;
sbit P04 = P0^4;
sbit P05 = P0^5;
sbit P06 = P0^6;
sbit P07 = P0^7;
sbit P10 = P1^0;
sbit P11 = P1^1;
sbit P12 = P1^2;
sbit P13 = P1^3;
sbit P14 = P1^4;
sbit P15 = P1^5;
sbit P16 = P1^6;
sbit P17 = P1^7;
sbit P20 = P2^0;
sbit P21 = P2^1;
sbit P22 = P2^2;
sbit P23 = P2^3;
sbit P24 = P2^4;
sbit P25 = P2^5;
sbit P26 = P2^6;
sbit P27 = P2^7;
sbit P30 = P3^0;
sbit P31 = P3^1;
sbit P32 = P3^2;
sbit P33 = P3^3;
sbit P34 = P3^4;
sbit P35 = P3^5;
sbit P36 = P3^6;
sbit P37 = P3^7;
sbit P40 = P4^0;
sbit P41 = P4^1;
sbit P42 = P4^2;
sbit P43 = P4^3;
sbit P44 = P4^4;
sbit P45 = P4^5;
sbit P46 = P4^6;
sbit P47 = P4^7;
sbit P50 = P5^0;
sbit P51 = P5^1;
sbit P52 = P5^2;
sbit P53 = P5^3;
sbit P54 = P5^4;
sbit P55 = P5^5;
sbit P56 = P5^6;
sbit P57 = P5^7;

/*************      Pin define      *****************************************************/

sbit      LCD_B7= P6^7;      //D7 -- Pin 14                LED- -- Pin 16
sbit      LCD_B6= P6^6;      //D6 -- Pin 13                LED+ -- Pin 15
sbit      LCD_B5= P6^5;      //D5 -- Pin 12                Vo   -- Pin 3
sbit      LCD_B4= P6^4;      //D4 -- Pin 11                VDD-- Pin 2
sbit      LCD_B3= P6^3;      //D3 -- Pin 10                VSS-- Pin 1
sbit      LCD_B2= P6^2;      //D2 -- Pin9
sbit      LCD_B1= P6^1;      //D1 -- Pin8
sbit      LCD_B0= P6^0;      //D0 -- Pin7

sbit      LCD_ENA      = P4^2;      //Pin 6
sbit      LCD_RW      = P4^4;      //Pin 5      //LCD_RS   R/W   DB7--DB0      FOUNCTION
sbit      LCD_RS      = P4^5;      //Pin 4      //      0                0          INPUT      write the command to LCD model
                                                                //      0                1   OUTPUT   read BF and AC pointer from LCD model
                                                                //      1                0   INPUT      write the data to LCDmodel
                                                                //      1                1   OUTPUT   read the data from LCD model

u8      hour,minute,second;

void RTC(void);
void ClearLine(u8 row);
void Initialize_LCD(void);
void PutString(u8 row, u8 column, u8 *puts);
void DisplayRTC(void);
void delay_ms(u16 ms);
void WriteChar(u8 row, u8 column, u8 dat);

void main(void)
{
    P0M1 = 0x00;   P0M0 = 0x00;   //设置为准双向口
    P1M1 = 0x00;   P1M0 = 0x00;   //设置为准双向口
    P2M1 = 0x00;   P2M0 = 0x00;   //设置为准双向口
    P3M1 = 0x00;   P3M0 = 0x00;   //设置为准双向口
    P4M1 = 0x00;   P4M0 = 0x00;   //设置为准双向口
    P5M1 = 0x00;   P5M0 = 0x00;   //设置为准双向口
    P6M1 = 0x00;   P6M0 = 0x00;   //设置为准双向口
    P7M1 = 0x00;   P7M0 = 0x00;   //设置为准双向口

    Initialize_LCD();
    ClearLine(0);
    ClearLine(1);

    PutString(0,0,"---Clock demo---");
      
    hour   = 12;      //初始化时间值
    minute = 0;
    second = 0;
    DisplayRTC();

    while(1)
    {
      delay_ms(1000);
      RTC();
      DisplayRTC();
    }
}


//========================================================================
// 函数: void delay_ms(u16 ms)
// 描述: 延时函数。
// 参数: ms,要延时的ms数, 这里只支持1~65535ms. 自动适应主时钟.
// 返回: none.
// 版本: VER1.0
// 日期: 2013-4-1
// 备注:
//========================================================================
void delay_ms(u16 ms)
{
   u16 i;
   do{
          i = MAIN_Fosc / 10000;
          while(--i);   //10T per loop
   }while(--ms);
}

//========================================================================
// 函数: void      DisplayRTC(void)
// 描述: 显示时钟函数
// 参数: none.
// 返回: none.
// 版本: VER1.0
// 日期: 2013-4-1
// 备注:
//========================================================================
void      DisplayRTC(void)
{
      if(hour >= 10)      WriteChar(1,4,hour / 10 + '0');
      else                        WriteChar(1,4,' ');
      WriteChar(1,5,hour % 10 +'0');
      WriteChar(1,6,'-');
      WriteChar(1,7,minute / 10+'0');
      WriteChar(1,8,minute % 10+'0');
      WriteChar(1,9,'-');
      WriteChar(1,10,second / 10 +'0');
      WriteChar(1,11,second % 10 +'0');
}

//========================================================================
// 函数: void      RTC(void)
// 描述: RTC演示函数
// 参数: none.
// 返回: none.
// 版本: VER1.0
// 日期: 2013-4-1
// 备注:
//========================================================================
void      RTC(void)
{
      if(++second >= 60)
      {
                second = 0;
                if(++minute >= 60)
                {
                        minute = 0;
                        if(++hour >= 24)      hour = 0;
                }
      }
}

/************* LCD1602相关程序      *****************************************************/
//8位数据访问方式      LCD1602                标准程序      梁工编写      2014-2-21

#define LineLength      16                //16x2

/*
total 2 lines, 16x2= 32
first line address:0~15
second line address: 64~79

*/

#define C_CLEAR                        0x01                //clear LCD
#define C_HOME                         0x02                //cursor go home
#define C_CUR_L                        0x04                //cursor shift left after input
#define C_RIGHT                        0x05                //picture shift right after input
#define C_CUR_R                        0x06                //cursor shift right after input
#define C_LEFT                         0x07                //picture shift left after input
#define C_OFF                        0x08                //turn off LCD
#define C_ON                           0x0C                //turn onLCD
#define C_FLASH                        0x0D                //turn onLCD, flash
#define C_CURSOR                0x0E                //turn onLCD and cursor
#define C_FLASH_ALL                0x0F                //turn onLCD and cursor, flash
#define C_CURSOR_LEFT      0x10                //single cursor shift left
#define C_CURSOR_RIGHT      0x10                //single cursor shift right
#define C_PICTURE_LEFT      0x10                //single picture shift left
#define C_PICTURE_RIGHT      0x10                //single picture shift right
#define C_BIT8                        0x30                //set the data is 8 bits
#define C_BIT4                        0x20                //set the data is 4 bits
#define C_L1DOT7                0x30                //8 bits,one line 5*7dots
#define C_L1DOT10                0x34                //8 bits,one line 5*10 dots
#define C_L2DOT7                0x38                //8 bits,tow lines 5*7 dots
#define C_4bitL2DOT7      0x28                //4 bits,tow lines 5*7 dots
#define C_CGADDRESS0      0x40                //CGRAM address0 (addr=40H+x)
#define C_DDADDRESS0      0x80                //DDRAM address0 (addr=80H+x)

#define                LCD_BusData(dat)      P6 = dat


void      LCD_DelayNop(void)
{
      _nop_();_nop_();_nop_();
      _nop_();_nop_();_nop_();
      _nop_();_nop_();_nop_();
      _nop_();_nop_();_nop_();
      _nop_();_nop_();_nop_();
}

//========================================================================
// 函数: void      CheckBusy(void)
// 描述: 检测忙函数
// 参数: none.
// 返回: none.
// 版本: VER1.0
// 日期: 2013-4-1
// 备注:
//========================================================================
void      CheckBusy(void)
{
      u16      i;
      for(i=0; i<5000; i++)      {if(!LCD_B7)      break;}                //check the LCD busy or not. With time out
//      while(LCD_B7);                        //check the LCD busy or not. Without time out
}

//========================================================================
// 函数: void IniSendCMD(u8 cmd)
// 描述: 初始化写命令(不检测忙)
// 参数: cmd: 要写的命令.
// 返回: none.
// 版本: VER1.0
// 日期: 2013-4-1
// 备注:
//========================================================================
void IniSendCMD(u8 cmd)
{
      LCD_RW = 0;
      LCD_BusData(cmd);
      LCD_DelayNop();
      LCD_ENA = 1;
      LCD_DelayNop();
      LCD_ENA = 0;
      LCD_BusData(0xff);
}

//========================================================================
// 函数: void Write_CMD(u8 cmd)
// 描述: 写命令(检测忙)
// 参数: cmd: 要写的命令.
// 返回: none.
// 版本: VER1.0
// 日期: 2013-4-1
// 备注:
//========================================================================
void Write_CMD(u8 cmd)
{
      LCD_RS= 0;
      LCD_RW = 1;
      LCD_BusData(0xff);
      LCD_DelayNop();
      LCD_ENA = 1;
      CheckBusy();                        //check the LCD busy or not.
      LCD_ENA = 0;
      LCD_RW = 0;
      
      LCD_BusData(cmd);
      LCD_DelayNop();
      LCD_ENA = 1;
      LCD_DelayNop();
      LCD_ENA = 0;
      LCD_BusData(0xff);
}

//========================================================================
// 函数: void Write_DIS_Data(u8 dat)
// 描述: 写显示数据(检测忙)
// 参数: dat: 要写的数据.
// 返回: none.
// 版本: VER1.0
// 日期: 2013-4-1
// 备注:
//========================================================================
void Write_DIS_Data(u8 dat)
{
      LCD_RS = 0;
      LCD_RW = 1;

      LCD_BusData(0xff);
      LCD_DelayNop();
      LCD_ENA = 1;
      CheckBusy();                        //check the LCD busy or not.
      LCD_ENA = 0;
      LCD_RW = 0;
      LCD_RS= 1;

      LCD_BusData(dat);
      LCD_DelayNop();
      LCD_ENA = 1;
      LCD_DelayNop();
      LCD_ENA = 0;
      LCD_BusData(0xff);
}

//========================================================================
// 函数: void Initialize_LCD(void)
// 描述: 初始化函数
// 参数: none.
// 返回: none.
// 版本: VER1.0
// 日期: 2013-4-1
// 备注:
//========================================================================
void Initialize_LCD(void)
{
      LCD_ENA = 0;
      LCD_RS= 0;
      LCD_RW = 0;

      delay_ms(100);
      IniSendCMD(C_BIT8);                //set the data is 8 bits

      delay_ms(10);
      Write_CMD(C_L2DOT7);                //tow lines 5*7 dots

      delay_ms(6);
      Write_CMD(C_CLEAR);                //clear LCD RAM
      Write_CMD(C_CUR_R);                //Curror Shift Right
      Write_CMD(C_ON);                //turn onLCD
}

//========================================================================
// 函数: void ClearLine(u8 row)
// 描述: 清除1行
// 参数: row: 行(0或1)
// 返回: none.
// 版本: VER1.0
// 日期: 2013-4-1
// 备注:
//========================================================================
void ClearLine(u8 row)
{
      u8 i;
      Write_CMD(((row & 1) << 6) | 0x80);
      for(i=0; i<LineLength; i++)      Write_DIS_Data(' ');
}

//========================================================================
// 函数: void WriteChar(u8 row, u8 column, u8 dat)
// 描述: 指定行、列和字符, 写一个字符
// 参数: row: 行(0或1),column: 第几个字符(0~15),dat: 要写的字符.
// 返回: none.
// 版本: VER1.0
// 日期: 2013-4-1
// 备注:
//========================================================================
void WriteChar(u8 row, u8 column, u8 dat)
{
      Write_CMD((((row & 1) << 6) + column) | 0x80);
      Write_DIS_Data(dat);
}

//========================================================================
// 函数: void PutString(u8 row, u8 column, u8 *puts)
// 描述: 写一个字符串,指定行、列和字符串首地址
// 参数: row: 行(0或1),column: 第几个字符(0~15),puts: 要写的字符串指针.
// 返回: none.
// 版本: VER1.0
// 日期: 2013-4-1
// 备注:
//========================================================================
void PutString(u8 row, u8 column, u8 *puts)
{
      Write_CMD((((row & 1) << 6) + column) | 0x80);
      for ( ;*puts != 0;puts++)                //遇到停止符0结束
      {
                Write_DIS_Data(*puts);
                if(++column >= LineLength)      break;
      }
}

//******************** LCD20 Module END ***************************






Yh117227 发表于 2023-4-4 08:35:10

神农鼎 发表于 2023-4-3 23:04
我不是研发十多年,已多年装不懂技术了,只是凭感觉帮你找些专业对口的资料




我的天,还可以这么操作!太强了!谢谢哥!我就是一个小白,啥都不懂,只会根据51的知识来套这个32,现在老师让用这个32做毕业设计,研究了还几天了都没进展{:4_184:}

芯征程 发表于 2023-4-4 08:37:17

Yh117227 发表于 2023-4-3 21:08
我用的stc32的话,大部分程序应该都是通用的吧?我是不是改改引脚就行?

管脚是一样的,其他的看下手册哦{:lol:}

芯征程 发表于 2023-4-4 10:13:12

本帖最后由 红心A 于 2023-4-4 10:19 编辑

Yh117227 发表于 2023-4-4 08:35
我的天,还可以这么操作!太强了!谢谢哥!我就是一个小白,啥都不懂,只会根据51的知识来套这个32,现在 ...
{:lol:}抓紧跟着冲哥一起学习起来
https://www.stcaimcu.com/forum.php?mod=viewthread&tid=323

Yh117227 发表于 2023-4-5 11:57:30

红心A 发表于 2023-4-4 10:13
抓紧跟着冲哥一起学习起来
https://www.stcaimcu.com/forum.php?mod=viewthread&tid=323
...

二刷了{:4_175:}

神农鼎 发表于 2023-4-5 14:29:29

楼上从 【STC8H8K64U实验箱的LCD1602】移植到【STC32G12K128+LCD1602】成功没有 ?
下面STC32G12K128的实验箱程序包只有 LCD12864的,如您还没成功,下周我们自己移植个【LCD1602+STC32G12K128】上来




Yh117227 发表于 2023-4-6 10:01:57

神农鼎 发表于 2023-4-5 14:29
楼上从 【STC8H8K64U实验箱的LCD1602】移植到【STC32G12K128+LCD1602】成功没有 ?
下面STC32G12K128的实验 ...

我没用用那个STC8H8K64U实验箱的程序,我根据89C51的程序改的,最后找到只发光不显示字母的原因是因为lcd1602的EN引脚所接的滑动变阻器的阻值没设置好,我调整了一下滑动变阻器的阻值,字母就出现了。

但是我现在又遇到一个问题!我现在打算加上HC—SR04测距模块,来实现所测得距离在lcd1602上显示。我这次也是先89C51实现,并且已经成功,但是改完程序移植到stc32上之后,不显示字母和数字。
我在思考是不是因为屠龙刀stc32上没有晶振的原因,所以导致定时器无法正常使用?

"D:\桌面\8CA7B8B317AF329C76215F9F2A32912D.png"
"D:\桌面\IMG_8705.JPG"

Yh117227 发表于 2023-4-6 10:06:31

神农鼎 发表于 2023-4-5 14:29
楼上从 【STC8H8K64U实验箱的LCD1602】移植到【STC32G12K128+LCD1602】成功没有 ?
下面STC32G12K128的实验 ...

我要用stc32实现超声波测距,在lcd1602上显示,所用的引脚都设置成准双向口就行吧?

芯征程 发表于 2023-4-6 10:34:19

Yh117227 发表于 2023-4-6 10:06
我要用stc32实现超声波测距,在lcd1602上显示,所用的引脚都设置成准双向口就行吧? ...

普通应用使用准双向模式是可以的

Yh117227 发表于 2023-4-6 10:56:38

红心A 发表于 2023-4-6 10:34
普通应用使用准双向模式是可以的

嗯嗯嗯嗯好的,感谢{:4_196:}
页: 1 [2] 3
查看完整版本: LCD1602, LCD12864, 求助