波形发生器程序
LED1 EQU P3.2 ;百位
LED2 EQU P3.1 ;十位
LED3 EQU P3.0 ;个位
DAT BIT P2.7 ;串行数据
CLK BIT P2.6 ;串行时钟,下降沿有效
LDAC BIT P2.5 ;DAC更新锁存控制,下降沿有效
LOAD BIT P2.4 ;串行数据装载,下降沿有效
ORG 0000H
MAIN: MOV SP, #40H
MOV 74H, #16 ;显示的初值
MOV 70H, #4 ;频率控制初值
ANL TMOD, #0xF0
ORL TMOD, #0x01
SETB DAT
SETB CLK
SETB LDAC
SETB LOAD
LCALL SinWave ;正弦波
MOV DPTR, #TAB
DISP: MOV A, 74H ;待显示数分解为个、十、百三位
MOV B, #100
DIV AB
MOV LED1, A
MOV A, B
MOV B, #10
DIV AB
MOV LED2, A
MOV LED3, B
;显示百位
MOV A, LED1
SETB P3.2
CLR P3.1
CLR P3.0
MOVC A, @A+DPTR
MOV P0, A
LCALL DELAY1
;显示十位
MOV A, LED2
SETB P3.1
CLR P3.2
CLR P3.0
MOVC A, @A+DPTR
MOV P0, A
LCALL DELAY1
;显示个位
MOV A, LED3
SETB P3.0
CLR P3.1
CLR P3.2
MOVC A, @A+DPTR
MOV P0, A
LCALL DELAY1
TAB: DB 3FH,06H,5BH,4FH,66H,6DH,7DH,07H,7FH,6FH
CORG 0200H
LOOP: SETB P0.0 ;按键1
JB P0.0, N2
LCALL DELAY1
SETB P0.0
JB P0.0, LOOP
LCALL SquWave ;方波
N1: JNB P0.0, $
LCALL DELAY1
N2: SETB P0.1 ;按键2
JB P0.1, N4
LCALL DELAY1
SETB P0.1
JB P0.1, N2
LCALL SinWave ;正弦波
N3: JNB P0.1, $
LCALL DELAY1
N4: SETB P0.2 ;按键3
JB P0.2, N6
LCALL DELAY1
SETB P0.2
JB P0.2, N4
LCALL RecWave ;矩形波
N5: JNB P0.2, $
LCALL DELAY1
N6: SETB P0.3 ;按键4
JB P0.3, N8
LCALL DELAY1
SETB P0.3
JB P0.3, N6
MOV A, 74H
RLC A
MOV 74H,A
MOV A,70H
DEC A
MOV 70H,A
CJNE 74H, #01, N7
MOV 74H,#128
MOV 70H,#1
N7: JNB P0.3, $
LCALL DELAY1
LCALL DISP
N8: SETB P0.4 ;按键5
JB P0.4, LOOP
LCALL DELAY1
SETB P0.4
JB P0.4, N8
MOV A,74H
RRC A
MOV 74H,A
MOV A,70H
INC A
MOV 70H,A
CJNE 74H, #128, N9
MOV 74H, #1
MOV 70H, #8
N9: JNB P0.4, $
LCALL DELAY1
LCALL DISP
DELAY1: MOV R6,#60 ;延时5ms
D1: MOV R7,#248
DJNZ R7,$
DJNZ R6,D1
RET
Dac: MOV R1, #2
MOV A, R7
D1: MOV C, ACC.1
MOV DAT, C
ADD A, ACC
CLR CLK
SETB CLK
DJNZ R1, D1
MOV C, 0x20.0 ;发送RNG位
MOV DAT, C
CLR CLK
SETB CLK
MOV R1, #8 ;发送8位DAC数据
MOV A, R6
D2: MOV C, ACC.7
MOV DAT, C
ADD A, ACC
CLR CLK
SETB CLK
DJNZ R1, D2
CLR LOAD ;//装载数据
SETB LOAD
CLR LDAC ;//数据锁存到DAC转换器
SETB LDAC
RET
Delay: MOV TH0, #0xFC ;功能:延时1~256ms,R7>0时,延时(R7)ms
MOV TL0, #0x66
SETB TR0
JNB TF0, $
CLR TR0
CLR TF0
DJNZ R7, Delay
RET
SquWave: MOV R2, #0 ;方波
D1: MOV A, R2
MOV DPTR, #SquTab
MOVC A, @A+DPTR
MOV R6, A
CLR 0x20.0
MOV R7, #00H
LCALL Dac
MOV R7, 70H
LCALL Delay
INC R2
CJNE R2, #40, D1
SJMP SquWave
RET
SquTab: DB 0,0,0,0,0,0,0,0,0,0
DB 0,0,0,0,0,0,0,0,0,0
DB 255,255,255,255,255,255,255,255,255,255
DB 255,255,255,255,255,255,255,255,255,255
SinWave: MOV R2, #0 ;正弦波
D1: MOV A, R2
MOV DPTR, #SinTab
MOVC A, @A+DPTR
MOV R6, A
CLR 0x20.0
MOV R7, #01H
LCALL Dac
MOV R7, 70H
LCALL Delay
INC R2
CJNE R2, #40, D1
SJMP SinWave
RET
SinTab: DB 128,147,167,185,202,218,231,241,249,253
DB 255,253,249,241,231,218,202,185,167,147
DB 127,108,88,70,53,37,24,14,6,2
DB 0,2,6,14,24,37,53,70,88,108
RecWave: MOV R2, #0 ;矩形波
D1: MOV A, R2
MOV DPTR, #RecTab
MOVC A, @A+DPTR
MOV R6, A
CLR 0x20.0
MOV R7, #02H
LCALL Dac
MOV R7, 70H
LCALL Delay
INC R2
CJNE R2, #40, D1
SJMP RecWave
RET
RecTab: DB 0,0,0,0,0,0,0,0,0,0
DB 255,255,255,255,255,255,255,255,255,255
DB 255,255,255,255,255,255,255,255,255,255
DB 255,255,255,255,255,255,255,255,255,255
评论0