PIC16F628A problems - It simply seems to doesn't work

Thread Starter

nolfolk

Joined Jun 9, 2010
10
Hi forum. I'm new ( in this forum and at microcontrollers ).

I'm having trouble trying to do my first project: let a led lights up :(

I'm using MPLab IDE 8.46 to program and simulate and IC-Prog 1.05D to program ( to the chip )

I'll put my source code and my schema, if someone help me i'll be greatful

Rich (BB code):
; INCLUDE OBRIGATORIO
#INCLUDE <P16F628A.INC>

; CONFIGURACOES
__CONFIG _BOREN_ON&_CP_ON&_DATA_CP_OFF&_PWRTE_ON&_WDT_OFF&_LVP_OFF&_MCLRE_ON&_XT_OSC

; BANCOS DE MEMORIA
#DEFINE BANCO_0 BCF STATUS,RP0 ; BANCO DE MEMORIA 0
#DEFINE BANCO_1 BSF STATUS,RP0 ; BANCO DE MEMORIA 1

; CONSTANTES DE SAIDA
#DEFINE LED PORTA,0 ; LED
#DEFINE LED2 PORTB,0 ; LED

; RESET
ORG 0x00
GOTO INICIO

; INTERRUPCOES
ORG 0x04
RETFIE ; RETORNA AO PONTO INTERROMPIDO

INICIO
    BANCO_1
    MOVLW B'00000000'
    MOVWF TRISA ; SETA PORTA COMO OUTPUT
    MOVWF TRISB ; SETA PORTB COMO OUTPUT
    MOVLW B'10000000'
    MOVWF OPTION_REG
    MOVLW B'00000000'
    MOVWF INTCON
    BANCO_0
    MOVLW B'00000111'
    MOVWF CMCON ; CONFIGURA RA3:RA0 COMO I/O
    CLRF PORTA
    CLRF PORTB
    
MAIN
    BSF LED ; ACENDE O LED
    BSF LED2 ; ACENDE O LED
    GOTO MAIN
    
END
And my protoboard schema:
http://img199.imageshack.us/i/lilod.png/

I'm using 4 1.5V batteries to gain ~4.6V as feeding.

