PDA

View Full Version : Advice-scrutiny for my serial controller



kevlar129bp
- 12th December 2008, 22:23
Hello all,
It's been a long while since my last project...here I go again! I would like some advise-scrutiny-thoughts regarding my latest endeavor. It involves two "boards": a master board (with loop-out to other masters), and daughter boards (4 per master). The master is a 16F871, and the daughters each have a 16F88. I would love some pointers as to proper register setups, uart setups, etc. Most of all, if it'll work at all with how I'm trying to command them! I'll let the code explain the rest.
Thank you all for helping a long-time newbie!


' MASTERBOARD.BAS

'TO DO: SET ALL REGISTERS AND DEFINES

Include "modedefs.bas"
define OSC 20
ADCON1 = %00000110
SerInPin var PORTC.7
SerOutPin var PORTC.6
ADRpin var PORTB.0
ProgLED var PORTB.7
HrtbtLED var PORTB.6

Address var byte
OutPin var byte
OutLevel var byte
BoardAdd var byte
UpdPinLvl var byte

low Progled
LOW HRTBTLED

Start:
if Adrpin then
serin SerInPin,t9600,["ADDRESS"],boardadd
IF (boardadd > 0) and (boardadd < 256) THEN
write 0,boardadd
high Progled
pause 1000
low progled
serout seroutpin,t9600,["Board address set to ",boardadd,13]
else
serout seroutpin,t9600,["Error: Address ",boardadd," not valid! Please use 1 to 256.",13]
endif
goto Start
else
read 0,boardadd
serin SerInPin,t9600,["A"],address '1 - 256

IF (ADDRESS > 0) and (ADDRESS <= 256) THEN

if address = boardadd then
serin SerInPin,t9600,["O"],outpin '1 - 24

if (outpin > 0) and (outpin <= 24) then
serin SerInPin,t9600,["L"],outlevel '0 = OFF, 5 = 5%, 10 = 10%, ...50 = 50%,...100 = 100% (by 5's)

if outlevel <= 100 then
write outpin,outlevel 'Output will match EEPROM location
read outpin,UpdPinLvl
serout seroutpin,t9600,["Updated output ",outpin," to level ",UpdPinLvl * 5,13]
select case outpin
case 1
serout portA.3,n9600,[UpdPinLvl] 'CAT5 JACK 1, PIN 1
case 2
serout portB.1,n9600,[UpdPinLvl] 'CAT5 JACK 1, PIN 2
case 3
serout portA.2,n9600,[UpdPinLvl] 'CAT5 JACK 1, PIN 3
case 4
serout portB.2,n9600,[UpdPinLvl] 'CAT5 JACK 1, PIN 4
case 5
serout portA.1,n9600,[UpdPinLvl] 'CAT5 JACK 1, PIN 5
case 6
serout portB.3,n9600,[UpdPinLvl] 'CAT5 JACK 1, PIN 6
case 7
serout portE.0,n9600,[UpdPinLvl] 'CAT5 JACK 2, PIN 1
case 8
serout portA.5,n9600,[UpdPinLvl] 'CAT5 JACK 2, PIN 2
case 9
serout portE.1,n9600,[UpdPinLvl] 'CAT5 JACK 2, PIN 3
case 10
serout portA.4,n9600,[UpdPinLvl] 'CAT5 JACK 2, PIN 4
case 11
serout portE.2,n9600,[UpdPinLvl] 'CAT5 JACK 2, PIN 5
case 12
serout portA.4,n9600,[UpdPinLvl] 'CAT5 JACK 2, PIN 6
case 13
serout portC.3,n9600,[UpdPinLvl] 'CAT5 JACK 3, PIN 1
case 14
serout portC.2,n9600,[UpdPinLvl] 'CAT5 JACK 3, PIN 2
case 15
serout portD.0,n9600,[UpdPinLvl] 'CAT5 JACK 3, PIN 3
case 16
serout portC.1,n9600,[UpdPinLvl] 'CAT5 JACK 3, PIN 4
case 17
serout portD.1,n9600,[UpdPinLvl] 'CAT5 JACK 3, PIN 5
case 18
serout portC.0,n9600,[UpdPinLvl] 'CAT5 JACK 3, PIN 6
case 19
serout portD.4,n9600,[UpdPinLvl] 'CAT5 JACK 4, PIN 1
case 20
serout portD.5,n9600,[UpdPinLvl] 'CAT5 JACK 4, PIN 2
case 21
serout portC.5,n9600,[UpdPinLvl] 'CAT5 JACK 4, PIN 3
case 22
serout portD.6,n9600,[UpdPinLvl] 'CAT5 JACK 4, PIN 4
case 23
serout portC.4,n9600,[UpdPinLvl] 'CAT5 JACK 4, PIN 5
case 24
serout portD.7,n9600,[UpdPinLvl] 'CAT5 JACK 4, PIN 6
END SELECT
else
serout seroutpin,t9600,["Error: Level ",outlevel," is not valid! Please use 0 to 100 (By FIVES).",13]
endif
else
serout seroutpin,t9600,["Error: Output ",outpin," is not valid! Please use 1 to 24.",13]
endif
endif
else
serout seroutpin,t9600,["Error: Address ",address," is not valid! Please use 1 to 256.",13]
endif
endif
TOGGLE HRTBTLED 'Flashing pretty damn fast!
goto Start



