嵌入式硬件--串口->LCD

功能:读取串口数据,并将其显示在LCD屏上
环境: keil(MDK)
使用ULINK2仿真器连接Embest EduKit-IV实验平台的主板JTAG接口;使用Embest EduKit-IV实验平台附带的交叉串口线,连接实验平台主板上的COM2和PC机的串口(一般PC只有一个串口,如果有多个请自行选择,笔记本没有串口设备的可购买USB转串口适配器扩充);使用EmbestEduKit-IV实验平台附带的电源适配器,连接实验平台主板上的电源接口。
串口接收设置
在PC机上运行windows自带的超级终端串口通信程序,或者使用实验平台附带光盘内设置好了的超级终端,设置超级终端:波特率115200、1位停止位、无校验位、无硬件流控制,或者使用其它串口通信程序。(注:超级终端串口的选择根据用户的PC串口硬件不同,请自行选择,如果PC机只有一个串口,一般是COM1)

#include 
#include "2410lib.h"
#include "lcdlib.h"
#include "glib.h"
#include "lcd.h"
#include 
#define LCD_BUF_SIZE        (SCR_XSIZE_TFT_640480*SCR_YSIZE_TFT_640480/2)
#define LCD_ACTIVE_BUFFER   (0x33800000)
#define LCD_VIRTUAL_BUFFER  (0x33800000 + LCD_BUF_SIZE)
#define LCD_D_OFF           rGPCDAT &= ~(1 << 4); 
#define LCD_D_ON            rGPCDAT |= (1 << 4); 

#ifdef BOARDTEST_EXH
#undef BOARDTEST
#endif  

#ifndef BOARDTEST   
extern const UINT8T g_ucBitmap[][76800];
extern UINT8T g_ucAscii8x16[];
extern UINT8T g_auc_Ascii6x8[];
extern UINT8T g_auc_hzk24[];
#endif

void Lcd_DspAscII6x8(UINT16T x0, UINT16T y0, UINT16T ForeColor, UINT8T * s);
void Lcd_DspAscII8x16(UINT16T x0, UINT16T y0, UINT16T ForeColor, UINT8T * s);
void Lcd_DspHz24(UINT16T x0, UINT16T y0, UINT16T ForeColor, UINT8T *s);
void Lcd_Draw_HLine(INT16T usX0, INT16T usX1, INT16T usY0, UINT16T ucColor, UINT16T usWidth);
void Lcd_Draw_VLine (INT16T usY0, INT16T usY1, INT16T usX0, UINT16T ucColor, UINT16T usWidth);
void lcd_clr(void);
extern void Lcd_port_init(void);
extern void Lcd_Port_Return(void);

/*********************************************************************************************
* name:     lcd_init_app
* func:     lcd application initialization code 
* para:     none
* ret:      none
* modify:
* comment:      
*********************************************************************************************/
void lcd_init_app()
{
    Lcd_port_init();
    Lcd_Init(MODE_TFT_16BIT_800480);
    Glib_Init(MODE_TFT_16BIT_800480);
    Glib_ClearScr(0, MODE_TFT_16BIT_800480);
    Lcd_PowerEnable(0, 1);
    Lcd_EnvidOnOff(1);    
}

/*********************************************************************************************
* name:     lcd_init_app_end
* func:     end lcd application initialization
* para:     none
* ret:      none
* modify:
* comment:      
*********************************************************************************************/
void lcd_init_app_end()
{
    Lcd_EnvidOnOff(0);
    Lcd_Port_Return();
}

#ifndef BOARDTEST   
/*********************************************************************************************
* name:     Lcd_DspHz24
* func:     display 24x24 Chinese Symbol on Lcd  
* para:     UINT16T x0          --  x coordinate of start point
*           UINT16T y0          --  y coordinate of start point
*           UINT16T ForeColor --  foreground color of lcd 
*           UINT8T *s        --  string to display 
* ret:      none
* modify:
* comment:      
*********************************************************************************************/
void Lcd_DspHz24(UINT16T x0, UINT16T y0, UINT16T ForeColor, UINT8T *s)
{
    INT16T i,j,k,x,y,xx;
    UINT8T qm,wm;
    INT32T ulOffset;
    INT8T hzbuf[72];

    for(i = 0; i < strlen((const char*)s); i++)
    {
        if(((UINT8T)(*(s+i))) < 161)
        {
            break;
        }
        else
        {
            qm = *(s+i) - 176;                                  //161;
                wm = *(s+i + 1) - 161;
                    ulOffset = (INT32T)(qm * 94 + wm) * 72;
                    for (j = 0; j < 72; j ++)
                    {
                        hzbuf[j] = g_auc_hzk24[ulOffset + j];
                    }
                    for(y = 0; y < 24; y++)
                    {
                    for(x = 0; x < 24; x++) 
                    {
                        k = x % 8;
                    if (hzbuf[y * 3 + x / 8]  & (0x80 >> k))
                    {
                        xx = x0 + x + i*12;
                        PutPixel( xx, y + y0, (UINT16T)ForeColor);
                    }
                  }
                    }
                i++;
            }
        }       
}
#endif

/*********************************************************************************************
* name:     Lcd_DspAscII6x8()
* func:     display 6x8 ASCII character string 
* para:     usX0,usY0 -- ASCII character string's start point coordinate
*           ForeColor -- appointed color value
*           pucChar   -- ASCII character string
* ret:      none
* modify:
* comment:      
*********************************************************************************************/
#define XWIDTH          6
extern UINT8T g_ucAscii6x8[];
void Lcd_DspAscII6x8(UINT16T usX0, UINT16T usY0,UINT16T ForeColor, UINT8T* pucChar)
{
    UINT32T i,j;
    UINT8T  ucTemp;

    while( *pucChar != 0 )
    {
        for( i=0; i < 8; i++ )
        {
            ucTemp = g_ucAscii6x8[(*pucChar) * 8 + i];
            for( j = 0; j < 8; j++ )
            {
                if( (ucTemp & (0x80 >> j)) != 0 )
                {
                    PutPixel(usX0 + i, usY0 + 8 - j, (UINT16T)ForeColor);
                }               
            }
        }
        usX0 += XWIDTH;
        pucChar++;
    }
} 

#ifndef BOARDTEST   
/*********************************************************************************************
* name:     Lcd_DspAscII8X16
* func:     display 8x16 AscII Symbol on Lcd  
* para:     UINT16T x0          --  x coordinate of start point
*           UINT16T y0          --  y coordinate of start point
*           UINT16T ForeColor --  foreground color of lcd 
*           UINT8T *s        --  string to display 
* ret:      none
* modify:
* comment:      
*********************************************************************************************/
void Lcd_DspAscII8x16(UINT16T x0, UINT16T y0, UINT16T ForeColor, UINT8T * s)
{
    INT16T i,j,k,x,y,xx;
    UINT8T qm;
    INT32T ulOffset;
    INT8T ywbuf[16];

    for(i = 0; i < strlen((const char*)s); i++)
    {
        if((UINT8T)*(s+i) >= 161)
        {
            return;
        }
        else
        {
            qm = *(s+i);
                    ulOffset = (INT32T)(qm) * 16;   
                    for (j = 0; j < 16; j ++)
                    {
                        ywbuf[j] = g_ucAscii8x16[ulOffset + j];
                    }

                    for(y = 0; y < 16; y++)
                    {
                    for(x = 0; x < 8; x++) 
                    {
                            k = x % 8;
                    if (ywbuf[y]  & (0x80 >> k))
                    {
                        xx = x0 + x + i*8;
                        PutPixel( xx, y + y0, (UINT16T)ForeColor);
                    }
                }
                    }
            }
        }
}
#endif  

/*********************************************************************************************
* name:     lcd_clr
* func:     clear LCD screen
* para:     none 
* ret:      none
* modify:
* comment:      
*********************************************************************************************/
void lcd_clr(void)
{
    UINT32T i;
    UINT32T *pDisp = (UINT32T*)LCD_ACTIVE_BUFFER;

    for (i = 0; i < (SCR_XSIZE_CSTN * SCR_YSIZE_CSTN /4); i++)
    {
        *pDisp = 0xffffffff;//ALLWHITE;
    }
}

/*********************************************************************************************
* name:     lcd_clr_rect
* func:     fill appointed area with appointed color
* para:     usLeft,usTop,usRight,usBottom -- area's rectangle acme coordinate
*           ucColor -- appointed color value
* ret:      none
* modify:
* comment:  also as clear screen function 
*********************************************************************************************/
void lcd_clr_rect(INT16T usLeft, INT16T usTop, INT16T usRight, INT16T usBottom, UINT16T ucColor)
{
    UINT32T i, j;
    for (i = usLeft; i < usRight; i++)
    for (j = usTop; j < usBottom; j++)
    {
        PutPixel(i,j,ucColor);
        //*(pDisp+i+j) = ucColor;
    }
}

/*********************************************************************************************
* name:     Lcd_Draw_Box()
* func:     Draw rectangle with appointed color
* para:     usLeft,usTop,usRight,usBottom -- rectangle's acme coordinate
*           ucColor -- appointed color value
* ret:      none
* modify:
* comment:      
*********************************************************************************************/
void Lcd_Draw_Box(INT16T usLeft, INT16T usTop, INT16T usRight, INT16T usBottom, UINT16T ucColor)
{
    Lcd_Draw_HLine(usLeft, usRight,  usTop,    ucColor, 1);
    Lcd_Draw_HLine(usLeft, usRight,  usBottom, ucColor, 1);
    Lcd_Draw_VLine(usTop,  usBottom, usLeft,   ucColor, 1);
    Lcd_Draw_VLine(usTop,  usBottom, usRight,  ucColor, 1);
}

/*********************************************************************************************
* name:     Lcd_Draw_Line()
* func:     Draw line with appointed color
* para:     usX0,usY0 -- line's start point coordinate
*           usX1,usY1 -- line's end point coordinate
*           ucColor -- appointed color value
*           usWidth -- line's width
* ret:      none
* modify:
* comment:      
*********************************************************************************************/
void Lcd_Draw_Line(INT16T usX0, INT16T usY0, INT16T usX1, INT16T usY1, UINT16T ucColor, UINT16T usWidth)
{
    INT16T usDx;
    INT16T usDy;
    INT16T y_sign;
    INT16T x_sign;
    INT16T decision;
    INT16T wCurx, wCury, wNextx, wNexty, wpy, wpx;

    if( usY0 == usY1 )
    {
        Lcd_Draw_HLine (usX0, usX1, usY0, ucColor, usWidth);
        return;
    }
    if( usX0 == usX1 )
    {
        Lcd_Draw_VLine (usY0, usY1, usX0, ucColor, usWidth);
        return;
    }
    usDx = abs(usX0 - usX1);
    usDy = abs(usY0 - usY1);
    if( ((usDx >= usDy && (usX0 > usX1)) ||
        ((usDy > usDx) && (usY0 > usY1))) )
    {
        GUISWAP(usX1, usX0);
        GUISWAP(usY1, usY0);
    }
    y_sign = (usY1 - usY0) / usDy;
    x_sign = (usX1 - usX0) / usDx;

    if( usDx >= usDy )
    {
        for( wCurx = usX0, wCury = usY0, wNextx = usX1,
             wNexty = usY1, decision = (usDx >> 1);
             wCurx <= wNextx; wCurx++, wNextx--, decision += usDy )
        {
            if( decision >= usDx )
            {
                decision -= usDx;
                wCury += y_sign;
                wNexty -= y_sign;
            }
            for( wpy = wCury - usWidth / 2;
                 wpy <= wCury + usWidth / 2; wpy++ )
            {
                PutPixel(wCurx, wpy, ucColor);
            }

            for( wpy = wNexty - usWidth / 2;
                 wpy <= wNexty + usWidth / 2; wpy++ )
            {
                PutPixel(wNextx, wpy, ucColor);
            }
        }
    }
    else
    {
        for( wCurx = usX0, wCury = usY0, wNextx = usX1,
             wNexty = usY1, decision = (usDy >> 1);
             wCury <= wNexty; wCury++, wNexty--, decision += usDx )
        {
            if( decision >= usDy )
            {
                decision -= usDy;
                wCurx += x_sign;
                wNextx -= x_sign;
            }
            for( wpx = wCurx - usWidth / 2;
                 wpx <= wCurx + usWidth / 2; wpx++ )
            {
                PutPixel(wpx, wCury, ucColor);
            }

            for( wpx = wNextx - usWidth / 2;
                 wpx <= wNextx + usWidth / 2; wpx++ )
            {
                PutPixel(wpx, wNexty, ucColor);
            }
        }
    }
}