well... that's it, i'm kinda furstrated because I am working on that the hole week, and I can't figure what's wrong :(

Please help ... I don't know what else can I do :(
 

Markd77

Joined Sep 7, 2009
2,806
I can't see anything too obvious apart from the missing LED resistor. I'm not sure if RA6 and 7 have to be set as inputs for the external crystal to work.
A 1K resistor is recommended between MCLR and VDD but I'm not sure if it will make any difference.
You could disconnect the crystal and try _INTRC_OSC_NOCLKOUT instead of _XT in the config.
 

t06afre

Joined May 11, 2009
5,934
As mentioned before you should put resistor between the output and LED. About 470 ohm should be a OK value. In the start I will also recommend using internal OSC and MCLR internally tied to VDD. And turn all the fancy functions OFF.
I have made a program you can try. It will blink the PORTA bit 0. I do not have any 16f628A so I have not tested it. But it compiles.
Rich (BB code):
;;==========================================================================
;;
;;       Configuration Bits
;;
;;==========================================================================
;
;_BODEN_ON                    EQU     H'3FFF' ;Backwards compatability to 16F62X
;_BODEN_OFF                   EQU     H'3FBF' ;Backwards compatability to 16F62X
;_BOREN_ON                    EQU     H'3FFF'
;_BOREN_OFF                   EQU     H'3FBF'
;_CP_ON                       EQU     H'1FFF'
;_CP_OFF                      EQU     H'3FFF'
;_DATA_CP_ON                  EQU     H'3EFF'
;_DATA_CP_OFF                 EQU     H'3FFF'
;_PWRTE_OFF                   EQU     H'3FFF'
;_PWRTE_ON                    EQU     H'3FF7'
;_WDT_ON                      EQU     H'3FFF'
;_WDT_OFF                     EQU     H'3FFB'
;_LVP_ON                      EQU     H'3FFF'
;_LVP_OFF                     EQU     H'3F7F'
;_MCLRE_ON                    EQU     H'3FFF'
;_MCLRE_OFF                   EQU     H'3FDF'
;_RC_OSC_CLKOUT               EQU     H'3FFF'
;_RC_OSC_NOCLKOUT             EQU     H'3FFE'
;_ER_OSC_CLKOUT               EQU     H'3FFF' ;Backwards compatability to 16F62X
;_ER_OSC_NOCLKOUT             EQU     H'3FFE' ;Backwards compatability to 16F62X
;_INTOSC_OSC_CLKOUT         EQU     H'3FFD'
;_INTOSC_OSC_NOCLKOUT         EQU     H'3FFC'    
;_INTRC_OSC_CLKOUT         EQU     H'3FFD' ;Backwards compatability to 16F62X
;_INTRC_OSC_NOCLKOUT      EQU     H'3FFC' ;Backwards compatability to 16F62X
;_EXTCLK_OSC                  EQU     H'3FEF'
;_HS_OSC                      EQU     H'3FEE'
;_XT_OSC                      EQU     H'3FED'
;_LP_OSC                      EQU     H'3FEC'
 
#include <p16F628a.inc>
    __config (_INTRC_OSC_NOCLKOUT & _MCLRE_OFF & _WDT_OFF & _PWRTE_OFF  & _CP_OFF & _BOREN_OFF & _BODEN_OFF & _DATA_CP_OFF   )
; Note using defualt setting for internal OSC=4MHz
    cblock 0x20
Delay1                   ; Define two file registers for the
Delay2                   ; delay loop
     endc
     org 0
Start:
 CLRF PORTA   ;Initialize PORTA by setting output data latches
 MOVLW 0x07   ;Turn comparators off and
 MOVWF CMCON  ;enable pins for I/O;functions
 banksel TRISA  ;select Register Page 1
 MOVLW 0x10   ;Value used to initialize data direction
 MOVWF TRISA  ;Set RA<4:0> as outputs TRISA<5> always read as ‘1’ TRISA<7:6> depend on oscillator mode    
    banksel PORTA   ; back to Register Page 0 See MPLAB help for banksel description
MainLoop:
     bsf       PORTA,0             ; turn on LED 
OndelayLoop:
     decfsz    Delay1,f            ; Waste time.  
     goto      OndelayLoop         ; The Inner loop takes 3 instructions per loop * 256 loopss = 768 instructions
     decfsz    Delay2,f            ; The outer loop takes and additional 3 instructions per lap * 256 loops
     goto      OndelayLoop         ; (768+3) * 256 = 197376 instructions / 1M instructions per second = 0.197 sec.
                                   ; call it a two-tenths of a second.
 
     bcf       PORTA,0             ; Turn off LED
OffDelayLoop:
     decfsz    Delay1,f            ; same delay as above
     goto      OffDelayLoop
     decfsz    Delay2,f
     goto      OffDelayLoop
     goto      MainLoop            ; Do it again...
     end
 

Thread Starter

nolfolk

Joined Jun 9, 2010
10
Thanks for the reply :)

> Man, if you don't put a resistor to limit the current to led, maybe you screw the led an pic, too.

That's true ? :( how the resistor in a out pin of the microcontroller would screw the microcontroller itself ? :(
I hope my microcontroller is fine. Well, he still reads and writes, that's a clue that he's still working ?
I don't understand why I have to put a resistor there, but ok, I'll buy one today and test it...

--

> I can't see anything too obvious apart from the missing LED resistor. I'm not sure if RA6 and 7 have to be set as inputs for the external crystal to work.
A 1K resistor is recommended between MCLR and VDD but I'm not sure if it will make any difference.
You could disconnect the crystal and try _INTRC_OSC_NOCLKOUT instead of _XT in the config.

The resistor again :S I'll buy it today, it may be that, then... I don't understand why.
I thought that maybe I was supose to put RA6 and RA7 as inputs ou CLKIn as input and CLKOut as output, but I'm not sure. If things remain wrong, I'll try that.

Again with resistors x_x 1K resistor to MCLR, ok, I'll try that too. I'll buy a bunch of resistors xD someone have any explanation for all that resistors ? I don't understand why they are necessary õ.o

I'll try without _XT too, as soon as I buy the resistors.

--

