这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » DIY与开源设计 » 电子DIY » s474133362的MCU DIY进程贴(板子基本焊完,进入调试阶段)

共11条 2/2 1 2 跳转至
助工
2011-12-12 11:22:37     打赏
11楼
lcd1602 C51程序
注:显示“I LOVE EEPW MCU   ID:s474133362”
#include <reg52.h>
#include <intrins.h>

#define uchar unsigned char                                               
#define uint unsigned int                                                 
                                                                          
sbit LCD_RS = P1^0;                                                       
sbit LCD_RW = P1^1;                                                       
sbit LCD_EN = P1^2;                                                       
                                                                          
uchar code dis1[] = {"I LOVE EEPW MCU"};                                 
uchar code dis2[] = {"ID:s474133362"};                                 
                                                                          
                                                                          
/*******************************************************************/     
/*                                                                        
/* 延时子程序                                                             
/*                                                                        
/*******************************************************************/     
char code SST516[3] _at_ 0x003b;                                                                        
void delay(int ms)                                                        
{                                                                         
   int i;                                                                 
   while(ms--)                                                            
   {                                                                      
     for(i = 0; i< 250; i++)                                              
     {                                                                    
      _nop_();                                                            
      _nop_();                                                            
      _nop_();                                                            
      _nop_();                                                            
     }                                                                    
   }                                                                      
}                                                                         
                                                                          
/*******************************************************************/     
/*                                                                        
/*检查LCD忙状态                                                           
/*lcd_busy为1时,忙,等待。lcd-busy为0时,闲,可写指令与数据。             
/*                                                                        
/*******************************************************************/     
                                                                          
bit lcd_busy()                                                            
{                                                                         
    bit result;                                                           
    LCD_RS = 0;                                                           
    LCD_RW = 1;                                                           
    LCD_EN = 1;                                                           
    _nop_();                                                              
    _nop_();                                                              
    _nop_();                                                              
    _nop_();                                                              
    result = (bit)(P0&0x80);                                             
    LCD_EN = 0;                                                           
    return result;                                                        
}                                                                         
                                                                          
/*******************************************************************/     
/*                                                                        
/*写指令数据到LCD                                                         
/*RS=L,RW=L,E=高脉冲,D0-D7=指令码。                                    
/*                                                                        
/*******************************************************************/     
                                                                          
void lcd_wcmd(uchar cmd)                                                  
{                                                                         
   while(lcd_busy());                                                     
    LCD_RS = 0;                                                           
    LCD_RW = 0;                                                           
    LCD_EN = 0;                                                           
    _nop_();                                                              
    _nop_();                                                              
    P0 = cmd;                                                             
    _nop_();                                                              
    _nop_();                                                              
    _nop_();                                                              
    _nop_();                                                              
    LCD_EN = 1;                                                           
    _nop_();                                                              
    _nop_();                                                              
    _nop_();                                                              
    _nop_();                                                              
    LCD_EN = 0;                                                           
}                                                                         
                                                                          
/*******************************************************************/     
/*                                                                        
/*写显示数据到LCD                                                         
/*RS=H,RW=L,E=高脉冲,D0-D7=数据。                                      
/*                                                                        
/*******************************************************************/     
                                                                          
void lcd_wdat(uchar dat)                                                  
{                                                                         
   while(lcd_busy());                                                     
    LCD_RS = 1;                                                           
    LCD_RW = 0;                                                           
    LCD_EN = 0;                                                           
    P0 = dat;                                                             
    _nop_();                                                              
    _nop_();                                                              
    _nop_();                                                              
    _nop_();                                                              
    LCD_EN = 1;                                                           
    _nop_();                                                              
    _nop_();                                                              
    _nop_();                                                              
    _nop_();                                                              
    LCD_EN = 0;                                                           
}                                                                         
                                                                          
/*******************************************************************/     
/*                                                                        
/*  设定显示位置                                                          
/*                                                                        
/*******************************************************************/     
                                                                          
void lcd_pos(uchar pos)                                                   
{                                                                         
   lcd_wcmd(pos|0x80);      //数据指针=80+地址变量                        
}                                                                         
                                                                          
/*******************************************************************/     
/*                                                                        
/*  LCD初始化设定                                                         
/*                                                                        
/*******************************************************************/     
                                                                          
void lcd_init()                                                           
{                                                                         
    delay(15);                     //等待LCD电源稳定                      
    lcd_wcmd(0x38);          //16*2显示,5*7点阵,8位数据                 
    delay(5);                                                             
    lcd_wcmd(0x38);                                                       
    delay(5);                                                             
    lcd_wcmd(0x38);                                                       
    delay(5);                                                             
                                                                          
    lcd_wcmd(0x0c);          //显示开,关光标                             
    delay(5);                                                             
    lcd_wcmd(0x06);          //移动光标                                   
    delay(5);                                                             
    lcd_wcmd(0x01);          //清除LCD的显示内容                          
    delay(5);                                                             
}                                                                         
                                                                          
/*******************************************************************/     
/*                                                                        
/*  清屏子程序                                                            
/*                                                                        
/*******************************************************************/     
                                                                          
void lcd_clr()                                                            
{                                                                         
    lcd_wcmd(0x01);          //清除LCD的显示内容                          
    delay(5);                                                             
}                                                                         
                                                                          
/*******************************************************************/     
/*                                                                        
/*  闪动子程序                                                            
/*                                                                        
/*******************************************************************/     
                                                                          
void flash()                                                              
{                                                                         
    delay(600);                     //控制停留时间                        
    lcd_wcmd(0x08);            //关闭显示                                 
    delay(200);                    //延时                                 
    lcd_wcmd(0x0c);            //开显示                                   
    delay(200);                                                           
    lcd_wcmd(0x08);            //关闭显示                                 
    delay(200);                     //延时                                
    lcd_wcmd(0x0c);            //开显示                                   
    delay(200);                                                           
}                                                                         
                                                                          
/*******************************************************************/     
/*                                                                        
/*  主程序                                                                
/*                                                                        
/*******************************************************************/     
                                                                          
main()                                                                    
{                                                                         
    uchar i,j;                                                            
    delay(10);                                                            
    lcd_init();                          //初始化LCD                      
                                                                          
    while(1)                                                              
   {                                                                      
      lcd_clr();                                                          
                                                                          
      lcd_pos(0x10);               //设置显示位置为第一行第17列           
      i = 0;                                                              
                                                                          
     while(dis1[i] != '\0')                                               
     {                                      //显示字符"         "  
       lcd_wdat(dis1[i]);                                                 
       i++;                                                               
     }                                                                    
                                                                          
      lcd_pos(0x50);              //设置显示位置为第二行第17列            
      i = 0;                                                              
                                                                          
     while(dis2[i] != '\0')                                               
     {                                                                    
       lcd_wdat(dis2[i]);         //显示字符"        "            
       i++;                                                               
     }                                                                    
                                                                          
     for(j=0;j<16;j++)           //向左移动16格                          
     {                                                                    
       lcd_wcmd(0x18);           //字符同时左移一格                       
       delay(100);                   //控制移动时间                       
     }                                                                    
                                                                          
      flash();                            //闪动二次                      
                                                                          
   }                                                                      
}

共11条 2/2 1 2 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册 ]