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.

Programming all PIC chips with PICkit 2

Status
Not open for further replies.
Thanks your a star :) I've been trying to find that info out for ages now reading all sorts but nothing actually says it can power the pic directly with nothing else needed!
BTW hope you didn't mind me modding your original pic, but I did leave your name on it so people knew where it came from
 
Last edited:
were did you get your breadboard power supply and the heatsink for it
The power supply came from **broken link removed**. I have three of them. Excellent product.

The heat sink came from the junk pile. No idea what I ripped apart to get it, but I had stacks of old printers, so probably that's what it came off. It's not really necessary for that power supply. I just wanted it to run cool, so... :p
 
Hey I like that breadboard Power supply! I think I will get one, or two.

I find I make sure I have two of everything, since I usually blow-up or use parts for something else. Always helps to have a backup...hehe

Cheers for the links and info,

-BaC
The power supply came from **broken link removed**. I have three of them. Excellent product.

The heat sink came from the junk pile. No idea what I ripped apart to get it, but I had stacks of old printers, so probably that's what it came off. It's not really necessary for that power supply. I just wanted it to run cool, so... :p
 
Sweet - thats good to know!

I have a breadboard already...just briefly do you have a circuit diagram of what pins I attach to what?

I see in one of your photo's you have resistors and capacitors across the values from the pin as well....its just a bit hard to see what pin number from the PICkit 2 kit goes to what terminals on the PIC chip.

I suppose P6 goes to Vdd for starters.

Thanks,

Craig

the pickit2 starters guide has the pinot of the header in it
 
Hey I like that breadboard Power supply! I think I will get one, or two.

I find I make sure I have two of everything, since I usually blow-up or use parts for something else. Always helps to have a backup...hehe

Cheers for the links and info,

-BaC

just use a 5 V wall wart from an old phone charger or something and put header/jumper teminals on the end that have 2.54 mm spacing so it plugs straight into the breadboard
 
source code for PIC16F887

Hi, can some experience help me with this very problem i am facing. i am trying to write a source code to generate a 200ms pulse at various interval, let say.. 5s, 50, 100..... using PIC16F887. I used the AD converter becos i wanted to use a rotary switch to control the frequency or speed.

i have only few days to make this work. i need to used this switch to drive a relay.

Can someone help me look at this code and the errors in it or better still help me debug or edit it. i have tried wit could not build. at an instance it did build when i started the program with

org 0x2100. but the Led did not light. i wanted to use the led to monitor the pulse time.

I tried to debug the code and i always recieve error

CORE-E0002: Stack under flow error occurred from instruction at 0x000005

Thank you





Code:
l   istp=16f887	; list directive to define processor
	#include	<p16f887.inc>	; processor specific variable definitions


; '__CONFIG' directive is used to embed configuration data within .asm file.
; The labels following the directive are located in the respective .inc file.
; See respective data sheet for additional information on configuration word.

	__CONFIG    _CONFIG1, _LVP_OFF & _FCMEN_ON & _IESO_OFF & _BOR_OFF & _CPD_OFF & _CP_OFF & _MCLRE_ON & _PWRTE_ON & _WDT_OFF & _INTRC_OSC_NOCLKOUT
	__CONFIG    _CONFIG2, _WRT_OFF & _BOR21V

; variable decleration

     cblock 0x00
Delay1           ; Assign an address to label Delay1
Delay2
Display          ; define a variable to hold the diplay
timer1      
incf_count

Endc
        org      0x0021     ;  Start  program  at  address   
count   equ      0x20  	    ;  Counter  variable        
        nop  			    ;  required for debugger

 ;  Initialize  PORT  D  to  be  all  outputs and PORT A all inputs.

Start
        BANKSEL PORTD
        CLRF    PORTD
        BANKSEL TRISD        
        movwf   TRISA          ;  Make PortA all input
        clrf    TRISD          ;  Set PORTD for output        
        clrf    PORTD          ;  write 0s to PORTD
        clrf    count          ;  Initialize  count  to  0. 

