機(jī)電之家資源網(wǎng)
單片機(jī)首頁|單片機(jī)基礎(chǔ)|單片機(jī)應(yīng)用|單片機(jī)開發(fā)|單片機(jī)文案|軟件資料下載|音響制作|電路圖下載 |嵌入式開發(fā)
培訓(xùn)信息
贊助商
ATMEGA8單片機(jī)驅(qū)動(dòng)LCD1602液晶C程序
ATMEGA8單片機(jī)驅(qū)動(dòng)LCD1602液晶C程序
 更新時(shí)間:2009-11-30 16:16:02  點(diǎn)擊數(shù):0
【字體: 字體顏色

調(diào)試過程總結(jié)一下:
1)由于找不到de1620資料,不知道它de操作時(shí)序?吹揭恍┨诱f16201602沒區(qū)別,還shi有點(diǎn)將信將疑。后面用網(wǎng)友編寫de1602程序試驗(yàn),才知道它們倆沒啥區(qū)別。
2)關(guān)于shi否檢測(cè)LCD處于空閑,覺得還shi最好在寫指令和寫數(shù)據(jù)時(shí)都加上。不檢測(cè),會(huì)導(dǎo)致顯示結(jié)果不正常。
3)由于連線很多14根(加上背光就shi16根),如果有一根除了問題就會(huì)影響到顯示。所以我shi用萬用表一根線一根線量了之后才通電試驗(yàn)de。
4)看到很多帖子都建議在初始化LCD前延時(shí)幾十毫秒。本例中并沒有延時(shí),顯示也正常。
5)總de說來,不shi很難。在遇到問題時(shí)只要能夠仔細(xì)分析,就能找出原因和解決方法。自己前幾天在試程序時(shí)就shi顯示結(jié)果不對(duì),一直在檢查自己de程序,沒想到原來shi單片機(jī)deFlash到壽命了……那個(gè)郁悶啊…………

頭文件中有五個(gè)函數(shù):

Function1.unsigned char AskBusy(void)          詢問LCD1620shi否空閑, 返回值"1"空閑, "0"為忙;
Function2.WritEDAta(char data)                        寫數(shù)據(jù)到LCD1620;
Function3.WriteCommand(char command)     寫指令到LCD1620;                         
Function4.PutOneCharLCD(x,y,*Disp)             在LCD1620上顯示一個(gè)字符, x,y定義位置
Function5.PutStringLCD(a,b,*DispString)       在LCD1620上顯示一個(gè)字符串, a,b定義初始位置.                                                                                         Function6.LCD_Init()                                           初始化LCD

在使用此頭文件之前,必須有以下宏定義:

#define DataPortPullup PORTx                
#define DataPortDirection DDRx            
#define ReadLCDPin PINx                                                                                                                       
#define CtrlPortPullup PORTx                  
#define CtrlPortDirection DDRx               
#define RS_Bit Px?                           
#define RW_Bit Px?                            
#define E_Bit Px? 

其中“x”代表B,C,D(mega8中無A口),"?"代表“0~1”

完整頭文件如下(解釋搞了一大堆,也學(xué)學(xué)用英語……):

/*******************************************************/
/*     Project     :1620 Display Headfile              */
/*     Date        :2008,3,11                          */
/*     Author      :lhy                                */
/*     Version     :v1.1                               */
/*     Rework      :lhy                                */
/*     Rework Date :2008.4.28                          */
/*                                                     */
/*     Comments    :                                   */
/*     1 Function Description                          */
/*         Function1.unsigned char AskBusy(void)       */
/*           --Calls this Fun to ask if LCD is busy,   */
/*           1--free,0--busy;                          */
/*         Function2.WritEDAta(char data)              */
/*           --Write data to LCD                       */
/*         Function3.WriteCommand(char command)        */
/*           --write command to LCD                    */
/*         Function4.PutOneCharLCD(x,y,*Disp)          */
/*           --Put one char to LCD.                    */
/*         Function5.PutStringLCD(a,b,*DispString)     */
/*           --Put String to LCD. Max length 32bits.   */
/*                                                     */
/*     2 How to use                                    */
/*         Before use this HeadFile,                   */
/*       need to define as below:                      */
/*         #define DataPortPullup PORTx                */
/*         #define DataPortDirection DDRx              */
/*         #define ReadLCDPin PINx                     */
/*         #define CtrlPortPullup PORTx                */
/*         #define CtrlPortDirection DDRx              */
/*         #define RS_Bit Px?                          */
/*         #define RW_Bit Px?                          */
/*         #define E_Bit Px?                           */
/*             x -> A,B,C,D...                       */
/*             ? -> 0,1,2,3,4,5,6,7                  */
/*         And set control port output,like this:      */
/*         CtrlPortDirection |=                        */
/*             (1<<E_Bit) | (1<<RS_Bit) | (1<<RW_Bit); */
/*     3 Add the Function "LCD_Init(void)"             */
/*         Before this Fun, you must set control port  */
/*       output.                                       */
/*******************************************************/

#ifndef _LCD1620_h
#define _LCD1620_h

//Calls this Fun to ask if LCD is busy,1--free,0--busy
unsigned char AskBusy(void)
{
  DataPortDirection = 0x00;
  DataPortPullup = 0xff;//enable pullup res
  CtrlPortPullup |= (1<<RW_Bit);//Set RW
  CtrlPortPullup &= ~(1<<RS_Bit);//Clear RS
  CtrlPortPullup |= (1<<E_Bit);//set enable pin
  if((ReadLCDPin & (1<<7))==0)
    return 1;//lcd is free
  else
    return 0;//lcd is busy
}

