標(biāo)題: 求助:關(guān)于單片機(jī)上1602只顯示一排方格的問(wèn)題!! [打印本頁(yè)]

作者: lytcreate    時(shí)間: 2018-6-7 12:28
標(biāo)題: 求助:關(guān)于單片機(jī)上1602只顯示一排方格的問(wèn)題!
我把代碼上傳了。大家看看!謝謝!這是.h文件:
#ifndef __LCD_H_
#define __LCD_H_
/**********************************
當(dāng)使用的是4位數(shù)據(jù)傳輸?shù)臅r(shí)候定義,
使用8位取消這個(gè)定義
**********************************/
#define LCD1602_4PINS

/**********************************
包含頭文件
**********************************/
#include<reg51.h>

//---重定義關(guān)鍵詞---//
#ifndef uchar
#define uchar unsigned char
#endif

#ifndef uint
#define uint unsigned int
#endif

/**********************************
PIN口定義
**********************************/
#define LCD1602_DATAPINS P0
sbit LCD1602_E=P2^6;
sbit LCD1602_RW=P2^5;
sbit LCD1602_RS=P2^4;

/**********************************
函數(shù)聲明
**********************************/
/*在51單片機(jī)12MHZ時(shí)鐘下的延時(shí)函數(shù)*/
void Lcd1602_Delay1ms(uint c);   //誤差 0us
/*LCD1602寫入8位命令子函數(shù)*/
void LcdWriteCom(uchar com);
/*LCD1602寫入8位數(shù)據(jù)子函數(shù)*/       
void LcdWriteData(uchar dat)        ;
/*LCD1602初始化子程序*/               
void LcdInit();                                                  

#endif

這是led.c文件:
#include"lcd.h"

/*******************************************************************************
* 函 數(shù) 名         : Lcd1602_Delay1ms
* 函數(shù)功能                   : 延時(shí)函數(shù),延時(shí)1ms
* 輸    入         : c
* 輸    出         : 無(wú)
* 說(shuō)    名         : 該函數(shù)是在12MHZ晶振下,12分頻單片機(jī)的延時(shí)。
*******************************************************************************/

void Lcd1602_Delay1ms(uint c)   //誤差 0us
{
    uchar a,b;
        for (; c>0; c--)
        {
                 for (b=199;b>0;b--)
                 {
                          for(a=1;a>0;a--);
                 }      
        }
           
}

/*******************************************************************************
* 函 數(shù) 名         : LcdWriteCom
* 函數(shù)功能                   : 向LCD寫入一個(gè)字節(jié)的命令
* 輸    入         : com
* 輸    出         : 無(wú)
*******************************************************************************/
#ifndef         LCD1602_4PINS         //當(dāng)沒有定義這個(gè)LCD1602_4PINS時(shí)
void LcdWriteCom(uchar com)          //寫入命令
{
        LCD1602_E = 0;     //使能
        LCD1602_RS = 0;           //選擇發(fā)送命令
        LCD1602_RW = 0;           //選擇寫入
       
        LCD1602_DATAPINS = com;     //放入命令
        Lcd1602_Delay1ms(1);                //等待數(shù)據(jù)穩(wěn)定

        LCD1602_E = 1;                  //寫入時(shí)序
        Lcd1602_Delay1ms(5);          //保持時(shí)間
        LCD1602_E = 0;
}
#else
void LcdWriteCom(uchar com)          //寫入命令
{
        LCD1602_E = 0;         //使能清零
        LCD1602_RS = 0;         //選擇寫入命令
        LCD1602_RW = 0;         //選擇寫入

        LCD1602_DATAPINS = com;        //由于4位的接線是接到P0口的高四位,所以傳送高四位不用改
        Lcd1602_Delay1ms(1);

        LCD1602_E = 1;         //寫入時(shí)序
        Lcd1602_Delay1ms(5);
        LCD1602_E = 0;

//        Lcd1602_Delay1ms(1);
        LCD1602_DATAPINS = com << 4; //發(fā)送低四位
        Lcd1602_Delay1ms(1);

        LCD1602_E = 1;         //寫入時(shí)序
        Lcd1602_Delay1ms(5);
        LCD1602_E = 0;
}
#endif
/*******************************************************************************
* 函 數(shù) 名         : LcdWriteData
* 函數(shù)功能                   : 向LCD寫入一個(gè)字節(jié)的數(shù)據(jù)
* 輸    入         : dat
* 輸    出         : 無(wú)
*******************************************************************************/                  
#ifndef         LCD1602_4PINS                  
void LcdWriteData(uchar dat)                        //寫入數(shù)據(jù)
{
        LCD1602_E = 0;        //使能清零
        LCD1602_RS = 1;        //選擇輸入數(shù)據(jù)
        LCD1602_RW = 0;        //選擇寫入

        LCD1602_DATAPINS = dat; //寫入數(shù)據(jù)
        Lcd1602_Delay1ms(1);

        LCD1602_E = 1;   //寫入時(shí)序
        Lcd1602_Delay1ms(5);   //保持時(shí)間
        LCD1602_E = 0;
}
#else
void LcdWriteData(uchar dat)                        //寫入數(shù)據(jù)
{
        LCD1602_E = 0;          //使能清零
        LCD1602_RS = 1;          //選擇寫入數(shù)據(jù)
        LCD1602_RW = 0;          //選擇寫入

        LCD1602_DATAPINS = dat;        //由于4位的接線是接到P0口的高四位,所以傳送高四位不用改
        Lcd1602_Delay1ms(1);

        LCD1602_E = 1;          //寫入時(shí)序
        Lcd1602_Delay1ms(5);
        LCD1602_E = 0;

        LCD1602_DATAPINS = dat << 4; //寫入低四位
        Lcd1602_Delay1ms(1);

        LCD1602_E = 1;          //寫入時(shí)序
        Lcd1602_Delay1ms(5);
        LCD1602_E = 0;
}
#endif
/*******************************************************************************
* 函 數(shù) 名       : LcdInit()
* 函數(shù)功能                 : 初始化LCD屏
* 輸    入       : 無(wú)
* 輸    出       : 無(wú)
*******************************************************************************/                  
#ifndef                LCD1602_4PINS
void LcdInit()                                                  //LCD初始化子程序
{
        LcdWriteCom(0x38);  //開顯示
        LcdWriteCom(0x0c);  //開顯示不顯示光標(biāo)
        LcdWriteCom(0x06);  //寫一個(gè)指針加1
        LcdWriteCom(0x01);  //清屏
        LcdWriteCom(0x80);  //設(shè)置數(shù)據(jù)指針起點(diǎn)
}
#else
void LcdInit()                                                  //LCD初始化子程序
{
        LcdWriteCom(0x32);         //將8位總線轉(zhuǎn)為4位總線
        LcdWriteCom(0x28);         //在四位線下的初始化
        LcdWriteCom(0x0c);  //開顯示不顯示光標(biāo)
        LcdWriteCom(0x06);  //寫一個(gè)指針加1
        LcdWriteCom(0x01);  //清屏
        LcdWriteCom(0x80);  //設(shè)置數(shù)據(jù)指針起點(diǎn)
}
#endif

這是main函數(shù):
/*******************************************************************************
* 實(shí) 驗(yàn) 名                 : 矩陣鍵盤實(shí)驗(yàn)
* 使用的IO             : 數(shù)碼管使用P0,鍵盤使用P3.0、P3.1、P3.2、P3.3
* 實(shí)驗(yàn)效果       : 按矩陣鍵盤分別顯示在數(shù)碼管上面顯示十六進(jìn)制的0到F。
* 注    意                 :
*******************************************************************************/
#include<reg51.h>       
#include "lcd.h"
#include <lcd.c>

unsigned char PuZh[]=" Pechin Science ";

/*******************************************************************************
* 函 數(shù) 名         : main
* 函數(shù)功能                   : 主函數(shù)
* 輸    入         : 無(wú)
* 輸    出         : 無(wú)
*******************************************************************************/
void main(void)
{
        unsigned char i;
        LcdInit();
        for(i=0;i<16;i++)
        {
                LcdWriteData(PuZh[i]);       
        }
        while(1)
        {
               
        }                               
}

大家看看,我是小白求調(diào)教~

作者: lytcreate    時(shí)間: 2018-6-7 14:00
已解決,端口問(wèn)題,軟件代碼的問(wèn)題




歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1