PIC not operating correctly out of programmer

Status
Not open for further replies.

Pete_UK

New Member
Hi Guys,

The project that I'm working on now is and operating exactly how I need it to. Today I've been taking the 16F628A out of the programmer environment and on to some breadboard to test it with an independent power supply (a 7805).

The PIC sends and receives information over USART but I keep on getting gobble-dee-gook in RealTerm and Hyperterminal when the PIC isn't connected to the PIC Kit 2. I began eliminating problems such as baud rate settings and have tried two different power supplies but still get the same results on screen. I've tried different bypass capacitors such as polyester, ceramic and electrolytic with different values across the power rails but still no luck.

Has anyone experianced this before? The configuration settings are as follows, but I'm pretty sure they're correct as the PIC works as expected in the programmer:
Code:
__config _BOREN_ON & _DATA_CP_OFF & _PWRTE_OFF & _WDT_OFF & _LVP_OFF & _MCLRE_OFF & _INTOSC_OSC_NOCLKOUT
I've been connecting MCLR directly to 5v.

Thanks for any help or suggestions!

Pete
 
Schematics, pictures and the pertinent code would help us to help you better.

With MCLR off you don't need to connect it.
 
MPLAB seems to add the debug code if you have the PK2 selected as a debugger. Select your PK2 as a programmer and it should work fine.

Mike.
 
Schematics, pictures and the pertinent code would help us to help you better.

With MCLR off you don't need to connect it.

Hi Jimlovell777,

I've attached the schematic and code below. The capacitor has been changed for a number of values and types to see if it affected the output.

Code:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;									USB Relay Interface										;;
;;								---------------------------									;;
;;	Author: Peter Stevens																	
;;	Version:0.9
;;	Date: 01/05/2010																	
;;	Description: Receives input over USB via UM232R USB to Serial converter, puts	
;;	input on to PORTA to control 32 bit channel relay card. Input is echoed back.		

;
		LIST P=16F628A, R=dec	;16F628A PROCESSOR
;
		#include "P16F628A.INC"  ; Include header file
;
   	 	__config _BOREN_ON & _DATA_CP_OFF & _PWRTE_OFF & _WDT_OFF & _LVP_OFF & _MCLRE_OFF & _INTOSC_OSC_NOCLKOUT
;
   	 	CBLOCK 0x20             ; Declare variable addresses starting at 0x20
     	 ; No Variables declared
    	ENDC
;
   	 	ORG    0x000            
;
		movlw 0x07					
		movwf CMCON				;Turn comparators off
;
; INITIALISE PORTS
;
			bsf	STATUS,RP0			;Select Bank 1
;
		movlw b'00100000'		;TRISA set to OUTPUT, RA5 INPUT
		movwf TRISA
;
		movlw b'00000110'		;Set RB2 to Tx, RB1 to RX
		movwf TRISB
;
; CONFIGURE BAUD RATE AND TX/RX REGISTER
; 9600 baud, 8 data bits, 1 stop bit, no parity bit
;	
		movlw 0x19				;9600 baud with 4Mhz Internal Oscillator
		movwf SPBRG
;	
		movlw b'00100100'		;Transmit enable, high baud rate
		movwf TXSTA
;
			bcf	STATUS,RP0			;Select Bank 0
;
		movlw b'10010000'		;Asyn. Comms enable. Continuous Rx enable.
		movwf RCSTA
;
; MAIN PROGRAM
;
		call cconf				;Comms Confirmation Message
main	clrf PORTA
;
receive btfss PIR1,RCIF			;Checks bit RCIF of PIR1 ("Is receive buffer full?")
		goto receive			;Loops until receive buffer is full
;
		movf RCREG,W			;Move the contents of RCREG in to W
		movwf PORTA				;Moves value in W on to PORTA
;
		call txmit				;Transmit sub-routine
		goto main				;Go to start of program
;
; Tx SUB-ROUTINE
;
txmit	movwf TXREG				;Moves contents of W in to TXREG. Echos back recieved byte.
;
			bsf STATUS,RP0			;Select Bank 1
;
txtest	btfss TXSTA, TRMT		;Checks bit TRMT of TXSTA ("Has message been sent?")
		goto txtest				;Loop until transmit complete
;
			bcf STATUS,RP0			;Select Bank 0
		return
