通用仪器仪表程序.docx
《通用仪器仪表程序.docx》由会员分享,可在线阅读,更多相关《通用仪器仪表程序.docx(18页珍藏版)》请在冰豆网上搜索。
通用仪器仪表程序
ORG0000H
LJMPMAIN
ORG0003H
LJMPDS12887
ORG000BH
LJMPDISPLAY
ORG0013H
LJMPVOLTAGE
;/************************主程序******************************/
MAIN:
LCALLDSINIT;DS12887初始化
MOVTMOD,#0D1H;定时器初始化,T0,T1均为十六位计数器
MOVTL0,#17H;T0定时赋初值,单片机主频为12MHz,机器周期为12个震荡周期,
;故计数周期为1us,初值为65536-1000=FC17
MOVTH0,#0FCH
MOVTL1,#0;T1计数清零
MOVTH1,#0
MOVSP,#20H;设堆栈地址
MOV60H,#00H;显示内容标志位,00H-时间,08H-日期,10H电压
MOV61H,#00H;闪烁间隔时间标志位
MOV62H,#00H;亮灭状态控制标志位
MOV63H,#0FFH;闪烁位置标志位
MOV64H,#00H;进入/退出设置标志位
SETBEA;中断初始化,允许INT0、INT1和T0溢出中断
SETBEX1;允许外部中断1
SETBEX0;允许外部中断0
SETBIT1;外部中断1为边沿触发方式
SETBIT0;外部中断0为边沿触发方式
SETBET0;允许定时器T0溢出中断
SETBTR0;开定时器T0
SETBTR1;开定时器T1
SETBPX1;外部中断1溢出中断优先
KEY:
MOVA,P1;判断是否有键按下
ANLA,#0FH
JNBACC.0,DD
JNBACC.1,DD
JNBACC.2,DD
JNBACC.3,DD
SJMPKEY
;/************************时钟芯片初始化******************************/
DSINIT:
MOVDPTR,#7F0BH;设置寄存器B,芯片停止工作
MOVA,#82H
MOVX@DPTR,A
MOVDPTR,#7F00H;秒设置
MOVA,#00H
MOVX@DPTR,A
MOVDPTR,#7F02H;分设置
MOVA,#30H
MOVX@DPTR,A
MOVDPTR,#7F04H;小时设置
MOVA,#08H
MOVX@DPTR,A
MOVDPTR,#7F06H;星期设置
MOVA,#02H
MOVX@DPTR,A
MOVDPTR,#7F07H;日设置
MOVA,#21H
MOVX@DPTR,A
MOVDPTR,#7F08H;月设置
MOVA,#04H
MOVX@DPTR,A
MOVDPTR,#7F09H;年低位设置
MOVA,#09H
MOVX@DPTR,A
MOVDPTR,#7F0EH;年高位设置
MOVA,#20H
MOVX@DPTR,A
MOVDPTR,#7F0AH;寄存器A设置
MOVA,#20H
MOVX@DPTR,A
MOVDPTR,#7F0CH;读寄存器C
MOVXA,@DPTR
MOVDPTR,#070DH;读寄存器D
MOVXA,@DPTR
MOVDPTR,#7F0BH;设置寄存器B,芯片开始工作
MOVA,#12H
MOVX@DPTR,A
RET
;/**********按键确认**************/
DD:
LCALLDL10MS;延时去抖动
MOVA,P1
ANLA,#0FH
JNBACC.0,TQ1
JNBACC.1,TQ2
JNBACC.2,TQ3
JNBACC.3,TQ4
SJMPKEY
TQ1:
MOVA,P1;判断按键1是否弹起
ANLA,#0FH
JBACC.0,DD1
SJMPTQ1
TQ2:
MOVA,P1;判断按键2是否弹起
ANLA,#0FH
JBACC.1,DD2
SJMPDD2
TQ3:
MOVA,P1;判断按键3是否弹起
ANLA,#0FH
JBACC.2,TQ3
SJMPDD3
TQ4:
MOVA,P1;判断按键4是否弹起
ANLA,#0FH
JBACC.3,TQ4
SJMPDD4
DD1:
LCALLDL10MS;按键1延时去抖动
MOVA,P1
ANLA,#0FH
JBACC.0,KEY1
LJMPKEY
DD2:
LCALLDL10MS;按键2延时去抖动
MOVA,P1
ANLA,#0FH
JBACC.1,KEY2
LJMPKEY
DD3:
LCALLDL10MS;按键3延时去抖动
MOVA,P1
ANLA,#0FH
JBACC.2,KEY3
LJMPKEY
DD4:
LCALLDL10MS;按键4延时去抖动
MOVA,P1
ANLA,#0FH
JBACC.3,KEY4
LJMPKEY
;/**********切换显示内容**************/
KEY4:
MOVA,60H;按键4处理,切换显示内容
D:
CJNEA,#00H,V;切换至日期
MOV60H,#08H
LJMPKEY
V:
CJNEA,#08H,T;切换至电压
MOV60H,#10H
LJMPKEY
T:
CJNEA,#10H,D;切换至时间
MOV60H,#00H
LJMPKEY
;/**********进入/退出设置**************/
KEY1:
MOVA,60H
CJNEA,#10H,USE;若此时显示的是电压,则为无效按键
LJMPKEY
USE:
MOVA,64H
CJNEA,#00H,OUT
IN:
INCA;进入设置,标志位置1
MOV64H,A
CLREX0;关读时钟中断
MOV63H,#3FH;初始末两位闪烁
LJMPKEY
OUT:
MOVA,#00H;退出设置,标志位置0
MOV64H,A
LCALLNEW;将修改后的新时间写入时钟芯片
SETBEX0
MOV63H,#0FFH;不闪烁
LJMPKEY
;/**********闪烁位选择**************/
KEY2:
MOVA,64H
CJNEA,#01H,VAIN2;判断是否设置状态
MOVA,63H;若在设置状态,闪烁位置左移两位
RRA
RRA
MOV63H,A
LJMPKEY
VAIN2:
LJMPKEY;若不在设置状态,无效按键
;/**********调整时间**************/
KEY3:
MOVA,64H
CJNEA,#01H,VAIN3;判断是否设置状态
LCALLDECIDE;若在设置状态,则判断修改内容并修改
LJMPKEY
VAIN3:
LJMPKEY;若不在设置状态,无效按键
;/**判断修改的内容**/
DECIDE:
MOVA,60H;判断显示内容
CJNEA,#00H,AGAIN;60H中内容为00H,则修改时间
TIME:
MOVA,63H
CJNEA,#00111111B,TAGAIN1;末两位闪烁时,修改秒
LCALLSECOND
RET
TAGAIN1:
CJNEA,#11001111B,TAGAIN2;五、六闪烁时,修改分
LCALLMINUTE
RET
TAGAIN2:
CJNEA,#11110011B,TAGAIN3;三、四闪烁时,修改小时
LCALLHOUR
RET
TAGAIN3:
LCALLWEEK;一、二闪烁时,修改星期
RET
AGAIN:
CJNEA,#08H,VAIN31;60H中内容为08H时,则修改日期
DATE:
MOVA,63H
CJNEA,#00111111B,DAGAIN1;末两位闪烁时,修改日
LCALLDAY
RET
DAGAIN1:
CJNEA,#11001111B,DAGAIN2;五、六闪烁时,修改月
LCALLMONTH
RET
DAGAIN2:
CJNEA,#11110011B,DAGAIN3;三、四闪烁时,修改年低位
LCALLYEARL
RET
DAGAIN3:
LCALLYEARH;一、二闪烁时,修改年高位
RET
VAIN31:
RET;60H中内容为10H时,则为无效按键
;/**加1修改**/
SECOND:
MOVA,30H;秒加1
ADDCA,#1
DAA
CJNEA,#60H,SYICHU
MOVA,#00H
SYICHU:
MOV30H,A
RET
MINUTE:
MOVA,31H;分加1
ADDCA,#1
DAA
CJNEA,#60H,MIYICHU
MOVA,#00H
MIYICHU:
MOV31H,A
RET
HOUR:
MOVA,32H;小时加1
ADDCA,#1
DAA
CJNEA,#24H,HYICHU
MOVA,#00H
HYICHU:
MOV32H,A
RET
WEEK:
MOVA,33H;星期加1
ADDCA,#1
DAA
CJNEA,#08H,WYICHU
MOVA,#01H
WYICHU:
MOV33H,A
RET
DAY:
MOVA,34H;日加1
ADDCA,#1
DAA
CJNEA,#32H,DYICHU
MOVA,#01H
DYICHU:
MOV34H,A
RET
MONTH:
MOVA,35H;月加1
ADDCA,#1
DAA
CJNEA,#12H,MYICHU
MOVA,#01H
MYICHU:
MOV35H,A
RET
YEARL:
MOVA,36H;年低位加1
ADDCA,#1
DAA
CJNEA,#99H,YLYICHU
MOVA,#00H
YLYICHU:
MOV36H,A
RET
YEARH:
MOVA,37H;年高位加1
ADDCA,#1
DAA
CJNEA,#99H,YHYICHU
MOVA,#00H
YHYICHU:
MOV37H,A
RET
;/**新时间**/
NEW:
PUSHACC
PUSHDPH
PUSHDPL
MOVDPTR,#7F0BH;设置寄存器B,芯片停止工作
MOVA,#82H
MOVX@DPTR,A
MOVDPTR,#7F00H;秒设置
MOVA,30H
MOVX@DPTR,A
MOVDPTR,#7F02H;分设置
MOVA,31H
MOVX@DPTR,A
MOVDPTR,#7F04H;小时设置
MOVA,32H
MOVX@DPTR,A
MOVDPTR,#7F06H;星期设置
MOVA,33H
MOVX@DPTR,A
MOVDPTR,#7F07H;日设置
MOVA,34H
MOVX@DPTR,A
MOVDPTR,#7F08H;月设置
MOVA,35H
MOVX@DPTR,A
MOVDPTR,#7F09H;年设置
MOVA,36H
MOVX@DPTR,A
MOVDPTR,#7F0EH;年设置
MOVA,37H
MOVX@DPTR,A
MOVDPTR,#7F0AH;寄存器A设置
MOVA,#20H
MOVX@DPTR,A
MOVDPTR,#7F0CH;读寄存器C
MOVXA,@DPTR
MOVDPTR,#070DH;读寄存器D
MOVXA,@DPTR
MOVDPTR,#7F0BH;设置寄存器B,芯片开始工作
MOVA,#12H
MOVX@DPTR,A
POPDPL
POPDPH
POPACC
RET
;/************************中断读时钟******************************/
DS12887:
CLREX0;关闭中断
CLREA
PUSHACC
PUSHPSW
PUSHDPH
PUSHDPL;保存ACC等参数
CLRPSW.3
SETBPSW.4;选择工作状态10,防止冲突
MOVDPTR,#7F00H;读秒寄存器并存在30H单元
MOVXA,@DPTR
MOV30H,A
MOVDPTR,#7F02H;读分寄存器并存在31H单元
MOVXA,@DPTR
MOV31H,A
MOVDPTR,#7F04H;读小时寄存器并存在32H单元
MOVXA,@DPTR
MOV32H,A
MOVDPTR,#7F06H;读星期寄存器并存在33H单元
MOVXA,@DPTR
MOV33H,A
MOVDPTR,#7F07H;读日寄存器并存在34H单元
MOVXA,@DPTR
MOV34H,A
MOVDPTR,#7F08H;读月寄存器并存在35H单元
MOVXA,@DPTR
MOV35H,A
MOVDPTR,#7F09H;读年寄存器并存在36H,37H单元
MOVXA,@DPTR
MOV36H,A
MOVDPTR,#7F0EH
MOVXA,@DPTR
MOV37H,A
MOVDPTR,#7F0CH;读寄存器C清除中断标志位
MOVXA,@DPTR
SETBEA;开中断
SETBEX0
POPDPL
POPDPH
POPPSW
POPACC;取回中断前ACC等参数值
RETI
;/************************中断显示******************************/
DISPLAY:
PUSHACC;中断显示程序
PUSHPSW
CLRPSW.4
SETBPSW.3;选择工作状态01,防止冲突
CLREA;关闭中断
CLRTR0
MOVTL0,#17H
MOVTH0,#0FCH
LCALLCHULI;处理寄存器内的内容
MOVR0,#40H;显示内容段码的存放的首地址
MOVA,60H;选择显示内容
ADDA,R0
MOVR0,A
MOVR7,#80H;显示位码的初始值,1为有效
MOVA,61H;设置闪烁间隔时间存放在61H中,为0.25ms
INCA
MOV61H,A
CJNEA,#020H,shine;间隔时间到,亮灭进行转换
MOVA,62H;亮灭状态控制,存放在62H中
XRLA,#0FFH;与1异或,按位取反
MOV62H,A
MOV61H,#00H;将61H重新清零
shine:
MOVA,62H
CJNEA,#00H,on;62H为00H时,则亮
CJNEA,#0FFH,off;62H位FFH时,则灭
on:
MOVA,R7;位码送A
MOVDPTR,#0DFFFH;取位码口地址
MOVX@DPTR,A;位码口输出位码
MOVA,@R0;段码送A
MOVDPTR,#TAB;查表得到显示代码
MOVCA,@A+DPTR
MOVDPTR,#0BFFFH;取段码口地址
MOVX@DPTR,A;段码口输出段码
LCALLDL1MS;调用0.5ms延迟
INCR0;指向下一个显示单元
MOVA,R7
RRA
MOVR7,A;指向下一个位码
CJNEA,#80H,on;一次循环未完成,则显示下一位
SETBTR0
SETBEA
POPPSW
POPACC
RETI
off:
MOVA,R7;位码送A
ANLA,63H
MOVDPTR,#0DFFFH;取位码口地址
MOVX@DPTR,A;位码口输出位码
MOVA,@R0;段码送A
MOVDPTR,#TAB;查表得到显示代码
MOVCA,@A+DPTR
MOVDPTR,#0BFFFH;取段码口地址
MOVX@DPTR,A;段码口输出段码
LCALLDL1MS;调用0.5ms延迟
INCR0;指向下一个显示单元
MOVA,R7
RRA
MOVR7,A;指向下一个位码
CJNEA,#80H,off;一次循环未完成,则显示下一位
SETBTR0
SETBEA
POPPSW
POPACC
RETI
;/**********缓冲区数据处理**************/
CHULI:
MOVR0,#40H;缓冲区的首地址
MOVR1,#30H
MOVR7,#08H
course:
MOVA,@R1
ANLA,#0FH
MOV@R0,A
INCR0
MOVA,@R1
ANLA,#0F0H
SWAPA
MOV@R0,A
INCR0
INCR1
DJNZR7,course;全部内容转换未完成,继续
RET
;/************************中断读电压值******************************/
VOLTAGE:
CLRTR1
CLREA
PUSHACC
PUSHPSW
SETBPSW.3;设置寄存器工作组为11,防止冲突
SETBPSW.4
MOVA,TL1
SUBBA,#10H;计数器1读取的计数值减10000
MOVR7,A
MOVA,TH1
SUBBA,#27H
MOVR6,A
MOVTL1,#0
MOVTH1,#0
LCALLCHANGE;调用十六进制转十进制程序
LCALLVDISPLAY;对转换后的电压值进行处理
POPPSW
POPACC
SETBEA
SETBTR1
RETI
;/**********十六进制转十进制程序**************/
CHANGE:
CLRA;十六进制转十进制BCD
MOVR3,A
MOVR4,A
MOVR5,A
MOVR2,#10H
LOOP:
MOVA,R7
MOVA,R7
RLCA
MOVR7,A
MOVA,R6
RLCA
MOVR6,A
MOVA,R5
ADDCA,R5
DAA
MOVR5,A
MOVA,R4
ADDCA,R4
DAA
MOVR4,A
MOVA,R3
ADDCA,R3
MOVR3,A
DJNZR2,LOOP
RET
;/**********电压值的处理**************/
VDISPLAY:
PUSHPSW
SETBPSW.3
SETBPSW.4
MOV55H,#0BH;AD数据处理程序
JBP1.7,VALUE;判断是否为负
MOV55H,#0AH
VALUE:
MOV57H,#0BH
MOV56H,#0BH;第一二位不显示
MOVR1,#50H
MOVA,R5;末两位拆字处理
ANLA,#0FH
MOV@R1,A
INCR1
MOVA,R5
ANLA,#0F0H
SWAPA
MOV@R1,A
INCR1
MOVA,R4;五、六位拆字处理
ANLA,#0FH
MOV@R1,A
INCR1
MOVA,R4
ANLA,#0F0H
SWAPA
MOV@R1,A
INCR1
MOVA,R3;第四位拆字处理
ADDA,#0CH
MOV@R1,A
POPPSW
RET
;/************************查表及延迟程序******************************/
DL1MS:
MOVR6,#250;1ms延时程序
DLT:
NOP
NOP
DJNZR6,DLT
RET
DL10MS:
MOVR6,#0FFH;10ms延时程序
DL41T:
MOVR5,#8H
DL40T:
NOP
NOP
DJNZR5,DL40T
DJNZR6,DL41T
RET
TAB:
DB5FH,06H,3BH,2FH,66H;段码表
DB6DH,7DH,07H,7FH,6FH
DB20H,00H,0DFH,86H
END