專注差異化嵌入式產(chǎn)品解決方案 給智能產(chǎn)品定制注入靈魂給予生命
提供開發(fā)工具、應用測試 完善的開發(fā)代碼案例庫分享
從全面的產(chǎn)品導入到強大技術(shù)支援服務 全程貼心伴隨服務,創(chuàng)造無限潛能!
十年專注單片機方案開發(fā)的方案公司英銳恩,分享CCSC LCD.c程序驅(qū)動MCD2demo LCD顯示。英銳恩現(xiàn)提供服務產(chǎn)品涉及主控芯片:8位單片機、16位單片機、32位單片機及各類運算放大器等。
/*=============================================================================
LCD第一行顯示"01234567890:;<=>?"; 第二行顯示"PIC16F877A CCSC "
=============================================================================*/
///////////////////////////////////////////////////////////////////////////////
// MPLAB IDE V7.11 + CCS C3.18
// LCD for MCD2-demo
//
// by LW7807@163.com
// 2005/06/20
///////////////////////////////////////////////////////////////////////////////
#include "16F877A.h" // PIC16F877 header file
#use delay(clock=4000000) // for 4Mhz crystal
#fuses XT, NOWDT, NOPROTECT, NOLVP // for debug mode
#define PORT_A 0 // define for function output()
#define PORT_B 1
#define PORT_C 2
#define PORT_D 3
#define PORT_E 4
#define NCHAR_PER_LINE 16 // max char numbers per line
#define LCD_RS PIN_A1
#define LCD_RW PIN_A2
#define LCD_E PIN_A3
#define LCD_DAT PORT_C
/////////////////////////////////////////////////////////// output()
//lcd data bus output
void output(int8 port, int8 dat)
{
switch(port)
{
case PORT_A: output_a(dat); break;
case PORT_B: output_b(dat); break;
case PORT_C: output_c(dat); break;
case PORT_D: output_d(dat); break;
case PORT_E: output_e(dat); break;
default : //??? port maybe error!
break;
}
}//end output()
/////////////////////////////////////////////////////////// lcd_write_cmd()
//
void lcd_write_cmd(int8 cmd)
{
delay_us(400);
output_low(LCD_RS);
output_low(LCD_RW);
output(LCD_DAT, cmd);
output_high(LCD_E);
delay_us(400);
output_low(LCD_E);
}//end lcd_write_cmd()
/////////////////////////////////////////////////////////// lcd_write_dat()
//
void lcd_write_dat(int8 dat)
{
delay_us(400);
output_high(LCD_RS);
output_low(LCD_RW);
output(LCD_DAT, dat);
output_high(LCD_E);
delay_us(400);
output_low(LCD_E);
}//end lcd_write_dat()
/////////////////////////////////////////////////////////// lcd_init()
//
void lcd_init(void)
{
output_low(LCD_E); // Let LCD E line low
lcd_write_cmd(0x38); // LCD 16x2, 5x7, 8bits data
delay_ms(15);
lcd_write_cmd(0x01); // Clear LCD display
delay_ms(10);
lcd_write_cmd(0x0f); // Open display & current
delay_ms(10);
lcd_write_cmd(0x06); // Window fixed
delay_ms(10);
}//end lcd_init()
/////////////////////////////////////////////////////////// lcd_display_char()
//
void lcd_display_char(int8 line, int8 pos, int8 ch)
{
int8 tmp;
line = (line==0) ? 0 : 1;
pos = (pos >NCHAR_PER_LINE) ? NCHAR_PER_LINE : pos;
tmp = 0x80 + 0x40*line + pos;
lcd_write_cmd(tmp);
lcd_write_dat(ch);
}//end lcd_display_char()
/////////////////////////////////////////////////////////// lcd_display_str()
//
void lcd_display_str(int8 line, char str[])
{
int8 i;
for(i=0; i<nchar_per_line;> {
lcd_display_char(line, i, ' ');
}
for(i=0; i<nchar_per_line;> {
if(str == '\0') break;
lcd_display_char(line, i, str);
}
}//end lcd_display_str()
/*===========================================================================*/
void main(void)
{
char LINE2[] = { "PIC16F877A CCSC LCD.C" };
int8 i;
lcd_init();
for(i=0; i<nchar_per_line;> {
lcd_display_char(0, i, '0'+i);
}
//lcd_display_str(1, "PIC16F877A CCSC LCD.C") //CCSC can't compiler
lcd_display_str(1, LINE2);
}//end main()
附上程序lst文件
CCS PCM C Compiler, Version 3.187, 16465
Filename: ccsc_LCD.LST
ROM used: 323 (4%)
Largest free fragment is 2048
RAM used: 30 (17%) at main() level
40 (23%) worst case
Stack: 4 locations
*
0000: MOVLW 00
0001: MOVWF 0A
0002: GOTO 0F8
0003: NOP
.................... /*=============================================================================
.................... LCD第一行顯示"01234567890:;<=>??; 第二行顯示"PIC16F877A CCSC "
.................... =============================================================================*/
.................... ///////////////////////////////////////////////////////////////////////////////
.................... // MPLAB IDE V7.11 + CCS C3.18
.................... // LCD for MCD2-demo
.................... //
.................... // by LW7807@163.com
.................... // 2005/06/20
.................... ///////////////////////////////////////////////////////////////////////////////
.................... #include "16F877A.h" // PIC16F877
header file
.................... //////// Standard Header file for the PIC16F877A device ////////////////
.................... #device PIC16F877A
.................... #list
....................
.................... #use delay(clock=4000000) // for 4Mhz crystal
*
005E: MOVLW 38
005F: MOVWF 04
0060: MOVF 00,W
0061: BTFSC 03.2
0062: GOTO 072
0063: MOVLW 01
0064: MOVWF 78
0065: CLRF 77
0066: DECFSZ 77,F
0067: GOTO 066
0068: DECFSZ 78,F
0069: GOTO 065
006A: MOVLW 4A
006B: MOVWF 77
006C: DECFSZ 77,F
006D: GOTO 06C
006E: NOP
006F: NOP
0070: DECFSZ 00,F
0071: GOTO 063
0072: RETLW 00
.................... #fuses XT, NOWDT, NOPROTECT, NOLVP // for debug mode
....................
.................... #define PORT_A 0 // define for function output()
.................... #define PORT_B 1
.................... #define PORT_C 2
.................... #define PORT_D 3
.................... #define PORT_E 4
....................
....................
.................... #define NCHAR_PER_LINE 16 // max char numbers per line
....................
.................... #define LCD_RS PIN_A1
.................... #define LCD_RW PIN_A2
.................... #define LCD_E PIN_A3
.................... #define LCD_DAT PORT_C
....................
.................... /////////////////////////////////////////////////////////// output()
.................... //lcd data bus output
.................... void output(int8 port, int8 dat)
.................... {
.................... switch(port)
.................... {
*
0004: MOVF 41,W
0005: MOVWF 77
0006: MOVF 77,F
0007: BTFSC 03.2
0008: GOTO 01A
0009: MOVLW 01
000A: SUBWF 77,W
000B: BTFSC 03.2
000C: GOTO 021
000D: MOVLW 02
000E: SUBWF 77,W
000F: BTFSC 03.2
0010: GOTO 028
0011: MOVLW 03
0012: SUBWF 77,W
0013: BTFSC 03.2
0014: GOTO 030
0015: MOVLW 04
0016: SUBWF 77,W
0017: BTFSC 03.2
0018: GOTO 037
0019: GOTO 03E
.................... case PORT_A: output_a(dat); break;
001A: MOVLW 00
001B: BSF 03.5
001C: MOVWF 05
001D: BCF 03.5
001E: MOVF 42,W
001F: MOVWF 05
0020: GOTO 03F
.................... case PORT_B: output_b(dat); break;
0021: MOVLW 00
0022: BSF 03.5
0023: MOVWF 06
0024: BCF 03.5
0025: MOVF 42,W
0026: MOVWF 06
0027: GOTO 03F
.................... case PORT_C: output_c(dat); break;
0028: MOVLW 00
0029: MOVWF 20
002A: BSF 03.5
002B: MOVWF 07
002C: BCF 03.5
002D: MOVF 42,W
002E: MOVWF 07
002F: GOTO 03F
*
0101: MOVLW FF
0102: MOVWF 20
.................... case PORT_D: output_d(dat); break;
*
0030: MOVLW 00
0031: BSF 03.5
0032: MOVWF 08
0033: BCF 03.5
0034: MOVF 42,W
0035: MOVWF 08
0036: GOTO 03F
.................... case PORT_E: output_e(dat); break;
0037: MOVLW 00
0038: BSF 03.5
0039: MOVWF 09
003A: BCF 03.5
003B: MOVF 42,W
003C: MOVWF 09
003D: GOTO 03F
.................... default : //????port maybe error!
.................... break;
003E: GOTO 03F
.................... }
.................... }//end output()
003F: RETLW 00
....................
.................... /////////////////////////////////////////////////////////// lcd_write_cmd()
.................... //
.................... void lcd_write_cmd(int8 cmd)
.................... {
.................... delay_us(400);
0040: MOVLW 85
0041: MOVWF 77
0042: DECFSZ 77,F
0043: GOTO 042
.................... output_low(LCD_RS);
0044: BSF 03.5
0045: BCF 05.1
0046: BCF 03.5
0047: BCF 05.1
.................... output_low(LCD_RW);
0048: BSF 03.5
0049: BCF 05.2
004A: BCF 03.5
004B: BCF 05.2
.................... output(LCD_DAT, cmd);
004C: MOVLW 02
004D: MOVWF 41
004E: MOVF 40,W
004F: MOVWF 42
0050: CALL 004
....................
.................... output_high(LCD_E);
0051: BSF 03.5
0052: BCF 05.3
0053: BCF 03.5
Configuration Fuses:
Word 1: 3F79 XT NOWDT NOPUT NODEBUG NOPROTECT BROWNOUT NOLVP NOCPD NOWRT
/*===========================================================================*/
void main(void)
{
char LINE2[] = { "PIC16F877A CCSC LCD.C" };
int8 i;
lcd_init();
for(i=0; i<nchar_per_line;> {
lcd_display_char(0, i, '0'+i);
}
//lcd_display_str(1, "PIC16F877A CCSC LCD.C") //CCSC can't compiler
lcd_display_str(1, LINE2);
}//end main()