Your English is quite good. There is the odd mistake eg. you used "advise" when you meant "advice" Advise is a verb. And "using" does not have an e.Ariff said:Hi,
im quite the newbie when it comes to setting up electronics circuits, but we all gotta start somewhere. I have been succesfully setting up a pic16f628 with rs232 control with a serial port on my pc. I can manage all that, and it works fine. Right now, im wondering how one would start creating the following :
I would like to have a "master" pic and several "slaves". As i read this should be perfectly possible but i dont have a clue on how to begin, or how to set this up. After burning my 3th pic this week, i wisely decided to stop and ask for advise first. The slave pics should have some sort of id code, so i can write software that will recognise the id. I can than select that certain pic and send/receive data from it. When done, i can select another pic id, and send some other data to it. As i go along searching on the internet, it appeared that useing RS485 for this system would be the best choice. I would very kindly ask for a helping hand in here. I'll post my current scheme, if anyone could take a look at it, and help me get it to work, ill be thankfull forever.
kind regards,
Ariff
(sorry for my bad english, it's not my native language)
LIST P=16F628A
include <P16F628A.INC>
__CONFIG _CP_OFF & _WDT_OFF & _PWRTE_ON & _RC_OSC_NOCLKOUT & _LVP_OFF & _INTOSC_OSC_NOCLKOUT & _BODEN_OFF
ERRORLEVEL -302
CBLOCK 0x20
EE_tlr
EE_data
header
cntmsec
pwm_tlr
r1
g1
b1
r2
g2
b2
r3
g3
b3
step
lptlr1a
lptlr2a
lptlr2b
fdtlr2
fdtlr3
fdmax
function
pointer
tijd
tijdtlr
delay
time_on
time_off
byte
char
chartlr
dataout
datain
ENDC
CBLOCK 0x70
W_temp
STATUS_temp
buf_tlr
buf_tmp
buf_data
buf_0
buf_1
buf_2
buf_3
buf_4
buf_5
idcode
ENDC
#define clr_C bcf STATUS, C
#define BANK0 bcf STATUS,RP0 ; RAM PAGE 0
#define BANK1 bsf STATUS,RP0 ; RAM PAGE 1
#define ledon byte,0
#define RTS PORTB,0
#define stage1 PORTA,0
#define stage3 PORTA,1
#define stage5 PORTA,2
#define stageok PORTA,3
#define stageint PORTB,7
#define red PORTB,4
#define blue PORTB,5
#define green PORTB,6
org 0x0000
goto start
org 0x0004
; -------------------------
; INTERRUPT SERVICE ROUTINE
; -------------------------
isr movwf W_temp ;SAVE CONTEXT
swapf STATUS,w ;SAVE CONTEXT
movwf STATUS_temp ;SAVE CONTEXT
BANK0
btfsc stageint ;test led
goto intledoff ;led is on
bsf stageint ;led is off
goto intledexit
intledoff
bcf stageint
intledexit
handleuart
movf RCREG, w
movwf buf_data
;buf_tmp = buf_tlr*8
clr_C
rlf buf_tlr, w
movwf buf_tmp
rlf buf_tmp, f
rlf buf_tmp, w
addwf PCL,f ;add offset to pc to generate a computed goto
;buf_tlr = 0
movlw 'r' ; move literal 'value' into register W
subwf buf_data, W ; subtract the contents of register1 from
W1
btfss STATUS, Z ; check the Z bit in the status register
goto char_nok
movf buf_data, w
movwf buf_0
bsf stage1
goto endhandleuart
;buf_tlr = 1
; nop
movlw 's' ; move literal 'value' into register W
subwf buf_data, W ; subtract the contents of register1 from
W2
btfss STATUS, Z ; check the Z bit in the status register
goto char_nok
movf buf_data, w
movwf buf_1
goto endhandleuart
;buf_tlr = 2
nop
movlw '4' ; move literal 'value' into register W
subwf buf_data, W ; subtract the contents of register1 from
W3
btfss STATUS, Z ; check the Z bit in the status register
goto char_nok
movf buf_data, w
movwf buf_2
bsf stage3
goto endhandleuart
;buf_tlr = 3
; nop
movlw '8' ; move literal 'value' into register W
subwf buf_data, W ; subtract the contents of register1 from
W4
btfss STATUS, Z ; check the Z bit in the status register
goto char_nok
movf buf_data, w
movwf buf_3
goto endhandleuart
;buf_tlr = 4
nop
movlw '5' ; move literal 'value' into register W
subwf buf_data, W ; subtract the contents of register1 from
W5
btfss STATUS, Z ; check the Z bit in the status register
goto char_nok
movf buf_data, w
movwf buf_4
bsf stage5
goto endhandleuart
;buf_tlr = 5
movf buf_data, w
andlw b'00011111' ; filter 5 LSB bits
subwf idcode, W
btfss STATUS, Z
goto char_nok
movf buf_data, w
movwf buf_5
;Header OKE
bsf stageok
call msec25
movf idcode, w
btfsc buf_5,5
iorlw b'00100000'
call send
clrf buf_tlr
goto isr_end
endhandleuart
incf buf_tlr,f
goto isr_end
char_nok
clrf buf_tlr
bcf buf_5,5 ;wissen activate bit
bcf stage1
bcf stage3
bcf stage5
bcf stageok
isr_end swapf STATUS_temp,W ;
movwf STATUS ;RESTORE CONTEXT
swapf W_temp,F ;
swapf W_temp,W ;
btfss buf_5,5 ;IF activated THEN
retfie ;RETURN with ISR on
bcf INTCON,PEIE ;disable Peripheral interrupt
return
r_init movlw D'7' ;
movwf CMCON ; Comparators off, all pins digital I/O
movlw b'00000000' ; set up portA
movwf PORTA
movlw b'00000100' ; RB2(TX)=1 others are 0
movwf PORTB
BANK1
movlw b'11110000'
movwf TRISA ; RA1 en RA2 output, others input
movlw b'00000010' ; RB1(RX)=input, others output
movwf TRISB
bsf PIE1,RCIE ;enable Usart Receive interrupt
BANK0
bsf INTCON,PEIE ;enable Peripheral interrupt
bsf INTCON,GIE ;enable Global interrupt
return
; ------------------------------------
; Reset registers
; ------------------------------------
r_set clrf r1
clrf g1
clrf b1
clrf r2
clrf g2
clrf b2
clrf r3
clrf g3
clrf b3
clrf EE_tlr
clrf pwm_tlr
clrf pointer
MOVVR D'50', time_on
MOVVR D'50', time_off
clrf buf_tlr
bcf RTS
MOVVR D'8', idcode
return
; ------------------------------------
; SET BAUD RATE TO COMMUNICATE WITH PC
; ------------------------------------
; Boot Baud Rate = 9600, No Parity, 1 Stop Bit
r_RS232_init
BANK1
movlw 0x19 ; 0x19=9600 bps (0x0C=19200 bps)
movwf SPBRG
movlw b'00100100' ; brgh = high
movwf TXSTA ; enable Async Transmission, set brgh
BANK0
movlw b'10010000' ; enable Async Reception
movwf RCSTA
; ------------------------------------
; Clear Receive buffer
; ------------------------------------
movf RCREG,W
movf RCREG,W
movf RCREG,W ; flush receive buffer
return
; ------
; Start
; ------
start call r_init
call r_RS232_init
call r_set
; ---------
; MAIN LOOP
; ---------
main
call lees_function
clr_C ; clear carry uit STATUS
rlf function, w ; functie * 2 (schuif 1 bit naar links)
addwf PCL,f ;add offset to pc to generate a computed goto
clrf pointer ;function = 0
goto main
call show_long ;function = 1
goto main
call show_short ;function = 2
goto main
call fade_slow ;function = 3
goto main
call fade_fast ;function = 4
goto main
call flash ;function = 5
goto main
call set_time ;function = 6
goto main
nop ;function = 7
goto main
;Lezen van informatie uit de EEPROM
lees_function
movf pointer, w ; lees eeprom met pointer
call readEE
andlw 0x07 ; maximaal 7
movwf function ; functie
incf pointer, f
MOVRR r1, r2 ; backup rood
movf pointer, w
call readEE
movwf r1 ; nieuwe rood
incf pointer, f
MOVRR g1, g2 ; backup groen
movf pointer, w
call readEE
movwf g1 ; nieuwe groen
incf pointer, f
MOVRR b1, b2 ; backup blauw
movf pointer, w
call readEE
movwf b1 ; nieuwe blauw
incf pointer, f
movf pointer, w
call readEE
movwf tijd ; nieuwe tijdseenheid
incf pointer, f
return
; Laat de kleuren zien
show_long
MOVVR D'20', delay
goto show
show_short
MOVVR D'1', delay
goto show ; deze instructie is overbodig
show MOVRR tijd, tijdtlr ; tijd x delay x 5ms
lp01 MOVRR delay, lptlr1a ;
lp01a call rgb_1a ; 5ms
decfsz lptlr1a, f
goto lp01a
decfsz tijdtlr, f
goto lp01
return
; knipperen van de kleuren
flash MOVRR tijd, tijdtlr
MOVRR r1 ,r2
MOVRR g1 ,g2
MOVRR b1 ,b2
clrf lptlr2b
bsf ledon
lp02 MOVVR D'10', lptlr2a ;10x
lp02a btfss ledon
goto lp02_off
goto lp02_on ; deze instructie is overbodig
lp02_on
IRNEQRT lptlr2b, time_on, lp02_out
clrf lptlr2b
clrf r1
clrf g1
clrf b1
bcf ledon
goto lp02_out
lp02_off
IRNEQRT lptlr2b, time_off, lp02_out
clrf lptlr2b
MOVRR r2 ,r1
MOVRR g2 ,g1
MOVRR b2 ,b1
bsf ledon
goto lp02_out ; deze instructie is overbodig
lp02_out
incf lptlr2b, f
call rgb_1a ; 5ms
call rgb_1a ; 5ms
decfsz lptlr2a, f
goto lp02a
decfsz tijdtlr, f
goto lp02
return
;Van de ene kleur naar de andere kleur
; kleur fade in 32 stappen van 32 met 32 kleurstappen
fade_slow
movlw D'32'
movwf fdmax
movlw D'8'
movwf step
goto fade
; kleur fade in 16 stappen van 16 met 16 kleurstappen
fade_fast
movlw D'16'
movwf fdmax
movlw D'16' ; deze instructie is overbodig in w staat al D'16'
movwf step
goto fade ; deze instructie is overbodig
; fade_fast is 4x sneller als fade_slow maar heeft 2x zo grote kleurstappen
fade call rgb_bck ; backup rgb_new + rgb_old
movf fdmax, w
movwf fdtlr2 ; For fdtlr2 = fdmax to 0
fade2 movf tijd, w ; tijdtlr = tijd
movwf tijdtlr
fade2a movf fdmax, w
movwf fdtlr3 ; For fdtlr3 = fdmax to 0
call rgb_new ; nieuw kleuren laden
fade3 movf fdtlr2, w ; fdtlr2 --> w
subwf fdtlr3, w ; fdtlr3 - w
btfsc STATUS, Z ; If fdtlr2 - fdtlr3 = 0 Then
call rgb_old ; oude kleuren laden
call rgb_2a ; uitsturen kleuren
decfsz fdtlr3, f
goto fade3 ; next fdtlr3
decfsz tijdtlr, f ; extra herhalingslus
goto fade2a
decfsz fdtlr2, f
goto fade2 ; Next fdtlr2
call rgb_new ; restore rgb_new
return
rgb_bck movf r2,w ; backup van de kleuren
movwf r3
movf g2,w
movwf g3
movf b2,w
movwf b3
movf r1,w
movwf r2
movf g1,w
movwf g2
movf b1,w
movwf b2
return
rgb_new movf r2,w ; zet nieuwe kleuren in uitsturing
movwf r1
movf g2,w
movwf g1
movf b2,w
movwf b1
return
rgb_old movf r3,w ; zet oude kleuren in uitsturing
movwf r1
movf g3,w
movwf g1
movf b3,w
movwf b1
return
; knippertijden instellen
set_time
MOVRR r1, time_on
MOVRR g1, time_off
return
; Hier gebeurt het echt.
; De LED's worden gestuurd.
; 256x256x256 = 16777216 kleuren (24 bits)
; wijziging
; kijk of er data in de buffer staat
rgb_1a btfsc PIR1,RCIF ; check for received data
call program
clrf pwm_tlr
rgb_1b decf pwm_tlr, f
btfsc STATUS, Z
goto rgb_1c
movf pwm_tlr, w
subwf r1, w
btfsc STATUS, Z
bsf red
movf pwm_tlr, w
subwf g1, w
btfsc STATUS, Z
bsf green
movf pwm_tlr, w
subwf b1, w
btfsc STATUS, Z
bsf blue
nop
nop
goto rgb_1b
rgb_1c bcf red
bcf green
bcf blue
return
; Hier gebeurt het ook echter lagere resolutie.
; De LED's worden gestuurd.
; 16x16x16 = 4096 kleuren (12 bits)
; 32x32x32 = 32768 kleuren (15 bits)
; wijziging
; kijk of er data in de buffer staat
rgb_2a btfsc PIR1,RCIF ; check for received data
call program
clrf pwm_tlr
rgb_2b movf step, w
subwf pwm_tlr, f
btfsc STATUS, Z
goto rgb_2c
; If pwm_tlr <= r1 then set red
movf pwm_tlr, w
subwf r1, w
btfsc STATUS, C
bsf red
; If pwm_tlr <= g1 then set green
movf pwm_tlr, w
subwf g1, w
btfsc STATUS, C
bsf green
; If pwm_tlr <= b1 then set blue
movf pwm_tlr, w
subwf b1, w
btfsc STATUS, C
bsf blue
goto rgb_2b
rgb_2c bcf red
bcf green
bcf blue
return
;**************************************************
;* Programmeren en uitlezen van de interne EEPROM *
;**************************************************
; Wat wordt het ?
; W = Write
; R = Read
; D = Deactivate Slave en Exit
; Overig = Echo en Exit
program clrf EE_tlr
call receive ; wacht op data
movwf header
call msec25
IREQVT header, 'W', progw
IREQVT header, 'R', progr
IREQVT header, 'D', progd
movf header, w
call send ; verstuur data
return
; Het antwoord op de 'grote' W is een 'kleine' w.
; Dit start de programmeer cyclus
progw movlw 'w'
call send ; verstuur data
progw2 call receive ; wacht op data
movwf EE_data
call msec25
movf EE_data, w
call send ; verstuur data
movf EE_tlr, w
call writEE ; data opslaan
movlw D'10' ; verwerkingstijd PIC 10 millisec
call nmsec
incf EE_tlr, f
IRLEQVT EE_tlr, D'128', progw2
return
; Het antwoord op de 'grote' R is een 'kleine' r.
; Dit start de uitlees cyclus
progr movlw 'r'
call send ; verstuur data
progr2 movf EE_tlr, w
call readEE
call send ; verstuur data
movlw D'10' ; wachttijd 10 msec
call nmsec
incf EE_tlr,f
IRLEQVT EE_tlr, D'128', progr2
return
progd movlw 'd'
call send ; verstuur data
bcf stage1
bcf stage3
bcf stage5
bcf stageok
bsf INTCON,PEIE ;enable Peripheral interrupt
bsf INTCON,GIE ;enable Global interrupt
return
;******************************************************
;* Ontvang een karakter via RS232 en plaats deze in w *
;******************************************************
; RS232 ontvangen (bron = internet)
receive btfss PIR1,RCIF ; controleer op ontvangen data
goto receive
movf RCREG,W ; opslaan van ontvangen data in w
return
;****************************************
;* Verzend een karakter uit w via RS232 *
;****************************************
; RS232 verzenden (bron = internet)
send movwf dataout
bsf RTS ; Slave neemt bus over
movlw D'1' ; wacht 1 msec
call nmsec
movf dataout, w
movwf TXREG ; verzend data in W
BANK1
send1 btfss TXSTA,TRMT ; verzenden gereed indien hoog
goto send1
BANK0
movlw D'1' ; wacht 1 msec
call nmsec
bcf RTS ; Slave geeft bus vrij
return
;*********************
;* Wachtlus Routines *
;*********************
; Hele fijne wachtroutines (bron = internet)
; Zijn alleen van toepassing bij 4Mhz
msec25 movlw D'25'
nmsec movwf cntmsec ; delay for N (in W) millisec
mseclp movlw D'248' ; 1 usec for load
call usec ; this instruction is 995 usec
nop ; 1 usec
decfsz cntmsec,f ; 1 usec, (2 if skip taken)
goto mseclp ; 2 usec, loop = 995+5 = 1 msec
return
usec addlw D'255' ; substract 1 from 'W'
btfss STATUS,Z ; skip when you reach zero
goto usec ; more loops
return
;***********************************
;* Lezen en schrijven in EE-Memory *
;***********************************
; Maak gebruik van het interne geheugen!
; Deze informatie blijft bewaard.
; Ook bij spanningsverlies.
; bron = internet en datascheet
readEE BANK1
movwf EEADR ; Te lezen adres in EE-Memory naar EEADR.
bsf EECON1, RD ; EE Read
movf EEDATA,w ; Byte naar w
BANK0
return
writEE BANK1
movwf EEADR ; Te beschrijven adres in EE-Memory naar EEADR.
BANK0
movf EE_data, w
BANK1
movwf EEDATA ; Te schrijven byte,
bsf EECON1, WREN; Enable write
bcf INTCON, GIE ; Disable INTs
btfsc INTCON, GIE ; AN576
goto $-2
movlw H'55' ;
movwf EECON2 ; Write 55h
movlw H'AA' ;
movwf EECON2 ; Write AAh
bsf EECON1, WR ; Set WR bit begin write
bsf INTCON, GIE ; Enable INTs.
BANK0
return
;************************
;* begin waarden EEPROM *
;************************
; Hier begint de EEPROM sectie
org 0x2100
de 0x01, 0xff, 0x00, 0x00, 0x1e
de 0x01, 0x00, 0xff, 0x00, 0x1e
de 0x01, 0x00, 0x00, 0xff, 0x1e
de 0x01, 0xff, 0xff, 0x00, 0x1e
de 0x01, 0xff, 0x00, 0xff, 0x1e
de 0x01, 0x00, 0xff, 0xff, 0x1e
de 0x01, 0xff, 0xff, 0xff, 0x1e
de 0x06, 0x32, 0x32, 0x00, 0x01
de 0x05, 0xff, 0x00, 0x00, 0x32
de 0x06, 0x19, 0x19, 0x00, 0x01
de 0x05, 0x00, 0xff, 0x00, 0x32
de 0x06, 0x0C, 0x0C, 0x00, 0x01
de 0x05, 0x00, 0x00, 0xff, 0x32
de 0x06, 0x06, 0x06, 0x00, 0x01
de 0x05, 0xff, 0xff, 0xff, 0x32
de 0x03, 0xff, 0x00, 0x00, 0x05
de 0x03, 0x7f, 0x7f, 0x00, 0x05
de 0x03, 0x00, 0xff, 0x00, 0x05
de 0x03, 0x00, 0x7f, 0x7f, 0x05
de 0x03, 0x00, 0x00, 0xff, 0x05
de 0x03, 0x7f, 0x00, 0x7f, 0x05
de 0x03, 0xff, 0x00, 0x00, 0x05
de 0x03, 0x00, 0x00, 0x00, 0x05
de 0x00, 0x00, 0x00, 0x00, 0x05
de 0x00, 0x00, 0x00, 0x00, 0x00
de 0x00, 0x00, 0x00
; Hier eindigt de EEPROM sectie
org 0x2000
;verplicht
END
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?