-
HSERIN problem
Hello everyone,
I'm having trouble getting HSERIN to work properly, I think there is something I don't understand.
I am controlling a DC motor through a Gamoto PID controller and want to read back the position. This is converted to a MIDI signal for use in a Max/Msp program. When I use the Serin2 command it works ok, but when I use the Hserin command my results are unpredictable, that is, I think I first get data that was left in the buffer and then the new data. The bytes are not in the correct order. If I leave out the CLROERR thing it does not work at all, so I think I get overflow errors. Any idea of how I can solve this? I would like to understand the Hserin command better so I can use that instead of the Serin2 command.
Here's (part of) my code:
@ device pic16F688, pwrt_on, mclr_off, protect_off, wdt_on, hs_osc
INCLUDE "modedefs.bas"
define OSC 20
DEFINE HSER_ 1
DEFINE ADC_BITS 10
DEFINE ADC_CLOCK 3
DEFINE ADC_SAMPLEUS 50
RCSTA = $90 ' Enable USART RX
TXSTA = $24 ' Enable USART TX
SPBRG = 129 ' 9600 Baud
OSCCON.3 = 1
ANSEL = %10000000 'AN7 analog input
ADCON0.7 = 1 '10-bit result right justified
CMCON0 = %00000111 'comparators off
PORTA = %00000000
TRISA = %00000000
PORTC = %00000000
TRISC = %00111010
ack var byte
rx_data1 VAR BYTE 'received serial data
rx_data2 VAR BYTE 'received serial data
rx_data3 VAR BYTE 'received serial data
checksum VAR BYTE
position var word
pos1 var byte
pos2 var byte
readpos:
hSerOut [$AA,$82,$33,$03,$B8] 'send request for position
hSerIn [ack,rx_data1,rx_data2,rx_data3,checksum]
' SerIn2 PORTC.5,84,10,readpos,[wait ($41),rx_data1,rx_data2,rx_data3,checksum]
position = ((rx_data1>>4) + (rx_data2<<4)) + (rx_data3<<12)
pos1 = position & 127 'split into two 7 bit position bytes
pos2 = (position >> 7) & 127
SerOut2 PORTC.2,12,[$90,pos1,pos2] 'send out midi
pause 50
goto readpos
Any help is appreciated
-
When you're using HSERIN/HSEROUT, you also need to use the according define with, unless the baudrate will be 2400 Bauds.
In your case @ 20 MHz you will need to use..
Code:
DEFINE HSER_RCSTA 90h ' Enable serial port & continuous receive
DEFINE HSER_TXSTA 24h ' Enable transmit, BRGH = 1
DEFINE HSER_SPBRG 129 ' 9600 Baud @ 20MHz, 0.16%
DEFINE HSER_CLROERR 1 ' Clear overflow automatically
-
Thank you Mr E for your reply
I am using version 2.45 which has a bug so I cannot use the HSER defines with a pic16F688.
I do use the DEFINE HSER_CLROERR 1 which works ok.
Is there a way to clear the buffer before using the HSERIN command?
thanks again