PDA

View Full Version : Old analog guy needs help with PIC16F57



yankee
- 23rd February 2009, 14:42
So I'm venturing away from the PIC16F168A and it's internal oscillator for the first time ever. I need to drive an old BIT display with +5v and -5v. I bought the PIC16F57 because it has enough I/Os to do the job. So I go into the data sheet and after reading it over and over (8 hours worth!), I'm thoroughly confused about the resonator setup. I bought some Murata CSTLS20M0X53-B0 20 MHZ resonators, but now I'm not sure if I can use them. If I can use them, how is the resonator connected to the IC correctly?

Now for the code ... if I could get an example of a simple code to toggle a port from low to high a few times a second, I can figure out the rest. Any help at all is greatly appreciated!

~ Dave

yankee
- 23rd February 2009, 15:30
I tried programming the following code onto two different brand new ICs.


loop:

high portb.4 ' turn the LED on
pause 500 ' wait 500ms
low portb.4 ' turn the LED off
pause 500 ' wait 500ms
goto loop ' repeat

I'm getting a popup that says "Device not blank. Program anyway?" and when I click yes, I get the error "Code programming error at 0000."

The program is compiled for the PIC16F57 and I made sure that I selected the correct IC in the programmer. I'm bummed.

mister_e
- 23rd February 2009, 15:43
Looks like a device programmer issue. Usually this error is caused by a too low VPP voltage on MCLR, and/or poor power supply filtering.

Which programmer are you using? If it's a JDM based + you're using a USB to Serial adapter, then yes, you run into usual problems.

You can try to erase the device, then reprogram it...

Acetronics2
- 23rd February 2009, 15:47
Hi,

I think I already have seen something like that on the Forum ...

So ...

1) Which is the programmer ???

2) Is it "up to date" for soft version ???

Alain

yankee
- 23rd February 2009, 16:18
OK, I think I've found the problem. The voltage pins are in odd places on this IC, so I may have to do in-circuit programming; something else that I've never done. I'm going to figure out how to do that next and I'll get back with any results.

Here's the code that I'll be using for a test:


@ device pic16F57, hs_osc, wdt_off, protect_off

main:

high portA.0
pause 500
low portA.0
pause 500
goto main

I'm using the old serial programmer and my first problem was not having the cable plugged into the correct header and the IC pin 1 was in the wrong place. Even now with the cable plugged into the correct header and the IC in the ZIF socket correctly, I'm getting a code programming error at 0000.

Bruce
- 23rd February 2009, 16:32
The 16F57 is laid out really funky with Vdd on pin #2, Vss on pin #4, Vpp, pgmdata, pgmclock, etc all in odd places, so there's probably not any ZIF adapter compatible with this one.

Wire it for in-circuit like shown here: http://melabs.com/support/icsp.htm

yankee
- 23rd February 2009, 16:48
Thanks again Bruce!

Acetronics2
- 23rd February 2009, 16:51
The 16F57 is laid out really funky with Vdd on pin #2, Vss on pin #4, Vpp, pgmdata, pgmclock, etc all in odd places, so there's probably not any ZIF adapter compatible with this one.

Wire it for in-circuit like shown here: http://melabs.com/support/icsp.htm

Hi Bruce ...

Microchip AC 162049 Universal adapter CAN do it ...

Alain

Bruce
- 23rd February 2009, 19:43
Hi Alain,

Yep. That would do it, but it might be easier and a lot cheaper to just wire his PIC for in-circuit directly to his MeLabs serial programmer..;o}

yankee
- 27th February 2009, 19:48
OK, I made up an adapter cable and programmed the IC. I can't get it to run and I suspect the resonator or the way that I have the resonator setup. I read the data sheet and I'm thoroughly confused. I'm using Murata CSTLS20M0X53-B0 20 MHZ resonators. Is this an OK resonator and if so, how should it be connected?

~ Dave

Bruce
- 28th February 2009, 00:36
3-pin resonators normally have built-in caps. The center pin goes to ground. The remaining two pins go to the PIC external osc pins. For 20MHz you'll want HS_OSC.

It's a lot easier for folks here to help if you show all your code & config settings.

yankee
- 28th February 2009, 04:21
Sorry ... here's the code:


@ device pic16F57, hs_osc, wdt_off, protect_off

main:

high porta.0
high porta.1
high porta.2
high porta.3
high porta.4
high porta.6
high porta.7
high portb.0
high portb.1
high portb.2
high portb.3
high portb.4
high portb.5
high portb.6
high portb.7

pause 2000

low porta.0
low porta.1
low porta.2
low porta.3
low porta.4
low porta.6
low porta.7
low portb.0
low portb.1
low portb.2
low portb.3
low portb.4
low portb.5
low portb.6
low portb.7

pause 2000

goto main

Jumper
- 28th February 2009, 05:49
It is not really recommended to modify bits like that quickly after each other. There is something called READ-MODIFY-WRITE problem and that could be what you are having. I also notice that you have not set the TRIS register for the port (telling the port to be input or putput) but that should be taken care with high and low but I rather do it myself.

Try to blink with just one LED och with the entire PORT i.e

main:
TRISB=0
here:
PORTB=0
Pause 2000
PORTB=255
pause 2000
goto here

end