> As mentioned before you should put resistor between the output and LED. About 470 ohm should be a OK value. In the start I will also recommend using internal OSC and MCLR internally tied to VDD. And turn all the fancy functions OFF.
I have made a program you can try. It will blink the PORTA bit 0. I do not have any 16f628A so I have not tested it. But it compiles.

Thanks! I'll buy the resistors and then I'll try your solution :D
How do I tie the MCLR to the VDD internally ?



Thanks everyone, I think maybe I can fix my lil' project :)
Thanks!
 

t06afre

Joined May 11, 2009
5,934
You do have downloaded the datasheet ;) Well let us assume you have done that. A very important section to read is 14.0 SPECIAL FEATURES OF THE CPU. Setting the configuration bit wrong may cause strange MCU behaviour. Like it do not do anything. Then you are in the electronics shop get some(a lot) 100nF decoupling capacitors also. Never build anything without it
http://en.wikipedia.org/wiki/Decoupling_capacitor
 
For in circuit serial programming (ICSP), I use a diode between MCLR and Vdd. I've also found that if my PicKit2 is plugged into the circuit with the pins attached for programming, I have to disconnect the wire running to MCLR when I am trying to run on battery power alone.
 

Thread Starter

nolfolk

Joined Jun 9, 2010
10
Nvm, I Think i broke the MCU ...
I try to write on it, but it keep givin me the error : "verificação falhou no endereço 0000h!" ("verify error at address 0000h")

 

Thread Starter

nolfolk

Joined Jun 9, 2010
10
Oooops now it worked =D I've changed _CP_ON to _CP_OFF and it worked fine :D

Code Protection sux ? :D

I'll try to put it on the protoboard and see what happens :D
 

Thread Starter

nolfolk

Joined Jun 9, 2010
10
I tryied that:

http://img824.imageshack.us/i/test002.png/

Rich (BB code):
; INCLUDE OBRIGATORIO
#INCLUDE <P16F628A.INC>

; CONFIGURACOES
__CONFIG  _BOREN_ON&_CP_OFF&_DATA_CP_OFF&_PWRTE_ON&_WDT_OFF&_LVP_OFF&_MCLRE_OFF&_INTRC_OSC_NOCLKOUT

; BANCOS DE MEMORIA
#DEFINE BANCO_0 BCF STATUS,RP0 ; BANCO DE MEMORIA 0
#DEFINE BANCO_1 BSF STATUS,RP0 ; BANCO DE MEMORIA 1

; CONSTANTES DE SAIDA
#DEFINE LED PORTA,0 ; LED

; RESET
ORG 0x00
GOTO INICIO

; INTERRUPCOES
ORG 0x04
RETFIE ; RETORNA AO PONTO INTERROMPIDO

INICIO
    BANCO_1
    MOVLW B'00000000'
    MOVWF TRISA ; SETA PORTA COMO OUTPUT
    MOVLW B'00000000'
    MOVWF INTCON ; DESLIGA CHAVE GERAL DE INTERRUPCAO
    BANCO_0
    MOVLW B'00000111'
    MOVWF CMCON ; CONFIGURA RA3:RA0 COMO I/O
    CLRF PORTA
    
MAIN
    BSF LED ; ACENDE O LED
    GOTO MAIN
    
END
But didn't work :(
Any sugestion ?
 

Thread Starter

nolfolk

Joined Jun 9, 2010
10
t06afre, I tryied your code, didn't worked.

I just realized:

If I write things on the MCU (with _CP_OFF ) and then I read it, the configuration comes DIFERENT!
For example, Oscilator that was IntRC I\O comes LP, CP gone OFF comes ON, MCRL goes OFF comes ON...
That's normal behavior ?

I'm using IC-Prog 1.05D, for PIC16F628A.
 

t06afre

Joined May 11, 2009
5,934
What do you use as programmer. Not software but the programming hardware. It looks to me that your programmer hardware has not done the job. Are you using a USB to serial converter for your programmer? Are you also sure your programming hardware works as it should? If your budget allows it I would recommend a PICKIT 2.
Edit: I also tested the program on a 16f690. I had do modify the setup part some. But the mainloop was the same. No problems at all
 
Last edited:

Thread Starter

nolfolk

