Thursday 20 June 2013

June 20 pcb





step motor, rs232 communication.

. ;-----------------------------------------
;ADC interrupt
;-----------------------------------------

ORG 0 ; reset vector
JMP main ; jump to the main program

ORG 3 ; external 0 interrupt vector
JMP ext0ISR ; jump to the external 0 ISR

ORG 0BH ; timer 0 interrupt vector
JMP timer0ISR ; jump to timer 0 ISR

ORG 30H ; main program starts here
main:
SETB IT0 ; set external 0 interrupt as edge-activated
SETB EX0 ; enable external 0 interrupt
CLR P0.7 ; enable DAC WR line
MOV TMOD, #2 ; set timer 0 as 8-bit auto-reload interval timer

MOV TH0, #0 ; |
    ; |

MOV TL0, #0fdh ; |
    ; |

SETB TR0 ; start timer 0
SETB ET0 ; enable timer 0 interrupt
SETB EA ; set the global interrupt enable bit
JMP $ ; jump back to the same line (ie: do nothing)

; end of main program


; timer 0 ISR - simply starts an ADC conversion
timer0ISR:
CLR P3.6 ; clear ADC WR line
SETB P3.6 ; then set it - this results in the required positive edge to start a conversion
RETI ; return from interrupt


; external 0 ISR - responds to the ADC conversion complete interrupt
ext0ISR:
CLR P3.7 ; clear the ADC RD line - this enables the data lines
MOV P1, P2 ; take the data from the ADC on P2 and send it to the DAC data lines on P1
SETB P3.7 ; disable the ADC data lines by setting RD
RETI ; return from interrupt

;-------------------------------------------------------
;serial communication - polling
;-----------------------------------------------------

cls11: ;code for state 11
cjne a, #11, cls12 ;save and confirm checksum
clr RI ;reset serial input
mov a, SBUF ;read serial buffer
mov cl_character, a
mov a, cl_checksum ;update checksum
add a, cl_character
mov cl_checksum, a
cjne a, #0, cls11l1 ;is checksum 0?
mov cl_next_state, #12 ;yes: proceed to state 12
ljmp cl_exit
cls11l1:
mov cl_next_state, #0 ;no: start over
ljmp cl_exit

cls80: ;code for state 80
cjne a, #80, cls81 ;save transaction id
clr RI ;reset serial input
mov a, SBUF ;read serial input
mov cl_character, a
mov cl_trans_id, a
mov a, cl_checksum ;update checksum
add a, cl_character
mov cl_checksum, a
mov cl_next_state, #81
ljmp cl_exit

cls81: ;code for state 81
cjne a, #81, cls100 ;is a character ready to be input?
jnb RI, cls81l1
mov cl_next_state, #11 ;yes: go to state 11
ljmp cl_exit
cls81l1:
mov cl_next_state, #81 ;no: stay here in state 81
ljmp cl_exit

cls100: ;xmt subroutine
cjne a, #100, cls101
jnb TI, cls100l1 ;jump if not ready
mov cl_next_state, #101
ljmp cl_exit
cls100l1:
mov cl_next_state, #100
ljmp cl_exit

cls101:
cjne a, #101, cls102
clr TI ;clr interrupt flag
mov SBUF, cl_xmt ;character to buffer
mov cl_next_state, cl_return
ljmp cl_exit

cls102: ;illegal state
lcall msg106 ;"COMMLINK PROCESS ILLEGAL STATE"
mov cl_next_state, #0

cl_exit:
mov a, cl_next_state
mov cl_state, a
ret

;-----------------------------------------------------------------------
;serial communication - interrupt
;-----------------------------------------------------------------

org 0 ;vector table setup
ljmp main ;reset to main
org 23h
ljmp uart_isr ;vector off to serial isr
org 30h start of main code
main:
mov p0, #offh ;initialize po as 8 inputs
mov tmod, #00100000b ;init timer1 auto reload (m2)
mov th1, #0fdh ;init timer1 for 9600 baud
mov scon, #01000000b ;init uart 8-n-1p; rx enabled
mov ie, #10010000b ;enable serial interrupts
setb tr1 ;start timer 1
back:
mov a, p0 ;read port 0
mov p1, a ;and echo it to both port 1
mov sbuf, a ; and the serial I/F
sjmp back ;loop endlessly

org 100h ;isr for the serial port
uart_isr:
jb ti, tx ;if ti then it's a tx interrupt
mov a, sbuf ; else it's rx so get char
clr ri ;clear ri interrupt flag
reti ;and leave
Tx:
clr ti ;it was a tx so clear ti flag
reti ;and leave
end

No comments:

Post a Comment