Im new to PIc programming and as a part of the interest to flash some projects onto pic's, I bought a PICkit2 programmer. So now Im trying to learn how to write programs on my own in Hi-tech C. Im using MPlab 8.53 and hi-tech PICC 9.7 modules and a PIC16F628A chip and the internal OSC. The problem I am facing is that I can flash my 16F628 well enough and blink my led's but it acts totally wierd.

Code ( (Unknown Language)):

#include <HTC.H>

#define _XTAL_FREQ 20000000

__CONFIG(INTIO & WDTDIS & PWRTDIS & BORDIS & LVPDIS );

void main()

{

TRISA=0;

TRISB=0;

unsigned char i=0;

while (1)

{

i=0;

while (i<2){

PORTA = 0xFF;

_delay(100000);

_delay(100000);

PORTA = 0x00;

_delay(100000);

_delay(100000);

i=i+1;

}

i=0;

while (i<2){

PORTB = 0xFF;

_delay(100000);

PORTB = 0x00;

_delay(100000);

i=i+1;

}

PORTB = 0x00;

}

}

The port A led's blink 2 times, then the port B blinks Twice and whole repeats..

However if I change the delay to _delay(1000), it doesnt blink at all.. and _delay_ms(100) gives unidetified errors.. how do I fix the delay problems??

Of course it would be usefull because I can 'know' all the ways to do something and then use whats best for the situation. I have a doubt though, why is this used?
CMCON = 0x07; // Disabling the analogue comparators its not enabled in the beginning??

Of course it would be usefull because I can 'know' all the ways to do something and then use whats best for the situation. I have a doubt though, why is this used?
CMCON = 0x07; // Disabling the analogue comparators its not enabled in the beginning??

Click to expand...

Actually, they are already enabled and on. PIC pins that have both analog and digital functions default to analog on reset.

One further point.
If you use the __delay_ms(); command you should correct the line

Code ( (Unknown Language)):

#define _XTAL_FREQ 20000000

The __delay_ms(); routine uses the _XTAL_FREQ information to calculate the number of machine cycles required to produce a given delay so as you are using the chip's internal oscillator your clock frequency will be 4 MHz and that line should read

Actually, they are already enabled and on. PIC pins that have both analog and digital functions default to analog on reset.

Click to expand...

Thank you for that, so can I vary the brightness of an led by varying the output level on just one pin in analogue mode? or do I need to use 4 pins tied up with resistors and connected to led and run a binary counter to do that?
Sorry If that was a dumb question

One further point.
If you use the __delay_ms(); command you should correct the line

Code ( (Unknown Language)):

#define _XTAL_FREQ 20000000

The __delay_ms(); routine uses the _XTAL_FREQ information to calculate the number of machine cycles required to produce a given delay so as you are using the chip's internal oscillator your clock frequency will be 4 MHz and that line should read

Code ( (Unknown Language)):

#define _XTAL_FREQ 4000000

Click to expand...

Wow wow.. finally Got __delay_ms() working with this! Thank You a lot. Actually I was trying with 20Mhz clock and it keeps throwing errors on compile, so I was about to post a screenshot question for your first post, then saw this reply!! Now its working, thanks a lot. I was trying to do this since a day and all websites just give info that the xtal frq must be defined to use it not that it shud be set to 4mhz..!! Thank you!!

Could you also tell me if I can test every change i make to the program by flashing it onto the pic, I mean, is it safe, or will I end the read write cycles allowed within a week with a 100 flashes a day? Or should I use some simulators instead? Chip is a 16F628a

Could you also tell me if I can test every change i make to the program by flashing it onto the pic, I mean, is it safe, or will I end the read write cycles allowed within a week with a 100 flashes a day? Or should I use some simulators instead? Chip is a 16F628a

Click to expand...

You can debug in MPLAB. But I do not almost sure you can not debug the 16f628 using the PICKIT hardware debugger. Without any extra hardware named a ICD header. An extra piece of hardware. Some PICs in the 16F series can be debugged directly by PICKIT 2. These are

But I think all 18F series PICs that are compatible with PICKIT 2 can be debugged without any extra hardware.
You can find info about it here http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=1406&dDocName=en027813
Many errors can also be tracked by the software simulator in MPLAB. You will find my sticky her useful http://forum.allaboutcircuits.com/showthread.php?t=44852
I would not worry about about your flash. Your chip can take about
100000 write Flash cycles.
I would also have changed to a newer version of HI-TECH C. In version 9.71Aa they changed the delay function so it could take MUCH larger numbers in the _delay() functions. The __delay_ms() functions use the latter functions. Now seconds delays is not a problem.

You can debug in MPLAB. But I do not almost sure you can not debug the 16f628 using the PICKIT hardware debugger. Without any extra hardware named a ICD header. An extra piece of hardware. Some PICs in the 16F series can be debugged directly by PICKIT

Click to expand...

Ok, Now I understand why mplab throws an error when i select debugger>select tool>pickit2 !! but there is an extra 6th pin on the ICSP header on my pickit2 which is not used but goes directly to pin 6 of the 18F on the PICKIT, any Idea what it is for?

But I think all 18F series PICs that are compatible with PICKIT 2 can be debugged without any extra hardware.

Click to expand...

It think for now I will be sticking to the 16F628 as its the only one I have in hand and is very cheap and small, but have to try on 18F's once I get a hold of all this.

I checked that thread yesterday and downloaded that very important file you had linked, thanks for that, but Im not quite getting a hold of how to show the port led's while in the SIM mode..

I would also have changed to a newer version of HI-TECH C. In version 9.71Aa they changed the delay function so it could take MUCH larger numbers in the _delay() functions. The __delay_ms() functions use the latter functions. Now seconds delays is not a problem.

Click to expand...

Yes, I tried that and something got messed up totally. It stopped recognizing my fuses and showed each of my fuse bits as an individual error, but I should try again because I could save a couple of lines of codes with that update.

I checked that thread yesterday and downloaded that very important file you had linked, thanks for that, but Im not quite getting a hold of how to show the port led's while in the SIM mode..

Yes, I tried that and something got messed up totally. It stopped recognizing my fuses and showed each of my fuse bits as an individual error, but I should try again because I could save a couple of lines of codes with that update.

Click to expand...

They have changed the config word naming style in the latest versions from 9.81 I think. You will find this in the header file. You can find the header here C:\Program Files\HI-TECH Software\PICC\9.81\include
But I also include a part of it it here

Btw, yesterday tried some crazy stuff.. I ripped out a vintage Nokia 6150 Long range cordless phone and found an HD44780 chip wired to the lcd.. tracked down the pins, removed the onboard uC, did some soldering and wired it down to the PIC16F628. Found an LCD Hitech c program in the samples folder and edited the ports and R/W pin data and flashed it, here's what I got

Now could you help me wire switches to the port? I tried using a 10k resistor pulled up to Vcc and then a 330ohm pulled down through a switch to ground but it doesnt seems to work. can I use conditions like

Yes, I realized it immediately after posting this and tested with both hex and binary values setting all of port b as inputs but I am not able to get it work. Can you help me on how to wire the switch?? presently it is wired as vcc to RB3 thru 10k resistor and RB3 to ground through switch. But I guess there is something wrong somewhere..

Now the RA1 led keeps lit up though I have pulled it low both through software and hardware and without the switch pressed. So switch always seems to be high, without an input even. Removing delays, does no good. No change on switch press.. Checked all ports, working fine otherwise, not damaged.

I realized I had another chance for a bug and put a low bit to the PORTA led after the delay so that it would switch off the led momentarily before the next state check and wouldnt keep it on and it worked!!