;  Setup  timer  1 

	    bcf     T1CON,TMR1ON        ;  Turn  Timer  1  off. 
	    bsf     T1CON,T1CKPS1       ;  Set  prescaler  for  divide bsf T1CON,T1CKPS0 by  8.                	       
        bcf     T1CON,T1OSCEN       ;  Disable  the  RC  oscillator. 
        bcf     T1CON,TMR1CS        ;  Use  the  Fosc/4  source. 
        clrf    TMR1L               ;  Start  timer  at  0000h 
        clrf    TMR1H               ;  clear the pre-scalar counter
        bsf     T1CON,TMR1ON        ;  Start  the  time

Time1   btfss   PIR1,TMR1IF              ;  Did  timer  overflow? 
        
;  Timer  overflowed,  increment  counter  and  display 

        bcf    PIR1,0     ;  Clear  the  flag 
        incf   count,F    ;  Bump  the  counter 
        movf   count,W    ;  Get  the  count 
        movwf  PORTD      ;  Send  to  Port  D. 
        bcf    T1CON,0    ;  Turn  the  timer  off. 
        movlw  80h        ;  Start  timer  at  80h 
        movwf  TMR1H
        clrf   TMR1L
        bsf    T1CON,0    ;  Turn  the  timer  on. 
  
        movlw     0xFF
        movlw     0x00           ; Left Justified, Vdd-Vss referenced
        movwf     ADCON1
		BANKSEL   TRISA        
        movlw     0xFF           ; we want all Port A pins Analog
        movwf     ANSEL
        bcf       STATUS,RP0     ; back to Register Bank 0    
        movlw     0x41
        movwf     ADCON0         ; configure A2D for Fosc/8, Channel 0 (RA0), and turn on the A2D module

        nop                      ; wait 5uS for A2D amp to settle and capacitor to charge.
        nop                      ; wait 1uS
        nop                      ; wait 1uS
        nop                      ; wait 1uS 
        nop                      ; wait 1uS
        bsf       ADCON0,GO_DONE ; start conversion
        btfss     ADCON0,GO_DONE ; this bit will change to zero when the conversion is complete
        goto      $-1

        movf      ADRESH, W      ; Copy the display to the LEDs
        movwf     PORTD

        goto      timer1          

        End               ; directive 'end of program'
 
@iyke20024,
Please try the following code. I have now seen on MPLAB simulator, that the bit 0 of PORTD changes accordingly. Also a zip file containing the .asm is attached for ready use to finalise to your needs.


Code:
   		list p=16f887  ; list directive to define processor 
 		#include  <p16f887.inc>  ; processor specific variable definitions 
 
 
; '__CONFIG' directive is used to embed configuration data within .asm file. 
; The labels following the directive are located in the respective .inc file. 
; See respective data sheet for additional information on configuration word. 
 
	__CONFIG    _CONFIG1, _LVP_OFF & _FCMEN_ON & _IESO_OFF & _BOR_OFF & _CPD_OFF & _CP_OFF & _MCLRE_ON & _PWRTE_ON & _WDT_OFF & _INTRC_OSC_NOCLKOUT
	__CONFIG    _CONFIG2, _WRT_OFF & _BOR21V
 
 	errorlevel -302
; variable decleration 
 
     cblock 0x00 
Delay1           ; Assign an address to label Delay1 
Delay2 
Display          ; define a variable to hold the diplay 
timer1       
incf_count 
 
		Endc 
        org      0x0021    ;  Start  program  at  address    
count   equ      0x20        ;  Counter  variable         
        nop          ;  required for debugger 
 
 ;  Initialize  PORT  D  to  be  all  outputs and PORT A all inputs. 
 