'DAUGHTERBOARD.BAS


Include "modedefs.bas"
DEFINE WRITE_INT 1
INTCON = %10010000

DimVal var byte
DimTblVal var byte
DimStore var byte
DimPause var byte
SerPin var portB.2
ZeroCross var portB.0
TriacTrig var portB.1

ON INTERRUPT GOTO ACDetect

Start:

serin serpin,t9600,dimval 'grab the Dim Value (0,5,10,15,...95,100)
write 0,dimval 'store the dim value in mem pos 0
read 0,DimStore 'read mem 0, put it in DimStore
dimstore = dimstore / 5 'convert 0-100 to 0-20 for lookup table (0=0, 5=1, 10=2, ... , 95=19, 100=20)
if DimStore > 20 then goto start 'testing for 0-20, exit if greater than 20
LOOKUP DimStore,["F",202,195,188,181,174,166,157,147,135,104,74,62,52, 43,35,28,21,14,7,"N"],DimPause
goto start

ACDetect:
low triactrig
select case dimpause
case "F" 'F = OFF
low triactrig
case "N" 'N = ON
high triactrig
case else
pauseus dimpause * 5 'multiply 8 bit value by 5 for true delay
high triactrig 'fire triac after delay
end select
RESUME

mackrackit
- 13th December 2008, 03:00
I would love some pointers as to proper register setups, uart setups, etc.
Are you wanting to change from SERIN/OUT to using the hardware ports?

kevlar129bp
- 13th December 2008, 15:17
Thanks for your inquiry.
I suppose, moreover, I'm in need of assistance with the intricacies of the USART registers, interrupts, etc. I've been reading a few posts here regarding "lost" bytes due to buffer overflows. I'd sure love a quick interrupt routine to avoid that problem (maybe parsing the bytes? I dunno?). I'm also interested in a pro reviewing the code and giving a yeah or nay to it, as far as basic functionality. If nay, I'll go back to the drawing board with suggestions-fixes in tow. I certainly don't expect any of you guys to do the project for me, just pro advice for a newbie that's a bit confused. Maybe one of you guys could put the boards through their paces on a simulator? Am I on the right track as far as the commands coming from the pc serial port?
"AxOyLz"
...where x = address 1-256 (8 bit)
After "A", grab next byte
...where y = output 1-24 (8 bit)
After "O", grab next byte
...where z = level 1-100 (8 bit)
After "L", grab next byte
I hope all this gibberish is not confusing to you! Just trying to fly-out my questions rattling in my noodle right now.
Anyhow, after posting the "masterboard" code, I revamped it a bit...hopefully for the better?
Thanks again for your time and advice.


' MASTERBOARD.BAS

'TO DO: SET ALL REGISTERS AND DEFINES
' MAYBE AN INTERRUPT ROUTINE FOR SERIAL IN?

Include "modedefs.bas"
define OSC 20
'DEFINE HSER_RCSTA 18h ' Set receive register to receiver enabled
'DEFINE HSER_TXSTA 98h ' Set transmit register to transmitter enabled

TXSTA = %00100100
RCSTA = %10010000

ADCON1 = %00000110
TRISA = %00000000
TRISB = %10000000
TRISC = %00000000
TRISD = %00000000

INTCON = %01000000
PIE1.5 = 1 'USART RX INTERRUPT ENABLE

SerRxOK var PORTB.4
SerInPin var PORTC.7
SerOutPin var PORTC.6
ADRpin var PORTB.0
ProgLED var PORTB.7
HrtbtLED var PORTB.6

Address var byte
OutPin var byte
OutLevel var byte
BoardAdd var byte
UpdPinLvl var byte

low Progled
LOW HRTBTLED

