16f628 test project - ok what am i doing wrong?

Thread Starter

bluebrakes

Joined Oct 17, 2009
252
I've built a test circuit for learning how to program the 16f628 pic but I can't understand why my circuit or my code isn't working properly.

My code is...
Still learning so bear with me. :)

LIST P=16F628, R=DEC ; Use the PIC16F628 and decimal system
#include "P16F628.INC" ; Include header file
__config _INTRC_OSC_NOCLKOUT & _LVP_OFF & _WDT_OFF & _PWRTE_ON & _BODEN_ON
CBLOCK 0x20 ; Declare variable addresses starting at 0x20
Loop1,Loop2
ENDC
;
; -----------
; INITIALIZE
; -----------
;
ORG 0x000 ; Program starts at 0x000

CLRF PORTA ; Initialize port A
CLRF PORTB ; Initialize port B

BSF STATUS,RP0 ; RAM bank 1

CLRF TRISA ; All pins port A output
CLRF TRISB ; All pins port B output

BCF STATUS,RP0 ; RAM bank 0
;
; ------------------------
; FUNCTION OF PORT A PINS
; ------------------------
;
MOVLW 7
MOVWF CMCON ; Comparators off, all pins digital I/O
;
; ----------
; MAIN LOOP
; ----------
;
Main BSF PORTA,2 ; Turn on LED connected to RA2
CALL delay
BCF PORTA,2 ; Turn off LED connected to RA2
CALL delay

BSF PORTB,2 ; Turn on LED connected to RB2
CALL delay
BCF PORTB,2 ; Turn off LED connected to RB2
CALL delay

BSF PORTB,3 ; Turn on LED connected to RB3
CALL delay
BCF PORTB,3 ; Turn off LED connected to RB3
CALL delay

BSF PORTB,4 ; Turn on LED connected to RB4
CALL delay
BCF PORTB,4 ; Turn off LED connected to RB4
CALL delay

BSF PORTB,5 ; Turn on LED connected to RB5
CALL delay
BCF PORTB,5 ; Turn off LED connected to RB5
CALL delay

GOTO Main
;
; ---------------
; DELAY 250 MSEC
; ---------------
;
delay MOVLW 250
MOVWF Loop1
Outer MOVLW 200
MOVWF Loop2
Inner NOP
NOP
DECFSZ Loop2,F
GOTO Inner ; Inner loop = 5 usec.
DECFSZ Loop1,F
GOTO Outer
RETURN

END

my circuit is attached.

Thanks,
Dominic
 

Attachments

Thread Starter

bluebrakes

Joined Oct 17, 2009
252
I forgot to mention d5, d6, d7 all flash as they should but d3 stays on all the time and none of the others flash/on (d1,d2, d4)

This is what the problem is.
 

Thread Starter

bluebrakes

Joined Oct 17, 2009
252
This is something I haven't quite worked out how to do properly. I can run it through the program but I can't seem to see the results of the coding?

Any hints?
 

eng1

Joined Oct 25, 2009
13
I forgot to mention d5, d6, d7 all flash as they should but d3 stays on all the time and none of the others flash/on (d1,d2, d4)

This is what the problem is.
D1,D2,D4 are not supposed to flash because you don't control the respective pins of PORTA in your code.

When RA5 function is /MCLR, as in your code, this is an active low pin and you need to connect it to Vdd through resistor R9 during operation, and the switch to ground.
 

Thread Starter

bluebrakes

Joined Oct 17, 2009
252
That's really bad of me. the schematic is slightly wrong, the MCLR it is indeed wired to switch to ground.

As for the coding, it doesn't explain why one of the LEDs is stuck on and why another won't even light.

Another problem i'm having is when I go to reprogram the pic, I erase it, which it says is sucessful but when i do a blank check, it says its not blank? If i erase it and put the pic back in the circuit it's still running the code. grrr!
 

BMorse

Joined Sep 26, 2009
2,675
what programmer are you using???


and try this for your config so it matches code and circuit....

__config _INTRC_OSC_NOCLKOUT & _LVP_ON & _WDT_OFF & _PWRTE_OFF & _BODEN_OFF

 

Thread Starter

bluebrakes

Joined Oct 17, 2009
252
picstart plus with the latest firmware chip 4.5

I keep getting this sometimes, when i erase and then blank check it.

----------------------------------
Erase operation is successful.

Device is NOT blank:
Program Memory
Configuration Bits
----------------------------------
 

BMorse

Joined Sep 26, 2009
2,675
picstart plus with the latest firmware chip 4.5

I keep getting this sometimes, when i erase and then blank check it.