;
;
; COMMS MESSAGE
;
cconf	movlw 'C'
		call txmit
		movlw 'O'
		call txmit
		movlw 'M'
		call txmit
		movlw 'M'
		call txmit
		movlw 'S'
		call txmit
		movlw 0x20				;Space
		call txmit
		movlw 'C'
		call txmit
		movlw 'O'
		call txmit
		movlw 'N'
		call txmit
		movlw 'N'
		call txmit
		movlw 'E'
		call txmit
		movlw 'C'
		call txmit
		movlw 'T'
		call txmit
		movlw 'E'
		call txmit
		movlw 'D'
		call txmit
		movlw 0xD				;Return
		call txmit
;
		return
;
		END

I've implemented all your suggestions as a trial and I'm still getting the erroneous results when using an independany PSU. The 16F628A doesn't have _DEBUG_OFF in its configuration fuses, but I did ensure that it was disabled as the debugger and selected as the programmer (up to now I've been using the PicKit 2 software and importing the hex file).

I did a small trial with a small PWM program that I wrote and that worked fine when connected to a separate power supply which makes the problem more peculiar.

Thanks for you time,

Peter

**broken link removed**
 
Last edited:
i just wrote some usart in C, this set of setup instructions was for a 18f4550 to a 18f2550. The data sheet will probably explain what everything does after you read it 348 times...

Code:
	// TXREG - Read/Write Transmit buffer Register 
	//		 - loaded with data by software
	//		 - empties once transfer to shift register occurs
	//		 - once empty TXIF Flag is set
	// TXIF	 - not alterable by sofware
	// 		 - if polling wait a tid after TXREG
	// TXIE  - interrupt clearing mechanism
	// TRMT  - shows shift register status. read only 
	//		 - poll to determine if Shift register is empty


	// SYNC  - Enable Asynchronous: 0;
	// SPEN  - Serial port setting bit: 1;
	// RCIE  - Interrupt enable
	// RX9   - 9 bit reception
	// CREN  - Enables reception
	// RCIE  - Flag to generate RCIF interrupts
	// RCIF  - Reception flag. Indicates complete reception
	// RCSTA - 9th bit location. error checking
	// RCREG - Reads 8 bit data received
	// CREN  - Clear error, clearing enable bit
	// GIE and PEIE should be set to use interrupts

	SYNC = 0 ;				// Asynchronous mode	
	SPEN = 1;				// Serial port enable.
	TRISC6 = 0;
	TRISC7 = 1;
	TXEN  = 1; 				// Transmit enable 
	CREN = 1;				// Enable the reception 
	RCIF = 0;
	RCIE = 1;				// Reception Interrupt Enable
	GIE = 1;				// Global Interrupt Enable
	PEIE = 1;				// Perapheral Interrupt Enable
	INTCON=0;	// purpose of disabling the interrupts.
	init_comms();	// set up the USART - clock
		
	RA3 = 1; // power / status

	while(1)
	{
		int delay = 15000;
		int i = 0;

		while (!RCIF){ // reception is finished

				scan(&ledArray, wait);
		}

 		//allOff(&ledArray); // clear led array
		setupScanArray(&ledArray, RCREG);
		scan(&ledArray, wait);

		CREN = 0; // reset the error bit
		CREN = 1;
   //etc
 
Hi again,

I've changed the PIC I'm using to an 16F876A and still encountering the same problem. It's becoming a real headache now and totally hit a brick wall. I've been looking through pages and pages of documentation and can't find what's going wrong.

This is the code I'm using for the 16F876A, but it's not too different from the previous I posted apart from a few configuration fuse settings. I've also attached a more comprehensive schematic of the power supply circuit I've been using.

Code:
    List p=16F876A, R=dec            ;16F876A Processor, Decimal radix enabled

    #include "P16F876A.INC"            ;Include header file

    __config _CP_OFF & _WRT_OFF & _DEBUG_OFF & _CPD_OFF & _LVP_OFF & _BODEN_ON & _PWRTE_ON & _WDT_OFF & _XT_OSC

    CBLOCK 0x20
    ;No variables declared
    ENDC
      
    ORG        0x0000                ;Program begins

                bsf        STATUS,RP0    ;Select Bank 1

        movlw    0x07
        movwf    CMCON            ;Turn Comparators off
  
;USART CONFIGURATION
;9600 baud, 8 data bits, 1 stop bit, no parity bit

        movlw    b'00000000'
        movwf    TRISB            ;TRISB configured for output

        movlw    b'11000000'
        movwf    TRISC            ;PORTC bits <7:6> enabled for communications

        movlw    b'00100100'
        movwf    TXSTA            ;Enable asynchronous transmission
  
        movlw    d'25'
        movwf    SPBRG            ;9600 baud with 4Mhz External Xtal Osc.

                bcf        STATUS,RP0    ;Select Bank 0

        movlw    b'10010000'
        movwf    RCSTA

;MAIN PROGRAM

        call cconf                ;Comms Confirmation Message

main    movlw    b'00110000'      
        movwf    PORTB            ;Default output: Decoders off (active low), Reset off

receive btfss    PIR1,RCIF        ;Checks bit RCIF of PIR1 ("Is receive buffer full?")
        goto     receive            ;Loops until receive buffer is full

        movf     RCREG,W            ;Move the contents of RCREG in to W

        movwf     PORTB            ;Move contents of W in to PORTB (set the relays)

        call     txmit            ;Echo back the input to terminal
        goto     main            ;Loop back to beginning of program

;Tx SUB-ROUTINE

txmit    movwf     TXREG            ;Moves contents of W in to TXREG. Echos back recieved byte.

                bsf STATUS,RP0    ;Select Bank 1

txtest    btfss     TXSTA, TRMT        ;Checks bit TRMT of TXSTA ("Has message been sent?")
        goto     txtest            ;Loop until transmit complete

                bcf STATUS,RP0    ;Select Bank 0
        return

;COMMS MESSAGE

cconf    movlw 'C'
        call txmit
        movlw 'O'
        call txmit
        movlw 'M'
        call txmit
        movlw 'M'
        call txmit
        movlw 'S'
        call txmit
        movlw 0x20                ;Space
        call txmit
        movlw 'C'
        call txmit
        movlw 'O'
        call txmit
        movlw 'N'
        call txmit
        movlw 'N'
        call txmit
        movlw 'E'
        call txmit
        movlw 'C'
        call txmit
        movlw 'T'
        call txmit
        movlw 'E'
        call txmit
        movlw 'D'
        call txmit
        movlw 0x0D                ;Carriage Return
        call txmit
        return
      
    END
**broken link removed**

I'm open to all ideas on how to make this darned thing work!

Regards,
Pete
 
In MPLAB under configure->configuration bits, is "Configuration bits set in code" ticked?

Can you add an LED and flash it at the start of your code to see if it's randomly reseting?

Then, light it at the end of your code to show it's completing.

Mike.
 
Hi,

Notice your code states you are using a usb to serial converter cable.
Are you outputting the pic direct or using a rs232 chip ?

Why don't you try using your code with the USART tool of the Pickit2, you can output the pic directly to the pk2 without any rs232 chip - hopefully that shoud eliminate any problems with that usb converter or the PC side of things.

Nigels rs232 tutorial used the same chip so why not try his code as well - that should isolate the error.
 
Hi all,

Thanks for the replies and suggestions. The configuration bits are set in code.

Today I inserted a small piece of code that flashes two LEDs on PORTB and they work as expected when the PIC is powered-up. Then when the communications start the transmitted message and echoed data bytes are scrambled up.

I've placed an oscilloscope probe on to the OSC2 drive pin of the PIC and I get a clean signal and the program woks absolutely fine with external power. The input capacitance of the probe is 30pF so I thought changing the ceramic capacitor for a 33pF (closest value I have) might work, but it didn't. The crystal I'm using is a ACT HC49/U with a peak-to-peak voltage of 0.2V.

With the probe attached I can send a byte to the PIC and turn on the LEDs correctly.

Thanks for your time. Regards,
Pete
 
Last edited:
How many wires do you have going to your RS232 adapter? Just the TX and RX? Do you have a common ground between your project and receiving computer? Sounds like maybe the PICKit 2 being connected to your computer provides that, double check.
 
Hi,

In addition to what Jim has jut said, you have not actually said what your normal power supply is ..? am I right in assuming you are using the +5v usb line.

I generally use the same set up and have run pwm and comms without problems, however on occassion I have had similar unexplainable errors and only on trying an external psu do the errors clear.

Would think you have a 'bad' usb or pc power line - extra filtering might cure it, but if your program work fine on the external psu I think thats case proven.
 
How many wires do you have going to your RS232 adapter? Just the TX and RX? Do you have a common ground between your project and receiving computer? Sounds like maybe the PICKit 2 being connected to your computer provides that, double check.

Ah ha! It works! You were right. I've been using a UM232R USB to TTL converter module and of course it's linked to ground in the USB hardware in the computer with the PicKit2. I added the grounds and it works!

That is definitely a lesson learnt. Thank you everyone for your input.

Regards,
Pete
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…