|
0802的LCD顯示屏 PIC單片機程序
- #include <xc.h>
- #include<pic.h>
- //#include"head.h"
- #define uchar unsigned char
- #define uint unsigned int
- #pragma config FOSC = XT // Oscillator Selection bits (XT oscillator)
- #pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
- #pragma config PWRTE = ON // Power-up Timer Enable bit (PWRT enabled)
- #pragma config BOREN = OFF // Brown-out Reset Enable bit (BOR disabled)
- #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)
- #pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
- #pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
- #pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
-
- //char tab[]="0123456789";
- #define uchar unsigned char
- #define uint unsigned int
- void LCD_write_cmd(uchar command);
- void LCD_write_command(uchar command);
- void LCD_en_write(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 LCD_write_data(unsigned char data);
- void delay_nus(unsigned int n);
- void delay_nms(unsigned int n);
- #define RS RC2
- #define EN RC3
- #define DB7 RC7
- #define DB6 RC6
- #define DB5 RC5
- #define DB4 RC4
- #define LCD_DATA_PORT PORTC
- #define LCD_DATA_TRIS TRISC
-
- /*------------------------------------------------------------------------------
- 函數說明
- ------------------------------------------------------------------------------*/
- void LCD_init()
- {
- TRISD=0x00; //數據口方向為輸出
- PORTD=0x00;
- TRISC=0x00; //數據口方向為輸出
- PORTC=0x00;//設置EN、RS/數據為輸出
-
- LCD_write_cmd(0x30);//4位的指令
- delay_nms(5);
- LCD_write_cmd(0x30);
- delay_nus(200);
- LCD_write_cmd(0x30);
- delay_nms(1);
- LCD_write_cmd(0x20);
- LCD_write_cmd(0x20); //4位顯示
- LCD_write_cmd(0x80);
- LCD_write_cmd(0x00); //顯示開 游標、閃爍不顯示
- LCD_write_cmd(0x80);
-
- LCD_write_cmd(0x00); //清屏
- LCD_write_cmd(0x01);
-
- LCD_write_cmd(0x00); //兩行 5*7
- LCD_write_cmd(0xc0);
- }
- void LCD_write_cmd(uchar command) //寫指令
- {
- delay_nus(10);
- RS=0;
- EN=0;//使能清零
- LCD_DATA_PORT&=0x0f; //清除端口
- delay_nus(2);
- EN=1;
- LCD_DATA_PORT |= (command & 0xf0);//高4位不用改
- delay_nus(2);
- EN=0;
- }
-
- void LCD_write_command(uchar command) //寫指令
- {
- LCD_write_cmd(command);
- LCD_write_cmd(command<<4);
- }
-
- void LCD_write_data(unsigned char data) //寫數據
- {
- delay_nus(10);
- RS=1;
- EN=0;//使能清零
- LCD_DATA_PORT&=0x0f;
- EN=1;
- LCD_DATA_PORT |= (data & 0xf0);//高4位不用改
- delay_nus(2);
- EN=0;
- delay_nus(2);
-
- LCD_DATA_PORT&=0x0F; //清低四位
- EN=1;
- LCD_DATA_PORT |= ((data << 4) & 0xf0);//發送低4位
- delay_nus(2);
- EN=0;
- }
- void LCD_write_string(unsigned char X,unsigned char Y,unsigned char *s) //列x=0~15,行y=0,1
- {
- LCD_set_xy( X, Y ); //寫地址
- while (*s) // 寫顯示字符
- {
- LCD_write_data( *s );
- s ++;
- }
- }
- 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_command( address);
-
- }
- void delay_nms(unsigned int n) //N ms延時函數
- {
- uint a,b;
- for(a=n;a>0;a--)
- for(b=80;b>0;b--);
- }
- void delay_nus(unsigned int n) //N us延時函數
- {
- unsigned int i;
- for (i=0;i<n/4;i++);
- }
- void main()
- {
- LCD_init();
- // LCD_write_command(0x0d); //光標開
- while(1)
- {
- RD0=1;
- delay_nms(1000);
- RD0=0;
- delay_nms(1000);
- LCD_write_string(0,0,"ceshiLCD");
- LCD_write_string(0,1,"hahahaha");
- delay_nms(2000);
-
- }
- }
復制代碼
|
-
-
try.rar
2016-4-19 09:42 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
1.4 KB, 下載次數: 23, 下載積分: 黑幣 -5
PIC單片機
評分
-
查看全部評分
|