Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

What's wrong with my really simple code?

Status
Not open for further replies.

cowana

New Member
I'm running a PIC16F722. Programming in C, via the Hi-tech compiler.

Code:
#include <htc.h>

void init(void)
{
	TRISC = 0b00000000;
	TRISA = 0b11110000;
}

void main(void) {
init ();

while (1) {
PORTC=0;
_delay (190000);
}
}

However, the voltage on all the PORTC pins is between 1.2v and 1.4v. What's going on? Shouldn't they be at 0v potential? I'm powering the PIC from 5v. There is nothing connected to the outputs.

Thanks

Andrew
 
I've added some CONFIG settings, but I'm still seeing 1.3v over all of PORTC.

Code:
#include <htc.h>
#include <pic.h>

__CONFIG(HS & WDTDIS & UNPROTECT & BORDIS);
__CONFIG(VCAPRA5);

void init(void)
{
	TRISC = 0b00000000;
	TRISA = 0b11110000;
}

void main(void) {
//init ();

while (1) {
PORTC=004;
_delay (190000);
}
}

Any ideas?
 
Last edited:
**broken link removed**
This board.

No schematic is provided, but it is the PIC with five of the outputs feeding a 74HC08 AND gate. I've drawn out the schematic, and it is really simple.
 
Last edited:
Can you post that scematic ?
One thing i see is that you've selected the HS oscillator in the config word, but i don't see any crystal on the board ?
The HS setting is for a high speed crystal oscillator.
 
Here's the schematic (plus a 15K pullup on pin 1).

I want to use the PICs internal oscillator - I'll try and work out what to change the config value to.
 

Attachments

  • IMAG0115.jpg
    IMAG0115.jpg
    436.9 KB · Views: 378
Last edited:
You'll also have to change the config word to set the MCLR/RE3 pin to internal MCLR operation.
This is the reset pin OR RE3. The config word chooses what it will be - if it's set to reset function and left floating like on your scematic the pic will go in and out of reset randomly.
 
Many thanks, Exo - now I can try and start to get this working.

From the header file:
Code:
// Oscillator configurations 
#define RCCLKO		0x3FFF
#define RCIO		0x3FFE
#define INTCLKO		0x3FFD
#define INTIO		0x3FFC
#define EC		0x3FFB
#define HS		0x3FFA
#define XT		0x3FF9
#define LP		0x3FF8
// Watchdog timer enable 
#define WDTEN		0x3FFF
#define WDTDIS		0x3FF7
// Power up timer enable 
#define PWRTEN		0x3FEF
#define PWRTDIS		0x3FFF
// MCLR pin function 
#define MCLREN		0x3FFF
#define MCLRDIS		0x3FDF

So does that mean I want MCLRDIS and INTIO in the config?

Thanks

Andrew
 
Yes, and according to the datasheet there should also be a bit in the config word called PLLEN allowing you to choose between 500Khz and 16Mhz for the internal oscillator.
 
I'm really confused here, so:


__CONFIG(MCLRDIS & INTIO & PLLEN & WDTDIS & UNPROTECT & BORDIS);

Would be correct?

Thanks

Andrew
 
This is very annoying!

Code:
#include <htc.h>

__CONFIG(MCLRDIS & INTIO & PLLEN & WDTDIS & UNPROTECT & BORDIS);
__CONFIG(VCAPDIS);


void init(void)
{
	TRISC = 0b00000000;
	TRISA = 0b11110000;
	PORTA = 0b00000000;
	PORTC = 0b00000000;
}

void main(void)
{

while (1){
PORTC=0b00000000;
_delay (190000);
PORTC=0b11111111;
_delay (190000);
}
}

Still no luck - can anyone see why that wouldn't run? I'm still getting ~1.3v on all the pins of PORTC.

Did I mention it was a 16F722?
 
Last edited:
You're not calling the init function from the main code.
Also check if the config word makes it into the hex file.

Also , the delay function in hi tech C requires you to define the oscillator speed somehow (forgot the specifics). without this definition the delay code might be way off - for a start, try just setting the outputs high and then enter a endless loop. If the outputs are high when you measure them then you at least know the program has been executed, then move on.
 
Last edited:
Thanks Exo - that seems to be the problem. Now the software is sorted, it's onto the hardware!

Thanks again.

Andrew
 
Status
Not open for further replies.

Latest threads

Back
Top