Save the A port for later since it is usually the port that has most Analog stuff that can also be causing problems.


And most important: Don't give up!! It is quite normal to have some problems getting started.

Bruce
- 28th February 2009, 13:29
What Jumper said + you'll want to place DEFINE OSC 20 in the top section when using
a 20MHz resonator. PBP assumes a 4MHz oscillator if you leave the define out.

yankee
- 2nd March 2009, 13:37
Still nothing.

Is a Murata CSTLS20M0X53-B0 20 MHZ resonator suitable for this IC?

I have the two outside leads connected to OSC1 and OSC2. The center pin is connected to ground.

I checked continuity and everything appears to be OK. I also checked the LED and it functions OK.

Here's the code that I tried:


@ device pic16F57, hs_osc, wdt_off, protect_off

DEFINE OSC 20

main:
TRISB=0
here:
PORTB=0
Pause 2000
PORTB=255
pause 2000
goto here

end

Bruce
- 2nd March 2009, 13:56
Specs on the resonator look fine so it should work.

I programmed one in-circuit this weekend with a PICKit2 and MeLabs USB programmer.
Both worked fine.

Do you have an external pull-up on /MCLR?

Have you tried reading it back with your programmer to verify config settings?

yankee
- 2nd March 2009, 14:47
Reading back produces this in the code tab:


0000- 006c 002b 0cff 01eb 0703 01ec 0703 0a1f
0008- 0c03 002a 0ce7 090e 0a02 006a 0029 0cfc
0010- 01e9 026a 0703 0a1a 0cff 0000 01e9 0603
0018- 0a15 01e9 0004 03ea 0a14 0800 0003 04c4
0020- 04a4 0004 0800 0064 0683 0a31 0663 0a31
0028- 0213 0002 0217 0005 0218 0006 0219 0007
0030- 0800 0cff 0033 0037 0038 0039 0c1a 0036
0038- 0078 0040 0006 0066 0c07 002c 0cd0 0063
0040- 0901 0cff 0026 0c07 002c 0cd0 0063 0901
0048- 0063 0a3b 0063 091e 0063 0a4a 0fff 0fff

Everything else is "0fff."

The configuration tab produces:


0000- 0ffa

I did not have MCLR pulled up, so I tried a 10k resistor. Still nothing.

yankee
- 2nd March 2009, 15:02
I also tried a 4.7k and still nothing.

Bruce
- 2nd March 2009, 15:05
Are you disconnecting the programmer after programming, and running the PIC on its own power supply?

You should also have a separate power supply for the PIC during ICSP. I.E. not trying to power it directly from the programmer.

If you are, then try this.

1. Program the part with your original .hex file.
2. Read it back with your programmer, and save the .hex file with another name.
3. Now open the new .hex file you just saved, and click the C box on the right.

Does it show the correct config settings?

yankee
- 2nd March 2009, 15:11
I'm not using ICSP. I made up an adapter cable to go directly from the programmer to the IC on a bread board.

I'm then disconnecting the program cable and testing the IC with an external power supply.

Bruce
- 2nd March 2009, 15:20
Have you tried the read-back yet?

yankee
- 2nd March 2009, 15:34
Yes, same exact results as above.

Bruce
- 2nd March 2009, 15:36
Strange. I would try another resonator or crystal.

yankee
- 2nd March 2009, 15:46
What code did you use?

Bruce
- 2nd March 2009, 16:13
@ device pic16F57, hs_osc, wdt_off, protect_off

DEFINE OSC 20

Time VAR BYTE

TRISB = 0 ' all outputs
Time = 10

Main:
PORTB = 0
PAUSE Time
PORTB = 255
PAUSE Time
Time=Time+10
IF Time >= 250 THEN Time = 10
GOTO Main

END

Source and .hex attached.

Note: You can't disable /MCLR on this one, so make sure you have the external pull-up.

yankee
- 2nd March 2009, 18:45
OK, when I open your code with the programmer the configuration reads:


0000- 000a

After I install it on the IC, then read it, this is what I get:


0000- 0ffa

I have no idea if this means anything or not.

Bruce
- 2nd March 2009, 18:56
Interesting. I opened the same .hex file, and it looks llike the attached.

hs osc, wdt off, protect off.

Sounds like something odd going on with your programmer, or you're not
selecting the correct PIC?

yankee
- 2nd March 2009, 19:09
OK, now I'm really confused. My snappy is attached and compared to yours.

Bruce
- 2nd March 2009, 19:22
What other options do you have enabled/disabled? See attached.

yankee
- 2nd March 2009, 19:49
I compared yours to mine and the only thing that I didn't have checked was "reread file before programming." I made mine match yours, tried again and still nothing. I did a read and the configuration on the IC is still 0000- 0ffa.

When you do a read, what do you get on your configuration?

Bruce
- 2nd March 2009, 20:37
When you do a read, what do you get on your configuration?

0ffa, which is correct.

Have you tried another resonator or crystal?

You might also try XT OSC - just in case you're over-driving the one you have.

I use resonators for pretty much everything, and have never had a single problem
with one, but I've never used the type you're using.

Trying a different resonator or crystal could save you a lot of work.

And if you're using a bread board it wouldn't hurt to meter every single connection.

yankee
- 6th March 2009, 13:32
Different resonators brought the IC to life.

~ Dave