日韩床上生活一级视频|能看毛片的操逼网站|色悠悠网站在线观看视频|国产免费观看A淫色免费|国产av久久久久久久|免费A级视频美女网站黄|国产毛片av日韩小黄片|热久久免费国产视频|中文字幕无码色色|成人在线视频99久久久

機(jī)電之家資源網(wǎng)
單片機(jī)首頁(yè)|單片機(jī)基礎(chǔ)|單片機(jī)應(yīng)用|單片機(jī)開(kāi)發(fā)|單片機(jī)文案|軟件資料下載|音響制作|電路圖下載 |嵌入式開(kāi)發(fā)
培訓(xùn)信息
贊助商
AVR單片機(jī)LCD1602顯示程序
AVR單片機(jī)LCD1602顯示程序
 更新時(shí)間:2008-7-26 19:21:37  點(diǎn)擊數(shù):3
【字體: 字體顏色
//定義MCU與LCD的接口
#define LCD_EN_PORT    PORTB
#define LCD_RW_PORT    PORTB
#define LCD_RS_PORT    PORTB
#define LCD_DATA_PORT  PORTB
#define LCD_DATA_DDR   DDRB
#define LCD_DATA_PIN   PINB

#define LCD_EN         0x01   //portd0         out
#define LCD_RW         0x02   //portd1         out/in
#define LCD_RS         0x04   //portc2         out
#define LCD_DATA       0xf0   //portd4/5/6/7   out

/*--------------------------------------------------------------------------------------------------
Public function prototypes
--------------------------------------------------------------------------------------------------*/
void LCD_init         (void);
void LCD_en_write     (void);
void dat_write_nibble(unsigned data_nibble);
void com_write_nibble(unsigned command_nibble);
void LCD_write_char   (unsigned command,unsigned data);
unsigned char LCD_wait_Ready   (void);
void LCD_set_xy       (unsigned char x, unsigned char y);
void LCD_write_string (unsigned char X,unsigned char Y,unsigned char *s);
void delay_nus        (unsigned int n);
void delay_nms        (unsigned int n);



/**********************************************************
光標(biāo)命令
LCD_write_char(0x0e,0);    //光標(biāo)開(kāi)
LCD_write_char(0x0d,0);    //光標(biāo)所在字符閃爍  
LCD_write_char(0x0c,0);    //光標(biāo)關(guān)  
**********************************************************/
/**********************************************************
TC1602B LCD DISPLAY
建立時(shí)間:2003年11月9號(hào)
修改日期:2003年11月14號(hào)
LCD_write函數(shù)功能:當(dāng)command=0時(shí),向LCD寫入數(shù)據(jù),否則向LCD寫
                   入命令
LCD第一行顯示寄存器地址:0X80-0X8F
LCD第二行顯示寄存器地址:0XC0-0XCF
**********************************************************/
void LCD_init(void)
  {
  LCD_DATA_DDR |= LCD_DATA | LCD_RW| LCD_RS   | LCD_EN;
    delay_nms(20);

com_write_nibble(0x30);
    delay_nms(5);
com_write_nibble(0x30);
    delay_nus(200);   
com_write_nibble(0x30);
delay_nus(200);  
com_write_nibble(0x20);
    LCD_write_char(0x28,0);      //4bit test
    LCD_write_char(0x0c,0);      //顯示開(kāi)
LCD_write_char(0x01,0);      //顯示清屏
//LCD_write_char(0x06,0);      //顯示光標(biāo)移動(dòng)設(shè)置
  }

void LCD_en_write(void)        //EN端產(chǎn)生一個(gè)高電平脈沖,寫LCD
  {
    LCD_EN_PORT |= LCD_EN;
    delay_nus(10);
    LCD_EN_PORT &= ~LCD_EN;
    delay_nus(10);
  }

/*-----------------------------------------------------------------------
LCD_write_char    : 英文字符串顯示函數(shù)

輸入?yún)?shù):*s      :英文字符串指針;
          X、Y    : 顯示字符串的位置,X:0-15,Y:0-1
                    LCD第一行顯示寄存器地址:0X80-0X8F
                    LCD第一行顯示寄存器地址:0XC0-0XCF

編寫日期          :2003-11-19 
最后修改日期      :2004-8-19 
-----------------------------------------------------------------------*/
void com_write_nibble(unsigned command_nibble)
{
LCD_RW_PORT &= ~LCD_RW;                 //RW=0
LCD_RS_PORT &= ~LCD_RS;            //RS=0
LCD_DATA_PORT &= 0X0F;
    LCD_DATA_PORT |= command_nibble&0xf0;//send high 4bit
    LCD_en_write();
}
void dat_write_nibble(unsigned data_nibble)
{
LCD_RW_PORT &= ~LCD_RW;                 //RW=0
LCD_RS_PORT |= LCD_RS;             //RS=1 
LCD_DATA_PORT &= 0X0F;
LCD_DATA_PORT |= data_nibble&0xf0;   //send high 4bit
    LCD_en_write();
}

