📄 ccsc_dcp_lcd.c
字号:
///////////////////////////////////////////////////////////////////////////////
// MPLAB IDE V7 + CCS-C V3
// LCD for MCD2-demo
// DCP for Xicor X9C102
// by nickwolfe@gmail.com
// 2008/10/10
///////////////////////////////////////////////////////////////////////////////
#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
#define DCP_INC PIN_E2
#define DCP_UD PIN_E1
#define DCP_CS PIN_E0
// 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 : //错误
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; i++)
{
lcd_display_char(line, i, ' ');
}
for(i=0; i<NCHAR_PER_LINE; i++)
{
if(str[i] == '\0') break;
lcd_display_char(line, i, str[i]);
}
}//end lcd_display_str()
/*==主程序==*/
void main(void)
{
char LINE2[] = { "DCP WAS SET TO HIGHEST VALUE" };
int8 i;
int8 tmp;
int8 tmp0;
int8 tmp1;
int8 tmp2;
lcd_init();
set_tris_b(0x00);
set_tris_e(0x07);
delay_ms(1000);
output_high(DCP_CS);
output_low(DCP_INC);
for(tmp=0;tmp<100;tmp++) {
output_low(DCP_CS); //DCP触点准备下降
delay_us(10);
output_high(DCP_INC);
delay_us(10);
output_low(DCP_UD); //DCP触点下降准备完毕
delay_us(20);
output_low(DCP_INC); //DCP触点下降!
delay_ms(10);
output_high(DCP_INC); //DCP触点保存准备
delay_us(10);
output_high(DCP_CS); //DCP触点保存!并进入待机状态
}
lcd_display_str(0, LINE2);
tmp = 0;
tmp2 = 0;
while(1){
while(1 == (input_b()&0x01)) // wait put button
{
delay_ms(10);
}
if( tmp < 99) {
tmp++;
}
tmp0 = tmp;
tmp1 = tmp0 / 10;
tmp0 = tmp0 - 10*tmp1;
lcd_display_char(1, 0, '0'+ tmp1);
lcd_display_char(1, 1, '0'+ tmp0);
if( tmp < 99) {
output_low(DCP_CS); //DCP触点准备下降上升
delay_us(10);
output_high(DCP_INC);
delay_us(10);
output_high(DCP_UD); //DCP触点上升准备完毕
delay_us(20);
output_low(DCP_INC); //DCP触点上升!
delay_ms(10);
output_high(DCP_INC); //DCP触点保存准备
delay_us(10);
output_high(DCP_CS); //DCP触点保存!并进入待机状态
}
while(0 == (input_b() & 0x01)) // wait free button
{
delay_ms(10);
}
}//end while(1)
}//end main()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -