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.

Nigels IR Question

Status
Not open for further replies.
i know nothing on baud rate lol im just trying to alter this code to work on my 18F1320. Its Nigels original code. I dont understand it 100% But i know some PIC ASM and can read a datasheet so i though ill give it a shot
 
AtomSoft said:
i know nothing on baud rate lol im just trying to alter this code to work on my 18F1320. Its Nigels original code. I dont understand it 100% But i know some PIC ASM and can read a datasheet so i though ill give it a shot

hi,
Reason for asking, I was using the software uart on the oshonsoft sim, it detects the individual bits in the stream, but it 'hic cups' on the 'stop' bit.?

The switches are being detected correctly and its going into the required SW subr and then returning to the SW scan/test. OK
 
ok works now yay!
Code:
;Tutorial 5.2 - Nigel Goodwin 2002
;Sony SIRC IR transmitter
	LIST	p=18F1320		;tell assembler what chip we are using
	include	<p18F1320.inc>		;include the defaults for the chip
	CONFIG	OSC = INTIO2, WDT = OFF, LVP = OFF, DEBUG = ON		;sets the configuration settings (oscillator type etc.)

	cblock 	0x00 			;start of general purpose registers
		count1 			;used in delay routine
		counta 			;used in delay routine 
		countb
		count
		Delay_Count
		Bit_Cntr
		Data_Byte
		Dev_Byte
		Rcv_Byte
		Pulse
	endc
	

IR_PORT	Equ	PORTB
IR_TRIS	Equ	TRISB
IR_Out	Equ	0x01
Ser_Out	Equ	0x01
SW1	Equ	7			;set constants for the switches
SW2	Equ	6
SW3	Equ	4

TV_ID		Equ	0x01			;TV device ID

But1		Equ	0x00			;numeric button ID's
But2		Equ	0x01
But3		Equ	0x02

		org	0x00
Start
		movlw	0x62			; 4MHz clock select
    	movwf	OSCCON
		clrf	IR_PORT			;make PortB outputs low
 
   		movlw 	b'11111101'		;set PortB all inputs, except RB1
   		movwf 	IR_TRIS
		movlw 	b'11111111'
	    movwf	TRISA
		movwf	LATA
		movlw 	b'11110000'
		movwf	PORTA

Read_Sw
		btfss	PORTA, SW1
		call	Switch1
		btfss	PORTA, SW2
		call	Switch2
		btfss	PORTA, SW3
		call	Switch3
		;call	Delay27
		goto	Read_Sw

Switch1	movlw	But1
		call	Xmit_RS232
		retlw	0x00

Switch2	movlw	But2
		call	Xmit_RS232
		retlw	0x00

Switch3	movlw	But3
		call	Xmit_RS232
		retlw	0x00

TX_Start	movlw	d'92'
		call	IR_pulse
		movlw	d'23'
		call	NO_pulse
		retlw	0x00

TX_One		movlw	d'46'
		call	IR_pulse
		movlw	d'23'
		call	NO_pulse
		retlw	0x00

TX_Zero		movlw	d'23'
		call	IR_pulse
		movlw	d'23'
		call	NO_pulse
		retlw	0x00

IR_pulse			 	
		MOVWF	count		;  Pulses the IR led at 38KHz
irloop	BSF	IR_PORT,	IR_Out 
		NOP			;
		NOP			;
		NOP			;
		NOP			; 
		NOP			;
		NOP			;
		NOP			;
		BCF	IR_PORT,	IR_Out
		NOP			;
		NOP			;
		NOP			;
		NOP			;
		NOP			;
		NOP			;	
		NOP			;
		NOP			;	
		NOP			;
		NOP			;
		NOP
		NOP
		NOP			;
		NOP			;
		DECFSZ	count,F
		GOTO 	irloop	
		RETLW	0

NO_pulse			 	
		MOVWF	count		;  Doesn't pulse the IR led
irloop2		BCF	IR_PORT,	IR_Out 
		NOP			;
		NOP			;
		NOP			;
		NOP			;
		NOP			;
		NOP			;
		NOP			;
		NOP			; 
		NOP			;
		NOP			;
		NOP			;
		BCF	IR_PORT,	IR_Out
		NOP			;
		NOP			;	
		NOP			;
		NOP			;	
		NOP			;
		NOP			;
		NOP
		NOP
		NOP			;
		NOP			;
		DECFSZ	count,F
		GOTO 	irloop2	
		RETLW	0

Xmit_RS232      MOVWF   Data_Byte            	;move W to Data_Byte
                MOVLW   0x07                 	;set 7 DATA bits out
                MOVWF   Bit_Cntr
                call	TX_Start		;send start bit
Ser_Loop        RRCF     Data_Byte , f        	;send one bit
                BTFSC   STATUS    , C
                call	TX_One
                BTFSS   STATUS    , C
                call	TX_Zero
                DECFSZ  Bit_Cntr  , f        	;test if all done
                GOTO    Ser_Loop
						;now send device data
		movlw	D'1'
		movwf	Dev_Byte		;set device to TV
		MOVLW   0x05                 	;set 5 device bits out
                MOVWF   Bit_Cntr
Ser_Loop2       RRCF     Dev_Byte , f        	;send one bit
                BTFSC   STATUS    , C
                call	TX_One
                BTFSS   STATUS    , C
                call	TX_Zero
                DECFSZ  Bit_Cntr  , f        	;test if all done
                GOTO    Ser_Loop2
                retlw	0x00



;Delay routines

Delay255	movlw	0xff		;delay 255 mS
		goto	d0
Delay100	movlw	d'100'		;delay 100mS
		goto	d0
Delay50		movlw	d'50'		;delay 50mS
		goto	d0
Delay27		movlw	d'27'		;delay 27mS
		goto	d0
Delay20		movlw	d'20'		;delay 20mS
		goto	d0
Delay5		movlw	0x05		;delay 5.000 ms (4 MHz clock)
d0		movwf	count1
d1		movlw	0xC7
		movwf	counta
		movlw	0x01
		movwf	countb
Delay_0		decfsz	counta, f
		goto	$+2
		decfsz	countb, f
		goto	Delay_0

		decfsz	count1	,f
		goto	d1
		retlw	0x00

;end of Delay routines

	end
had to remove a 4.7k resistor from base of transistor..Will post schematic now..1 min
 
blueroomelectronics said:
Should R6 be 47ohms? 4.7K seems very high. You can pump a fair amount of current through an IR LED.

Or even 4.7 ohms?, I would suggest using two LED's in series (twice the power for no extra current), and check the value of resistor in my tutorial.
 
dude you know you forgot to fix this:

Code:
						;now send device data
movlw	D'16';	D'1'
movwf	Dev_Byte		;set device to TV

Mines is like:
Code:
						;now send device data
;movlw	D'16';	D'1'
movff	TV_ID, Dev_Byte ;Load Preset Device #
;movwf	Dev_Byte		;set device to TV

Just noticed still isnt good like that i have to put a Decimal 16 like
D'16' for mines lol
 
Last edited:
Nigel Goodwin said:
Or even 4.7 ohms?, I would suggest using two LED's in series (twice the power for no extra current), and check the value of resistor in my tutorial.

I currently Have 1 in stock so i will be sure to make the schematic for 2 and add the other when i get to goto my radio shack
 
Ok i modified the code for all who would like ot understand it more....
If anything looks wrong please comment on it
Code:
;AtomSoft Conversion to 18F1320
;Tutorial 5.2 - Nigel Goodwin 2002
;Sony SIRC IR transmitter
	LIST	p=18F1320		;tell assembler what chip we are using
	include	<p18F1320.inc>		;include the defaults for the chip
	CONFIG	OSC = INTIO2, WDT = OFF, LVP = OFF, DEBUG = ON		;sets the configuration settings (oscillator type etc.)

	cblock 	0x00 				;start of general purpose registers
		count1 					;used in delay routine
		counta 					;used in delay routine 
		countb
		count
		PCount
		Delay_Count
		Bit_Cnt
		Data_Byte
		Dev_Byte
		Rcv_Byte
		Pulse
	endc

IR_Port Equ	PORTB				;Set IR_Port to equal your IR Port
IR_Tris Equ	TRISB				;Set IR_Tris to equal IR Tris
IR_Bit	Equ	0x01				;Set IR_Bit to equal the bit on the port your IR LED is on

SW_Port Equ	PORTA				;Set SW_Port to equal your button port
SW_Tris Equ	TRISA				;Set SW_Tris to equal your button tris
SW1_Bit Equ	7					;Set SW1_Bit to equal the bit on the port of your first button
SW2_Bit Equ	6					;Set SW1_Bit to equal the bit on the port of your second button

CMD1	Equ	0x00				;Set this to equal to the Command to send when you press button 1
CMD2	Equ	0x01				;Set this to equal to the Command to send when you press button 2
	
	org	0x00					;Start of Code
Start
	MOVLW	0x62				;Used to set OSCCON to 4Mhz
	MOVWF	OSCCON
	BCF		IR_Port, IR_Bit		;Make IR Port Bit Low
	BCF		IR_Tris, IR_Bit		;Make IR Port Bit Output
	BSF		SW_Tris, SW1_Bit	;Make Switch 1 Input
	BSF		SW_Tris, SW2_Bit	;Make Switch 2 Input
	BSF		SW_Port, SW1_Bit	;Make Switch 1 High
	BSF		SW_Port, SW2_Bit	;Make Switch 2 High

Read_Sw
	BTFSS	SW_Port, SW1_Bit	;Check if SW1 went Low
	CALL	Command1			;Yes is did so call command #1
	BTFSS	SW_Port, SW2_Bit	;SW1 did go low or finished so check SW2 now
	CALL	Command2			;SW2 went low so call command #2
	GOTO	Read_Sw				;Loop back

Command1
	MOVLW	CMD1				;Move the Byte in CMD1 to WREG
	CALL 	TXIR				
	RETLW	0x00				

Command2
	MOVLW	CMD2				;Move the Byte in CMD2 to WREG
	CALL 	TXIR
	RETLW	0x00
	
PulseIR
	MOVWF	PCount				;Move WREG contents to PCount
PLoop
	BSF		IR_Port, IR_Bit		;Beginning of pulse
	NOP						
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP							;7uS Long Pulse
	BCF		IR_Port, IR_Bit		;Flat Line End pulse
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	DECFSZ	PCount, F			;15-18uS Long FlatLine End Pulse if count is 0... Return
	GOTO	PLoop				;Pulse Again
	RETLW	0

FlatLine
	MOVWF	PCount		
NPLoop
	BCF		IR_Port, IR_Bit		;Flatline for 25uS
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	BCF		IR_Port, IR_Bit
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	DECFSZ	PCount, F
	GOTO	NPLoop
	RETLW	0

Tx_Start
	MOVLW	d'96'				;Move 96 to WREG which is the pulse number for 2.4mS which is START Burst
	CALL	PulseIR				;Pulse IR with 96 in WREG
	MOVLW	d'24'				;Move 24 to WREG which is the pulse number for 600uS
	CALL	FlatLine			;Send a FlatLine
	RETLW	0x00		

Tx_One
	MOVLW	d'48'				;Move 48 to WREG which is the number for a Logical 1 which is 1.2mS
	CALL	PulseIR				;Pulse IR with 48 in WREG
	MOVLW	d'24'				;Another FlatLine
	CALL	FlatLine
	RETLW	0x00

Tx_Zero							
	MOVLW	d'24'				;Move 24 to WREG which is the number for a Logical 0 which is 600uS
	CALL	PulseIR				
	MOVLW	d'24'				;Send FlatLine for 600uS
	CALL	FlatLine
	RETLW	0x00

TXIR
	MOVWF	Data_Byte			;Collect Info from WREG and Place in Dat_Byte
	MOVLW	0x07				;Move 0x07 to Bit_Cnt which is the Command Bit Length
	MOVWF	Bit_Cnt		
	CALL	Tx_Start			;Send Start Burst
