Here is my code, feel free to offer suggestions or modifications.
Code:
' Read and write hardware USART
B1 var byte
Done var byte
TxBuf var byte[3]
STX var byte
ETX var byte
Period var byte
i var byte
tmpval var byte
' Initialize USART
TRISC = %10111111 ' Set TX (PortC.6) to out, rest in
SPBRG = 25 ' Set baud rate to 2400
RCSTA = %10010000 ' Enable serial port and continuous receive
TXSTA = %00100000 ' Enable transmit and asynchronous mode
' Define ADCIN parameters
Define ADC_BITS 10 ' Set number of bits in result
Define ADC_CLOCK 3 ' Set clock source (3=rc)
Define ADC_SAMPLEUS 50 ' Set sampling time in uS
adval var word ' Create adval to store result
TRISA = %11111111 ' Set PORTA to all input
ADCON1 = %10000010 ' Set PORTA analog and right justify result
Pause 500 ' Wait .5 second
' Echo received characters in infinite loop
STX = 2
ETX = 3
Period = 46
TxBuf[1] = 51
TxBuf[2] = 54
TxBuf[3] = 56
mainloop:
ADCIN 0, adval ' Read channel 0 to adval
'basicaly here i want to convert advar into 3 ascii numbers
'representing advar
'So a result like 936 would put
'57 in txbuf[1]
'51 in txbuf[2]
'54 in txbuf[3]
'Then call the serial output routine which adds a stx char, 2 digits
'a period then the last digit followed by an ETX char
Gosub StringOut ' Send character to serial output
pause 500 ' Wait half second
Goto mainloop ' Do it forever
' Subroutine to get a character from USART receiver
charin: B1 = 0 ' Preset to no character received
If PIR1.5 = 1 Then ' If receive flag then...
B1 = RCREG ' ...get received character to B1
Endif
ciret: Return ' Go back to caller
' Subroutine to send a character to USART transmitter
charout: If PIR1.4 = 0 Then charout ' Wait for transmit register empty
TXREG = B1 ' Send character to transmit register
done = 1
Return ' Go back to caller
' Subroutine to send a character string to USART transmitter
'First Send STX (02) then 2 digits, then period (46), then 1 digit, then ETX (03)
StringOut: If PIR1.4 = 0 Then StringOut ' Wait for transmit register empty
done = 1
TXREG = STX ' Send character to transmit register
while PIR1.4 = 0
wend
for i = 1 to 2
TXREG = TxBuf[i] ' Send character to transmit register
while PIR1.4 = 0
wend
next i
TXREG = Period ' Send character to transmit register
while PIR1.4 = 0
wend
TXREG = TxBuf[3] ' Send character to transmit register
while PIR1.4 = 0
wend
TXREG = ETX ' Send character to transmit register
while PIR1.4 = 0
wend
Return ' Go back to caller
Bookmarks