Start:
if Adrpin then
serin SerInPin,t9600,["A"],boardadd
IF (boardadd > 0) and (boardadd < 256) THEN
write 0,boardadd
high Progled
pause 1000
low progled
serout seroutpin,t9600,["Board address set to ",boardadd,13,10]
else
serout seroutpin,t9600,["Error: Address ",boardadd," not valid! Please use 1 to 256.",13,10]
endif
goto Start
else
read 0,boardadd
IF (BOARDADD = 0) OR (BOARDADD > 256) THEN
serout seroutpin,t9600,["Board is not addressed. Please set address between 1 and 256.",13,10]
goto start
endif
serin SerInPin,t9600,["A"],address '1 - 256
IF ADDRESS <> boardadd THEN
goto start
endif
serin SerInPin,t9600,["O"],outpin '1 - 24
if (outpin = 0) or (outpin > 24) then
serout seroutpin,t9600,["Error: Output ",outpin," is not valid! Please use 1 to 24.",13,10]
goto start
endif
serin SerInPin,t9600,["L"],outlevel '0 = OFF, 5 = 5%, 10 = 10%, ...50 = 50%,...100 = 100% (by 5's)
if outlevel > 100 then
serout seroutpin,t9600,["Error: Level ",outlevel," is not valid! Please use 0 to 100 (By FIVES).",13,10]
goto start
endif

write outpin,outlevel 'Output will match EEPROM location
read outpin,UpdPinLvl
serout seroutpin,t9600,["Updated output ",outpin," to level ",UpdPinLvl * 5,13,10]
select case outpin
case 1
serout portA.3,n9600,[UpdPinLvl] 'CAT5 JACK 1, PIN 1
case 2
serout portB.1,n9600,[UpdPinLvl] 'CAT5 JACK 1, PIN 2
case 3
serout portA.2,n9600,[UpdPinLvl] 'CAT5 JACK 1, PIN 3
case 4
serout portB.2,n9600,[UpdPinLvl] 'CAT5 JACK 1, PIN 4
case 5
serout portA.1,n9600,[UpdPinLvl] 'CAT5 JACK 1, PIN 5
case 6
serout portB.3,n9600,[UpdPinLvl] 'CAT5 JACK 1, PIN 6
case 7
serout portE.0,n9600,[UpdPinLvl] 'CAT5 JACK 2, PIN 1
case 8
serout portA.5,n9600,[UpdPinLvl] 'CAT5 JACK 2, PIN 2
case 9
serout portE.1,n9600,[UpdPinLvl] 'CAT5 JACK 2, PIN 3
case 10
serout portA.4,n9600,[UpdPinLvl] 'CAT5 JACK 2, PIN 4
case 11
serout portE.2,n9600,[UpdPinLvl] 'CAT5 JACK 2, PIN 5
case 12
serout portA.4,n9600,[UpdPinLvl] 'CAT5 JACK 2, PIN 6
case 13
serout portC.3,n9600,[UpdPinLvl] 'CAT5 JACK 3, PIN 1
case 14
serout portC.2,n9600,[UpdPinLvl] 'CAT5 JACK 3, PIN 2
case 15
serout portD.0,n9600,[UpdPinLvl] 'CAT5 JACK 3, PIN 3
case 16
serout portC.1,n9600,[UpdPinLvl] 'CAT5 JACK 3, PIN 4
case 17
serout portD.1,n9600,[UpdPinLvl] 'CAT5 JACK 3, PIN 5
case 18
serout portC.0,n9600,[UpdPinLvl] 'CAT5 JACK 3, PIN 6
case 19
serout portD.4,n9600,[UpdPinLvl] 'CAT5 JACK 4, PIN 1
case 20
serout portD.5,n9600,[UpdPinLvl] 'CAT5 JACK 4, PIN 2
case 21
serout portC.5,n9600,[UpdPinLvl] 'CAT5 JACK 4, PIN 3
case 22
serout portD.6,n9600,[UpdPinLvl] 'CAT5 JACK 4, PIN 4
case 23
serout portC.4,n9600,[UpdPinLvl] 'CAT5 JACK 4, PIN 5
case 24
serout portD.7,n9600,[UpdPinLvl] 'CAT5 JACK 4, PIN 6
END SELECT
TOGGLE HRTBTLED 'Flashing pretty damn fast!
endif
goto Start

Archangel
- 13th December 2008, 17:11
Usart is used with HSERIN /HSEROUT, and have a 2 byte buffer. Check out post 16 in the thread below for code which contains a ring buffer up to 64 bytes if I remember correctly.
http://www.picbasic.co.uk/forum/showthread.php?t=4972&highlight=cren