danners430
Member
Hey guys,
I've finished writing this annoyingly long program for the master chip in a serial master-slave system. Unfortunately everyone I know who is also a programmer has no experience with the PICAXE system. My simulations seem to work just fine, but would some kind soul here be willing to check over the code?
Cheers guys!
View attachment control panel main.txt
I've finished writing this annoyingly long program for the master chip in a serial master-slave system. Unfortunately everyone I know who is also a programmer has no experience with the PICAXE system. My simulations seem to work just fine, but would some kind soul here be willing to check over the code?
Cheers guys!
Code:
'This is a program to receive inputs from a push-button matrix, and send commands to satellite PICAXE chips over a wired serial bus.
'
'Serial bus has two connection: One for communication to satellite chips, and one for replies from those chips. They only respond when spoken to.
'
'Buttons connected in matrix, with 11 rows & 11 columns giving 232 buttons - theoretical maximum for 8-bit operation due to addressing system used: 11 x 22 = 242. If more buttons needed, program would need updating to use words instead of individual bytes, giving a theoretical maximum of over 30,000 buttons.
'
'Chip used is a PICAXE 28X2
'
'I/O:
' 1x Serial in C.0
' 1x Serial out C.1
' 11x Matrix outputs B.0-7, C.5-7
' 11x Matrix Inputs A.0-7, C.2-4
'
'Standard serial formats:
'Serial Out: For communication with satellite chips
' serout C.1, N9600_16, (*address*, *command*, *command*)
'Serial In:
' Serin C.0, N9600_16, (*address from*, *data*) *address from* used for readiness checking
'
'Standard Serout commands:
' (*address*, 255, 1) Requesting chip to confirm readiness to receive data. Also used to indicate main chip ready to receive data.
'
'Standard chip responses:
' (*address from*, %00000001) Bit 0 = 1: no data to transmit
' (*address from*, %*******0) Bit 0 = 0: data to transmit, data contained in bits 1-7. Will be implemented into program if ever needed, but groundwork implemented.
' Both of the above are also confirmations of readiness to receive data.
'--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
' MATRIX CONTROL
init:
setfreq m16 'Set frequency to 16MHz
let dirsb = 255 'B.0-7 matrix outputs
let dirsa = 0 'A.0-7 matrix inputs
let dirsc = %11100010 'C.0 serin, C.1 serout, C.2-4 matrix inputs, C.5-7 matrix outputs
let pinsb = 0
let pinsa = 0
let pinsc = 0
let b0 = 0 'Matrix outputs
let b1 = 0 'Matrix inputs
let b2 = 0 'Matrix result
let b3 = 0 'Serout address byte
let b4 = 0 'Serout data byte 1
let b5 = 0 'Serout data byte 2
let b6 = 0 'serin address byte
let b7 = 0 'serin data byte
main:
b1 = 0
b2 = 0
b3 = 0
b4 = 0
b5 = 0
let pinsB = %10000000 'Chip switches on outputs one at a time and scans inputs after each change
b0 = 1 'Outputs assigned addresses 1-11, inputs addresses 12-22
gosub check
let pinsB = %01000000
b0 = 2
gosub check
let pinsB = %00100000
b0 = 3
gosub check
let pinsB = %00010000
b0 = 4
gosub check
let pinsB = %00001000
b0 = 5
gosub check
let pinsB = %00000100
b0 = 6
gosub check
let pinsB = %00000010
b0 = 7
gosub check
let pinsB = %00000001
b0 = 8
gosub check
let pinsb = 0
high c.5
b0 = 9
gosub check
low c.5
high c.6
b0 = 10
gosub check
low c.6
high c.7
b0 = 11
gosub check
if b1 > 0 then multiply 'If a button press detected, ie. b1 contains a value, then continue to carry out command
goto main
multiply:
b2 = b0*b1 'Multiply the addresses of the output & input together to give a unique ID of button pressed
b2 = b2-10 'Take 10 away to make smallest possible address 1.
if b2 = 0 then main
actuate:
if pinsA = 1 or pinC.2 = 1 or pinC.3 = 1 or pinC.4 = 1 then actuate
on b2 goto p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15, p16, p17, p18, p19, p20, p21, p22, p23, p24, p25, p26, p27, p28, p29, p30, p31, p32, p33, p34, p35, p36, p37, p38, p39, p40, p41, p42, p43, p44, p45, p46, p47, p48, p49, p50, p51, p52, p53, p54, p55, p56, p57, p58, p59, p60, p61, p62, p63, p64, p65, p66, p67, p68, p69, p70, p71, p72, p73, p74, p75, p76, p77, p78, p79, p80, p81, p82, p83, p84, p85, p86, p87, p88, p89, p90, p91, p92, p93, p94, p95, p96, p97, p98, p99, p100, p101, p102, p103, p104, p105, p106, p107, p108, p109, p110, p111, p112, p113, p114, p115, p116, p117, p118, p119, p120, p121, p122, p123, p124, p125, p126, p127, p128, p129, p130, p131, p132, p133, p134, p135, p136, p137, p138, p139, p140, p141, p142, p143, p144, p145, p146, p147, p148, p149, p150, p151, p152, p153, p154, p155, p156, p157, p158, p159, p160, p161, p162, p163, p164, p165, p166, p167, p168, p169, p170, p171, p172, p173, p174, p175, p176, p177, p178, p179, p180, p181, p182, p183, p184, p185, p186, p187, p188, p189, p190, p191, p192, p193, p194, p195, p196, p197, p198, p199, p200, p201, p202, p203, p204, p205, p206, p207, p208, p209, p210, p211, p212, p213, p214, p215, p216, p217, p218, p219, p220, p221, p222, p223, p224, p225, p226, p227, p228, p229, p230, p231, p232
goto main 'Goto script for button pressed
transmit: 'Transmit sub-procedure, to be called up by scripts.
serout C.1, N9600_16, (b3, 255, 1) 'Request readiness confirmation from satellite chip
serin [200, transmit], c.0, N9600_16, b6, b7 'Await response from satellite chip, store response in variables b6 & b7
if b6 != b3 then transmit 'Ensure responding chip has correct address
if b7 bit0 = 0 then readser 'If b7 bit 0 = 0, then chip has data to transmit.
transmit2:
b6 = 0 'Reset serin variables.
b7 = 0
pause 5
serout c.1, N9600_1, (b3, b4, b5) 'Transmit data to satellite chip. Data preset in script.
serin [200, transmit], c.0, N9600_16, b6, b7 'Await receipt confirmation for 50ms, else attempt transfer again
if b6 != b3 then transmit 'Confirm address and response transmitted by satellite chip correct, else attempt transfer again
if b7 != 1 then transmit
goto main
readser:
goto transmit 'To be implemented if needed
end
'------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
'SUB PROCEDURES
check:
if pina.0 = 1 then let b1 = 12
endif
if pina.1 = 1 then let b1 = 13
endif
if pina.2 = 1 then let b1 = 14
endif
if pina.3 = 1 then let b1 = 15
endif
if pina.4 = 1 then let b1 = 16
endif
if pina.5 = 1 then let b1 = 17
endif
if pina.6 = 1 then let b1 = 18
endif
if pina.7 = 1 then let b1 = 19
endif
if pinc.2 = 1 then let b1 = 20
endif
if pinc.3 = 1 then let b1 = 21
endif
if pinc.4 = 1 then let b1 = 22
endif
return
goto main
end
'------------------------------------------------------------------------------------------------------------------------------------------------------------------------
' SCRIPTS
p1:
goto main 'Scripts can be written here as needed
p2:
goto main
p3:
goto main
p4:
goto main
p5:
goto main
p6:
goto main
p7:
goto main
p8:
goto main
p9:
goto main
p10:
goto main
'Program truncated, full program can be found below in txt file