----------------------------------
Erase operation is successful.

Device is NOT blank:
Program Memory
Configuration Bits
----------------------------------

If you are programming the uc via ICSP, you need to set LOW VOLTAGE PROGRAMMING to ON, and you can not have anything connected to the LVP/PGM pin RB4, since you have an LED on that pin, could cause programming and erasing issues on the uc when you are programming it....
 

BMorse

Joined Sep 26, 2009
2,675
here's what it says in the datasheet:

14.13 Low Voltage Programming
The LVP bit of the configuration word, enables the low
voltage programming. This mode allows the microcontroller
to be programmed via ICSP using only a 5V
source. This mode removes the requirement of VIHH to
be placed on the MCLR pin. The LVP bit is normally
erased to '1', which enables the low voltage programming.
In this mode, the RB4/PGM pin is dedicated to
the programming function and ceases to be a general
purpose I/O pin. The device will enter Programming
mode when a '1' is placed on the RB4/PGM pin. The
HV Programming mode is still available by placing VIHH
on the MCLR pin.
If Low voltage Programming mode is not used, the LVP
bit can be programmed to a '0', and RB4/PGM
becomes a digital I/O pin. To program the device, VIHH
must be placed onto MCLR during programming. The
LVP bit may only be programmed when programming
is entered with VIHH on MCLR. The LVP bit cannot be
programmed when programming is entered with RB4/
PGM.
It should be noted, that once the LVP bit is programmed
to 0, High voltage Programming mode can be used to
program the device.
Note 1: While in this mode, the RB4 pin can no
longer be used as a general purpose I/O
pin.
2: VDD must be 5.0V +10% during erase/
program operations while in low voltage
Programming mode.
I ran into same issues with the 628A just a few days ago.....
 

Thread Starter

bluebrakes

Joined Oct 17, 2009
252
I do the programming on the picstart ZIF, not in the circuit, so the pic is removed from the circuit each time for erasing/programming.
 

t06afre

Joined May 11, 2009
5,934
Unless you have a ZIF socket on your trainer I would recommend that you do incircuit programming. As this reduce wear and tear on your circuit board and PIC. You must do a similar arrangement as explained here. http://ww1.microchip.com/downloads/en/DeviceDoc/30277d.pdf The signal is routed to the corresponding pins on the picstart plus unit(as your pic was plugged in the ZIF). The picstart plus have very poor driver capabilities so you must use a short cable, and perhaps also disconnect the decoupling cap. And while programming the pins used must be disconnected from everything but the pic. Use jumper or dip-switch
Here is a picture to give you and idea. If I am not wrong this picture is from blueroomelectronics.
 

Markd77

Joined Sep 7, 2009
2,806
You might also be running into the read, modify, write problem. When you do a bit operation on a port, the whole port is read and then rewritten. This can mean that pins that you didn't want to change could be affected. It is much better to do the bit operations on a file and only use MOV on the port itself.
 

Thread Starter

bluebrakes

Joined Oct 17, 2009
252
thanks guys.

I really like the idea of that in-circuit programmer mod.

Would you be able to explain a bit more on the bit operations?

I just can't see why this would affect programming it off the circuit and in the pic programmer zif? I know you guys probably are right on this, I'm just not experienced enough to understand.
 

Markd77

Joined Sep 7, 2009
2,806
Put TB3009 into the search box on the microchip site.
Title: Common 8-Bit PIC® Microcontroller I/O Pin Issues.
The read modify write problem may not be affecting you this time - it normally only happens with fast port switching or with high loads, but worth knowing about.
 

t06afre

Joined May 11, 2009
5,934
I just can't see why this would affect programming it off the circuit and in the pic programmer zif? I know you guys probably are right on this, I'm just not experienced enough to understand.
In the theory it is the same. I do not know what kind of prototype board you have. But you have perhaps some solder type and a dip socket for the PIC. Every time you jack your PIC out of the socket you will tear the socket,the PIC, and the solder joints. So pretty soon your system will fail due to this wear and tear. A DIP socket is not made for this, in contrast to a ZIF socket. That is my experience. I also guess your work bench is not ESD protected. So each time you take your PIC out for programming you may risk ESD damage. But if it stay in the socket it will be more protected.
 

Thread Starter

bluebrakes

Joined Oct 17, 2009
252
I feel like a right idiot!

I came back to this project tonight and i still couldn't get it to erase.

Anyway to cut a long story short I found the answer, feel free to laugh...

I'm using a 16f628 pic but I missed one crucial detail in the selection of the chip.

I selected 16f628 instead of 16f628A on the MPLAB device select.

A real head slap! DOH! :-D
 
Top