Dev_Loop
	RRCF	Data_Byte			;Rotate Right f through Carry
	BTFSC   STATUS    , C		;Check if the carry is a 0 if so skip... 
	call	Tx_One				;If its a 1 then send a 1
	BTFSS   STATUS    , C		;Check if the carry is a 1 if so skip... 
	call	Tx_Zero				;If its a 0 then send a 0
	DECFSZ  Bit_Cnt  , f		;Decrement f, Skip if 0
	GOTO    Dev_Loop		
	movlw	D'16'				;Input your Device ID Here
	movwf	Dev_Byte			;Move the device ID to Dev_Byte
	MOVLW   0x05				;Set Bit_Cnt to 5 since thats the Device ID Length to send
	MOVWF   Bit_Cnt		

Dev_Loop2
	RRCF     Dev_Byte , f
	BTFSC   STATUS    , C
	call	Tx_One
	BTFSS   STATUS    , C
	call	Tx_Zero
	DECFSZ  Bit_Cnt  , f
	GOTO    Dev_Loop2
	retlw	0x00

Delay255	
	movlw	0xff		;delay 255 mS
	goto	d0
Delay100
	movlw	d'100'		;delay 100mS
	goto	d0
Delay50
	movlw	d'50'		;delay 50mS
	goto	d0
Delay27	
	movlw	d'27'		;delay 27mS
	goto	d0
Delay20	
	movlw	d'20'		;delay 20mS
	goto	d0
Delay5	
	movlw	0x05		;delay 5.000 ms (4 MHz clock)
d0	movwf	count1
d1	movlw	0xC7
	movwf	counta
	movlw	0x01
	movwf	countb
Delay_0	
	decfsz	counta, f
	goto	$+2
	decfsz	countb, f
	goto	Delay_0
	decfsz	count1	,f
	goto	d1
	retlw	0x00
	END
 
20 Mhz Code
Code:
;AtomSoft Conversion to 18F248 @ 20Mhz
;Tutorial 5.2 - Nigel Goodwin 2002
;Sony SIRC IR transmitter
	LIST	p=18F248		;tell assembler what chip we are using
	include	<p18F248.inc>		;include the defaults for the chip
	CONFIG	OSC = HS, WDT = OFF, LVP = OFF	;sets the configuration settings (oscillator type etc.)

	cblock 	0x00 				;start of general purpose registers
		count1 					;used in delay routine
		counta 					;used in delay routine 
		countb
		PCount
		Delay_Count
		Bit_Cnt
		Data_Byte
		Dev_Byte
		Pulse
		d1
		d2
	endc

IR_Port Equ	PORTB				;Set IR_Port to equal your IR Port
IR_Tris Equ	TRISB				;Set IR_Tris to equal IR Tris
IR_Bit	Equ	4 					;Set IR_Bit to equal the bit on the port your IR LED is on

SW_Port Equ	PORTC				;Set SW_Port to equal your button port
SW_Tris Equ	TRISC				;Set SW_Tris to equal your button tris
SW1_Bit Equ	4					;Set SW1_Bit to equal the bit on the port of your first button
SW2_Bit Equ	5					;Set SW1_Bit to equal the bit on the port of your second button

CMD1	Equ	0x00				;Set this to equal to the Command to send when you press button 1
CMD2	Equ	0x01				;Set this to equal to the Command to send when you press button 2
	
	org	0x00					;Start of Code
Start
	BCF		IR_Port, IR_Bit		;Make IR Port Bit Low
	BCF		IR_Tris, IR_Bit		;Make IR Port Bit Output
	BSF		SW_Tris, SW1_Bit	;Make Switch 1 Input
	BSF		SW_Tris, SW2_Bit	;Make Switch 2 Input
	BSF		SW_Port, SW1_Bit	;Make Switch 1 High
	BCF		SW_Port, SW2_Bit	;Make Switch 2 High

Read_Sw
	BTFSS	SW_Port, SW1_Bit	;Check if SW1 went Low
	CALL	Command1			;Yes is did so call command #1
	BTFSS	SW_Port, SW2_Bit	;SW1 did go low or finished so check SW2 now
	CALL	Command2			;SW2 went low so call command #2
	GOTO	Read_Sw				;Loop back