Start 
        BANKSEL PORTD 
        CLRF    PORTD 
        BANKSEL TRISD         
		MOVLW   0X00
		MOVWF   TRISD          ;  Set PORTD for output         
        BANKSEL PORTA
        MOVLW   0XFF
        movwf   TRISA           ;  Make PortA all input 
        MOVLW   0X00
;                clrf    PORTD          ;  write 0s to PORTD 
        clrf    count          ;  Initialize  count  to  0.  
 
;  Setup  timer  1  
 
     	 bcf   	T1CON,TMR1ON        ;  Turn  Timer  1  off.  
     	 bsf  	T1CON,T1CKPS1       ;  Set  prescaler  for  divide bsf T1CON,T1CKPS0 by  8.                          
        bcf     T1CON,T1OSCEN       ;  Disable  the  RC  oscillator.  
        bcf     T1CON,TMR1CS        ;  Use  the  Fosc/4  source.  
        clrf    TMR1L               ;  Start  timer  at  0000h  
        clrf    TMR1H               ;  clear the pre-scalar counter 
        bsf     T1CON,TMR1ON        ;  Start  the  time 
 
Time1   btfss   PIR1,TMR1IF              ;  Did  timer  overflow?  
         
;  Timer  overflowed,  increment  counter  and  display  
 
        bcf    PIR1,0     ;  Clear  the  flag  
        incf   count,F    ;  Bump  the  counter  
        movf   count,W    ;  Get  the  count  
        movwf  PORTD      ;  Send  to  Port  D.  
        bcf    T1CON,0    ;  Turn  the  timer  off.  
        movlw  80h        ;  Start  timer  at  80h  
        movwf  TMR1H 
        clrf   TMR1L 
        bsf    T1CON,0    ;  Turn  the  timer  on.  
   
        movlw     0xFF 
        movlw     0x00           ; Left Justified, Vdd-Vss referenced 
        movwf     ADCON1 
   		BANKSEL   TRISA         
        movlw     0xFF           ; we want all Port A pins Analog 
        movwf     ANSEL 
        bcf       STATUS,RP0     ; back to Register Bank 0     
        movlw     0x41 
        movwf     ADCON0         ; configure A2D for Fosc/8, Channel 0 (RA0), and turn on the A2D module 
 
        nop                      ; wait 5uS for A2D amp to settle and capacitor to charge. 
        nop                      ; wait 1uS 
        nop                      ; wait 1uS 
        nop                      ; wait 1uS  
        nop                      ; wait 1uS 
        bsf       ADCON0,GO_DONE ; start conversion 
        btfss     ADCON0,GO_DONE ; this bit will change to zero when the conversion is complete 
        goto      $-1 
 
        movf      ADRESH, W      ; Copy the display to the LEDs 
       BANKSEL 	PORTD
        movwf     PORTD 
 
        goto      timer1           
 
        End               ; directive 'end of program'
 

Attachments

  • iykkke20024.zip
    1.5 KB · Views: 252
Last edited:
Hi serma,

The problem was because i used a linker file.

check the code below. It built, but did not output to PORTD. the led did not blink.

See, i am not too Good with this stuff, but i am learning everyday.

you can look at the code and try to debug it and see if there is something wrong.

Does the precedence mater. i.e setting up the timer first before converting from analogue to digital?

Code:
	list	p=16f887	; list directive to define processor
		#include <p16f887.inc>	; processor specific variable definitions
			 

; '__CONFIG' directive is used to embed configuration data within .asm file.  
; The labels following the directive are located in the respective .inc file.  
; See respective data sheet for additional information on configuration word.  

		__config _CONFIG1, _LVP_OFF & _FCMEN_ON & _IESO_OFF & _BOR_OFF & _CPD_OFF & _CP_OFF & _MCLRE_ON & _PWRTE_ON & _WDT_OFF & _INTRC_OSC_NOCLKOUT
		__config _CONFIG2, _WRT_OFF & _BOR21V

			 
; variable decleration 
		udata