Joined Jun 9, 2010
10
In other forum, a guy realised that I put _cp_on and WROTE in the MCU.
He said that as I did so, I CANT write on the MCU anymore... Is that true ? So then I have to buy another MCU because of that ? :(

I realy did that, by mistake, but, just because I did it, I cant clean up / rewrite the MCU anymore ? :(

I did not build the programmer, I bought it online.
I used him to program a PIC16F84A once, and it did fine, I made a 7 segment display count from 0 to 9. And I didn't used any capacitors, just 7 resistors for the segment display, because once I turned it directly in the MCU and it "burned" :p

The programmer is SERIAL and it is a "JDM Programmer". A PICKIT 2 is way to expensive where I live. a JDM is like R$24,00 while a PICKIT2 is R$90,00 to R$150,00 !
 

Thread Starter

nolfolk

Joined Jun 9, 2010
10
As I read the manual, I think the guy was mistaken. The manual says:
"Only a Bulk Erase function can set the CP
and CPD bits by turning off the code
protection. The entire data EEPROM and
Flash program memory will be erased to
turn the code protection off."

:D So I still can write on my MCU! Great!

Maybe is something on the IC-Prog itself... I'll try to clear the MCU and start all over, with my code, most simple as I can. Then I'll post here what I'm doing.
Be Right Back.
 

t06afre

Joined May 11, 2009
5,934
In other forum, a guy realised that I put _cp_on and WROTE in the MCU.
He said that as I did so, I CANT write on the MCU anymore... Is that true ? So then I have to buy another MCU because of that ? :(

I realy did that, by mistake, but, just because I did it, I cant clean up / rewrite the MCU anymore ? :(

I did not build the programmer, I bought it online.
I used him to program a PIC16F84A once, and it did fine, I made a 7 segment display count from 0 to 9. And I didn't used any capacitors, just 7 resistors for the segment display, because once I turned it directly in the MCU and it "burned" :p

The programmer is SERIAL and it is a "JDM Programmer". A PICKIT 2 is way to expensive where I live. a JDM is like R$24,00 while a PICKIT2 is R$90,00 to R$150,00 !
Try this. The problem with serial port programmers is that they might not work and it depends solely on the hardware of the serial port on your PC or Laptop. Serial COM port on very old PC usually works but most laptop COM port won't.
 

t06afre

Joined May 11, 2009
5,934
Your chip can be reprogrammed by the "bulk erase" method only
Only a Bulk Erase function can set the CP and CPD bits by turning off the code protection. The entire data EEPROM and Flash program memory will be erased to turn the code protection off.
The PICKITS can do this, and also other more advanced programmers. Perhaps you know someone with such a programmer.
 

Thread Starter

nolfolk

Joined Jun 9, 2010
10
Here's what I did:

- Put the MCU on the Programmer.
- The hardware is a TecTrony PicStarS for Serial Port. The TecTrony site is off (www.tectrony.com) and I don't have the schematic of the programmer :(
But I Still have the manual (in portuguese)
- I'm using Windows7, so, I'm openning IcProg in compatibility mode to WindowsXP SP 3, and execute it as ADMINISTRATOR.
- Also, as I'm using windows 2000 or later, I have the icprog.sys file, as required
- In IC-Prog I selected PIC 16F628A as my PIC.
- In configurations>hardware I put:
Programmer: JDM Programmer
Ports: Com1
Interface: Windows API
Comunication: [nothing selected]
I/O Delay: 20

As I have in the ScreenShot:
[http://img710.imageshack.us/i/19590923.png/]

- Now I'll ERASE ALL MCU
- Devide is erased: [http://img252.imageshack.us/i/16550597.png/]
- Now I'll READ ALL
- Done. Screenshot here: [http://img816.imageshack.us/i/19668768.png/] and here [http://img704.imageshack.us/i/53159749.png/]

- Now, in MPLab IDE I got my project and made sure the device [http://img693.imageshack.us/i/97809405.png/] and that the configurations are set by ME [http://img251.imageshack.us/i/24449746.png/]
- Then I compile the project.
- HERE IS THE CODE:

Rich (BB code):
; INCLUDE OBRIGATORIO
#INCLUDE <P16F628A.INC>

; CONFIGURACOES
__CONFIG _BOREN_OFF&_CP_OFF&_DATA_CP_OFF&_PWRTE_OFF&_WDT_OFF&_LVP_OFF&_MCLRE_OFF&_INTRC_OSC_NOCLKOUT

; BANCOS DE MEMORIA
#DEFINE BANCO_0 BCF STATUS,RP0 ; BANCO DE MEMORIA 0
#DEFINE BANCO_1 BSF STATUS,RP0 ; BANCO DE MEMORIA 1

; CONSTANTES DE SAIDA
#DEFINE LED PORTA,0 ; LED

; RESET
ORG 0x00
GOTO INICIO

; INTERRUPCOES
ORG 0x04
RETFIE ; RETORNA AO PONTO INTERROMPIDO

INICIO
    BANCO_1
    MOVLW B'00000000'
    MOVWF TRISA ; SETA PORTA COMO OUTPUT
    MOVLW B'00000000'
    MOVWF INTCON ; DESLIGA CHAVE GERAL DE INTERRUPCAO
    BANCO_0
    MOVLW B'00000111'
    MOVWF CMCON ; CONFIGURA RA3:RA0 COMO I/O
    CLRF PORTA
    
MAIN
    BSF LED ; ACENDE O LED
    GOTO MAIN
    
END

- HERE IS THE OUTPUT:
Rich (BB code):
----------------------------------------------------------------------
Debug build of project `C:\Users\tarcisio\Documents\MPLab Projects\Projeto001\Projeto001.mcp' started.
Language tool versions: MPASMWIN.exe v5.35, mplink.exe v4.35
Preprocessor symbol `__DEBUG' is defined.
Fri Jun 11 14:41:00 2010
----------------------------------------------------------------------
Make: The target "C:\Users\tarcisio\Documents\MPLab Projects\Projeto001\principal.o" is out of date.
Executing: "C:\Program Files\Microchip\MPASM Suite\MPASMWIN.exe" /q /p16F628A "principal.asm" /l"principal.lst" /e"principal.err" /o"principal.o" /d__DEBUG=1
Warning[205] C:\USERS\TARCISIO\DOCUMENTS\MPLAB PROJECTS\PROJETO001\PRINCIPAL.ASM 5 : Found directive in column 1. (__CONFIG)
Warning[205] C:\USERS\TARCISIO\DOCUMENTS\MPLAB PROJECTS\PROJETO001\PRINCIPAL.ASM 15 : Found directive in column 1. (ORG)
Warning[203] C:\USERS\TARCISIO\DOCUMENTS\MPLAB PROJECTS\PROJETO001\PRINCIPAL.ASM 16 : Found opcode in column 1. (GOTO)
Warning[205] C:\USERS\TARCISIO\DOCUMENTS\MPLAB PROJECTS\PROJETO001\PRINCIPAL.ASM 19 : Found directive in column 1. (ORG)
Warning[203] C:\USERS\TARCISIO\DOCUMENTS\MPLAB PROJECTS\PROJETO001\PRINCIPAL.ASM 20 : Found opcode in column 1. (RETFIE)
Message[302] C:\USERS\TARCISIO\DOCUMENTS\MPLAB PROJECTS\PROJETO001\PRINCIPAL.ASM 25 : Register in operand not in bank 0.  Ensure that bank bits are correct.
Warning[205] C:\USERS\TARCISIO\DOCUMENTS\MPLAB PROJECTS\PROJETO001\PRINCIPAL.ASM 37 : Found directive in column 1. (END)
Make: The target "C:\Users\tarcisio\Documents\MPLab Projects\Projeto001\Projeto001.cof" is out of date.
Executing: "C:\Program Files\Microchip\MPASM Suite\mplink.exe" /p16F628A "principal.o" /u_DEBUG /z__MPLAB_BUILD=1 /z__MPLAB_DEBUG=1 /o"Projeto001.cof" /M"Projeto001.map" /W
MPLINK 4.35, Linker
Copyright (c) 1998-2010 Microchip Technology Inc.
Errors    : 0

MP2HEX 4.35, COFF to HEX File Converter
Copyright (c) 1998-2010 Microchip Technology Inc.
Errors    : 0

Loaded C:\Users\tarcisio\Documents\MPLab Projects\Projeto001\Projeto001.cof.
----------------------------------------------------------------------
Debug build of project `C:\Users\tarcisio\Documents\MPLab Projects\Projeto001\Projeto001.mcp' succeeded.
Language tool versions: MPASMWIN.exe v5.35, mplink.exe v4.35
Preprocessor symbol `__DEBUG' is defined.
Fri Jun 11 14:41:04 2010
----------------------------------------------------------------------
BUILD SUCCEEDED

- Then I simulated and it goes as I spected: LED will turn on (PORTA VALUE = 0x01) [http://img808.imageshack.us/i/40124172.png/]
- Ok, now I have the HEX data. If you want its here: [http://rapidshare.com/files/397919257/Projeto001.hex.html]
- Back to the IC-PROG... I Open File... Projeto001.hex it is that:

Rich (BB code):
; Generated by WinDis84, (c) Nigel Goodwin 1998.

            LIST      P=16F84, F=INHX8M
            include "P16FXX.inc"
            ORG     0x0000

            GOTO    Label_0001
            RETLW   0x00
            RETLW   0x00
            ORG     0x0004
            RETFIE
Label_0001  BSF     STATUS    , RP0
            MOVLW   0x00
            MOVWF   TRISA
            MOVLW   0x00
            MOVWF   INTCON
            BCF     STATUS    , RP0
            MOVLW   0x07
            MOVWF   0x1F
            CLRF    PORTA
Label_0002  BSF     PORTA     , 00
            GOTO    Label_0002
 
            ORG     0x2000
            DATA    0x0F
            DATA    0x0F
            DATA    0x0F
            DATA    0x0F
 
            ORG     0x2007
            DATA    0x18
 
            ORG     0x2100
            [ LOTS OF DATA 0xFF ]

            END
- I choose PROGRAM ALL
- It program, then it verify, and.... OK verified with sucess! [http://img706.imageshack.us/i/87934891.png/]
- Then now, I choose READ ALL...
- It read EXACTLY this:

Rich (BB code):
; Generated by WinDis84, (c) Nigel Goodwin 1998.

            LIST      P=16F84, F=INHX8M
            include "P16FXX.inc"
            ORG     0x0000

            GOTO    Label_0001
            RETLW   0x00
            RETLW   0x00
            ORG     0x0004
            RETFIE
Label_0001  BSF     STATUS    , RP0
            MOVLW   0x00
            MOVWF   TRISA
            MOVLW   0x00
            MOVWF   INTCON
            BCF     STATUS    , RP0
            MOVLW   0x07
            MOVWF   0x1F
            CLRF    PORTA
Label_0002  BSF     PORTA     , 00
            GOTO    Label_0002
 
            ORG     0x2000
            DATA    0x0F
            DATA    0x0F
            DATA    0x0F
            DATA    0x0F
 
            ORG     0x2007
            DATA    0x00
 

            END
Hmm... We really seem to have a problem there :O
As You guys noted before, the code itself is ok, just now there's no more LOTS OF DATA 0XFF and
ORG 0x2007
DATA 0x18
is now
ORG 0x2007
DATA 0x00

That's bad, isn't, it ?

I searched on google, and people say that may be a problem with the JDM Programmer hardware.
http://www.asm51.eng.br/phpBB/viewt...ghlight=&sid=885baa9ef2ec2180d29b0ef4154b58dd
"O programador JDM possui uma variante para o pic16f628. O pino 10 (RB4) é desconectado de onde estava e conectado ao nó dos pinos Vss, RB0, 1, 2, 3, RA1, RA3 e os componentes que estão ligados a este nó. "

That sucks :(
 

Thread Starter

nolfolk

Joined Jun 9, 2010
10
IT WORKED !!!!

Thanks everyone :D I did it :D
Thanks, t06afre for your patience :D

A guy in another forum said to change the programmer from IC-Prog to WINPIC800, and that's all!

And I didn't even put a capacitor in the circuit, I did it like this:
Pin5 = VSS -> - on my battery
Pin14 = VDD -> + on my battery
Pin17 = RA0 -> resistor // resistor -> LED // LED -> - on my battery

Thanks everyone. I'm very happy now :D
I know it sounds stupid, I just turned a LED on, but ... I don't know, I'm just very happy now :D Hahahaha

Thanks, thanks :D
 
Top