找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 4914|回复: 0
打印 上一主题 下一主题
收起左侧

0802LCD 4位显示程序 PIC单片机C语言程序

[复制链接]
跳转到指定楼层
楼主
0802的LCD显示屏 PIC单片机程序



  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.     函数说明
  36.     ------------------------------------------------------------------------------*/
  37. void LCD_init()
  38. {
  39.     TRISD=0x00; //数据口方向为输出
  40.     PORTD=0x00;
  41.     TRISC=0x00; //数据口方向为输出
  42.     PORTC=0x00;//设置EN、RS/数据为输出
  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); //显示开 游标、闪烁不显示
  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) //写数据
  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);//发送低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 )  //写地址函数
  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延时函数
  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延时函数
  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); //光标开
  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. }
复制代码


try.rar

1.4 KB, 下载次数: 23, 下载积分: 黑币 -5

PIC单片机

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表