PIC单片机lcd显示时钟演示程序
扫描二维码
随时随地手机看文章
include
;*----
;定义LCD数据&控制I/O口
LCD_DATAequPORTB
LCD_CNTL1equPORTB
LCD_CNTL2equPORTA
;定义LCD控制相应的I/O引脚
RSequ1
Eequ5
;LCD模块命令
DISP_ONEQU0x00C;开显示
DISP_ON_CEQU0x00E;开显示,开指针
DISP_ON_BEQU0x00F;开显示,开指针,开闪烁
DISP_OFFEQU0x008;关显示
CLR_DISPEQU0x001;清显示的操作数
ENTRY_INCEQU0x006;操作数
ENTRY_INC_SEQU0x007;操作数
ENTRY_DECEQU0x004;操作数
ENTRY_DEC_SEQU0x005;操作数
DD_RAM_ADDREQU0x080;初始化第一行显示的操作数
DD_RAM_ULEQU0x0c0;初始化第二行显示的操作数
;设置显示相关寄存器
ByteEQU20H
CountEQU21H
Count1EQU22H
Count2EQU23H
LCD_DATA1EQU24H
PORTDBEQU25H
TempEQU26H
TempHEQU27H
TempLEQU28H
INTSIGEQU29H
RTCCSIG1EQU2AH
RTCCSIGEQU2BH
ALRSIGEQU2CH
HourEQU2DH
HourHEQU2EH
HourLEQU2FH
HourL1EQU30H
HourH1EQU31H
MinsEQU32H
Mins1EQU33H
MinsHEQU34H
MinsLEQU35H
MinsH1EQU36H
MinsL1EQU37H
SecsEQU38H
SecsHEQU39H
SecsLEQU3AH
ALRH1EQU3BH
ALRM1EQU3CH
ALRH2EQU3DH
ALRM2EQU3EH
ALRH3EQU3FH
ALRM3EQU40H
_WREGEQU41H
_STATUSEQU42H
Hour1EQU43H
ADRESBEQU44H
BKMEQU45H
;*----
org0
nop
gotostart
org4
gotoISR
org10
CalTable
bcfSTATUS,RP0
bcfPCLATH,0
movlw0x66;进行校准处理
subwfTemp,w;
addwfPCL,f;将补偿结果与PCL相加
dt0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5
dt6,6,6,7,7,7,8,8,8,9,9,0xa,0xa,0xb,0xb,0xc,0xc
dt0xd,0xd,0xe,0xe,0xf,0xf,0xf,0x10,0x10,0x10
dt0x11,0x11,0x12,0x12,0x12,0x13,0x13,0x14,0x14,0x14
dt0x15,0x15,0x15,0x16,0x16,0x16,0x17,0x17,0x18,0x18
dt0x19,0x19,0x1a,0x1a,0x1b,0x1b,0x1c,0x1c,0x1c
dt0x1d,0x1d,0x1d,0x1e,0x1e,0x1f,0x1f,0x20,0x20
dt0x21,0x21,0x22,0x22,0x23,0x23,0x24,0x24,0x25,0x25
dt0x26,0x26,0x27,0x28,0x29,0x29,0x2a,0x2a,0x2b,0x2b
dt0x2c,0x2d,0x2e,0x2f,0x30,0x32
start
callInitLCD
clrfRTCCSIG
clrfRTCCSIG1
clrfINTSIG
clrfHour
clrfHour1
clrfMins
clrfMins1
clrfSecs
clrfALRSIG
movlw20h
movwfBKM
movlwb'10001111'
movwfPORTDB
movlw0ffh
movwfTMR0
movlw00
movwfTMR1L
movlw80h
movwfTMR1H
movlwb'00001111'
movwfT1CON
bsfSTATUS,RP0
bsfPIE1,TMR1IE
bcfSTATUS,RP0
movlwb'10111111'
OPTION
movlwb'11110000'
movwfINTCON
main
movlw80h
callSendCmd
callDisplayTime;显示时间
movlw0c0h
callSendCmd
movlw'H'
callputcLCD
movlw'e'
callputcLCD
movlw'l'
callputcLCD
movlw'l'
callputcLCD
movlw'o'
callputcLCD
movlw''
callputcLCD
movlw'!'
callputcLCD
movlw''
callputcLCD
movlw':'
callputcLCD
movlw')'
callputcLCD
movlw''
callputcLCD
movlw''
callputcLCD
callBkLight;背光控制
callTempM;显示温度
btfscALRSIG,1
callALR1
btfscALRSIG,2
callALR2
btfscALRSIG,3
callALR3
bcfSTATUS,RP0
bcfPCLATH,0
movfRTCCSIG,W
addwfPCL,1
gotomain
gotoRtccS1
gotoRtccS2
gotoRtccs3
gotoRtccs4
clrfRTCCSIG
clrfRTCCSIG1
gotomain
;*=====
ALR1bcfSTATUS,RP0
movfHour,W
xorwfALRH1,W
btfssSTATUS,Z
gotoALREND
movfMins,W
xorwfALRM1,W
btfssSTATUS,Z
gotoALREND
bsfSTATUS,RP0
clrfTRISD
bcfSTATUS,RP0
bcfSTATUS,C
rlfPORTDB,1
btfscSTATUS,C
bsfPORTDB,0
movfPORTDB,W
movwfPORTD
return
;*---
ALR2bcfSTATUS,RP0
movfHour,W
xorwfALRH2,W
btfssSTATUS,Z
gotoALREND
movfMins,W
xorwfALRM2,W
btfssSTATUS,Z
gotoALREND
bsfSTATUS,RP0
clrfTRISD
bcfSTATUS,RP0
bcfSTATUS,C
rlfPORTDB,1
btfscSTATUS,C
bsfPORTDB,0
movfPORTDB,W
movwfPORTD
return
;*----
ALR3bcfSTATUS,RP0
movfHour,W
xorwfALRH3,W
btfssSTATUS,Z
gotoALREND
movfMins,W
xorwfALRM3,W
btfssSTATUS,Z
gotoALREND
bsfSTATUS,RP0
clrfTRISD
bcfSTATUS,RP0
bcfSTATUS,C
rlfPORTDB,1
btfscSTATUS,C
bsfPORTDB,0
movfPORTDB,W
movwfPORTD
return
;*---
ALREND
bsfSTATUS,RP0
movlw0ffh
movwfTRISD
return
;*=====
RtccS1
movlw80h
callSendCmd
callDisplayTime
callLongDelay
movlw0c0h
callSendCmd;line2,00
movlw'S'
callputcLCD
movlw'e'
callputcLCD
movlw't'
callputcLCD
movlw''
callputcLCD
movlw'T'
callputcLCD
movlw'i'
callputcLCD
movlw'm'
callputcLCD
movlw'e'
callputcLCD;show"SetTime"
movlw''
callputcLCD
movlw''
callputcLCD
movlw''
callputcLCD
movlw''
callputcLCD
bsfPCLATH,0
movfRTCCSIG1,W
addwfPCL,1
gotoSetHour
gotoSetMins
clrfINTSIG
incfRTCCSIG
clrfRTCCSIG1
gotomain
;*----
SetHour
movlw80h
callSelshow
movlwd'24'
subwfINTSIG,W
btfscSTATUS,C
goto$+4
movfINTSIG,w
movwfHour
gotoRtccS1
clrfINTSIG
movwfHour
gotoRtccS1
;*----
SetMins
movlw83h
callSelshow
movlwd'60'
subwfINTSIG,W
btfscSTATUS,C
goto$+4
movfINTSIG,w
movwfMins
gotoRtccS1
clrfINTSIG
movwfMins
gotoRtccS1
;*=====
RtccS2
bcfSTATUS,RP0
movlw0c0h
callSendCmd
callAlarm
movlw'1'
callputcLCD
movlw''
callputcLCD
S2movlw80h
callSendCmd
callDisplayTime
callDispTime
callLongDelay
bsfPCLATH,0
movfRTCCSIG1,W
addwfPCL,1
gotoSetAlH2
gotoSetAlM2
movfHour1,w
movwfALRH1
movfMins1,w
movwfALRM1
clrfINTSIG
clrfRTCCSIG1
incfRTCCSIG
gotomain
;*---
SetAlH2
movlw0c7h
callSelshow
movfINTSIG,W
btfssSTATUS,Z
bsfALRSIG,1
movfINTSIG,W
movwfHour1
movlwd'24'
subwfHour1,W
btfssSTATUS,C
gotoS2
clrfINTSIG
clrfHour1
gotoS2
SetAlM2
movlw0cah
callSelshow
movfINTSIG,W
btfssSTATUS,Z
bsfALRSIG,1
movfINTSIG,W
movwfMins1
movlwd'60'
subwfMins1,W
btfssSTATUS,C
gotoS2
clrfINTSIG
clrfMins1
gotoS2
;*----
Alarmmovlw'A'
callputcLCD
movlw'l'
callputcLCD
movlw'a'
callputcLCD;
movlw'r'
callputcLCD
movlw'm'
callputcLCD;
return
;*----
Selshow
callSendCmd
movlw''
callputcLCD
movlw''
callputcLCD
callLongDelay
return
;*=====
Rtccs3
bcfSTATUS,RP0
movlw0c0h
callSendCmd
callAlarm
movlw'2'
callputcLCD
movlw''
callputcLCD
S3movlw80h
callSendCmd
callDisplayTime
callDispTime
callLongDelay
bsfPCLATH,0
movfRTCCSIG1,w
addwfPCL,1
gotoSetAlH3
gotoSetAlM3
movfHour1,w
movwfALRH2
movfMins1,w
movwfALRM2
clrfINTSIG
clrfRTCCSIG1
incfRTCCSIG
gotomain
;*---
SetAlH3
movlw0c7h
callSelshow
movfINTSIG,W
btfssSTATUS,Z
bsfALRSIG,2
movfINTSIG,W
movwfHour1
movlwd'24'
subwfHour1,W
btfssSTATUS,C
gotoS3
clrfINTSIG
clrfHour1
gotoS3
SetAlM3
movlw0cah
callSelshow
movfINTSIG,W
btfssSTATUS,Z
bsfALRSIG,1
movfINTSIG,W
movwfMins1
movlwd'60'
subwfMins1,W
btfssSTATUS,C
gotoS3
clrfINTSIG
clrfMins1
gotoS3
;*=====
Rtccs4
movlw80h
callSendCmd
callDisplayTime
bcfSTATUS,RP0
movlw0c0h
callSendCmd
callAlarm
movlw'3'
callputcLCD
movlw''
callputcLCD
S4callDispTime
callLongDelay
bsfPCLATH,0
movfRTCCSIG1,w
addwfPCL,1
gotoSetAlH4
gotoSetAlM4
movfHour1,w
movwfALRH3
movfMins1,w
movwfALRM3
clrfINTSIG
clrfRTCCSIG1
incfRTCCSIG
gotomain
;*---
SetAlH4
movlw0c7h
callSelshow
movfINTSIG,W
btfssSTATUS,Z
bsfALRSIG,3
movfINTSIG,W
movwfHour1
movlwd'24'
subwfHour1,W
btfssSTATUS,C
gotoS4
clrfINTSIG
clrfHour1
gotoS4
SetAlM4
movlw0cah
callSelshow
movfINTSIG,W
btfssSTATUS,Z
bsfALRSIG,1
movfINTSIG,W
movwfMins1
movlwd'60'
subwfMins1,W
btfssSTATUS,C
gotoS4
clrfINTSIG
clrfMins1
gotoS4
;*=====
ISR
Push
movwf_WREG;存WREG状态
swapfSTATUS,W;存STATUS
movwf_STATUS
;*----
bcfSTATUS,RP0
btfscPIR1,TMR1IF
callTimeAdj
btfscINTCON,INTF
callIntDeal
btfscINTCON,T0IF
callRtccDeal
;*----
Pop
swapf_STATUS,W;RestoreSTATUS
movwfSTATUS
swapf_WREG,F;RestoreWREGw/o
swapf_WREG,W;affectingSTATUS
retfie
;*=====
TimeAdj
bcfSTATUS,RP0
bcfPIR1,TMR1IF
movlw00h
movwfTMR1L
movlw80h
movwfTMR1H
incfSecs,1
movlwd'60'
xorwfSecs,W
btfssSTATUS,Z
gotoTE
clrfSecs
incfMins,1
movlwd'60'
xorwfMins,W
btfssSTATUS,Z
gotoTE
clrfMins
incfHour
movlwd'24'
xorwfHour,W
btfssSTATUS,Z
gotoTE
clrfHour
TE
return
;*=====
IntDeal
bcfINTCON,INTF
bsfPORTB,0
btfscPORTB,0;
goto$+5;
callDelay
bsfPORTB,0;按键去抖动
btfscPORTB,0
goto$+2;/
incfINTSIG
return
;*=====
RtccDeal
bcfSTATUS,RP0
movlw0ffh;
movwfTMR0
bcfINTCON,T0IF
bsfPORTA,4
btfscPORTA,4;
gotoRtcWr;
callDelay
bsfPORTA,4;按键去抖动
btfscPORTB,4
gotoRtcWr;/
movfRTCCSIG,W
sublw04h
btfssSTATUS,C
gotoWRONG
movfRTCCSIG1,W
sublw03h
btfssSTATUS,C
gotoWRONG
movfRTCCSIG,W
addwfRTCCSIG1,W
btfscSTATUS,Z
goto$+3
incfRTCCSIG1
gotoRtcWr
incfRTCCSIG
gotoRtcWr
WRONG
clrfRTCCSIG
clrfRTCCSIG1
RtcWrreturn
;*=====
BkLight
bsfSTATUS,RP0
bcfADCON1,ADFM;ADRESHall8-bit,H
bcfSTATUS,RP0
movlwb'01010001'
movwfADCON0;selectAD2,Fosc/8
decfszBKM
goto$-1
bsfADCON0,GO
btfssPIR1,ADIF
goto$-1
bcfPIR1,ADIF
;READADRES---
movfADRESH,W
movwfADRESB
bsfSTATUS,RP0
bcfTRISE,0;SELECTRE0OUTPUT
bcfSTATUS,RP0
movlwb'10000000';critICalvalueoflightness
subwfADRESB,W
btfssSTATUS,C
goto$+4;c=0,ADRESB
bsfPORTE,0;c=1,backlighton
goto$+3
bcfSTATUS,RP0
bcfPORTE,0
movlw20h
movwfBKM
return
;*=====
TempM;tempraturemeasure
bsfSTATUS,RP0
bcfADCON1,ADFM;ADRESHall8-bit
bcfSTATUS,RP0
movlwb'01001001';selectAD1,FOSC/8
movwfADCON0
decfszBKM
goto$-1
bsfADCON0,GO
btfssPIR1,ADIF
goto$-1
bcfPIR1,ADIF
;bsfSTATUS,RP0
movfADRESH,W
;bcfSTATUS,RP0
movwfTemp
callCalTable
movwfTemp
callBin2BCDT;进行BCD码调整
movlw08ah;从第一行的第0A个位置开始
callSendCmd
movfTempH,W;显示温度
callputcLCD
movfTempL,W
callputcLCD
movlw'C';显示“C”(表示摄氏度)
callputcLCD
movlw20h
movwfBKM
return
;************************************************
;*Bin2BCDT-将Temp寄存器内的二进制数转*
;*换为相应的ASCII码并存为TempH:TempL的形式*
;************************************************
Bin2BCDT
bcfSTATUS,RP0
clrfTempH;Initializevariables
movfTemp,W
movwfTempL
Ttenth;Subtract10fromlowvariable
movlw.10;untilresultis<0.Oneach
subwfTempL,W;subtraction,incrementthe
btfssSTATUS,C;temperaturehighbyte.
gotoOver0
movwfTempL
incfTempH,F
gotoTtenth
Over0
movlw0x30;MakedecimalASCIIbyadding
addwfTempH,F;'0'tohighandlowbyte
addwfTempL,F
return
;*************************************************
;*DisplayTime-将显示指针移至第一行,调用Bin2BCD*
;*子程序将Secs和Mins寄存器内的数据转换为相应的*
;*ASCII码,然后送LCD显示.*
;*************************************************
DisplayTime
bcfSTATUS,RP0
callBin2BCD;调用二进制与bcd码转换子程序
movfHourH,W;显示小时高位
callputcLCD;
movfHourL,W;显示小时低位
callputcLCD;
movlw':'
callputcLCD;显示':'
movfMinsH,W;显示分钟高位
callputcLCD;
movfMinsL,W;显示分钟低位
callputcLCD;
movlw':';显示':'
callputcLCD
movfSecsH,W;显示秒钟高位
callputcLCD;
movfSecsL,W;显示秒钟低位
callputcLCD;
return
;************************************************
;*Bin2BCD-将Secs&Mins寄存器内的二进制数转*
;*换为相应的ASCII码并存为SecsH:SecsL及*
;*MinsH:MinsL,HourH:HourL的形式.*
;************************************************
Bin2BCD
bcfSTATUS,RP0
clrfHourH;初始化变量
movfHour,W;转换“时”
movwfHourL
Hourth;Looptosubtract10
movlw.10;fromlowbyteand
subwfHourL,W;incrementhighbyte
btfssSTATUS,C;whilelowbyteis>0
gotoNext
movwfHourL
incfHourH,F
gotoHourth
Next
clrfMinsH;初始化变量
movfMins,W;转换“分”
movwfMinsL
Mtenth;Looptosubtract10
movlw.10;fromlowbyteand
subwfMinsL,W;incrementhighbyte
btfssSTATUS,C;whilelowbyteis>0
gotoNext1
movwfMinsL
incfMinsH,F
gotoMtenth
Next1
clrfSecsH;初始化变量
movfSecs,W;转换“秒”
movwfSecsL
Stenth;Looptosubtract10
movlw.10;fromlowbyteand
subwfSecsL,W;incrementhighbyte
btfssSTATUS,C;whilelowbyteis>0
gotoOver
movwfSecsL
incfSecsH,F
gotoStenth
Over
movlw0x30;
addwfHourH,F;
addwfHourL,F;Add0x30('0')toall
addwfMinsH,F;variablestoconvert
addwfMinsL,F;anumber0-9toan
addwfSecsH,F;ASCIIequivalent
addwfSecsL,F;'0'to'9'
return
;*====
DispTime
bcfSTATUS,RP0
movlw0c7h
callSendCmd
callBin2BCa;调用二进制与bcd码转换子程序
movfHourH1,W;显示小时高位
callputcLCD;
movfHourL1,W;显示小时低位
callputcLCD;
movlw':';显示':'
callputcLCD
movfMinsH1,W;显示分钟高位
callputcLCD;
movfMinsL1,W;显示分钟低位
callputcLCD;
return
;*----
Bin2BCa
bcfSTATUS,RP0
clrfHourH1;初始化变量
movfHour1,W;转换“时”
movwfHourL1
Hourth1;Looptosubtract10
movlw.10;fromlowbyteand
subwfHourL1,W;incrementhighbyte
btfssSTATUS,C;whilelowbyteis>0
gotoNexta
movwfHourL1
incfHourH1,F
gotoHourth1
Nexta
clrfMinsH1;初始化变量
movfMins1,W;转换“分”
movwfMinsL1
Mtenth1;Looptosubtract10
movlw.10;fromlowbyteand
subwfMinsL1,W;incrementhighbyte
btfssSTATUS,C;whilelowbyteis>0
gotoOver1
movwfMinsL1
incfMinsH1,F
gotoMtenth1
Over1
movlw0x30;
addwfHourH1,F;
addwfHourL1,F;Add0x30('0')toall
addwfMinsH1,F;variablestoconvert
addwfMinsL1,F;anumber0-9toan
return;ASCIIequivalent
;*******************************************************************
;*LCD模块子程序*
;*******************************************************************
;InitLCD——初始化LCD模块*
;*******************************************************************
InitLCD
bcfSTATUS,RP0;Bank0
bcfSTATUS,RP1
clrfLCD_DATA;清LCD数据&控制位
bsfSTATUS,RP0;Bank1
movlw0xc1;设置RB1~RB5为输出
movwfLCD_DATA
bcfTRISA,5
bcfSTATUS,RP0;
movlw0x00;
movwfLCD_DATA
bsfLCD_CNTL2,E
bcfLCD_CNTL2,E
movlw0x08;
movwfLCD_DATA
bsfLCD_CNTL2,E
bcfLCD_CNTL2,E
callLongDelay
callLongDelay
movlwb'00101000'
callSendCmd
movlwDISP_ON;开显示
callSendCmd
movlwENTRY_INC;设置指针移动方式
callSendCmd
movlwDD_RAM_ADDR;设置起始地址
callSendCmd
InitLCDEnd;
bcfSTATUS,RP0;
movlwCLR_DISP;
callSendCmd
return
;*******************************************************************
;*putcLCD——将字符数据送入LCD*
;*本程序将字符数据分成了高半字节和低半字节分别输入*
;*先送入的是高半字节*
;*******************************************************************
putcLCD
bcfSTATUS,RP0
movwfByte;存WREG到Byte寄存器
swapfByte,W;写入高半字节
movwfLCD_DATA1
rlfLCD_DATA1,1
rlfLCD_DATA1,1
movlw0x3c
andwfLCD_DATA1,1
movfLCD_DATA1,w
movwfLCD_DATA
bsfLCD_CNTL1,RS;设置为送数据模式
bsfLCD_CNTL2,E;发送数据信号
bcfLCD_CNTL2,E
movfByte,W;接着送低半字节
movwfLCD_DATA1
rlfLCD_DATA1,1
rlfLCD_DATA1,1
movlw0x3c
andwfLCD_DATA1,1
movfLCD_DATA1,w
movwfLCD_DATA
bsfLCD_CNTL1,RS;设置为送数据模式
bsfLCD_CNTL2,E;发送数据信号
bcfLCD_CNTL2,E
callDelay
return
;*******************************************************************
;*SendCmd-送命令到LCD*
;*本程序将命令数据分成了高半字节和低半字节分别输入*
;*先送入的是高半字节*
;*******************************************************************
SendCmd
bcfSTATUS,RP0
movwfByte;存WREG到Byte寄存器
swapfByte,W;送入命令的高字节
movwfLCD_DATA1
rlfLCD_DATA1,1
rlfLCD_DATA1,1
movlw0x3c
andwfLCD_DATA1,1
movfLCD_DATA1,w
movwfLCD_DATA
bcfLCD_CNTL1,RS;设置为送命令模式
bsfLCD_CNTL2,E;发送数据信号
bcfLCD_CNTL2,E
movfByte,W;接着送命令的低字节
movwfLCD_DATA1
rlfLCD_DATA1,1
rlfLCD_DATA1,1
movlw0x3c
andwfLCD_DATA1,1
movfLCD_DATA1,w
movwfLCD_DATA
bcfLCD_CNTL1,RS;设置为送命令模式
bsfLCD_CNTL2,E;发送数据信号
bcfLCD_CNTL2,E
callDelay
return
;*******************************************************************
;*Delay-延时子程序*
;*******************************************************************
Delay;call指令占用2个指令周期
bcfSTATUS,RP0
clrfCount;清Count占用1个指令周期
Dloop
decfszCount,f;这两行指令将延时
gotoDloop;(256*3)-1个指令周期
return;return占用2个指令周期
;*******************************************************************
;*LongDelay-长延时子程序*
;*******************************************************************
LongDelay
bcfSTATUS,RP0
clrfCount
clrfCount1
movlw0x03
movwfCount2
LDloop
decfszCount,f
gotoLDloop
decfszCount1,f
gotoLDloop
decfszCount2,f
gotoLDloop
return
;*------
END