//wait until LCD was free
void WaitForFree(void)
{
  unsigned char BusyBit;
  do
   {
   BusyBit = AskBusy();
   }
  while(BusyBit !=1);
  CtrlPortPullup &= (~1<<E_Bit);//Clear enable pin
}

//Write data to LCD
void WritEDAta(char data)
{
  WaitForFree();
  CtrlPortPullup &= ~(1<<RW_Bit);//Clear RW
  CtrlPortPullup |= (1<<RS_Bit);//Set RS
 
  DataPortDirection = 0xff;
  DataPortPullup = data;
  CtrlPortPullup |= (1<<E_Bit);//set E
  CtrlPortPullup &= ~(1<<E_Bit);//Clear E
}

//write command to LCD
void WriteCommand(char command)
{
  WaitForFree();
  CtrlPortPullup &= ~(1<<RS_Bit);//Clear RS
  CtrlPortPullup &= ~(1<<RW_Bit);//Clear RW
  DataPortDirection |= 0xff;
  DataPortPullup = command;
 
  CtrlPortPullup |= (1<<E_Bit);//set E
  CtrlPortPullup &= ~(1<<E_Bit);//Clear E
}

//Write one char to LCD.
void PutOneCharLCD(unsigned char x,unsigned char y,unsigned char *Disp)
{
  unsigned char Location;
  if(x == 0)
    {
    Location = y | (1<<7);
    }
  else if(x == 1)
    {
    Location = y | (1<<6) |(1<<7);
    }
  WriteCommand(Location);
  WritEDAta(*Disp);
}

//Write String to LCD. Max length 32bits(determined by the location of the first byte).
void PutStringLCD(unsigned char a,unsigned char b,unsigned char *DispString)
{
  do
    {
    PutOneCharLCD(a,b,DispString);
    b ++;
    DispString ++;
    }
  while((b <= 15) && (*DispString));
 
  if(*DispString)
    {
    a ++;//Jump to 2nd row.
    b = 0;
    do
      {
      PutOneCharLCD(a,b,DispString);
      b ++;
      DispString ++;
      }
    while((b <= 15) && (*DispString));
    }
}

//Initialize the LCD1620
void LCD_Init(void)
{
  WriteCommand(0x38);//16X2,5X7 lattice,8 bit for transmit
  WriteCommand(0x08);//Close the display and cursor
  WriteCommand(0x0f);//Set cursor and enable dispaly
  WriteCommand(0x06);
  WriteCommand(0x01);//Clear the screen
}

#endif

把此頭文件包含到主程序中:

/*******************************************************/
/*     Project   :1620 Display Function                */
/*     Compiler  :ICCAVR 6.03A                         */
/*     Date      :2008,4,28                           */
/*     Author    :lhy                                  */
/*     Chip type :Atmega8                              */
/*     Comments  :                                     */
/*******************************************************/

#i nclude "iom8v.h"

#define DataPortPullup PORTD
#define DataPortDirection DDRD
#define ReadLCDPin PIND
#define CtrlPortPullup PORTC
#define CtrlPortDirection DDRC
#define RS_Bit PC0
#define RW_Bit PC1
#define E_Bit PC2

#i nclude "LCD1620.h"

void main()
{
  OSCCAL=0XA5;
  CtrlPortDirection |= ((1<<E_Bit)|(1<<RS_Bit)|(1<<RW_Bit));
  LCD_Init();
  PutStringLCD(0,0,"Li Heng Yuan is a good boy!!");
}

  • 上一篇: ATMEGA8單片機(jī)驅(qū)動(dòng)4線步進(jìn)機(jī)C程序
  • 下一篇: 沒有了
  • 發(fā)表評(píng)論   告訴好友   打印此文  收藏此頁  關(guān)閉窗口  返回頂部
    熱點(diǎn)文章
     
    推薦文章
     
    相關(guān)文章
    網(wǎng)友評(píng)論:(只顯示最新5條。)
    關(guān)于我們 | 聯(lián)系我們 | 廣告合作 | 付款方式 | 使用幫助 | 機(jī)電之家 | 會(huì)員助手 | 免費(fèi)鏈接

    點(diǎn)擊這里給我發(fā)消息66821730(技術(shù)支持)點(diǎn)擊這里給我發(fā)消息66821730(廣告投放) 點(diǎn)擊這里給我發(fā)消息41031197(編輯) 點(diǎn)擊這里給我發(fā)消息58733127(審核)
    本站提供的機(jī)電設(shè)備,機(jī)電供求等信息由機(jī)電企業(yè)自行提供,該企業(yè)負(fù)責(zé)信息內(nèi)容的真實(shí)性、準(zhǔn)確性和合法性。
    機(jī)電之家對(duì)此不承擔(dān)任何保證責(zé)任,有侵犯您利益的地方請(qǐng)聯(lián)系機(jī)電之家,機(jī)電之家將及時(shí)作出處理。
    Copyright 2007 機(jī)電之家 Inc All Rights Reserved.機(jī)電之家-由機(jī)電一體化網(wǎng)更名-聲明
    電話:0571-87774297 傳真:0571-87774298
    杭州濱興科技有限公司提供技術(shù)支持

    主辦:杭州市高新區(qū)(濱江)機(jī)電一體化學(xué)會(huì)
    中國(guó)行業(yè)電子商務(wù)100強(qiáng)網(wǎng)站

    網(wǎng)站經(jīng)營(yíng)許可證:浙B2-20080178-1