Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

PIC master -> several slaves

Status
Not open for further replies.

Ariff

New Member
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)

**broken link removed**
 
Last edited:
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)
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.

I assume that the PIC shown on your circuit is the master.

Do you need the slaves connected to the computer port? Or does the master do all communication with the computer?

If so, (and assuming that the PICs are all on the one PCB) I would use a one wire data link (ie. one wire for each direction - 2 wires in total) between the master and the slaves. And I would be inclined to use RS232 between the master and the slaves.

I think that your idea of sending "some sort of id code" from the master to the slaves so the appropriate slave can become active is good.

I suggest that when the slave detects its "address" it could respond with a suitable message. Then the master could communicate with the slave. When finished, it would then send a "finished" code to indicate to all slaves that the communication with the addressed slave is finished and to return to the "await address" data.

Incidentally, your LEDs are pointing the wrong way and they each need a series resistor. About 330 Ohm should suffice.
 
Last edited:
Have a good read up on the 9 bit data mode with address detect enable of the RS22 module of most pics. In this mode the pic will only receive data with the 9th bit set. In this mode, a master pic can communicate with 256 slaves.

It is all explained very well in the 16f88 data sheet in section 11.2.3

Mike.
 
Hi Ariff,

The Parallax.com site has a tutorial on using the RS485 protocol with the Basic Stamp which is a PIC. There are also numerous articles written on the subject of what you are wanting to do on www.picbasic.co.uk. Mister E on this site has posted a program and schematic on setting up a Master and Slave system that might be of interest to you. It uses a dip switch to program the "CODE" for the slaves.

I recently completed building two 160-Zone Annunciator Panels with 20 PIC's giving information to the master using 1 wire for serial communications and 1 wire for a "busy" indicator. I received assistance from several sources on the correct manner to setup the PICs so there is no interference from one another.

Look at the sites above and let me know if this is what you want to do.

You didn't specify a programming language that you are using so I hope this will help you out.

Alarmman
 
You also really need to clarify what you're wanting to do, RS485 is certainly one possible method, but may be more complicated than you really need?.

There was a nice website featuring 'PICNET' which was a serial or USB system for connecting multiple PIC's in a simple 'network' round a house - but the site seems to have disappeared?.
 
Hi all,

what a warm welcome on these boards ! Thats great, i might stay around here for a while. I am sorry that i didn't make my intentions quite clear as i should have. I'll explain a bit. I have been experimenting with RGB leds lately. I created some sort of prototype RGB spot. This was PWM controlled using a PIC16f628, serial connection to pc. I wrote a program in VB so i could control the colour fades, strobe modes, delay modes, and so on. Since my original idea was to build several "circuits" of rgb spots in my house and i want every circuit to be controlled by my pc, i figured some sort of "pic network" should be the solution. Now, i have written the asm file for my single pic to rgb spot prototype thingy, but thats how far my knowledge goes. The vb software sends out colour patterns to the pic. The pic itself stores this "pattern" into eeprom memory, and loops the pattern infinitly, untill a new pattern arrives, or the software tells it to shut down. Im practically 100% sure im close to the solution, but i cannot get it to work like i want. I have been testing 2 pic's which i loaded with an adapted version of my first asm code. There's so much information on this subject to be found on the net, that i cannot filter anymore what is relevant for me. I have used a stripped down version of the above scheme for my prototype, and this works fine. Tested it for about 3 months and no single error to be found in the asm nor in the vb programming. Since im not used to work with several pic's at once, i kindly asked for help here. (after posting on other forums too, with a lot of laughter and flame reply's, and urls who didnt give me that crucial information i need so badly) I hope ive been a bit more clear about my intentions. Maybe a few questions are answered now.

If i want to control the pic's using serial connection with a pc, do i need to setup a master pic ? or can the pc serve as a master who controls everything. I suppose if the pc is down, the pic's can continue running there eeprom pattern. Or do they have to send some sort of alive message every now and than ?

Which type of "network" setup would i need. (lets say i will have about 10 different circuits running through the house, each controlling about 10-15 spots) I have read about I2C, CAN bus and RS485... Not a clue really what there specs are, and what i would use best to be honest.

I know its a lot of questions, but i hope someone would be kind enough to give me just that little push in the right direction. When everything is ready, i will put everything (source, scheme's, asm,...) online for your pleasure ;)

Kind regards,
Ariff
 
If you're running 10-15 nodes around the house, IMO, I'd go with RS485 in a single master, multi slave arrangement. I2C is for shorter distances (originally intended for PCB level comms, but can go a few feet slowly...), RS232 is not multidrop and probably won't be quite long enough, CAN I'm not sure....

RS485 is relatively easy. Connect a RS-485 driver to the USART. Have the master talk to the CPU and the RS 485 network. Slaves will talk to the RS-485 network and run the LEDs. Run twisted pair wire in a daisy chain fashion, not star. Terminate each end of the cable with a 100 ohm resistor (that's the 'normal' value). A common misconception is that you only need 2 wires between nodes, but you need to also include a common ground. RS-485 is a hardware protocol, not software, so you can do your communications however you want. For the network protocol, have the master initiate all messages, slaves can only respond. This will eliminate the need to design in collision detection. The message packet can be something like:

Byte 1: start byte (whatever you want)
Byte 2: sender ID
Byte 3: receiver ID
Byte 4: this entire packet length
Byte 5: data
Byte 6: (more data...)
.
.
.
Byte x: last data byte OR checksum

Assign each node a seperate ID. When the master sends a message, all slaves listen, but only the addressed slave will respond/reply.

Good luck,
Rick
 
Last edited:
Just another quick thought... Your schematic showed network biasing resistors to hold the network in a known state - only use 1 set of these on the network, not at every node.

I don't know what MAX346 are? Perhaps you should use MAX483 or MAX485...

Rick
 
Last edited:
Check out this...

**broken link removed**


Also, you should probably put in a command byte in the packet, before or after the length byte...

Rick
 
Hi, after a long time of trial/error i think i made a working scheme. Problem is i can not test it to see if it works or not hehe. Well to be honest im a bit lost trying to write a simple testprogram for the pic16f628a. Im trying to make it send some char's over rs485 to the serial port. I just wanted to read those using hyperterminal or something likewise. Anyone who can kick me in the right direction ? :) thanks already !!

Ariff
 
I'm looking to construct somthing very similar to Ariff, but am totally confused by RS485. There is just too much information out there.

Does anyone know of some good tutorials to get started, so I can build up a demo circuit of a master and client nodes and try and learn. I had a look on Parallax.com but couldn't find anything of use.

Any links or information would be of great help.

Thanks
 
its me again :D

i managed to get myself up and running this afternoon. I currently have the following setup working :

pc master -> rs485 -> pic1 (id : 8) / pic2 (id : 15)

I have hardcoded the id numbers in the pic16f628a. Now i wrote a vb program in which i can select a certain id number, and send/receive data from it. This works fine for 1 pic, but if i select the other one, it looks like the bus is being held open by the first pic or something in that genre. I think my asm code for the pic's itself is not right, if someone would have time to take a look at it, it would be very much appreciated.

Code:
        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

greetings !
Ariff
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top