找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

搜索
查看: 5737|回復(fù): 0
打印 上一主題 下一主題
收起左側(cè)

0802LCD 4位顯示程序 PIC單片機(jī)C語言程序

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
0802的LCD顯示屏 PIC單片機(jī)程序



  1. #include <xc.h>
  2. #include<pic.h>
  3. //#include"head.h"
  4. #define uchar unsigned char
  5. #define uint unsigned int
  6. #pragma config FOSC = XT        // Oscillator Selection bits (XT oscillator)
  7. #pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled)
  8. #pragma config PWRTE = ON       // Power-up Timer Enable bit (PWRT enabled)
  9. #pragma config BOREN = OFF      // Brown-out Reset Enable bit (BOR disabled)
  10. #pragma config LVP = ON        // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
  11. #pragma config CPD = OFF        // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
  12. #pragma config WRT = OFF        // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
  13. #pragma config CP = OFF         // Flash Program Memory Code Protection bit (Code protection off)

  14. //char tab[]="0123456789";
  15. #define uchar unsigned char
  16. #define uint unsigned int
  17. void LCD_write_cmd(uchar command);
  18. void LCD_write_command(uchar command);
  19. void LCD_en_write(void);
  20. void LCD_set_xy( unsigned char x, unsigned char y );
  21. void LCD_write_string(unsigned char X,unsigned char Y,unsigned char *s);
  22. void LCD_write_data(unsigned char data);
  23. void delay_nus(unsigned int n);
  24. void delay_nms(unsigned int n);   
  25. #define RS RC2
  26. #define EN RC3
  27. #define DB7 RC7
  28. #define DB6 RC6
  29. #define DB5 RC5
  30. #define DB4 RC4
  31. #define LCD_DATA_PORT PORTC
  32. #define LCD_DATA_TRIS TRISC
  33.    
  34.     /*------------------------------------------------------------------------------
  35.     函數(shù)說明
  36.     ------------------------------------------------------------------------------*/
  37. void LCD_init()
  38. {
  39.     TRISD=0x00; //數(shù)據(jù)口方向?yàn)檩敵?
  40.     PORTD=0x00;
  41.     TRISC=0x00; //數(shù)據(jù)口方向?yàn)檩敵?
  42.     PORTC=0x00;//設(shè)置EN、RS/數(shù)據(jù)為輸出
  43.       
  44.     LCD_write_cmd(0x30);//4位的指令
  45.     delay_nms(5);
  46.     LCD_write_cmd(0x30);
  47.     delay_nus(200);  
  48.     LCD_write_cmd(0x30);
  49.     delay_nms(1);
  50.     LCD_write_cmd(0x20);
  51.     LCD_write_cmd(0x20); //4位顯示
  52.     LCD_write_cmd(0x80);

  53.     LCD_write_cmd(0x00); //顯示開 游標(biāo)、閃爍不顯示
  54.     LCD_write_cmd(0x80);
  55.    
  56.     LCD_write_cmd(0x00); //清屏
  57.     LCD_write_cmd(0x01);
  58.    
  59.     LCD_write_cmd(0x00); //兩行 5*7
  60.     LCD_write_cmd(0xc0);
  61. }
  62. void LCD_write_cmd(uchar command) //寫指令
  63. {
  64.     delay_nus(10);
  65.     RS=0;
  66.     EN=0;//使能清零
  67.     LCD_DATA_PORT&=0x0f; //清除端口
  68.     delay_nus(2);
  69.     EN=1;
  70.     LCD_DATA_PORT |= (command & 0xf0);//高4位不用改
  71.     delay_nus(2);
  72.     EN=0;
  73. }

  74. void LCD_write_command(uchar command) //寫指令
  75. {
  76.     LCD_write_cmd(command);
  77.     LCD_write_cmd(command<<4);
  78. }

  79. void LCD_write_data(unsigned char data) //寫數(shù)據(jù)
  80. {
  81.     delay_nus(10);
  82.     RS=1;
  83.     EN=0;//使能清零
  84.     LCD_DATA_PORT&=0x0f;
  85.     EN=1;
  86.     LCD_DATA_PORT |= (data & 0xf0);//高4位不用改
  87.     delay_nus(2);
  88.     EN=0;
  89.     delay_nus(2);
  90.    
  91.     LCD_DATA_PORT&=0x0F; //清低四位
  92.     EN=1;
  93.     LCD_DATA_PORT |= ((data << 4) & 0xf0);//發(fā)送低4位
  94.     delay_nus(2);
  95.     EN=0;
  96.   }

  97. void LCD_write_string(unsigned char X,unsigned char Y,unsigned char *s) //列x=0~15,行y=0,1
  98. {
  99.     LCD_set_xy( X, Y ); //寫地址
  100.     while (*s) // 寫顯示字符
  101.     {
  102.         LCD_write_data( *s );
  103.         s ++;
  104.     }
  105. }

  106. void LCD_set_xy( unsigned char x, unsigned char y )  //寫地址函數(shù)
  107. {
  108.     unsigned char address;
  109.     if (y == 0) address = 0x80 + x;
  110.     else        address = 0xc0 + x;
  111.     LCD_write_command( address);
  112.    
  113. }

  114. void delay_nms(unsigned int n) //N ms延時(shí)函數(shù)
  115. {
  116.     uint a,b;
  117.     for(a=n;a>0;a--)
  118.         for(b=80;b>0;b--);
  119. }

  120. void delay_nus(unsigned int n) //N us延時(shí)函數(shù)
  121. {
  122.     unsigned int i;
  123.     for (i=0;i<n/4;i++);
  124. }
  125. void main()
  126. {
  127.      LCD_init();
  128. //    LCD_write_command(0x0d); //光標(biāo)開
  129.      while(1)
  130.      {
  131.          RD0=1;
  132.          delay_nms(1000);
  133.          RD0=0;
  134.          delay_nms(1000);
  135.         LCD_write_string(0,0,"ceshiLCD");
  136.         LCD_write_string(0,1,"hahahaha");
  137.         delay_nms(2000);
  138.         
  139.      }
  140. }
復(fù)制代碼


try.rar

1.4 KB, 下載次數(shù): 23, 下載積分: 黑幣 -5

PIC單片機(jī)

評(píng)分

參與人數(shù) 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎(jiǎng)勵(lì)!

查看全部評(píng)分

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩

相關(guān)帖子

回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表