;*************************************************************************** ;* ;* SERIAL2 COMMS FUNCTIONS ;* ;*************************************************************************** ; ; these routines don't use the usart to communicate, it's busy... ; instead, we simulate a uart in software ; we could do this simply by using delays, but unfortunately we have to ; do other things at the same time; in particular, we need to be able to ; send and receive data simultaneously... ; ; So, we use a polled method driven by a timer interrupt and controlling two ; state machines; one to transmit and one to receive ; ; while we're at it, we count the ticks to give an (eventual)25Hz internal clock .def int0 = r20 .def int1 = r21 .def int2 = r22 .def int3 = r23 .dseg next_read_2: .byte 1 next_write_2: .byte 1 input_buffer_2: .byte buffer_size ;local variables uart_rxd: .byte 1 ;the byte being received uart_txd: .byte 1 ;the byte being transmitted uart_txtick: .byte 1 ;tick count for the transmitter uart_rxtick: .byte 1 ;tick count for the receiver uart_txbit: .byte 1 ;bit count for tx uart_rxbit: .byte 1 ;bit count for rx uart_status: .byte 1 ;uart status ;status bits .equ txbusy = 0 ;set if a byte is in transmission .equ rxbusy = 1 ;set if a byte is being received .equ rxframe = 2 ;set if the stop byte isn't a one ;clock store mickey: .byte 1 ;192 of these make 1/25th second twentyfifth: .byte 1 ;25 of these make a second second: .byte 1 ;60 of these make a minute minute: .byte 1 ;60 of these to an hour hour: .byte 1 ;we stop counting with hours .cseg ;we run a tick at four times baud rate; this lets us hit the start of a received ;character with an average error of +/- 1/8th of a bit; having detected the bit start ;sometime between t=0 and t=0.25, we can arrange to sample the incoming bits somewhere ;between 0.25 and 0.5 of a bit period in. ; ;we'd *like* to sample at 16 times baud rate, like the hardware does, but at baud*4 ;we will be getting interrupts every 1667 clocks; the processor has other things to do. ; ;on each tick, we first implement the transmit tick process, then the receive tick ;we need to ensure we take significantly fewer than 1667 clocks on each tick ; ;this routine also works at 2400 baud; in this case the appropriate divider is 883 .equ uarttx = pd1 ;can be any pin, need to change port .equ uartrx = pd0 ;will be pd3,2 when debugged ser2_tick: ;we arrive here on a tick ;the txbusy bit is set by the ser2_out routine ;the status reg is *not* automatically preserved in int2,sreg push int2 push zh push zl ;first we do the tx tick stuff lds int2,uart_status andi int2,1<