



;this is a hello world program written in microchip assembler,
;
;the string is transmitted serial to an terminal. the baudrate is 
;determined by the value of "bitcntr", loaded with "delay"
;19200 at 4 MHz clock speed needs at all 52 cycles pro bit
;
;after sending all characters pic runs an endless loop, a reset is necessary
;to send string again. this reset is performed by a watchdog time out. the
;watchdogtimer is set to 2.3 seconds (128 * 18 ms)
;
;string lenght is defined by a zero byte at end of string
;
;
; http://linux.rz.fh-hannover.de/~duesterb/




		list	p=pic16c84, r=dec, s=off	;radix = decimal
							;case sensitivity=off

back


clockspeed	=	4000000				;clockspeed is 4 Mhz
baudrate	=	19200				;enter baudrate here

delay		=	(clockspeed/4/baudrate-12)/4	;value for baudrate, 12 cycles fixed, 4 cycles delay

ram		=	0ch		;beginning of RAM register

#define	happy


    CBLOCK ram

	delaycntr				;bit dely counter
	charcntr				;counter shows actual character of string
	bitcntr					;counter shows actual bit of byte
	xmitdata				;data to transmit

    ENDC




PC		=	02h		;programcounter
PortA		=	05h		;register 05h is PortA
	


#define		TXD	PortA,0			;TXD line is bit 0 from Port A
#define		c	03h,0			;carry bit is bit 0 from status register
#define		z	03h,2			;zero bit is bit 2 from status register




; Remember to change device info if programming a different PIC. Do not use RC
; devices. Their clock speed is not sufficiently accurate or stable for serial
; communication. 



begin		movlw	0		;move 0 to tristate register, set port to output.
		tris	PortA

		clrf	charcntr	;we start at the first character
					 

send_byte	movlw	8h		;Eight bits in a byte. 
		movwf	bitcntr

		call	string		;Get character from string. 
		movwf	xmitdata	;Put character into the transmit byte. 

		movf	xmitdata,f	;test xmitdata on zero
		btfsc	z
		goto	endless		;do endless loop if xmitdata was zero
		

		bsf	TXD		;set TXD line, dirct connection

		incf	charcntr,f	;next position of string. 

		call	bitdelay	;Start bit. ((delay * 4) + 4) cycles



xmit		rrf	xmitdata,f	;Rotate right moves data bits into
					;carry, starting with bit 0. 

		btfsc	c		;clear TXD if carry bit is set
		bcf	TXD		
		btfss	c		;set TXD if carry bit is clear
		bsf	TXD



		call	bitdelay	;Data bit.

		decfsz	bitcntr,f	;Not eight bits yet? Send next data bit
		goto	xmit

		bcf	TXD		;clear TXD



		call	bitdelay	;Stop bit. ((delay * 4) + 4) cycles
		call	bitdelay	;Stop bit. ((delay * 4) + 4) cycles


		goto	send_byte	;goto send_byte if line feed is not send




endless		goto	endless		;Endless loop. Reset controller to run
					;program again.










; To change the baud rate, substitute a new value for bitdelay at the beginning of
; this program. 



bitdelay	movlw	delay		;this bitdelay delays ((delay * 4) + 4) cycles
		movwf	delaycntr
:loop		nop
		decfsz	delaycntr,f
		goto	:loop		;this is an local loop
		retlw	0







string		movf	charcntr,w
		addwf	PC,f		;String consisting of "Hello world"
					;followed by a linefeed. 




		ifdef	happy

		  space	2		;2 blank lines to listfile
		  messg "I`m happy"	;message to listfile
		  space	2

		  retlw	"Hello world",0ah,0ah	;string, two linefeeds

		else

		  error	"don`t worry, be happy !"

		endif


		retlw	0dh,0		;one cr, Zerobyte => stop sending




		end
