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.

New to PIC - Need some insight

Status
Not open for further replies.

rethier7

New Member
Hey all,

I'm teaching myself how to program PIC's. After about a week of pouring through tutorials and datasheets I came up with the attached code. I've also attached the datasheet for the 16F72 that was given to me to play with. I'm using the latest MPLAB and an McCon (PICStart) programmer, no motherboard. I was also careful to create adequate file paths in this case C:\PIC\PulseSw\

I'm not sure if I've calculated my delay loops correctly. And I had to create my own circuit to test my code.

I can successfully build and write this program except the intended LED goes on at power-up and the switch does nothing.

My circuit is simple enough:

12V 2A transformer, a rectifier with appropriate filter, and a 7805 voltage regulator + filters
+5V on Vdd (Pin20)
GND on Vss (Pin8)
GND on Vss (Pin19)
+5V on MCLR with series 1kΩ resistor
Normally open push-button switching GND directly on Pin 23 (pull-up resistors set on PORTB)
LED with series 330Ω resistor from Pin 22 to GND

I'm at a loss as to why this isn't working.
So thanks in advance for taking the time with giving me a hand!!
 

Attachments

  • 16F72 Datasheet.pdf
    2.5 MB · Views: 224
  • PulseSw.asm
    3.2 KB · Views: 156
The config you are using stated you are using a RC osc. Resistor Capacitor. Is that correct? What do you have on those OSCx pins?
PWRT is enabled
 
Last edited:
Try this if your using a crystal OSC:

Code:
		__CONFIG	3FBA

also you can simply post full code
Code:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;							Pulse Switch							;;
;;																	;;
;;Author: 	Ryan Ethier												;;
;;Version:	Beta													;;
;;Device:   PIC 16F72												;;
;;																	;;
;;Description:	pushbutton pulse on Pin 23 will source Pin 22		;;
;;				Second pulse sinks Pin 22							;;
;;																	;;
;;																	;;
;;																	;;
;;																	;;
;;																	;;
;;																	;;
;;																	;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;; DEVICE CONFIGURATION ;;;;;;;;;;;;;;;;;;;;;

		PROCESSOR	16F72
		#INCLUDE	<P16F72.INC>
		__CONFIG	3FBA    ;3FB3
		
;;;;;;;;;;;;;;;;;;;;;;;; LABELS ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

#DEFINE	COUNT1	0X08			; First counter for delay loops
#DEFINE	COUNT2	0X09			; Second counter for delay loops
#DEFINE	COUNT3	0X0D			; Third counter for delay loops
#DEFINE	OPREG	0X81			; Address of the OPTION register

;;;;;;;;;;;;;;;;;;;;; RESET VECTOR ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

		ORG		0X00
		GOTO	START

;;;;;;;;;;;;;;;;;;;;; DEVICE SETUP ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

START
		BSF		OPREG,7			; Set pull-up resistors on PortB
		BSF		INTCON,7		; Disable all interrupts
		BSF		STATUS,5		; Select memory bank 1
		MOVLW	B'00000100'		; Set RB2 as input
		MOVWF	TRISB			; Set RB2 as input
		BCF		STATUS,5		; Select memory bank 1

;;;;;;;;;;;;;;;;;;;;;; SCAN PIN 23 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

SCAN1	BTFSC	PORTB,2			; Bit Test F and Skip if Clear on PortB Bit 2
		GOTO	SCAN1			; Do this until PortB Pin 23 goes low
		
;;;;;;;;;;;;;;;;;;;;;;;; SOURCEOUT;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

		BSF		PORTB,1			; Pin 23 went high so source Pin 22

;;;;;;;;;;;;;;;;;;;;;;;;; TIMER1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; a delay loop so that the program doesn't scan pin 23 
; a zillion times for each press of the button

		MOVLW	0X08
		MOVWF	COUNT3		
LOOP1	DECFSZ	COUNT1,1		; Decrement F COUNT1 by 1 and Skip Zero 
		GOTO	LOOP1			; 2 Instructions x 256 = 512 intructions
		DECFSZ	COUNT2,1
		GOTO	LOOP1			; 512 + 2 more intructions 256 times = 131,584 instructions
		DECFSZ	COUNT3,1
		GOTO	LOOP1			; 15 X 131,584 @ 1 MIPS (4MHz/4 insturctions per cycle) = 2 second delay
	
;;;;;;;;;;;;;;;;;;;;;; SCAN PIN 23 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

SCAN2	BTFSC	PORTB,2			; Bit Test F and Skip if Clear on PortB Bit 2
		GOTO	SCAN2			; Do this until PortB Pin 23 goes high

;;;;;;;;;;;;;;;;;;;;;;;;; SINKOUT ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

		BCF		PORTB,1			; Clear Pin 22

;;;;;;;;;;;;;;;;;;;;;;;;; TIMER2 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; a delay loop so that the program doesn't scan pin 23 
; a zillion times for each press of the button

		MOVLW	0X08
		MOVWF	COUNT3		
LOOP2	DECFSZ	COUNT1,1		; Decrement F COUNT1 by 1 and Skip Zero
		GOTO	LOOP2			; 2 Instructions x 256 = 512 intructions
		DECFSZ	COUNT2,1
		GOTO	LOOP2			; 512 + 2 more intructions 256 times = 131,584 instructions
		DECFSZ	COUNT3,1
		GOTO	LOOP2			; 15 X 131,584 @ 1 MIPS (4MHz/4 insturctions per cycle) = 2 second delay

;;;;;;;;;;;;;;;;;;;;;;; PROGRAM LOOP ;;;;;;;;;;;;;;;;;;;;;;;;;;

		GOTO	SCAN1			; Start scanning again

	END
 
Last edited:
AtomSoft:

Thanx for looking my code over....

I'm not entirely sure (i think i saw this somewhere in the datasheet) but the RC configuration is for the internal clock.
Please correct me if i'm wrong.
 
No problem :)
There are only 4 OSC modes for that PIC...

LP, XT, HS, RC

No internal OSC sorry (from what i can see)


From Page 64 of datasheet (Page 63 has the available types)
clipboard02-jpg.48048
 

Attachments

  • Clipboard02.jpg
    Clipboard02.jpg
    56.3 KB · Views: 302
Last edited:
Thanx Atom

After all the homework i did i feel a little retarded for that oversight.

100k R and 22pF C should do the trick.

gotta admit though.... not bad for a self taught, first try!!
 
yeah i was way worse than you trust me... your doing better than me when i started heh! Keep up the good work and remember we are here to help... (if you need it)
 
Status
Not open for further replies.

Latest threads

Back
Top