PIC external clock help

Thread Starter

victorment

Joined Jul 4, 2010
52
Does the external clock value and the clock value that you had program to the PIC must match?

I have programmed a PIC 16f877A using Flowcode and set the clock speed in 3276800. I load the program to the PIC and verify and it was successful. After then I interface the pic, I connected the supply vdd, vss and a 4MHZ crystal oscillator and the leds but the PIC won't run. The leds won't light. What seem to be the problem can you help me?
 

SgtWookie

Joined Jul 17, 2007
22,210
Try removing the xtal oscillator. Did you use the proper caps for the oscillator?

You really should have programmed it with the right frequency.
 

BMorse

Joined Sep 26, 2009
2,675
Does the external clock value and the clock value that you had program to the PIC must match?

I have programmed a PIC 16f877A using Flowcode and set the clock speed in 3276800. I load the program to the PIC and verify and it was successful. After then I interface the pic, I connected the supply vdd, vss and a 4MHZ crystal oscillator and the leds but the PIC won't run. The leds won't light. What seem to be the problem can you help me?

Yes they have to match, and can you post a copy of the circuit schematic for your circuit?
 

ErnieM

Joined Apr 24, 2011
8,006
Well, it depends. Flowcode (and several other programming packages) only need that value so they can time delays and such that are set by code. Meaning of you told it you have a 1uS instruction rate and you use a 1mS delay, it will waste 1,000 instructions to make that delay. If you then go and use say a .1uS instruction rate (10x higher frequency) then you will only get a .1mS delay.

The code will run, but the results may not be any good. That will vary on why you needed that delay.

That said, if you are just say lighting a LED when it starts that should work just fine as no delays are involved.

Your schematic looks fine, only exception I see offhand is you can't do in circuit programming with the way you have MCLR set up.

You should look at how you set the configuration bits as those tell the processor what kind of oscillator you are using, if that is set wrong it doesn't work.
 

SgtWookie

Joined Jul 17, 2007
22,210
Post your source code.
Click the "Go Advanced" button, then select the "#" on the menu bar, and then paste your source code in between the code blocks.
 

ErnieM

Joined Apr 24, 2011
8,006
This PIC has no internal oscillator (or I would have suggested for the OP to use it as a sanity check).

victorment: look in your Flowcode help file for configuration bits. It has to be there, along with how to set them. We can help you get what you set them to, you need to find how you set them.
 

Thread Starter

victorment

Joined Jul 4, 2010
52
Here is the C Code:


//************************************************************************************
//**
//** File name: C:\Users\Public\Documents\Vic PIC\ADC for design project.c
//** Generated by: Flowcode v4.2.3.58
//** Date: Sunday, September 25, 2011 20:14:20
//** Licence: Professional
//** Registered to: victory
//**
//**
//** http://www.matrixmultimedia.com
//************************************************************************************


#define MX_PIC

//Defines for microcontroller
#define P16F877A
#define MX_EE
#define MX_EE_TYPE2
#define MX_EE_SIZE 256
#define MX_SPI
#define MX_SPI_C
#define MX_SPI_SDI 4
#define MX_SPI_SDO 5
#define MX_SPI_SCK 3
#define MX_UART
#define MX_UART_C
#define MX_UART_TX 6
#define MX_UART_RX 7
#define MX_I2C
#define MX_MI2C
#define MX_I2C_C
#define MX_I2C_SDA 4
#define MX_I2C_SCL 3
#define MX_PWM
#define MX_PWM_CNT 2
#define MX_PWM_TRIS1 trisc
#define MX_PWM_1 2
#define MX_PWM_TRIS2 trisc
#define MX_PWM_2 1

//Functions
#define MX_CLK_SPEED 4000000
#ifdef _BOOSTC
#include <system.h>
#endif
#ifdef HI_TECH_C
#include <pic.h>
#endif

//Configuration data
#ifdef _BOOSTC
#pragma DATA 0x2007, 0x3fb1
#endif
#ifdef HI_TECH_C
__CONFIG(0x3fb1);
#endif

//Internal functions
#include "C:\Program Files (x86)\Matrix Multimedia\Flowcode V4\FCD\internals.h"

//Macro function declarations


//Variable declarations
char FCV_SWITCH;
char FCV_ADC;


//Defines:

/**** Macro Substitutions ****
0 = Which ADC Channel
40 = Acquisition time
3 = Conversion Speed
0 = VRef+ Option
500 = VRef Voltage x 0.01V
******************************/




//ADC0: //Macro function declarations

void FCD_ADC0_SampleADC();
char FCD_ADC0_ReadAsByte();
short FCD_ADC0_ReadAsInt();
float FCD_ADC0_ReadAsVoltage();
void FCD_ADC0_ReadAsString(char* FCR_RETVAL, char FCR_RETVAL_SIZE);
//Defines:

/**** Macro Substitutions ****
porta = Switch Port Register
trisa = Switch Data Direction Register
1 = Switch Pin
0 = Switch Polarity Control
0 = Switch Debounce Delay Value
******************************/




//SWITCH0: //Macro function declarations

char FCD_SWITCH0_ReadState();
void FCD_SWITCH0_WaitUntilHigh();
void FCD_SWITCH0_WaitUntilLow();



//ADC0: //Macro implementations


void FCD_ADC0_SampleADC()
{

/*******Supported Devices******************************************************************
// 16C72, 16C72A, 16CR72, 16F72, 16C73, 16C73A, 16C73B, 16F73, 16C74, 16C74A, 16C74B, 16F74,
// 16C76, 16F76, 16C77, 16F77, 16F818, 16F819, 16F870, 16F871, 16F872, 16F873, 16F873A,
// 16F874, 16F874A, 16F876, 16F876A, 16F877, 16F877A,
******************************************************************************************/

#define MX_ADC_CHANNEL 0
#define MX_ADC_SAMP_TIME 40
#define MX_ADC_CONV_SP 3
#define MX_ADC_VREF_OPT 0

//set up ADC conversion
char old_tris, cnt;

//find appropriate bit
#if (MX_ADC_CHANNEL == 0)
#define MX_ADC_TRIS_MSK 0x01
#define MX_ADC_TRIS_REG trisa
#if (MX_ADC_VREF_OPT == 0)
adcon1 = 0x0E;
#else
adcon1 = 0x05;
#endif
#endif
#if (MX_ADC_CHANNEL == 1)
#define MX_ADC_TRIS_MSK 0x02
#define MX_ADC_TRIS_REG trisa
#if (MX_ADC_VREF_OPT == 0)
adcon1 = 0x04;
#else
adcon1 = 0x05;
#endif
#endif
#if (MX_ADC_CHANNEL == 2)
#define MX_ADC_TRIS_MSK 0x04
#define MX_ADC_TRIS_REG trisa
#if (MX_ADC_VREF_OPT == 0)
adcon1 = 0x02;
#else
adcon1 = 0x03;
#endif
#endif
#if (MX_ADC_CHANNEL == 3)
#define MX_ADC_TRIS_MSK 0x08
#define MX_ADC_TRIS_REG trisa
#if (MX_ADC_VREF_OPT == 0)
adcon1 = 0x04;
#else
#pragma error "Target device is currently using AN3 for VREF+"
#endif
#endif
#if (MX_ADC_CHANNEL == 4)
#define MX_ADC_TRIS_MSK 0x20
#define MX_ADC_TRIS_REG trisa
#if (MX_ADC_VREF_OPT == 0)
adcon1 = 0x02;
#else
adcon1 = 0x03;
#endif
#endif
#if (MX_ADC_CHANNEL == 5)
#define MX_ADC_TRIS_MSK 0x01
#define MX_ADC_TRIS_REG trise
#if (MX_ADC_VREF_OPT == 0)
adcon1 = 0x09;
#else
adcon1 = 0x01;
#endif
#endif
#if (MX_ADC_CHANNEL == 6)
#define MX_ADC_TRIS_MSK 0x02
#define MX_ADC_TRIS_REG trise
#if (MX_ADC_VREF_OPT == 0)
adcon1 = 0x00;
#else
adcon1 = 0x01;
#endif
#endif
#if (MX_ADC_CHANNEL == 7)
#define MX_ADC_TRIS_MSK 0x04
#define MX_ADC_TRIS_REG trise
#if (MX_ADC_VREF_OPT == 0)
adcon1 = 0x00;
#else
adcon1 = 0x01;
#endif
#endif

//sanity check
#ifndef MX_ADC_TRIS_REG
#pragma error "ADC Type 1 conversion code error - please contact technical support"
#endif

//assign conversion speed
#if (MX_ADC_CONV_SP > 3)
st_bit(adcon1, ADCS2);
#endif

//store old tris value, and set the i/o pin as an input
old_tris = MX_ADC_TRIS_REG;
MX_ADC_TRIS_REG = MX_ADC_TRIS_REG | MX_ADC_TRIS_MSK;

//turn ADC on
adcon0 = (0x01 | (MX_ADC_CONV_SP << 6)) | (MX_ADC_CHANNEL << 3);

//wait the acquisition time
cnt = 0;
while (cnt < MX_ADC_SAMP_TIME) cnt++;

//begin conversion and wait until it has finished
adcon0 = adcon0 | 0x04;
while (adcon0 & 0x04);

//restore old tris value, and reset adc registers
MX_ADC_TRIS_REG = old_tris;
adcon1 = 0x07;
adcon0 = 0x00;

#undef MX_ADC_CHANNEL
#undef MX_ADC_TRIS_REG
#undef MX_ADC_TRIS_MSK
#undef MX_ADC_SAMP_TIME
#undef MX_ADC_CONV_SP
#undef MX_ADC_VREF_OPT

}

char FCD_ADC0_ReadAsByte()
{

FCD_ADC0_SampleADC();

return adresh;

}

short FCD_ADC0_ReadAsInt()
{

short iRetVal;

FCD_ADC0_SampleADC();

iRetVal = (adresh << 2);
iRetVal = iRetVal | (adresl >> 6);

return (iRetVal);

}

float FCD_ADC0_ReadAsVoltage()
{


int iSample;
float fSample, fVoltage, fVperDiv;

#define MX_ADC_VREF_V 500

iSample = FCD_ADC0_ReadAsInt(); //Read as 10-bit Integer

#ifdef _BOOSTC
fVoltage = float32_from_int32(MX_ADC_VREF_V); //Convert reference voltage count to floating point (0 - 500 x 10mV)
fVoltage = float32_mul(fVoltage, 0.01); //Convert reference voltage count to actual voltage (0 - 5)
fVperDiv = float32_mul(fVoltage, 0.000976); //Convert actual voltage to voltage per division (VRef / 1024)
fSample = float32_from_int32(iSample); //Convert to floating point variable
fVoltage = float32_mul(fSample, fVperDiv); //Calculate floating point voltage
#endif

#ifdef HI_TECH_C
fVoltage = MX_ADC_VREF_V; //Convert reference voltage count to floating point (0 - 500 x 10mV)
fVoltage = fVoltage * 0.01; //Convert reference voltage count to actual voltage (0 - 5)
fVperDiv = fVoltage * 0.000976; //Convert actual voltage to voltage per division (VRef / 1024)
fSample = iSample; //Convert to floating point variable
fVoltage = fSample * fVperDiv; //Calculate floating point voltage
#endif

#undef MX_ADC_VREF_V
return (fVoltage);

}

void FCD_ADC0_ReadAsString(char* FCR_RETVAL, char FCR_RETVAL_SIZE)
{

float fVoltage;

fVoltage = FCD_ADC0_ReadAsVoltage();
FCI_FLOAT_TO_STRING(fVoltage, 2, FCR_RETVAL, FCR_RETVAL_SIZE); //Convert to String

}



//SWITCH0: //Macro implementations


char FCD_SWITCH0_ReadState()
{

char switchval;

set_bit ( trisa, 1 ); //Convert pin to input
delay_us(2); //Allow input voltage to settle

if (test_bit( porta, 1 )) //Read state on input
switchval = 1;
else
switchval = 0;

if ( 0 == 1 ) //Active low polarity
{
if (switchval) //Invert logic
switchval = 0;
else
switchval = 1;
}
return (switchval); //Return state of switch as least significant bit

}

void FCD_SWITCH0_WaitUntilHigh()
{

set_bit ( trisa, 1 ); //Convert pin to input
delay_us(2); //Allow input voltage to settle

if ( 0 == 0 ) //Active high polarity
{
while (test_bit( porta, 1 ) == 0); //Wait for pin to go high
}
else //Active low polarity
{
while (test_bit( porta, 1 )); //Wait for pin to go low
}
if ( 0 ) //Timeout specified?
Wdt_Delay_Ms(0);

}

void FCD_SWITCH0_WaitUntilLow()
{

set_bit ( trisa, 1 ); //Convert pin to input
delay_us(2); //Allow input voltage to settle

if ( 0 == 0 ) //Active high polarity
{
while (test_bit( porta, 1 )); //Wait for pin to go low
}
else //Active low polarity
{
while (test_bit( porta, 1 ) == 0); //Wait for pin to go high
}
if ( 0 ) //Timeout specified?
Wdt_Delay_Ms(0);

}

//Macro implementations

void main()
{

//Initialisation
adcon1 = 0x07;


//Interrupt initialisation code
option_reg = 0xC0;


//Loop
//Loop: While 1
while (1)
{
//Call Component Macro
//Call Component Macro: adc=ADC(0)::ReadAsByte
FCV_ADC = FCD_ADC0_ReadAsByte();


//Output
//Output: adc -> PORT C
trisc = 0x00;
portc = FCV_ADC;


//Call Component Macro
//Call Component Macro: switch=SWITCH(0)::ReadState
FCV_SWITCH = FCD_SWITCH0_ReadState();


//Decision
//Decision: switch = 1?
if (FCV_SWITCH == 1)
{
//Output
//Output: 1 -> B0
trisb = trisb & 0xfe;
if (1)
portb = (portb & 0xfe) | 0x01;
else
portb = portb & 0xfe;


} else {
//Output
//Output: 0 -> B0
trisb = trisb & 0xfe;
if (0)
portb = (portb & 0xfe) | 0x01;
else
portb = portb & 0xfe;


}


}


mainendloop: goto mainendloop;
}

void MX_INTERRUPT_MACRO(void)
{
}
 
Top