![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
deepakomanna
Joined: 06 Mar 2007 Posts: 92 Location: Pune,India
|
problem with using arrays & pointers |
Posted: Mon Apr 02, 2007 7:56 am |
|
|
Dear sir,
here i am using 16f684, MPLAB 7.5 Ver. & CCS PCM C Compiler, Version 3.249, 34534
Here i am getting garbage values in the called function CALCULATION_FUNCTION();
plz tell me what modification i have to do,
below is my programme
#if defined(__PCM__)
#include <16F684.h>
#fuses INTRC_IO,NOWDT,PUT,NOMCLR,PROTECT,NOCPD,NOBROWNOUT,NOPROTECT,NOIESO,NOFCMEN
//Internal RC Osc, no CLKOUT,No Watch Dog Timer,Power Up Time ON,Internal MCLR,Code Protect ON,No EE protection,Brownout detect OFF,
#use delay(clock=8000000)
void init_CPU();
void FUN1();
void FUN2();
void ON();
int16 CALCULATION_FUNCTION(unsigned int16 *,int8 *, int16 *,int );
unsigned int16 COUNT [4] = {60000,7500,6000,5000};
int8 SL[4] = {18,89,0,-106};
int16 DEL[4] = {4167,521,0,0};
unsigned int16 CNT;
int16 DLY_CNT;
int1 flag = 0;
void main()
{
CNT = 0;
SETUP_TIMER_1(T1_DISABLED);
SET_TIMER1(0);
init_CPU();
while(TRUE)
{
while(INPUT(PIN_A3)); //wait for ADVACE PULSE
FUN1(); //Jump to advance pulse routine
while(INPUT(PIN_C4)); // wait for zero degree pulse
FUN2(); // Jump to zero degree pulse routine.
}
}
void FUN1()
{
SETUP_CCP1(CCP_OFF); //Turn off PWM
SETUP_TIMER_2(T2_DISABLED,0,1); //disable Timer2
SET_TIMER2(0); // Timer_2 = 0
SET_TIMER1(0); //Timer_1 = 0
SETUP_TIMER_1(T1_INTERNAL | T1_DIV_BY_8); //TIMER 1 ON,Prescalar is 1:8
if(flag == 1)
{
while(DLY_CNT >= GET_TIMER1()); //wait till fire delay = Timer1
ON(); // Jump to SCR On routine
}
}
void FUN2()
{
CNT = 4500; //MOSFET will HEAT.
SETUP_CCP1(CCP_PWM_L_L); //PWM mode; P1A, P1C active low; P1B, P1D active low
SETUP_TIMER_2(T2_DIV_BY_1, 19, 1); // Setup for 100 KHz
SET_ADC_CHANNEL(0); // CHANNEL_0 FOR TPS I/P.
DELAY_US(10);
DLY_CNT = CALCULATION_FUNCTION(COUNT,SL,DEL,4);
}
int16 CALCULATION_FUNCTION(unsigned int16 *R_CNT,int8 *SLP, int16 *D,int n)
{
int8 i;
int16 DLY;
int32 DLY_CNT1;
for(i = 0; i <= n-1; i++)
{
if((CNT <R_CNT>= *(R_CNT + 1)))
{
flag = 1;
DLY_CNT1 = R_CNT - CNT;
DLY_CNT1 = (int32)(*SLP * DLY_CNT1); // int32
DLY_CNT1 = (int32)DLY_CNT1 >> 8;
DLY = (int16) (*D - DLY_CNT1);
break;
}
R_CNT++;
SLP++;
D++;
}
return(DLY);
}
void ON()
{
OUTPUT_HIGH(PIN_A5);
DELAY_US(60);
OUTPUT_LOW(PIN_A5);
}
void init_CPU()
{
/**************** PORT SETTINGS ***********************/
PORT_A_PULLUPS(FALSE);
SET_TRIS_A(0X01); // I/P = RA0,RA1,RA3. O/P = RA2,RA4,RA5.
SET_TRIS_C(0X10); // I/P = RC4. O/P = RC0,RC1,RC2,RC5.
/**************** COMPARATOR SETTINGS ***************/
SETUP_COMPARATOR(NC_NC_NC_NC);
/*************** PWM SETTINGS *********************/
SET_PWM1_DUTY(10); // 50% duty cycle ....2 LSB in (DC1B1 DC1B0) of CCP1CON register,
// The eight MSbs are found in CCPR1L.
SETUP_CCP1(CCP_PWM_L_L); //PWM mode; P1A, P1C active low; P1B, P1D active low
SETUP_TIMER_2(T2_DIV_BY_1, 19, 1); // Start PWM & Set PWM Freq. to 100 KHz
/*************** END OF CPU INIT ******************/
} _________________ Thank You,
With Best Regards,
Deepak. |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah Guest
|
|
Posted: Mon Apr 02, 2007 9:02 am |
|
|
Without going many lines into the code, one thing leaps out. You declare 'sl', as an 'int8', then put a signed value into it. An 'int8', is _unsigned_, unless you specify it as 'signed'. This alone may be causing your problems...
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
deepakomanna
Joined: 06 Mar 2007 Posts: 92 Location: Pune,India
|
problem with arrays & pointers not sloved....... |
Posted: Mon Apr 02, 2007 10:03 pm |
|
|
Dear Sir,
As per your suggestion i make chages in my code,
1)int8 SL to signed int8 SL;
2)int16 CALCULATION_FUNCTION(unsigned int16 *,signed int8 *, int16 *,int );
3)int16 CALCULATION_FUNCTION(unsigned int16 *R_CNT,signed int8 *SLP, int16 *D,int n)
but still the problem is not solved. plz reply _________________ Thank You,
With Best Regards,
Deepak. |
|
![](templates/subSilver/images/spacer.gif) |
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Tue Apr 03, 2007 6:33 am |
|
|
Please don't start a new thread for the same subject. As long as nobody has answered in the other thread you can still delete it. Please do so.
You say your program is not working, but what is it supposed to do and which behaviour do you observe?
I had troubles understanding your CALCULATION_FUNCTION(), so I took the freedom to rewrite it to a function doing exactly the same but easier to understand. Read my comments in the code below.
Code: | int16 CALCULATION_FUNCTION()
{
#define ARRAY_MAX 4
const unsigned int16 COUNT[ARRAY_MAX] = {60000,7500,6000,5000};
const signed int8 SL[ARRAY_MAX] = {18,89,0,-106};
const int16 DEL[ARRAY_MAX] = {4167,521,0,0};
int8 i;
int16 DLY;
int32 DLY_CNT1;
for (i = 0; i < ARRAY_MAX; i++)
{
if ((CNT <= COUNT[i]) && (CNT >= COUNT[i+1])) <-- BUG: i+1 is going to exceed the array boundary
{
flag = 1;
// DLY_CNT1 = R_CNT - CNT; <-- BUG: should have been *R_CNT (fixed below)
DLY_CNT1 = COUNT[i] - CNT; <-- Not nice: CNT is a global value, pass as a parameter instead.
DLY_CNT1 = (int32)(SL[i] * DLY_CNT1); // int32
DLY_CNT1 = (int32)DLY_CNT1 >> 8;
DLY = (int16) (DEL[i] - DLY_CNT1); <-- BUG: for i==2 and i==3 you are going to return
a negative value but DLY is unsigned.
break;
}
}
return DLY; <-- BUG: when CNT is not found (between 0 and 5000) an undefined value is returned
} |
|
|
![](templates/subSilver/images/spacer.gif) |
deepakomanna
Joined: 06 Mar 2007 Posts: 92 Location: Pune,India
|
problem with arrays & pointers yet not sloved....... |
Posted: Wed Apr 04, 2007 5:39 am |
|
|
Dear Sir,
Many Thanks for your reply.But still i have problem with this.As per your suggestion i made changes in my code. But if i used " const " keyword for arrays the value showing in watch window is different what i declared. Insted if haven't used " const " keyword the programe works fine.
So, may i want to know why the value is changing.
Below is my modified programe
Code: |
#if defined(__PCM__)
#include <16F684.h>
#fuses INTRC_IO,NOWDT,PUT,NOMCLR,PROTECT,NOCPD,NOBROWNOUT,NOPROTECT,NOIESO,NOFCMEN
//Internal RC Osc, no CLKOUT,No Watch Dog Timer,Power Up Time ON,Internal MCLR,Code Protect ON,No EE protection,Brownout detect OFF,
#use delay(clock=8000000)
void init_CPU();
void FUN1();
void FUN2();
void ON();
void CALCULATION_FUNCTION();
unsigned int16 CNT;
int16 DLY_CNT;
int1 flag = 0;
void main()
{
SETUP_TIMER_1(T1_DISABLED);
SET_TIMER1(0);
init_CPU();
while(TRUE)
{
while(INPUT(PIN_A3));
FUN1();
while(INPUT(PIN_C4));
FUN2();
}
}
void FUN1()
{
SETUP_CCP1(CCP_OFF); //Turn off PWM
SETUP_TIMER_2(T2_DISABLED,0,1); //disable Timer2
SET_TIMER2(0); // Timer_2 = 0
SET_TIMER1(0); //Timer_1 = 0
SETUP_TIMER_1(T1_INTERNAL | T1_DIV_BY_8); //TIMER 1 ON,Prescalar is 1:8
if(flag == 1)
{
while(DLY_CNT >= GET_TIMER1()); //wait till fire delay = Timer1
ON(); // Jump to SCR On routine
flag = 0;
}
}
void FUN2()
{
CNT = 4500;
SETUP_CCP1(CCP_PWM_L_L); //PWM mode; P1A, P1C active low; P1B, P1D active low
SETUP_TIMER_2(T2_DIV_BY_1, 19, 1); // Setup for 100 KHz
CALCULATION_FUNCTION();
}
void CALCULATION_FUNCTION()
{
/***** Value in watch window getting different when "using const " **/
//const unsigned int16 COUNT [6] = {7500,6000,5000,4839,3571,3333};
//const signed int8 SL[5] = {89,1,-106,4,24};
/const int16 DEL[4] = {542,17,14,81,60};
/*********************************************************
/*******programe works fine without using " const " ********/
unsigned int16 COUNT [6] = {7500,6000,5000,4839,3571,3333};
signed int8 SL[5] = {89,1,-106,4,24};
int16 DEL[4] = {542,17,14,81,60};
/***************************************************/
int8 i;
int16 DLY;
int32 DLY_CNT1;
for(i = 0; i <= 4; i++)
{
if((CNT <= COUNT[i]) && (CNT >= COUNT[i + 1]))
{
flag = 1;
DLY_CNT1 = COUNT[i] - CNT;
DLY_CNT1 = (signed int32)(SL[i] * DLY_CNT1); // int32
DLY_CNT1 = (signed int32)DLY_CNT1 >> 8;
DLY = (signed int16) (DEL[i] - DLY_CNT1);
break;
}
}
}
void ON()
{
OUTPUT_HIGH(PIN_A5);
DELAY_US(60);
OUTPUT_LOW(PIN_A5);
}
void init_CPU()
{
/**************** PORT SETTINGS ***********************/
PORT_A_PULLUPS(FALSE);
SET_TRIS_A(0X01); // I/P = RA0,RA1,RA3. O/P = RA2,RA4,RA5.
SET_TRIS_C(0X10); // I/P = RC4. O/P = RC0,RC1,RC2,RC5.
/**************** COMPARATOR SETTINGS ***************/
SETUP_COMPARATOR(NC_NC_NC_NC);
/*************** PWM SETTINGS *********************/
SET_PWM1_DUTY(10); // 50% duty cycle ....2 LSB in (DC1B1 DC1B0) of CCP1CON register,
// The eight MSbs are found in CCPR1L.
SETUP_CCP1(CCP_PWM_L_L); //PWM mode; P1A, P1C active low; P1B, P1D active low
SETUP_TIMER_2(T2_DIV_BY_1, 19, 1); // Start PWM & Set PWM Freq. to 100 KHz
/*************** END OF CPU INIT ******************/
}
|
_________________ Thank You,
With Best Regards,
Deepak. |
|
![](templates/subSilver/images/spacer.gif) |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|