NewBie to PIC 16F84A C programming problems..

Thread Starter

Icarusbop

Joined May 10, 2009
3
Hello
I am brand new to PICS and am trying to program a 16F84A
I have a HEX file provided by the supplier of my programmer, I have successfully programmed this into my PIC and it works fine (flashes LED) so I know that my PIC, programmer and test board are all OK.
I have been trying to write and compile my own simple program (in C), but could not get it to work so I went to the simplest program I could think of as follows:

#include <htc.h>
#include <pic.h>
void main()
{
TRISA=0 ;
TRISB=0;
while(1)
{
PORTA=255;
PORTB=255;
}
}
Theoretically this should set all outputs to High, thus lighting the LED, no matter which pin it is connected to.
It compiles and programs fine, but nothing happens, when the test board is turned on.
I am using the Hi-Tec Pic compiler (it is set to the correct processor), I suspect I am doing something wrong with the compiling stage, as the program is so simple, but cannot see anything wrong.
Please can someone help solve my issue?
 

Thread Starter

Icarusbop

Joined May 10, 2009
3
What are your configuration bits set to?
No idea: I assume (I am very new to this) you mean the bits for protecting code and things, that normally goes at the top of the program? I was under the impression I didn't need to set them as I don't want to protect the code yet.

Of course, I wouldn't know how to set them anyhow yet... I didn't think it was important for a program as simple as this.
 

AlexR

Joined Jan 16, 2008
732
Amongst other things the configuration bits set your oscillator mode so they can be very important. Make it a practice to always set them at the start of any program.

Other than than that your program looks OK so if it compiles without errors and still doesn't work, take a look at the hardware.
Is Vdd present?
Is MCLR pulled up to Vdd?
 

Thread Starter

Icarusbop

Joined May 10, 2009
3
Hello:

Thanks for the help, this is now working: I had to set the config bits as suggested.

However: the command '#fuses' would not work (it was RED, and not recognised by Hi-Tec compiler), I had to use another method to set the fuses as follows...
'__CONFIG (WDTDIS & XT & UNPROTECT & PWRTEN);'

now it is working fine. Out of interest, can anyone explain why the
'#fuses' command would not work?

Thanks.
 

AlexR

Joined Jan 16, 2008
732
I must admit that I don't usually use Hi-Tech C but have never heard of a #fuses command. The # would indicate its a preprocessor directive but I could not find it in the manual.
In any case the '__CONFIG' comand is the normal way you set fuses in HI-Tech C.
 

RWh

Joined Feb 4, 2010
2
The #fuses command is used to set the config fuses in the CCS C compiler - the Hi-tech compiler uses _config to do the same job.

I am also new to the Hi-Tech compliler - I'm using a 16f627a, I know the hardware works (tested with an assembly code program assembled using MPASM) but the code below won't run. I've tried setting up the MPLAB IDE in debug and release mode, reconfiguring the config, deleting every line of code except the ones that read an input and write an output, pulling my hair out etc... The only difference I can see from the lst files is that the C compiled version of the code uses memory in the region 0x3xx where my assembled code uses memory in the region 0x0xx - looks like I'm missing something?

#include "htc.h"

#if defined (_16F627A)
#warning PIC16F627A with external oscillator
__CONFIG(UNPROTECT & LVPDIS & BOREN & MCLREN & PWRTEN & WDTDIS & HS);
#else
#error Unsupported PIC micro selected
#endif

void myInit()
{
OPTION = 0x0D7; //check option register

//port configuration

TRISA = 0xFF; //test all input - 11111111
TRISB = 0x00; //test all output - 00000000
}

void main()
{

myInit();

while(1)
{
if(RA0)
{
RB0 = 0x01;
}
else
{
RB0 = 0x00;
}
}
}
 

AlexR

Joined Jan 16, 2008
732
First up, adding your own question to the end of someone else's thread is called hijacking and is frowned upon in this forum. If you have a question start your own new thread.

Having gotten the commercial out of the way back to your query.

The 16F627 supports analogue functions on port A. Microchip in their wisdom have made all ports capable of analogue on the PIC default to analogue. So to use port A as a digital port you have to first turn off its analogue functions, in this case turn off comparators by writing 0x07 to the CMCON register.
 

RWh

Joined Feb 4, 2010
2
Doh..! My first post and already in trouble - I did answer the question though... Sorry Icarusbop - In future I'll start new threads.

Thanks for the help AlexR - looking through my assembly I did write 0x07 to the CMCON reg... I'll give it a go with the C...
 
Top