📄 switchdemo.a51
字号:
$NOMOD51
#include "reg66x.h"
NAME switch_demo
; demo program that interface with the on-board switches, LEDs and LCD screen
; Programmer : A/Prof Soon Ing Yann
EXTRN CODE (i2c_init, SPI_init, SC16_init, LCD_init, lcd_wrtdata, lcd_str, lcd_loc, lcd_clr)
PUBLIC sw_code, xy, ldata
?STACK SEGMENT IDATA
RSEG ?STACK
DS 30H ; reserve 30h bytes for stack
data_seg_name SEGMENT DATA
RSEG data_seg_name
xy: DS 1 ; desired location to write on LCD before provoke LCD_LOC routine
ldata: DS 1 ; 1 character data to write to LCD before provoke LCD_WRTDATA
sw_code: DS 1 ; contains switch data
redled EQU P1.0 ;red led is connected to pin 0 of port 1
blueled EQU P1.4 ;blue led is connected to pin 4 of port 1
bit_seg_name SEGMENT BIT
RSEG bit_seg_name
delay_on: DBIT 1 ;not used, for illustration only
CSEG AT 0
LJMP start
code_seg_name SEGMENT CODE
RSEG code_seg_name
USING 0
line1: DB 'Hello world !!!$'
line2: DB 'Welcome to P312$'
line3: DB 'Switch pressed: $'
;------------------------------------------------------------------------------
; Main program
;------------------------------------------------------------------------------
Start: MOV SP,#?STACK-1
CALL System_init ;system initialisation
CALL LCD_clr ;clear LCD
MOV xy, #00h ;display string starts at top line column 0
MOV DPTR, #line1 ;display Hello world
CALL lcd_str
MOV xy, #10h ;display string starts at bottom line column 0
MOV DPTR, #line2 ;display welcome to P312
call lcd_str
mov R0, #05h ;number of times to blink LEDs
blink:
clr redled ;on red led
clr blueled ;on blue led
MOV a, #0FFh ;delay LSB in A
MOV b, #01h ;delay MSB in B
CALL TimerDelay ;short delay to blink led
setb redled ;off red led
setb blueled ;off blue led
MOV a, #0FFh ;delay LSB in A
MOV b, #02h ;delay MSB in B
CALL TimerDelay ;short delay to blink led
Dec R0 ;count counter down
cjne R0,#0,blink
repeat: ;loop to detect key pressed
mov R1,#'1' ;R1 will contains the button number to display
MOV A, sw_code ;sw_code contains switch status
loop: RRC A ;Rotate right into carry to check bit by bit
JNC disp_key ;check if switch "R1" pressed
INC R1 ;if not increase "R1" to represent the next button
cjne R1,#'8',loop ; finished
JMP Repeat
disp_key:
MOV xy, #00h ;Set to top line left column
MOV DPTR, #line3 ;display string "key preseed :"at top line
CALL lcd_str ;
mov xy, #0Fh ;Display button number at last location
mov ldata, R1
call lcd_loc ;set Lcd location
call lcd_wrtdata ;
MOV sw_code,#7fh ;Reset sw_code to all off
jmp repeat
;------------------------------------------------------------------------------
; Initialize the system upon power on
;------------------------------------------------------------------------------
System_init:
CALL SPI_init ;initialised Serial Peripheral Interface
CALL i2c_init ;initialised I2C interface
SETB EA ;use internal ROM
CALL LCD_init ;initialised LCD display
CALL SC16_init ;
MOV sw_code, #7Fh ;initialise switch status to off
RET
;------------------------------------------------------------------------------
; Delay routine (Input: A, B) uses Timer 0
;------------------------------------------------------------------------------
TimerDelay:
push AR0 ;push r0 into stack
anl tcon,#0cfh ; Clear T0 overflow & run flags only
anl tmod,#0f0h ; T0 enabled; T1 control unchanged
orl tmod,#01h ; set T0 to mode 1 (16 bit up counter)
onems:
mov TL0, #18h ;set T0 to count up from FC18h
mov TH0, #0fch
orl TCON, #10h ;start T0 timer
wait:
jbc Tf0, dwnab ; poll T0 overflow flag until set
sjmp wait ; loop until Overflow flag set
dwnab:
anl TCON, #0efh ;stop T0; other TCON bits unchanged
dec a ;count A down
cjne a, #0ffh, noroll ;
dec b ; count B down
noroll:
mov r0, b
cjne r0,#00h,onems
done: pop AR0 ; pop r0 out of stack
ret
;------------------------------------------------------------------------------
; The END directive is ALWAYS required.
;------------------------------------------------------------------------------
END ; End Of File
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -