Test whether the code is highlighted

Keywords: C

Reprinted from ReCclayhttp://blog.csdn.net/recclay/article/details/79332846
LCD strategy to play with the pointer, the pointer is not the C language, not the enemy of 51!

/*
*******************************************************************************
* File name: main.c
* Description: the pointer of 51
* By CLAY
* Version number: v1.0.0
* Date: February 17, 2018
* Note: corresponding instruction, display corresponding array
*         
*******************************************************************************
*/

#include <stc15.h>
#define u8 unsigned char
#define u16 unsigned int
#define u32 unsigned long

bit CmdArrived = 0;
u8 CmdIndex = 0;
u8 *PtrTxd;
u8 CntTxd = 0;
u8 arr1[] = {1,2};
u8 arr2[] = {1,2,3,4};
u8 arr3[] = {1,2,3,4,5,6};
u8 arr4[] = {1,2,3,4,5,6,7,8};


void ConfigUART(u32 baud);
void CloseFucker();

void main()
{
    EA = 1;
    ConfigUART(9600);
    CloseFucker();

    while(1)
    {
        if(CmdArrived)
        {
            CmdArrived = 0;//Don't forget to clear
            switch(CmdIndex)
            {
                case 1:
                        PtrTxd = arr1;
                        CntTxd = sizeof(arr1);
                        TI = 1; //Note here is the manual setting!!!
                        break;
                case 2:
                        PtrTxd = arr2;
                        CntTxd = sizeof(arr2);
                        TI = 1;
                        break;
                case 3:
                        PtrTxd = arr3;
                        CntTxd = sizeof(arr3);
                        TI = 1;
                        break;
                case 4:
                        PtrTxd = arr4;
                        CntTxd = sizeof(arr4);
                        TI = 1;
                        break;
                default: 
                        break;                                      
            }
        }
    }
}

void CloseFucker()
{
    P2 = (P2 & 0x1F) | 0xA0;
    P0 = 0xAF;
    P2 = 0x00;
}

void ConfigUART(u32 baud)
{

    PCON &= 0x7F;
    SCON = 0x50;        
    AUXR &= 0xBF;   
    AUXR &= 0xFE;

    TMOD &= 0x0F;   
    TMOD |= 0x20;
    TH1 = 256 - (11059200/12/32)/baud;      
//  TH1 = 0xFD; 
    TL1 = TH1;
    ET1 = 0;
    ES = 1;     
    TR1 = 1;        

}

void InterruptUART() interrupt 4
{
    if(RI)
    {
        RI = 0;
        CmdIndex = SBUF;
        CmdArrived = 1;  //Set flag
    }
    if(TI)
    {
        TI = 0;
        if(CntTxd > 0)
        {
            SBUF = *PtrTxd;
            CntTxd--;
            PtrTxd++;
        }
    }
}

In fact, the pointer operation is more like a string, that is, an array of characters. Let's change the four arr arrays above to...

u8 arr1[] = "1-Hello!\r\n";
u8 arr2[] = {'2', '-', 'H', 'e', 'l', 'l', 'o', '!', '\r', '\n'};
u8 arr3[] = {51,  45,  72,  101, 108, 108, 111, 33,  13,   10};
u8 arr4[] = "4-Hello!\r\n";

At this time, we should pay special attention to the difference between sizeof and strlen. For details, see this blog: http://blog.csdn.net/recclay/article/details/78947296.

The short note is: sizeof is occupied by memory which includes' \ 0 '! There may be some places to pay attention to, here is a special statement!

/*
*******************************************************************************
* File name: main.c
* Description: LCD1602 timing
* By CLAY
* Version number: v1.0.0
* Date: February 17, 2018
* Note: LCD bottom layer. Two lines of the screen are displayed.
*         
*******************************************************************************
*/

#include <stc15.h>
#define u8 unsigned char
#define u16 unsigned int
#define u32 unsigned long

#define LCD1602_DB P0
sbit LCD1602_RS = P2^0;
sbit LCD1602_RW = P2^1;
sbit LCD1602_E  = P1^2;

void InitLcd1602();
void LcdShowStr(u8 x, u8 y, u8 *str);

void main()
{
    u8 str[] = "CLAY";

    InitLcd1602();
    LcdShowStr(0, 1, str);
    LcdShowStr(1, 1, "Bo -- bo");
    while(1);
}

void LcdWaitReady()
{
    u8 sta;

    LCD1602_DB = 0xFF;
    LCD1602_RS = 0;
    LCD1602_RW = 1;
    do{
        LCD1602_E = 1;
        sta = LCD1602_DB;
        LCD1602_E = 0;
    }while(sta & 0x80);
}

void LcdWriteCmd(u8 cmd)
{
    LcdWaitReady();
    LCD1602_RS = 0;
    LCD1602_RW = 0;
    LCD1602_DB = cmd;
    LCD1602_E  = 1;
    LCD1602_E  = 0;
}

void LcdWriteDat(u8 dat)
{
    LcdWaitReady();
    LCD1602_RS = 1;
    LCD1602_RW = 0;
    LCD1602_DB = dat;
    LCD1602_E  = 1;
    LCD1602_E  = 0;
}

void LcdSetCursor(u8 x, u8 y)
{
    u8 addr;

    if(y == 0)
        addr = 0x00 + x;
    else
        addr = 0x40 + x;
    LcdWriteCmd(addr | 0x80);
}
void LcdShowStr(u8 x, u8 y, u8 *str)
{
    LcdSetCursor(x, y);
    while(*str != '\0')
    {
        LcdWriteDat(*str++);
    }
}

void InitLcd1602()
{
    LcdWriteCmd(0x38);//1602 fixed order
    LcdWriteCmd(0x0C);//On display, off cursor
    LcdWriteCmd(0x06);//Text fixed, pointer plus 1
    LcdWriteCmd(0x01);//Clean screen
}

Whole screen movement

/*
*******************************************************************************
* File name: main.c
* Description: 1602 move left operation
* By CLAY
* Version number: v1.0.0
* Date: February 17, 2018
* Note: the overall left shift is realized
*         
*******************************************************************************
*/

#include <stc15.h>
#define u8 unsigned char
#define u16 unsigned int
#define u32 unsigned long

#define LCD1602_DB P0
sbit LCD1602_RS = P2^0;
sbit LCD1602_RW = P2^1;
sbit LCD1602_E  = P1^2;

bit flag500ms = 0;
u8 T0RH = 0;
u8 T0RL = 0;
u8 code str1[] = "Test...";
u8 code str2[] = "Move...";


void ConfigTimer0(u16 ms);
void InitLcd1602();
void LcdShowStr(u8 x, u8 y, u8 *str, u8 len);

void main()
{
    u8 i = 0;
    u8 index = 0;
    u8 bufMove1[16+sizeof(str1)+16];
    u8 bufMove2[16+sizeof(str2)+16]; 

    EA = 1;
    ConfigTimer0(10);
    InitLcd1602();

    for(i=0; i<16; i++)
    {
        bufMove1[i] = ' ';
        bufMove2[i] = ' ';
    }
    for(i=0; i<sizeof(str1)-1; i++)
    {
        bufMove1[16+i] = str1[i];
        bufMove2[16+i] = str2[i];
    }
    for(i=(16+sizeof(str1)-1); i<sizeof(bufMove1); i++)
    {
        bufMove1[i] = ' ';
        bufMove2[i] = ' ';
    }

    while(1)
    {
        if(flag500ms)
        {
            flag500ms = 0;

            LcdShowStr(0, 0, bufMove1+index, 16);
            LcdShowStr(1, 0, bufMove2+index, 16);

            index ++;

            if(index >= (16+sizeof(str1) - 1))
            {
                index = 0;
            }
        }           
    }
}

void ConfigTimer0(u16 ms)
{
    u32 tmp = 0;

    tmp = 11059200 / 12;
    tmp = (tmp * ms) / 1000;
    tmp = 65536 - tmp;
    T0RH = (u8)(tmp>>8);
    T0RL = (u8)tmp;
    TMOD &= 0xF0;
    TMOD |= 0x01;
    TH0 = T0RH;
    TL0 = T0RL;
    ET0 = 1;
    TR0 = 1;        
}

void LcdWaitReady()
{
    u8 sta;

    LCD1602_DB = 0xFF;
    LCD1602_RS = 0;
    LCD1602_RW = 1;
    do{
        LCD1602_E = 1;
        sta = LCD1602_DB;
        LCD1602_E = 0;
    }while(sta & 0x80); 
}

void LcdWriteCmd(u8 cmd)
{
    LcdWaitReady();
    LCD1602_RS = 0;
    LCD1602_RW = 0;
    LCD1602_DB = cmd;
    LCD1602_E  = 1;
    LCD1602_E  = 0;
}

void LcdWriteDat(u8 dat)
{
    LcdWaitReady();
    LCD1602_RS = 1;
    LCD1602_RW = 0;
    LCD1602_DB = dat;
    LCD1602_E  = 1;
    LCD1602_E  = 0;
}

void LcdSetCursor(u8 x, u8 y)
{
    u8 addr = 0;

    if(y == 0)
    {
        addr = 0x00 + x;
    }
    else
    {
        addr = 0x40 + x;
    }
    LcdWriteCmd(addr | 0x80);
}

void LcdShowStr(u8 x, u8 y, u8 *str, u8 len)
{
    LcdSetCursor(x, y);

    while(len--)
    {
        LcdWriteDat(*str);
    }
}

void InitLcd1602()
{
    LcdWriteCmd(0x38);
    LcdWriteCmd(0x0C);
    LcdWriteCmd(0x06);
    LcdWriteCmd(0x01);
}

void interrupTimer0() interrupt 1
{
    static u8 tmr500ms = 0;

    TH0 = T0RH;
    TL0 = T0RL;

    tmr500ms++;
    if(tmr500ms == 50)
    {
        tmr500ms = 0;
        flag500ms = 1;
    }
}