void LCD_write_char(unsigned command,unsigned data)
  {
    unsigned command_temp,data_temp;        
   
    command_temp = command;
    data_temp = data;

while((LCD_wait_Ready()&0x80)!=0);   
//   LCD_wait_Ready();
   
    if (command == 0)
    {
dat_write_nibble(data_temp);
    }
    else 
    {
com_write_nibble(command_temp);
    } 
    command_temp=command_temp << 4;         //send low 4bit
    data_temp=data_temp       << 4;
   
    if (command==0)
dat_write_nibble(data_temp);
    else
com_write_nibble(command_temp);

   LCD_RW_PORT |= LCD_RW;
  
   LCD_RS_PORT ^= LCD_RS;    
}


unsigned char LCD_wait_Ready(void)                  //等待LCD空閑
  {
unsigned charbusy_bit=0;
    LCD_DATA_DDR &= ~0x80;                 //PD7 I/O口方向設(shè)置為輸入
   
    LCD_RW_PORT |= LCD_RW;                 //RW=1
   
    LCD_RS_PORT &= ~LCD_RS;                //RS=0
   
    LCD_EN_PORT |= LCD_EN;                 //EN=1
delay_nus(10);

busy_bit|=LCD_DATA_PIN;
LCD_EN_PORT &= ~LCD_EN;                //EN=0

   
//while (!( LCD_DATA_PIN&0x80 ) == 0);   //RW=1,讀PD7,為0表示空閑;
   
LCD_en_write();  
    LCD_DATA_DDR |= 0xf0;
return(busy_bit);
  }
/*-----------------------------------------------------------------------
LCD_set_xy        : 設(shè)置LCD顯示的起始位置

輸入?yún)?shù):x、y    : 顯示字符串的位置,X:0-15,Y:0-1
                    LCD第一行顯示寄存器地址:0X80-0X8F
                    LCD第一行顯示寄存器地址:0XC0-0XCF

編寫日期          :2004-8-19 
最后修改日期      :2004-8-19 
-----------------------------------------------------------------------*/
void LCD_set_xy( unsigned char x, unsigned char y )
  {
    unsigned char address;
    if (y == 0) address = 0x80 + x;
    else 
       address = 0xc0 + x;
    LCD_write_char( address, 0 );
  }
/*-----------------------------------------------------------------------
LCD_write_string  : 英文字符串顯示函數(shù)

輸入?yún)?shù):*s      :英文字符串指針;
          X、Y    : 顯示字符串的位置

編寫日期          :2004-8-19 
最后修改日期      :2004-8-19 
-----------------------------------------------------------------------*/
void LCD_write_string(unsigned char X,unsigned char Y,unsigned char *s)
{
if((X<16)&(Y<2))
    LCD_set_xy( X, Y );
    
    while (*s) 
    {
        LCD_write_char( 0, *s );
s ++;
    }
}
void LCD_write_space(unsigned char X,unsigned char Y,unsigned char num)
{
unsigned char i;

if((X<16)&(Y<2))
    LCD_set_xy( X, Y );

for(i=0;i<num;i++)
{
LCD_write_char(0,’ ’);
}
}
/*-----------------------------------------------------------------------
延時(shí)函數(shù)
系統(tǒng)時(shí)鐘:8M
-----------------------------------------------------------------------*/
void delay_1us(void)                 //1us延時(shí)函數(shù)
  {
   asm("nop");
  }

void delay_nus(unsigned int n)       //N us延時(shí)函數(shù)
  {
   unsigned int i=0;
   for (i=0;i<n;i++)
   delay_1us();
  }
  
void delay_1ms(void)                 //1ms延時(shí)函數(shù)
  {
   unsigned int i;
   for (i=0;i<1140;i++);
  }
  
void delay_nms(unsigned int n)       //N ms延時(shí)函數(shù)
  {
   unsigned int i=0;
   for (i=0;i<n;i++)
   delay_1ms();
  } 
  • 上一篇: AVR單片機(jī)讀寫EEPROM子程序
  • 下一篇: AVR單片機(jī)ADC轉(zhuǎn)換程序
  • 發(fā)表評(píng)論   告訴好友   打印此文  收藏此頁(yè)  關(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