PDA

View Full Version : PBP 16F876A 2 Way Communication



dmugan
- 27th August 2006, 05:52
I am fairly new to PICs and PBP (v2.47). I have designed a small general purpose A/D I/O interface card utilizing a PIC 16F876A. It will be used by my students to gather and graph data using LabVIEW graphical programming language. It must communicate through a USB converter module (FTDI UM232R) because my students are online and use Macs or laptop PCs with no serial port.

Eventually, I would like to have the card execute several commands, and so need two way communication. So far, I have the Defines setup and a program written to read AD port 0 and send 10 bit result to the hardware serial port. It sends data continuously without a request from the PC. It works fine, except that if I stop receiving data, the serial buffer fills or overflows and when I start receiving data again, it is erratic for many seconds before it returns to normal operation. I need a program that tells the PIC chip to read A/D Port 0 and send result to the hardware serial port just once and then wait for another command. Any thread or archive with such a program? Any suggestions for writing one? Thanks for your help.

Charles Linquis
- 27th August 2006, 06:03
Why not send a character, take a conversion, and then sit in a loop waiting for a character to come in using HSERIN? When the character is received (it can be *any* character, or a specific one of your own choosing),
then send another character...

If the sample has to be current (i.e. taken just before the command), you could continuously take A/D readings in a loop. In that loop check PIR1.5 = 1. If it is set, grab the character with HSERIN. If it the right character - send...

dmugan
- 27th August 2006, 06:33
Charles,

I understand only part of what you are saying. If I send a charactor from the PC and the PIC program running has a HSERIN comand in the loop, it will set bit 5 of the PIR register? How to I cause a result to be sent back to the PC? In other words, how do I read the PIR flag or register? Could you provide an example of this loop? Thanks for your help.

Charles Linquis
- 28th August 2006, 00:31
Something like -


LoopTop:

If PIR1.5 = 1 THEN ' Check to see if Character was received
HSERIN[Char] ' Grab the character out of the UART
HSEROUT [ADVAL] ' Send the last A/D value
ENDIF

ADCIN 1, ADVAL ' Continuously sample the A/D

GOTO LoopTop

;---------------------------------------------------------------

PIR1.5 will be set (to "1") whenever ANY character has been received
in the USART. HSERIN grabs that character and sets PIR1.5 to "0" at
the same time. Then it sends the A/D value out.

If no character is received, the A/D converter converts merrily along...

mister_e
- 28th August 2006, 16:05
http://www.picbasic.co.uk/forum/showpost.php?p=8601&postcount=11

dmugan
- 29th August 2006, 04:07
Thank you Charles Linquis and Mister E. Both of your suggestions were helpful. I have incorporated your suggestions in the program below, but I get an error when the compiler reaches the HSERIN command.

ERROR Line 38: Expected '['.

I have tried many different ways to confugure the HSERIN command, but always get this or sometimes additional errors. Any suggestions?

Thanks again for your help.

DEFINE ADC_BITS 10
DEFINE ADC_CLOCK 3
DEFINE ADC_SAMPLEUS 10
adcVar VAR WORD
TRISA = %11111111
ADCON1 = %10000010
DEFINE HSER_RCSTA 90h
DEFINE HSER_TXSTA 24h
DEFINE HSER_BAUD 9600
DEFINE HSER_CLOERR 1
PAUSE 005

main:
If PIR1.5 = 1 THEN
HSERIN[char]
HSEROUT [DEC adcvar, 10, 13]
ENDIF
ADCIN 0, adcVar
PAUSE 50
GOTO main

mat janssen
- 29th August 2006, 07:21
I think you forgot to declare "char"
char var byte

Ioannis
- 29th August 2006, 08:55
And put a space before '['

Ioannis

dmugan
- 29th August 2006, 15:17
Thanks Mat and Ioannis,

The HSERIN command now reads:

HSERIN [char var byte]

I still get the same error

ERROR Line 38: Expected '['.

Anything else I should try? Thanks everyone.

mat janssen
- 29th August 2006, 15:39
There is al small misunderstanding.
You should do this !
DEFINE ADC_BITS 10
DEFINE ADC_CLOCK 3
DEFINE ADC_SAMPLEUS 10
adcVar VAR WORD

char VAR BYTE

TRISA = %11111111
ADCON1 = %10000010
DEFINE HSER_RCSTA 90h
DEFINE HSER_TXSTA 24h
DEFINE HSER_BAUD 9600
DEFINE HSER_CLOERR 1
PAUSE 005

main:
If PIR1.5 = 1 THEN

HSERIN [char] 'and than this

HSEROUT [DEC adcvar, 10, 13]
ENDIF
ADCIN 0, adcVar
PAUSE 50
GOTO main

dmugan
- 31st August 2006, 04:34
Mat,

Thanks a million. The program now compiles ok, but it seems to hang during execution of the program. I have tried re-arranging the order of the instructions, but so far no luck. I will appreciate more suggestions

Charles Linquis
- 31st August 2006, 04:40
I'm going to tell you to READ THE PBP MANUAL now.

mister_e
- 31st August 2006, 13:57
a little typo error...


DEFINE HSER_CLOERR 1


should be


DEFINE HSER_CLROERR 1

dmugan
- 31st August 2006, 23:19
Thanks to everyone, and a second time to Mister E for finding my typo. It now works like a charm! Have a great day!