|
|
View previous topic :: View next topic |
Author |
Message |
assaad
Joined: 09 Dec 2009 Posts: 37
|
Stack over flow problem |
Posted: Sat May 14, 2011 9:30 am |
|
|
Hi all
Here down my code. I could don't see why I am getting stack overflow.
When I close the line set_digits() in while in main there is no problem.
Could you guide me please ?
Regards
Code: |
#include <16F628A.h>
#include <main.h>
//#include <irs.h>
#device adc=10
#device ICD=TRUE
#include <stdlib.h>
//#include <stdio.h>
//#include <math.h>
//--------------------------------------------
const char seven_segments[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x67};
volatile signed int8 ntc=0;
volatile signed int16 delay_500ms=0;
volatile signed int8 displayflash=0;
unsigned char digits[2]={0x30,0x30};
int1 digit0_flash=0;
int1 digit1_flash=0;
//---------------------------------------------------
#use fast_io (a)
#use fast_io (b)
#use delay(clock=4000000) //,RESTART_WDT)
//#fuses NOWDT,INTRC_IO, NOPUT, PROTECT, BROWNOUT, NOLVP, NOCPD, NOMCLR,NOBROWNOUT,
#fuses NOWDT,INTRC_IO, NOPUT, PROTECT, BROWNOUT, NOLVP, NOCPD, NOMCLR,NOBROWNOUT,
//--------------------------------------
void set_digits(void);
//----------------------------------------------------------
void button_1_control(void)
{
static char flag=0;
if(!input(button1))
{
ntc++;
set_digits();
}
}
//------------
void init()
{
output_a(0x00);
output_b(0x00);
set_tris_a(0x71);
set_tris_b(0x00);
port_b_pullups(FALSE);
setup_oscillator(OSC_4MHZ);
// setup_adc_ports(sAN0|VSS_VDD);
//setup_adc(ADC_CLOCK_DIV_32);
//setup_wdt(WDT_OFF);
setup_timer_1(T1_DISABLED);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
setup_timer_0(T0_INTERNAL|T0_DIV_8);
TMR0IF=0;
//TMR08bit=0;
TMR0ON=0;
set_timer0(130);// 1ms interrupt
//set_adc_channel(0);
enable_interrupts ( INT_TIMER0 ); /* enable timer interrupt */
enable_interrupts ( GLOBAL );
// 0.250 0.5 1
// set_timer0(64535); // 57722= 200 ms///set_timer0(57722); //(55829); //(26473); ///(46004);
set_timer0(130);
TMR0ON=1;
}
//*****************
void main() {
/////////////////////////////////////
init();
/////////////////////////////////////
ntc=95;
set_digits();
while(1)
{
digit1_flash=1;
set_digits();
button_1_control();
// output_b(0xff);
/*
output_high(pin_b7); // BUZZER ON
output_b(0xff);
delay_ms(1000);
output_low(pin_b7); // BUZZER off
output_b(0x00);
delay_ms(1000);
*/
}
//////////////////////////////////////////////
}
//---------
void set_digits(void)
{
sprintf(digits, "%02u",ntc);
digits[0]=digits[0] - 0x30;
digits[1]=digits[1] - 0x30;
}
/////////////////////////////////////////////////////////////////////
//#INT_RTCC
#int_timer0
void timer0_isr(void)
{
unsigned int8 temp=0;
static int1 flag=0;
static unsigned int16 ticks=0;
static unsigned int16 half_second_ticks=0;
TMR0IF=0;
set_timer0(130);
//-----------
ticks++;
if(half_second_ticks ++>499)
{
half_second_ticks =0;
}
//--------------------------------------------
if(half_second_ticks > 200)
{
displayflash=1;
}
else
{
displayflash=0;
}
//-----------------------------------------
//_7segment();
//-----------------------------------------
output_low(digit1); //
output_low(digit0); //
output_b(0x00);
if(flag==0)
{
flag=1;
output_low(digit1); //
output_high(digit0); //
temp=seven_segments[digits[1]];
if(digit0_flash==1)
{
if((displayflash==1))
{
output_b(temp);
}
else
{
output_b(0);
}
}
else
{
output_b(temp);
}
}
else
{
flag=0;
output_low(digit0); //
output_high(digit1); //
temp=seven_segments[digits[0]];
if(digit1_flash==1)
{
if((displayflash==1))
{
output_b(temp);
}
else
{
output_b(0);
}
}
else
{
output_b(temp);
}
}
//--------
if(ticks++>999)
{
ticks=0;
output_toggle(triac); //
/*
set_digits();
if(++ntc>99)
{
ntc=0;
}
*/
}
}
|
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sat May 14, 2011 1:32 pm |
|
|
How do you know that you get stack overflow ? Are you testing it in
MPLAB simulator ? If so, what version of MPLAB ? Are you setting a
breakpoint and then clicking on "Run" ? Post the full error message
that you get.
Tell us the conditions of the test. Tell us how to duplicate the problem.
Tell us what software tools you are using.
Tell us the versions of MPLAB and the CCS compiler. |
|
|
assaad
Joined: 09 Dec 2009 Posts: 37
|
|
Posted: Sat May 14, 2011 2:33 pm |
|
|
Thank you for reply
I tested in proteus, MPLAB and real circuit: Proteus gives a stack over flow message.
I moved to 16F819 with the same circuit;
it seems I that can't figure out B6 and B7 as high. They still zero, also power up time is always disables although I enable it.
I tried also with 16F88; it seems that the fuses does not work.
For 16F628A; when I erases sprintf and I used BCD to hex, the stack flow problem is solved but I don't want to use 16F628A because I need analog input, so I MUST use 16F819 or 16F88.
Here is the code.
My version is 4.106
MPLAB version is 8.56
Thank you again
Code: |
#include <16F819.h>
#include <main.h>
//#include <irs.h>
#device adc=8
//#device ICD=TRUE
#include <stdlib.h>
//#include <stdio.h>
//#include <math.h>
#fuses INTRC_IO, NOWDT,PUT,NOMCLR,NOBROWNOUT, NOLVP,CPD,NOWRT,NODEBUG,CCPB2,NOPROTECT
#use delay(clock=4000000) //,RESTART_WDT)
#use fast_io (a)
#use fast_io (b)
//#fuses NOWDT,INTRC_IO, NOPUT, PROTECT, BROWNOUT, NOLVP, NOCPD, NOMCLR,NOBROWNOUT,
//#fuses NOWDT,INTRC_IO, PUT, PROTECT, NOBROWNOUT, NOLVP, NOCPD, NOMCLR,
//--------------------------------------
void set_digits(void);
void Hex2Decimal(unsigned int8 num);
//--------------------------------------------
const char seven_segments[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x67};
volatile unsigned int8 ntc=0;
volatile unsigned int16 delay_500ms=0;
volatile unsigned int8 displayflash=0;
unsigned char digits[2]={0x30,0x30};
int1 digit0_flash=0;
int1 digit1_flash=0;
//---------------------------------------------------
unsigned int8 keypad(void)
{
unsigned int8 key=no_key;
if((!input(button1))&&(!input(button2) ))
{
key=left_right;
}
else if((!input(button1)))
{
key=left;
}
else if ((!input(button2)))
{
key=right;
}
return key;
}
//------------
void bin2bcd(int8 value) {
char retval;
retval = 0;
while(1)
{
// Get the tens digit by doing multiple subtraction
// of 10 from the binary value.
if(value >= 10)
{
value -= 10;
retval += 0x10;
}
else // Get the ones digit by adding the remainder.
{
retval += value;
break;
}
}
digits[1]=(retval&0xf);
digits[0]=((retval>>4)&0xf);
}
//----------------------------------------------------------
void button_1_control(void)
{
static char flag=0;
if((!input(button1))&&(flag==0))
{
if(++ntc>99)
{
ntc=0;
}
//set_digits();
//Hex2Decimal(ntc);
bin2bcd(ntc);
flag=1;
}
//------------------------
if(input(button1))
{
flag=0;
}
}
//----------
void button_2_control(void)
{
static char flag1=0;
if((!input(button2))&&(flag1==0))
{
ntc--;
if(ntc>99)
{
ntc=99;
}
//set_digits();
//Hex2Decimal(ntc);
bin2bcd(ntc);
flag1=1;
}
//------------------------
if(input(button2))
{
flag1=0;
}
}
//----------
void button_3_control(void)
{
static char flag2=0;
if((!input(button3))&&(flag2==0))
{
ntc=0;
//set_digits();
//Hex2Decimal(ntc);
bin2bcd(ntc);
flag2=1;
}
//------------------------
if(input(button3))
{
flag2=0;
}
}
//------------
void init(void)
{
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_ccp1(ccp_off);
output_a(0x00);
output_b(0x00);
set_tris_a(0x71);
set_tris_b(0x00);
port_b_pullups(FALSE);
setup_oscillator(OSC_4MHZ );
// setup_adc_ports(sAN0|VSS_VDD);
setup_adc(ADC_CLOCK_DIV_32);
// setup_wdt(WDT_OFF);
setup_timer_1(T1_DISABLED);
// setup_comparator(NC_NC_NC_NC);
// setup_vref(FALSE);
setup_timer_0(T0_INTERNAL|T0_DIV_8);
TMR0IF=0;
//TMR08bit=0;
TMR0ON=0;
set_timer0(5);// 1ms interrupt
set_adc_channel(0);
// enable_interrupts ( INT_TIMER0 ); /* enable timer interrupt */
// enable_interrupts ( GLOBAL );
// 0.250 0.5 1
set_timer0(5);
TMR0ON=1;
}
//------------------
void control_keypad(void)
{
static unsigned int8 hit_old=0;
unsigned int8 hit=0;
hit=keypad();
if(hit!=hit_old)
{
if(hit==left_right)
{
ntc=0;
}
else if(hit==left)
{
ntc++;
}
else if(hit==right)
{
ntc--;
}
}
hit_old=hit;
}
//*****************
void main() {
/////////////////////////////////////
init();
/////////////////////////////////////
ntc=95;
bin2bcd(ntc);
//Hex2Decimal(ntc);
//set_digits();
while(1)
{
output_high(digit1); //
output_high(digit0); //
output_b(0xff);
while(1);
//Hex2Decimal(ntc);
//digit1_flash=1;
//control_keypad();
//set_digits();
//button_1_control();
//button_2_control();
//button_3_control();
//bin2bcd(ntc);
// output_b(0xff);
/*
output_high(pin_b7); // BUZZER ON
output_b(0xff);
delay_ms(1000);
output_low(pin_b7); // BUZZER off
output_b(0x00);
delay_ms(1000);
*/
}
//////////////////////////////////////////////
}
//--------------------
void Hex2Decimal(unsigned int8 num)
{
unsigned char value[2];
unsigned char i = 0;
while(num > 0)
{
value[i] = num % 10;
num /= 10;
i = i + 1;
}
digits[0]=value[1];
digits[1]=value[0];
}
//---------
void set_digits(void)
{
sprintf(digits, "%02u",ntc);
digits[0]=digits[0] - 0x30;
digits[1]=digits[1] - 0x30;
}
/////////////////////////////////////////////////////////////////////
//#INT_RTCC
#int_timer0
void timer0_isr(void)
{
unsigned int8 temp=0;
static int1 flag=0;
static unsigned int16 ticks=0;
static unsigned int16 half_second_ticks=0;
TMR0IF=0;
set_timer0(5);
//-----------
ticks++;
if(half_second_ticks ++>499)
{
half_second_ticks =0;
}
//--------------------------------------------
if(half_second_ticks > 200)
{
displayflash=1;
}
else
{
displayflash=0;
}
//-----------------------------------------
//_7segment();
//-----------------------------------------
output_low(digit1); //
output_low(digit0); //
output_b(0x00);
if(flag==0)
{
flag=1;
output_low(digit1); //
output_high(digit0); //
temp=seven_segments[digits[1]];
if(digit0_flash==1)
{
if((displayflash==1))
{
output_b(temp);
}
else
{
output_b(0);
}
}
else
{
output_b(temp);
}
}
else
{
flag=0;
output_low(digit0); //
output_high(digit1); //
temp=seven_segments[digits[0]];
if(digit1_flash==1)
{
if((displayflash==1))
{
output_b(temp);
}
else
{
output_b(0);
}
}
else
{
output_b(temp);
}
}
//--------
if(ticks++>999)
{
ticks=0;
output_toggle(triac); //
}
} |
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sat May 14, 2011 3:53 pm |
|
|
What does the CCS compiler report for the number of stack levels that
are used ? This information is given at the top of the .LST file.
Post what it says. The .LST file will be in your project directory. |
|
|
assaad
Joined: 09 Dec 2009 Posts: 37
|
|
Posted: Sun May 15, 2011 12:04 am |
|
|
Stack used: 3 worst case (out of 7 total available)
What is the case for 16f819 ? I still could not use portb.6 and portb.7 as output .
I configured it as NOLVP; what could be the problem ?
Thank you |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19496
|
|
Posted: Sun May 15, 2011 2:25 am |
|
|
Start by tidying your code. Use some logic. So whenever you start a loop section or new routine, indent, and when these end remove this indent. Remove lines you are not using. You may well find that just doing this will make the fault obvious. Generally, if code is untidy, it is many thousands of times harder to spot errors. Funnily, your bin2bcd routine, is I guess copied directly from somebody who does layout their code reasonably. Note how much easier it is to read, than just about every other routine in your code...
Now, you have a couple of enable interrupt lines, but these appear to be remmed out. Are they?. A 'classic' cause of a stack overflow, would be having an interrupt enabled without a handler. You are manually fiddling with the interrupt flag later on, and you have a section of code, presumably containing the definitions used for this, that is not included (irs.h). The code as posted, won't compile, with a lot of defines missing (TMR0IF, no_key etc.).
NOLVP is right. With LVP enabled, you _must_ have a resistor on the LVP programming line to ensure it does not float, and random crashes without this would be normal.
Proteus has a _lot_ of problems in it's emulation. I have the full latest professional release, but do not use it's PIC emulation. I will use the analog emulation as an easier graphical alternative to spice, but very rarely find the PIC emulation completely works.
What is the drive circuit used on the LED's?.
Group thing together, that work together. You have (for example), a line to select the ADC clock, then one many lines further on, selecting an ADC channel, but the one to actually select what pins are available for ADC, appears to be remmed out. You need all three. because of the layout (lack of!....), this type of error is not obvious, and could cause problems...
I'd guess (but without the code actually being complete, and readable, it is nothing more than a guess....), that you are somewhere enabling an interrupt, and have not got a handler present - crash...
Best Wishes |
|
|
assaad
Joined: 09 Dec 2009 Posts: 37
|
|
Posted: Sun May 15, 2011 4:43 am |
|
|
Thank you ;
down is a simple program for 16f819 ; I simulated in MPLAB; Proteus and is real circuit ; PORTA always zero ! And portb.6 and portb.7 always zero .
what did I miss ; any problem in my sample code ?
Code: |
#include <16F819.h>
#include <main.h>
#device adc=8
#include <stdlib.h>
//--------------------------------------------
const char seven_segments[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x67};
volatile signed int8 ntc=0;
char digits[2]={0,0};
int1 sa;
//---------------------------------------------------
#use fast_io (a)
#use fast_io (b)
#use delay(clock=4000000))
#fuses NOWDT,INTRC_IO, PUT, PROTECT, BROWNOUT, NOLVP, NOCPD, NOMCLR,NODEBUG
//--------------------------------------
#byte porta = 0X05
/////////////////////////////////////////////////////////////////////
//------------
void init(void)
{
setup_oscillator(OSC_4MHZ);
output_a(0x00);
output_b(0x00);
set_tris_a(0x00);
set_tris_b(0x00);
port_b_pullups(FALSE);
setup_timer_1(T1_DISABLED);
SETUP_CCP1(CCP_OFF );
SETUP_ADC_PORTS(ADC_OFF );
}
//------------------
//*****************
void main() {
/////////////////////////////////////
init();
/////////////////////////////////////
//output_a(0x06);
OUTPUT_A(0x06);
//output_high(digit1); //
//delay_ms(1);
//output_high(digit0); //
delay_ms(1);
while(1)
{
output_b(0xff);
delay_ms(5000);
output_b(0x00);
delay_ms(5000);
}
//////////////////////////////////////////////
}
|
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun May 15, 2011 1:35 pm |
|
|
Post this file so that we can compile it. |
|
|
assaad
Joined: 09 Dec 2009 Posts: 37
|
|
Posted: Mon May 16, 2011 5:13 am |
|
|
thank you
Code: |
//////////////////////////////////
#byte ADCON1 = 0X09F
//********************************
#define digit0 PIN_A2
#define digit1 PIN_A1
#define triac PIN_A3
#define button1 PIN_A4
#define button2 PIN_A6
#define button3 PIN_A7
//////////////////////
#define left 1
#define right 2
#define middle 3
#define disable 0
#define enable 0
|
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19496
|
|
Posted: Mon May 16, 2011 7:15 am |
|
|
Tidies it up a bit, threw away some of the dross, and it runs both on a real chip, and in MPLAB.
Code: |
#include <16F819.h>
#device adc=8
#fuses NOWDT,INTRC_IO, PUT, PROTECT, BROWNOUT, NOLVP, NOCPD, NOMCLR,NODEBUG
#use delay(clock=4000000))
//This stuff should always be in front of just about anything else
//You _must_ have setup your RS232, and clock, _before_ including libraries like this
#include <stdlib.h>
//--------------------------------------------
const char seven_segments[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x67};
signed int8 ntc=0; //volatile does _nothing_ on the PIC
char digits[2]={0,0};
int1 sa;
//---------------------------------------------------
#use fast_io (a)
#use fast_io (b)
//------------
void init(void) {
setup_oscillator(OSC_4MHZ);
output_a(0x00);
output_b(0x00);
set_tris_a(0x00);
set_tris_b(0x00);
port_b_pullups(FALSE);
setup_timer_1(T1_DISABLED);
SETUP_CCP1(CCP_OFF );
SETUP_ADC(ADC_OFF);
SETUP_ADC_PORTS(NO_ANALOGS); //suspect part of the problem is here..
}
//------------------
//*****************
void main() {
/////////////////////////////////////
init();
/////////////////////////////////////
OUTPUT_A(0x06);
delay_ms(1);
do {
output_b(0xff);
delay_ms(5000);
output_b(0x00);
delay_ms(5000);
} while(TRUE); //Avoid compiler error
}
|
Best Wishes |
|
|
assaad
Joined: 09 Dec 2009 Posts: 37
|
|
Posted: Sat May 21, 2011 12:17 am |
|
|
Sorry for late reply, I was out of the city.
When I returned back, it works as expected. I don't know what was wrong ! Thats strange.
However thank you all for help.
Have a nice day. |
|
|
|
|
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
|