/*********************************************************************************************
* name:     Lcd_Draw_HLine()
* func:     Draw horizontal line with appointed color
* para:     usX0,usY0 -- line's start point coordinate
*           usX1 -- line's end point X-coordinate
*           ucColor -- appointed color value
*           usWidth -- line's width
* ret:      none
* modify:
* comment:      
*********************************************************************************************/
void Lcd_Draw_HLine(INT16T usX0, INT16T usX1, INT16T usY0, UINT16T ucColor, UINT16T usWidth)
{
    INT16T usLen;

    if( usX1 < usX0 )
    {
        GUISWAP (usX1, usX0);
    }

    while( (usWidth--) > 0 )
    {
        usLen = usX1 - usX0 + 1;
        while( (usLen--) > 0 )
        {
            PutPixel(usX0 + usLen, usY0, ucColor);
        }
        usY0++;
    }
}

/*********************************************************************************************
* name:     Lcd_Draw_VLine()
* func:     Draw vertical line with appointed color
* para:     usX0,usY0 -- line's start point coordinate
*           usY1 -- line's end point Y-coordinate
*           ucColor -- appointed color value
*           usWidth -- line's width
* ret:      none
* modify:
* comment:      
*********************************************************************************************/
void Lcd_Draw_VLine (INT16T usY0, INT16T usY1, INT16T usX0, UINT16T ucColor, UINT16T usWidth)
{
    INT16T usLen;

    if( usY1 < usY0 )
    {
        GUISWAP (usY1, usY0);
    }

    while( (usWidth--) > 0 )
    {
        usLen = usY1 - usY0 + 1;
        while( (usLen--) > 0 )
        {
            PutPixel(usX0, usY0 + usLen, ucColor);
        }
        usX0++;
    }
}

/*********************************************************************************************
* name:     color_lcd_test()
* func:     LCD test function
* para:     none
* ret:      none
* modify:
* comment:      
*********************************************************************************************/
void color_lcd_test(void)
{
    char c;
    UINT8T *cInput;
    UINT8T ucInNo=0;
    UINT32T g_nKeyPress;
    lcd_init_app();
    lcd_clr_rect(0,0,800,500,BLACK);
    Glib_Rectangle(20,150,780,250,GREEN);
    Lcd_DspAscII8x16(30,200,RED,"The words that you input are: ");
    while(1){   
    ucInNo = 0;
    uart_printf(" \nPlease input words, then press Enter: ");
    g_nKeyPress = 1;
    while(g_nKeyPress==1)           // only for board test to exit
    {
        c=uart_getch();
        uart_printf("%c",c);
        if(c!='\r')
            cInput[ucInNo++]=c;
        else
        {
            cInput[ucInNo]='\0';
            break;
        }
    }
    lcd_clr_rect(270,200,700,240,BLACK);    
    Lcd_DspAscII8x16(270,200,RED,cInput);
    }
 }

嵌入式硬件--串口->LCD_第1张图片
嵌入式硬件--串口->LCD_第2张图片
完整工程:http://download.csdn.net/detail/qq_29777421/9892878

你可能感兴趣的:(嵌入式硬件)