Command1
	MOVLW	CMD1				;Move the Byte in CMD1 to WREG
	CALL 	TXIR			
	RETLW	0x00				

Command2
	MOVLW	CMD2				;Move the Byte in CMD2 to WREG
	CALL 	TXIR
	RETLW	0x00
	
PulseIR
	MOVWF	PCount				;Move WREG contents to PCount
PLoop
	BSF		IR_Port, IR_Bit		;Beginning of pulse
	CALL	Delay7				;7uS Long Pulse
	BCF		IR_Port, IR_Bit		;Flat Line End pulse
	CALL	Delay18				;17uS Long Pulse
	DECFSZ	PCount, F			;15-18uS Long FlatLine End Pulse if count is 0... Return
	GOTO	PLoop				;Pulse Again
	RETLW	0

FlatLine
	MOVWF	PCount		
NPLoop
	BCF		IR_Port, IR_Bit		;Flatline for 25uS
	CALL	Delay7				;7uS Long Pulse
	NOP
	NOP
	CALL	Delay18				;17uS Long Pulse
	DECFSZ	PCount, F
	GOTO	NPLoop
	RETLW	0

Tx_Start
	CALL	Delay60	
	MOVLW	d'96'				;Move 96 to WREG which is the pulse number for 2.4mS which is START Burst
	CALL	PulseIR				;Pulse IR with 96 in WREG
	MOVLW	d'23'				;Move 24 to WREG which is the pulse number for 600uS
	CALL	FlatLine			;Send a FlatLine
	RETLW	0x00		

Tx_One
	MOVLW	d'48'				;Move 48 to WREG which is the number for a Logical 1 which is 1.2mS
	CALL	PulseIR				;Pulse IR with 48 in WREG
	MOVLW	d'20'				;Another FlatLine
	CALL	FlatLine
	RETLW	0x00

Tx_Zero							
	MOVLW	d'23'				;Move 24 to WREG which is the number for a Logical 0 which is 600uS
	CALL	PulseIR				
	MOVLW	d'20'				;Send FlatLine for 600uS
	CALL	FlatLine
	RETLW	0x00

TXIR
	MOVWF	Data_Byte			;Collect Info from WREG and Place in Dat_Byte
	MOVLW	0x07				;Move 0x07 to Bit_Cnt which is the Command Bit Length
	MOVWF	Bit_Cnt		
	CALL	Tx_Start			;Send Start Burst
Dev_Loop
	RRCF	Data_Byte			;Rotate Right f through Carry
	BTFSC   STATUS    , C		;Check if the carry is a 0 if so skip... 
	call	Tx_One				;If its a 1 then send a 1
	BTFSS   STATUS    , C		;Check if the carry is a 1 if so skip... 
	call	Tx_Zero				;If its a 0 then send a 0
	DECFSZ  Bit_Cnt  , f		;Decrement f, Skip if 0
	GOTO    Dev_Loop		
	movlw	D'16'				;Input your Device ID Here
	movwf	Dev_Byte			;Move the device ID to Dev_Byte
	MOVLW   0x05				;Set Bit_Cnt to 5 since thats the Device ID Length to send
	MOVWF   Bit_Cnt		

Dev_Loop2
	RRCF     Dev_Byte , f
	BTFSC   STATUS    , C
	call	Tx_One
	BTFSS   STATUS    , C
	call	Tx_Zero
	DECFSZ  Bit_Cnt  , f
	GOTO    Dev_Loop2
	retlw	0x00

Delay7
	movlw	0x09
	movwf	d1
Delay7_0
	decfsz	d1, f
	goto	Delay7_0
	NOP
	retlw	0x00

Delay18
	movlw	0x1A
	movwf	d1
Delay18_0
	decfsz	d1, f
	goto	Delay18_0
	NOP
	NOP
	retlw	0x00

Delay60
	movlw	0x5E
	movwf	d1
	movlw	0xEB
	movwf	d2
Delay60_0
	decfsz	d1, f
	goto	$+4
	decfsz	d2, f
	goto	Delay60_0
	goto	$+2
	nop
	retlw	0x00
	END
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top