
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;											;
;	PIC Microcontroller Tutorial Template	;
;											;
;	By Brad Slattery 2009					;
;	www.bradsprojects.com					;
;	brad@bradsprojects.com					;
;											;
;	'Designing electronic projects,			;
;	to spread the name of Jesus'			;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

								;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
								;																						;
								;	NOTE: If you are brand new to programming, then don't concern yourself to			;
								;	much with all of these initial lines of code. You can read up all about this		;
								;	at a later date if you wish. I'm sure that you would rather make an LED flash		;
								;	or make something interesting happen straight away right?							;
								;	All you have to know for now is that we will be placing our code just underneath	;
								;	the SETUP label, and then underneath the BEGIN label. it's really quite simple!		;
								;																						;
								;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


	LIST p=16f648a 				;	tell assembler what chip we are using (if you are using the 16f628a, then
	include "P16f648a.inc"		;	make sure you change this line and the previous line to read p16f628a
	__config h'3f18'			;	sets the configuration settings - internal oscillator, watchdog timer OFF
								;	Power Up Timer DISABLED, Master Clear DISABLED, Brown Out Detect DISABLED,
								;	Low Voltage Programming DISABLED, Data EE Read Protect Disabled,
								;	Code Protect OFF. (this will remain the same for all tutorials)


PC equ h'02'					;	The program counter will be refered to as PC - The program counter is
								;	a little counter within the microcontroller to let itself know what line number
								;	it is upto when running a program. We can make the microcontroller jump to a certain
								;	line number by changing the value stored in PC. (we will do this in a later tutorial)

	cblock h'20'				;	Within this cblock and endc, we can define our variables. More info on this, later.
	delay_1						;	These next two lines will set aside 1byte of ram for delay_1 and 1byte for delay_2
	delay_2						;	what we will do with these variables is load them with a number each so that we can
	endc						;	count down from that number to zero. when we reach zero, we continue with the program!
								;	more info on these two variables can be found in the delay routine.



	org h'0000'					;	This line just tells the microcontroller what address to start running our program from.
								;	It will always be 0000 hex for all the tutorials.


	movlw h'07'					;	This will turn the comparators OFF.	
	movwf CMCON 				;	(we just want to use the ports as digital ports)


	bsf STATUS, RP0 			;	select bank 1 (to enable us to change the Input / Output status of our ports)
	movlw b'00000000' 			;	set PORTB all outputs (A '0' means output, A '1' means input. We can set each
	movwf TRISB					;	We can set each bit individualy. Each port having 8-bits or 8 pins.
	movlw b'00100000' 			;	set PORTA all outputs except for bit 5. Bit 5 is an input ONLY pin.
	movwf TRISA 				;	It cannot be set to an output!
	bcf STATUS, RP0 			;	select bank 0




setup							;	I always have a title labeled 'setup' this is where we set everything up
	clrf PORTB					;	Turn all LEDs connected to PORTB OFF
								;	i.e. our variables and flags etc.. so they are ready for the main program


begin							;	This is where our main program starts.					
	movlw b'00000001'			;	move binary value 00000001 to the working register
	movwf PORTB					;	then send it to PORTB. Now the LED on the right will be ON
	call delay					;	call the delay to hold the LED ON for just a moment
				
	movlw b'00000010'			;	move binary value 00000010 to the working register
	movwf PORTB					;	then send it to PORTB. Now the LED second from the right will be ON
	call delay					;	call the delay to hold the LED ON for just a moment
				
	movlw b'00000100'			;	move binary value 00000100 to the working register
	movwf PORTB					;	then send it to PORTB. Now the LED third from the right will be ON
	call delay					;	call the delay to hold the LED ON for just a moment
				
	movlw b'00001000'			;	move binary value 00001000 to the working register
	movwf PORTB					;	then send it to PORTB. Now the LED fourth from the right will be ON
	call delay					;	call the delay to hold the LED ON for just a moment
		
	movlw b'00010000'			;	move binary value 00010000 to the working register
	movwf PORTB					;	then send it to PORTB. Now the LED fifth from the right will be ON
	call delay					;	call the delay to hold the LED ON for just a moment
				
	movlw b'00100000'			;	move binary value 00100000 to the working register
	movwf PORTB					;	then send it to PORTB. Now the LED sixth from the right will be ON
	call delay					;	call the delay to hold the LED ON for just a moment
				
	movlw b'01000000'			;	move binary value 01000000 to the working register
	movwf PORTB					;	then send it to PORTB. Now the LED seventh from the right will be ON
	call delay					;	call the delay to hold the LED ON for just a moment
				
	movlw b'10000000'			;	move binary value 10000000 to the working register
	movwf PORTB					;	then send it to PORTB. Now the LED eighth from the right will be ON
	call delay					;	call the delay to hold the LED ON for just a moment
				
	movlw b'01000000'			;	move binary value 01000000 to the working register
	movwf PORTB					;	then send it to PORTB. Now the LED seventh from the right will be ON
	call delay					;	call the delay to hold the LED ON for just a moment
				
	movlw b'00100000'			;	move binary value 00100000 to the working register
	movwf PORTB					;	then send it to PORTB. Now the LED sixth from the right will be ON
	call delay					;	call the delay to hold the LED ON for just a moment
				
	movlw b'00010000'			;	move binary value 00010000 to the working register
	movwf PORTB					;	then send it to PORTB. Now the LED fifth from the right will be ON
	call delay					;	call the delay to hold the LED ON for just a moment
				
	movlw b'00001000'			;	move binary value 00001000 to the working register
	movwf PORTB					;	then send it to PORTB. Now the LED fourth from the right will be ON
	call delay					;	call the delay to hold the LED ON for just a moment
				
	movlw b'00000100'			;	move binary value 00000100 to the working register
	movwf PORTB					;	then send it to PORTB. Now the LED third from the right will be ON
	call delay					;	call the delay to hold the LED ON for just a moment
				
	movlw b'00000010'			;	move binary value 00000010 to the working register
	movwf PORTB					;	then send it to PORTB. Now the LED second from the right will be ON
	call delay					;	call the delay to hold the LED ON for just a moment

	goto begin					;	and now go and do it all again (it will run in a continuous loop)
	


delay							;	here is a nice and simple delay routine
	movlw d'100'				;	copy the maximum number to our working register (decimal 255)
	movwf delay_1				;	and now copy it from the w register to delay_1 and delay_2
	movwf delay_2				;	Now the rest of the routine will focus on counting down to zero.
delay_loop						;	We come back to this label when we have not yet reached zero.
	decfsz delay_1, f			;	decrement whatever is in delay_1 by 1 and store the answer back in delay_1
		goto delay_loop			;	if the answer is not zero, then go back to the delay_loop label. but if the
	decfsz delay_2, f			;	answer is zero then decrement delay_2 by one and store the answer in delay_2
		goto delay_loop			;	if the answer is not zero, then go back to delay_loop label. but if the answer
	return						;	is zero, then we have completed our delay and now we can return to our main program!



	end							;	We always need to have end at the end, even if we don't want the program
								;	to actually end, it still must be here!