Multiple files

/*
*******************************************************************************
* File name: main.c
* Description: full screen move
* By CLAY
* Version number: v1.0.1
* Date: February 17, 2018
* Note: use multiple documents
*         
*******************************************************************************
*/

#include <stc15.h>
#include <Lcd1602.h>

bit flag500ms = 0;
u8 T0RH = 0;
u8 T0RL = 0;
u8 code str1[] = "TEST...";
u8 code str2[] = "MOVE...";

void ConfigTimer0(u16 ms);
void main()
{
    u8 i = 0;
    u8 index = 0;
    u8 bufMove1[16+sizeof(str1)+16];
    u8 bufMove2[16+sizeof(str2)+16];

    EA = 1;
    ConfigTimer0(10);
    InitLcd1602();

    for(i=0; i<16; i++)
    {
        bufMove1[i] = ' ';
        bufMove2[i] = ' ';
    }   
    for(i=0; i<(sizeof(str1)-1); i++)
    {
        bufMove1[16+i] = str1[i];
        bufMove2[16+i] = str2[i];
    }
    for(i=(16+sizeof(str1)-1); i<sizeof(bufMove1); i++)
    {
        bufMove1[i] = ' ';
        bufMove2[i] = ' ';
    }

    while(1)
    {
        if(flag500ms)
        {
            flag500ms = 0;

            LcdShowStr(0, 0, bufMove1+index, 16);
            LcdShowStr(1, 0, bufMove2+index, 16);

            index++;
            if(index >= (16+sizeof(str1)-1))
            {
                index = 0;
            }
        }
    }
}

void ConfigTimer0(u16 ms)
{
    u32 tmp;

    tmp = 11059200/12;
    tmp = (tmp*ms)/1000;
    tmp = 65536 - tmp;
    T0RH = (u8)(tmp>>8);
    T0RL = (u8)tmp;

    TMOD &= 0xF0;
    TMOD |= 0x01;
    TH0 = T0RH;
    TL0 = T0RL;
    ET0 = 1;
    TR0 = 1;    
}

void InterruptTimer0() interrupt 1
{
    static u8 tmr500ms = 0;

    TH0 = T0RH;
    TL0 = T0RL;

    tmr500ms++;
    if(tmr500ms == 50)
    {
        tmr500ms = 0;
        flag500ms = 1;
    }
}
/*
*******************************************************************************
* File name: 1602.c
* Description: 1602 bottom drive
* By CLAY
* Version number: v1.0.1
* Date: February 17, 2018
* Remarks:
*         
*******************************************************************************
*/

#include <stc15.h>
#include <Lcd1602.h>

#define LCD1602_DB P0
sbit LCD1602_RS = P2^0;
sbit LCD1602_RW = P2^1;
sbit LCD1602_E  = P1^2;

void LcdWaitReady()
{
    u8 sta;

    LCD1602_DB = 0xFF;
    LCD1602_RS = 0;
    LCD1602_RW = 1;
    do{
        LCD1602_E = 1;
        sta = LCD1602_DB;
        LCD1602_E = 0;
    }while(sta & 0x80); 
}

void LcdWriteCmd(u8 cmd)
{
    LcdWaitReady();
    LCD1602_RS = 0;
    LCD1602_RW = 0;
    LCD1602_DB = cmd;
    LCD1602_E  = 1;
    LCD1602_E  = 0;
}
void LcdWriteDat(u8 dat)
{
    LcdWaitReady();
    LCD1602_RS = 1;
    LCD1602_RW = 0;
    LCD1602_DB = dat;
    LCD1602_E  = 1;
    LCD1602_E  = 0;     
}

void LcdSetCursor(u8 x, u8 y)
{
    u8 addr;

    if(y == 0)
    {
        addr = 0x00 + x;
    }
    else
    {
        addr = 0x40 + x;
    }
    LcdWriteCmd(addr | 0x80);
}

void LcdShowStr(u8 x, u8 y, u8 *str, u8 len)
{
    LcdSetCursor(x, y);
    while(len--)
    {
        LcdWriteDat(*str++);
    }
}

void InitLcd1602()
{
    LcdWriteCmd(0x38);
    LcdWriteCmd(0x0C);
    LcdWriteCmd(0x06);
    LcdWriteCmd(0x01);
}
/*
*******************************************************************************
* File name: Lcd1602.h
* Description:
* By CLAY
* Version number: v1.0.0
* Date: 
* Remarks:
*         
*******************************************************************************
*/

#ifndef LCD1602_H
#define LCD1602_H

#define u8 unsigned char
#define u16 unsigned int
#define u32 unsigned long


void InitLcd1602();
void LcdShowStr(u8 x, u8 y, u8 *str, u8 len);

#endif

Attach a practical battle related to LCD1602:
http://blog.csdn.net/recclay/article/details/79334658

Posted by jake8 on Tue, 31 Mar 2020 16:15:00 -0700