PDA

View Full Version : Student Project



aggie
- 6th July 2006, 23:07
Hi,

I am a student at Texas A&M University, and am working with PICs for the first time. I need to use PICS to communicate via an RF Module, so that we can transmit some data.

Any help is greatly appreciated.

I have PIC 18F4520.

Thanks

mister_e
- 6th July 2006, 23:12
Welcome on the forum!

SERIN
SEROUT
SERIN2
SEROUT2
HSERIN
HSEROUT
are the PBP statement you should use and learn.

Manchester encoding is another thing you should read about on ths forum.

There's a ton of Serial comm here and there... worth a few minutes of search and reading.
http://www.picbasic.co.uk/forum/search.php


Good luck!

aggie
- 7th July 2006, 19:30
Thank you for your help. I will look into these

aggie
- 7th July 2006, 19:40
Could you maybe explain more about the qualifiers it requires in the SERIN command? Is it looking for an address? Suppose I use SERIN like this :

SERIN PORTC.0, T2400, {[1100]}{X, Y}

Is that a valid SERIN Command? What would SEROUT look like so it recieves the right data from the other PIC through RF?

Thanks

yourmomOS
- 7th July 2006, 20:36
Hi, Ive been working on this serial code that will accept bytes into the uart. it uses a different chip, the 16f628, but it uses circular buffers, an interrupt, and a start and a stop character. If you find anything wrong with it, let me know, I havent fully tested it yet but this should get you started.

'************************************************* ***************
'* Name : bufferdesign.BAS *
'* Author : David Kinsler *
'* Notice : Copyright (c) 2006 *
'* : All Rights Reserved *
'* Date : 7/6/2006 *
'* Version : 1.1 *
'* Notes : BUFFER with interrupt v1.1 w/ buffer overrun *
'* : resetting AND no endfound variable *
'************************************************* ***************
'Defines area
'set OSC freq
Define OSC 20
' Set receive register to receiver enabled
DEFINE HSER_RCSTA 90h
' Set transmit register to transmitter enabled
DEFINE HSER_TXSTA 20h
' Set baud rate
DEFINE HSER_BAUD 9600
'clear flag automatically when serial hardware buffer overflows
DEFINE HSER_CLROERR 1

INTCON.7 = 1 ' Enable Global Interrupt Handler
INTCON.6 = 1 ' Enable USART Receive Interrupt
PIE1.5 = 1

pause 100 'safe startup time
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''
on interrupt goto INTERRUPTROUTINE
ENABLE
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''
'Declare variables and counters
commandsize con 10
serbuffsize con 10
BuffSize con 30 'buffer size
Buffer var byte[BuffSize] 'buffer array
SerBuff var byte[serbuffsize] 'used in interrupt to catch incomming data
StChr con "$" 'determines beginning of command
EndChr con "^" 'determines end of command
FirstChr var byte 'holds position of first character in buffer
LastChr var byte 'holds position of last character in buffer
FirstSrlChrPos var byte 'holds position of first serial character
LastSrlChrPos var byte 'holds positon of last serial character
StartFound var bit 'boolean for testing if stchr found
Chr Var Byte 'holds inbound character
Temp var byte[commandsize]
i var word 'counter for blinking led's
x var byte
y var byte 'used to store index of temp
'EndFound var byte 'found the end of the command String?
SrlReceived var bit 'bit says weather this is something in serial buffer or not to transfer
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''
'Initialize variables and counters
FirstChr = 0
LastChr = 0
FirstSrlChrPos = 0
LastSrlChrPos = 0
StartFound = 0
SrlReceived = 0
'endfound = 0
x = 0
y = 0
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''
MAIN:
'do something to like blink and led
high PORTB.4
for i = 0 to 1000
PAUSE 1
NEXT I
LOW PORTB.4
FOR I = 0 TO 1000
PaUSE 1
NEXT I

If SrlReceived = 1 Then
gosub TRANSFERBUFFER
gosub FINDCOMMAND
ENDIF
GOTO MAIN
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''
DISABLE 'disables interrupt
INTERRUPTROUTINE:

Hserin [SerBuff[LastSrlChrPos]]
hserout [serBuff[lastsrlchrpos]]

