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.

PIC16F1937, Trouble with useing Functions,

Status
Not open for further replies.

badscr

New Member
Have a project do.
I am using a PIC16F1937. and HI-Tech C compiler v9.65 with MPLAB v8.46
I can not get a Function to work at all. ( I can put stuff in the main loop and it works )

Have use functions on a PIC16F690, PIC16F616, PIC16F887 And they all work fine, But not on a PIC 16F1937.


In the example below RC0 will turn ON but RA1 will not.
***** sample of code*****

//Setup the chip __Config.

void main() { // Start OF main

void myfunction (void); // declare the function

// Setup ports/pins/stuff

//*************************
while(1) {

myfunction();

RC0=1;

} }// End of Main Loop
//*************************


void myfunction (void)
{
RA1=1;
}

// END of All CODE
 
To send the code to the chip, I am having to use PICkit 2 v2.61 software instead of the MPLAB like I normally do.

MPLAB want let me send the code to the PICkit2.
but PICkit 2 v2.61 software will let me send the code to the PICkit2.
 
Did you disable analog functions on port A? It's on by default. Also, is there more code? I'm not familiar with that compiler, but most require that you set the pins as input or output.
 
#include <htc.h>

// __CONFIG sets the Configuration Words.
__CONFIG ( CLKOUTEN_ON & CPD_OFF & CP_OFF & MCLRE_OFF & WDTE_OFF & FOSC_INTOSC ) ;
__CONFIG ( VCAPEN_OFF ) ;


void main() { // Start of MAIN


void test (void); // Declare Function
//int getADC (void); // ( Originally )


ANSA0=1; // set as Analog
ANSELD=0x00; //set as digital

TRISA = 0xFF; // set up PORTA are as All Inputs Analog Inputs
TRISB = 0x00; // set up PORTB are as All Outputs Red LEDS 9~10 & Yellow LEDS 9~10
TRISC = 0x00; // set up PORTC are as All Outputs Red LEDS 1~8
TRISD = 0x00; // set up PORTD are as All Outputs Yellow LEDS 1~8

PORTA=0x00; //Clear PORTA
PORTB=0x00; //Clear PORTB (Other LEDs)
PORTC=0x00; //Clear PORTC (Red LEDS 1~8 )
PORTD=0x00; //Clear PORTD (Yellow LEDS 1~8 )


//*****************************SETUP ADC*********************
ADCON1=0x10; //Left justified, FOSC/8, VREF- is VSS, VREF+ is VDD,
ADCON0=0x03; //Analog Channel AN0, GO/DONE=1, ADON=1,


//***********************************************************
while(1) //INF LOOP
{

//PORTC = getADC (); // ( Originally )

test(); // myfunction (Not Original )
RC3=1; // (Not Original )
RC4=0; // (Not Original )
RC5=1; // (Not Original )
PORTD=0x26; // (Not Original )

} //End INF LOOP
} //END main


//**************** TEST *****************
void test (void) // Return A/D reading from AD0 ( Not Originally )
{
PORTC=0b01100111; // ( Not Originally )
RC7=1; // ( Not Originally )
} // End Function




//****************Return A/D reading from AD0*****************
//int getADC (void) // ( Originally )
//{
//int advalue=2; // ( Originally )
//GO_nDONE=1; //start a/d process ( Originally )
//while(GO_nDONE==1) { } //wait for a/d to finish ( Originally )
//advalue = ADRESH; //Copy ADC result into advalue varible ( Originally )
//return advalue; // send the result back ( Originally )
//} // End Function
 
Last edited:
With the code above none of the LEDs turn ON. (PORTD or PORTC)

But if I delete "test(); // myfunction" from the main loop, Then PORTD and PORTC will turn ON the LEDs. ( RC3=1; RC4=0; RC5=1; PORTD=0x26; )

There is something to do with calling the function?
Everything is wired right. Parts/LED are good....
I can not think of a single reason why it want work.
 
Last edited:
A very important part of debugging is to not try to debug more than one thing at a time. You've got several things going on and you've changed it from your first post.

What are you trying to do?

If part of your program is to turn on LEDs, then get that working before you add your analog stuff.
 
I have two sets of 10 LEDs (20 LEDs in all) that I have to sequence into about 20+ basic patterns.

I have gotten the LEDs, Analog, and other stuff to work; as long as there is no functions used.
But the use of any functions will couse everything to stop working.
The functions cut down on the space that is needed, which I seem to always use every bet of.


I have working code for 10 LEDs that run on a PIC16F690 if you want to see it. which is what I am trying to do with the 16F1937 but with more LEDs.
 
Last edited:
nothing, same.
using any function, nothing works.


HI-Tech compiler supports the Chip
MPLAB supports the Chip
PICkit2 supports the Chip
 
Last edited:
Each LED is direct drive with a 1K-ohm resistor. (PIC to Resistor to LED to Ground ).
In short 4.5Volts DC. (Am using a Computer Power Supply with a Load bank and using a diode to drop 0.7Volts off) (with about 70mVp-p of noise)
There is no problems with a 16F690.

I setup a program to blink one LED (RC0) three times, Then blink another LED (RC1) three times, Works perfect.
but if I call a function anywhere in the code then nothing works.
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top