Delay1		res	1		; Assign an address to label Delay1
Delay2		res	1
Display		res	1		; define a variable to hold the diplay
timer1		res	1
incf_count	res	1

count		equ	0x20		;  Counter  variable 

Reset		code	0x0000		;  Start  program  at  address      
		goto	main

IRQ		code	0x0004
		retfie

		code
					;  Initialize  PORT  D  to  be  all  outputs and PORT A all inputs. 

main
		
		BANKSEL	TRISD		
		clrf	TRISD		;  Set PORTD for output
	    BANKSEL	PORTD
		clrf	PORTD		;  write 0s to PORTD
		clrf	count		;  Initialize  count  to  0. 

;  Setup  timer  1  

		bcf	T1CON,TMR1ON	;  Turn  Timer  1  off. 
		bsf	T1CON,T1CKPS1	;  Set  prescaler  for  divide bsf T1CON,T1CKPS0 by  8.                	       
		bcf	T1CON,T1OSCEN	;  Disable  the  RC  oscillator. 
		bcf	T1CON,TMR1CS	;  Use  the  Fosc/4  source. 
		clrf	TMR1L		;  Start  timer  at  0000h 
		clrf	TMR1H		;  clear the pre-scalar counter
		bsf	T1CON,TMR1ON	;  Start  the  time

Timer1		btfss	PIR1,TMR1IF	;  Did  timer  overflow? 
			 
;  Timer  overflowed,  increment  counter  and  display   

		bcf	PIR1,0		;  Clear  the  flag 
		incf	count,F		;  Bump  the  counter 
		movf	count,W		;  Get  the  count 
		movwf	PORTD		;  Send  to  Port  D. 
		bcf	T1CON,0		;  Turn  the  timer  off. 
		movlw	80h		;  Start  timer  at  80h 
		movwf	TMR1H
		clrf	TMR1L
		bsf	T1CON,0		;  Turn  the  timer  on. 
			 
		movlw	0xFF		
		movwf	ADCON1
		BANKSEL	TRISA
        movlw	B'11111111'		; Left Justified, Vdd-Vss referenced
        movwf	TRISA		;  Make PortA all input
		movlw	0xFF		; we want all Port A pins Analog
		movwf	ANSEL
		bcf	    STATUS,RP0	; back to Register Bank 0    
		movlw	0x41
		movwf	ADCON0		; configure A2D for Fosc/8, Channel 0 (RA0), and turn on the A2D module

		nop			; wait 5uS for A2D amp to settle and capacitor to charge.
		nop			; wait 1uS
		nop			; wait 1uS
	    nop			; wait 1uS 
		nop			; wait 1uS

		bsf	    ADCON0,GO_DONE	; start conversion
		btfss	ADCON0,GO_DONE	; this bit will change to zero when the conversion is complete
		goto	$-1

		movf	ADRESH,W	; Copy the display to the LEDs

		movwf	PORTD

		goto	timer1          

		end			; directive 'end of program'
 
Hi,
Code:
	bsf	T1CON,0		;  [COLOR="Red"]bank 0, correct[/COLOR]
			 
	movlw	0xFF		
	movwf	ADCON1  ;  [COLOR="Red"]supposed to be in bank 1[/COLOR]
	BANKSEL	TRISA
	movlw	B'11111111'		; Left Justified, Vdd-Vss referenced
	movwf	TRISA		;  Make PortA all input
	movlw	0xFF		; we want all Port A pins Analog
	movwf	ANSEL ;  [COLOR="Red"]this is in bank 3[/COLOR]


And, shouldn't this to be:
Code:
	btfs[COLOR="Red"]c[/COLOR]	ADCON0,GO_DONE	; this bit will change to zero when the conversion is complete
	goto	$-1
you're reading the result register before the conversion ends.
 
does it really needs pull-down resistor? every time I program a PIC18F2550 without using pulldown resistor it works fine. XD
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top