'when the SerBuff is read into the larger array later, you increment FirstSrlChrPos
'as you read it
lastSrlChrPos = lastSrlChrPos + 1 'increment last character received position
If LastSrlChrPos = serbuffsize then 'if position = serbuffsize after inc, then position = 0
LastSrlChrPos = 0 'making a circular buffer, this also insures FirstSrlChrPos
endif
If LastSrlChrPos = FirstSrlChrPos then
'SERBUFF BUFFER OVERUN, reset serial buffer data
HSEROUT ["Serial Buffer overrun, clearing serial buffer", 10]
LastSrlChrPos = 0
FirstSrlChrpos = 0
srlreceived = 0
goto SRLBUFFOVERRUN
endif
SrlReceived = 1 'new data in serial buffer
SRLBUFFOVERRUN:
Resume 'resumes where left off
ENABLE 'reneables the interrupt
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''
TRANSFERBUFFER:
while FirstSrlChrPOS <> LastSrlChrPos 'while not equal, buffer has not been totaly read.
Buffer[LastChr] = SerBuff[FirstSrlChrPOS]
LastChr = LastChr + 1 'FirstChr is incremented in the PROCESSCOMMAND routine
if LastChr = buffsize then
LastChr = 0
endif
disable 'makes sure interrupt can not change lastchr while clearing
If LastChr = FirstChr Then
'DO SOMETHING ABOUT BUFFER OVERRUN : ADD CODE HERE
HSEROUT ["Transfer Buffer overrun, clearing transfer buffer", 10, 13]
FirstChr = 0
LastChr = 0
goto ENDEDTRANFER
enable 'turns interrupt back on
endif
FirstSrlChrPos = FirstSrlChrPos + 1 'inc serial buffer start Position
if FirstSrlChrPos = serbuffsize then 'if its 10, cycle it back to 0
FirstSrlChrPos = 0 'this creates a circular buffer
endif
wend
ENDEDTRANSFER:
SrlReceived = 0 'all data read from serial buffer
RETURN 'go back to gosub in main
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''
FINDCOMMAND:
'find the start character in buffer
If Buffer[FirstChr] <> StChr then
while FirstChr <> LastChr And StartFound = 0
If Buffer[FirstChr] = StChr then
x = FirstChr
TEmp[0] = Buffer[x]
y = 1
StartFound = 1
else
FirstChr = FirstChr + 1
if FirstChr = 30 then
FirstChr = 0
endif
endif
wend
Return
else
if startfound = 0 then
x = firstChr
startfound = 1
endif

'we have now found the start character, do not advance firstchr until you
'have the whole thing in the temp variable
While X <> LastChr 'you have not already found the end of the command

if x <> FirstChr and Buffer[x] = StChr then
'MISSED COMMAND AND NEW ONE SENT!!!!!!!!!!!!!!!!!
'START NEW COMMAND AT THIS POINT
FirstChr = x
y = 0
startfound = 1
endif

Temp[y] = Buffer[x] 'read next character into temp

if Buffer[x] = EndChr then
goto PROCESSCOMMAND 'Whole command found, goto process it
else 'keep counting
y = y + 1 'inc y for temp
X = x + 1
If x = 30 then
x = 0
endif
endif
wend
'eNDFOUND = 0 'BREAK out of while after end is found and reset endfound
return
endif
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''
PROCESSCOMMAND:
hserout [10, 13, "RECEIVED COMMAND: ", STR temp\y + 1, 10, 13]
FirstChr = x + 1 'remove found commmand from the buffer
if FirstChr = 30 then
FirstChr = 0
endif
y = 0
x = 0 'remove the temp counter
startfound = 0 'reset startfound
goto main

yourmomOS
- 7th July 2006, 20:54
aggie,
Forgot to mention that the xbee and xbee pro rf zigbee modules from www.maxstream.net work great. They will communicate right out of the box and allow for different kinds of network configurations. The only problem with them is they require a 2.8 to 3.3 Volt power supply which is hard to come by unless you use variable regulator which requires extra parts.
Cheers!

mister_e
- 8th July 2006, 00:56
BA33BC0T easy to find 3.3 volt voltage regulator, 1 amp, LDO. Often use here, no problem, close to be cheap to. Don't need to mess with any external resistor, make life easy.. there's tons of other too.

Microchip do some too... worth to surf in most major parts provider website.

yourmomOS
- 8th July 2006, 02:18
BA33BC0T easy to find 3.3 volt voltage regulator, 1 amp, LDO. Often use here, no problem, close to be cheap to. Don't need to mess with any external resistor, make life easy.. there's tons of other too.

Microchip do some too... worth to surf in most major parts provider website.


M_E, I looked all over the place like digikey and jameco and there search engine wouldnt show me these but they do sell them. As for the model, the BA30BC0t looks like the best choice. The xbee's work better on a straight 3v supply. The sleep power increases exponentially it looks like with ever .1 volt above that. Unfortunaly, I just ordered like twenty 317T's not knowing these existed. :(

mister_e
- 8th July 2006, 08:56
hehe, anyways, how many here can really say that he/she know all component in the whole world... i guess there's at least one new each day... DARRGH hard to be up-to-date :)