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.

PIC16F877A freezes after a certain event

Status
Not open for further replies.

abicash

Member
Hello forum

My setup:
I am using a 16f877a with 11.0592MHz crytal and MPLAB IDE ver 8.92
Programmer is PICkit3.
Configuration :
HS OSC , WDT-disabled , PWRT-disabled, BOR-disabled,RB3-dig i/o,&MCLR pulled up to VCC=5v
All memory protections are off

I am driving 8 nos of 7 segment displays and 4 keys as input (all keys individual and not in a 2x2 matrix)

I am using a couple of unsigned long variables and no re-entrants in ISR's.
I also have an LED toggling in a one second TMR0 interrupt to show that the MCU is alive.

I am trying to display an 8-digit BCD number on the display on few key-presses.
Simulation works fine in proteus.

Display shows 0 0 0 0 0 0 0 0 - on reset
Press SET key
Display shows S E t
Press ENTER key
Display shows 1 2 3 4 5 6 7 8 (converted from decimal 12345678 or 0xBC614E)

On actual hardware
Display shows 0 0 0 0 0 0 0 0 - on reset
Press SET key
Display shows S E t
Press ENTER key
Display turns blank
MCU freezes
One second tick LED stops ticking.

I thought that this might be related to stack overflow , but I don't have re-entrant levels in ISR as I mentioned earlier.

I am now unable to understand this phenomenon, so requesting some learned members help on this one.

Thanks in advance
 
You have code that doesn't work and you want to know why but you don't include the code.

We're not mind readers.

Mike.
 
Thanks for replying.
I am attaching the code , but its a bit messy since yesterday , lots of commented lines.
Please bear with me
C:
#include <pic.h>
#include "stdio.h"

const unsigned char SET_KEY_PRESSED = 0x30;
const unsigned char UP_KEY_PRESSED = 0x24;
const unsigned char ENTER_KEY_PRESSED = 0x14;

const unsigned char seg_sel[8]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
const unsigned char seg[15]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0x77,0x7C,0x39,0x5E,0x71};
const unsigned char set[8]  ={0xFF,0xFF,0xFF,0xFF,0xFF,0x92,0x86,0x87};
const unsigned char rst[8]  ={0xFF,0xFF,0xFF,0xFF,0xFF,0xAF,0x92,0x87};
const unsigned char out[8]  ={0xFF,0xFF,0xFF,0xFF,0xFF,0xC0,0xC1,0x87};
const unsigned char yes[8] = {0xFF,0xFF,0xFF,0xFF,0xFF,0x91,0x86,0x92};
const unsigned char Noo[8] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xAB,0xC0};
const unsigned char test[8] = {0xFF,0xFF,0x92,0x86,0x87,0xFF,0xAB,0xC0};

unsigned int count=0;
unsigned char seg_counter;
unsigned char val[10];
unsigned char Key_Scan_Value=0,Key_Value=0;
unsigned char Key_Pressed=0;
unsigned int set_delay,delay_count=0,start_delay=0;
unsigned char Chk_All_Key_Open=0;
unsigned char Start_Set_Timer=0,Setting_mode=0;
unsigned int Set_Timer_Count=0,Set_timer_complete=0;
unsigned char display_string=0;
unsigned char Setting_Case=0;
unsigned char position_couter=0;
unsigned int Start_Blinking=0,Blink_Count=0,Blink_Value=0;
unsigned char Disp_Position=0;
unsigned long Set_Count_Value=0;
unsigned char temp1=0,temp2=0,temp3=0,temp4=0;

 signed char index=0 ;

void delayms(unsigned int count);
unsigned char Check_Key_Pressed(void);

void timer0_init(void)
{
    OPTION = 0x83;//option reg as 0x83
    TMR0 = 83;
    T0IE = 1;//timer 0 interrupt enable flag
    GIE = 1;
}


//--------------------------------------------------------------------------------------------------------
void timer1_init(void)
{
    T1CON = 0x01;
    TMR1H = 0xF5;
    TMR1L = 0x33;
    TMR1IE = 1;
}
//--------------------------------------------------------------------------------------------------------
void delayms(unsigned int count)
{
     delay_count = 0;
     set_delay = count;
      start_delay = 1;
      while(start_delay == 1);
}
//---------------------------------------------------------------------------------------------------------

//--------------------------------------------------------------------------------------------------------
void display_number_upto_99(unsigned char value)
{
    unsigned char k;
    k = value;
    val[0] = k/10;
    k = k%10;
    val[1] = k;
}
//--------------------------------------------------------------------------------------------------------
void display_set_on_display()
{
    unsigned char i;
    display_string = 1;
    for(i=8;i>0;i--)
    val[i-1] = set[8-i];
}
//--------------------------------------------------------------------------------------------------------
void display_test_on_display()
{
    unsigned char i;
    display_string = 1;
    for(i=8;i>0;i--)
    val[i-1] = test[8-i];
}
//--------------------------------------------------------------------------------------------------------
void display_rst_on_display()
{
    unsigned char i;
    display_string = 1;
    for(i=8;i>0;i--)
    val[i-1] = rst[8-i];
}
//--------------------------------------------------------------------------------------------------------
void display_out_on_display()
{
    unsigned char i;
    display_string = 1;
    for(i=8;i>0;i--)
    val[i-1] = out[8-i];
}
//--------------------------------------------------------------------------------------------------------------------------
void interrupt any_ISR(void)
{
//--------------------------------------------------------------------------------------------------------
    if (T0IF==1)
    {
        seg_counter++;
        if(seg_counter > 7)
            seg_counter = 0;
        PORTD = 0x00;
        PORTD = seg_sel[seg_counter];
        PORTB = 0xFF;
        if(display_string == 0)
        {
                        PORTB = seg[val[seg_counter]];
        }
        else
            PORTB = val[seg_counter];
    
        T0IF=0;
        TMR0 = 83;
    }
//--------------------------------------------------------------------------------------------------------
    if(TMR1IF)
    {
        count++;
        if(count > 1000)
        {
            count = 0;
            RC3 ^=1;
        }
        if(Start_Set_Timer==1)
        {
            Set_Timer_Count++;
        if(Set_Timer_Count > 1000)
            {
                Set_Timer_Count = 0;
                Start_Set_Timer = 0;
                Set_timer_complete = 1;
            }
        }
    
        if(start_delay == 1)
            delay_count++;
        if(delay_count > set_delay)
        {
               set_delay = 0;
               start_delay = 0;
        }

        TMR1IF = 0;
        TMR1H = 0xF5;
        TMR1L = 0x33;
    }
}
//--------------------------------------------------------------------------------------------------------
void soft_delay(void)
{
    unsigned int i;
    for(i=0;i<1000;i++);
}
//-------------------------------------------------------------------------------------------------------------------------
unsigned char Check_Key_Pressed(void)
{
        Set_Timer_Count = 0;
        Start_Set_Timer = 0;
        Key_Value = 0;
        Key_Scan_Value = 0;
        Key_Scan_Value = PORTA & 0x34;
        if(Key_Scan_Value != 0x34)
            {
                Key_Scan_Value = PORTA & 0x34;
                RC4 = 1;
                soft_delay();//soft_delay();soft_delay();
                RC4 = 0;
                Key_Scan_Value = PORTA & 0x34;
                if(Key_Scan_Value != 0x34)
                {
                    Chk_All_Key_Open = 1;
                    Key_Value  = Key_Scan_Value;
                }
            }
    if(Chk_All_Key_Open == 1)
    {
        do
        {
            Key_Scan_Value = PORTA & 0x34;
        }while(Key_Scan_Value != 0x34);
        Chk_All_Key_Open = 0;
    }
        return(Key_Value);
}
//--------------------------------------------------------------------------------------------------------

//-----------------------------------------------------------------------------------------
void main(void)
{
    unsigned long temp_i=0;
    unsigned char k=0;
  
    TRISC = 0x00;
    TRISD = 0x00;
    TRISB = 0x00;
    timer0_init();
    timer1_init();
    ADCON1 = 0x04;
    TRISA = 0x3F;
    
    delayms(250);

    display_string = 0;
    Setting_Case = 0;
    position_couter = 0;
//-----------------------------------------------------------------------------------------------------

    while(1)
    {
            Key_Pressed = Check_Key_Pressed();
            if((Key_Pressed == SET_KEY_PRESSED) && (Setting_mode ==0))
            {
                Key_Pressed = 0;
                    Setting_mode = 1;
                display_set_on_display();
            }

          if(Setting_mode == 1)
            {
                switch(Setting_Case)
                {
                    case 0:
                                
                                    if(Key_Pressed == UP_KEY_PRESSED)
                                        {
                                            Key_Pressed = 0;
                                            position_couter++;
                                            if(position_couter > 2)
                                                position_couter = 0;
                                    
                                            if(position_couter == 0)
                                                             display_set_on_display();
                                             else if(position_couter == 1)
                                                            display_rst_on_display();
                                            else if(position_couter == 2)
                                                            display_out_on_display();
                                         }
                                        if(Key_Pressed == ENTER_KEY_PRESSED)
                                        {
                                            Key_Pressed = 0;
                                               if(position_couter == 0)
                                               {
                                                   Setting_Case = 1;
                                                   Blink_Value = 0;
                                                   Blink_Count = 0;
                                                   Start_Blinking = 1;
                                                }
                                        }
                                    k=0;
                            break;

                    case 1:        
                               display_string = 0;
                                if(k==0)
                                {
                                       k=1;
                                      Set_Count_Value = 12345678;
                                      for ( index=0; Set_Count_Value>0; index++  )
                                       {
                                            temp_i = Set_Count_Value / 10 ;
                                            val[index] = Set_Count_Value-10*temp_i ;
                                            Set_Count_Value = temp_i ;
                                        }
                                 }
                        
                            break;
                }
            }
                     Key_Pressed = 0;
    }//while(1)
}//main()
 
Last edited:
Can I suggest that the first thing you do is to go through your code and properly indent it as some of it makes no sense such as this opening brace,
Code:
    case 1:
        display_string = 0;
            if(k==0){
                k=1;
                //if(Start_Blinking == 0)
                Set_Count_Value = 12345678;
                {                    <-------------This one
                for ( index=0; Set_Count_Value>0; index++  )
                {
                    temp_i = Set_Count_Value / 10 ;
                    val[index] = Set_Count_Value-10*temp_i ;
                    Set_Count_Value = temp_i ;
                }
                //Start_Blinking = 1;
            }//display_test_on_display();

Mike.
Edit, did you think this line was remarked out?
/**/ for ( index=0; Set_Count_Value>0; index++ )
 
Mike

I have edited the code for indents and other parses

EDIT : I have been debugging the code with a Pickit3 for some time now.

And i have found that the debugger shows the variable <temp_i> as <out of scope> after one pass through the <for ( index=9; Set_Count_Value>0; index-- )> loop.

Is this of some relevance?
 
Mike

I have edited the code for indents and other parses

EDIT : I have been debugging the code with a Pickit3 for some time now.

And i have found that the debugger shows the variable <temp_i> as <out of scope> after one pass through the <for ( index=9; Set_Count_Value>0; index-- )> loop.

Is this of some relevance?

The indenting above is still all wrong and I can't even find that (for) line in your code.

Mike.
 
And i have found that the debugger shows the variable <temp_i> as <out of scope> after one pass through the <for ( index=9; Set_Count_Value>0; index-- )> loop.

I am extremely sorry for the wrong message above.
Actually it should be

Code:
for ( index=0; Set_Count_Value>0; index++  )

I checked the indents for braces again, and i think they are in line (opening and closing braces)
can you kindly point out the area , which i will correct.

UPDATE : I have been able to move a bit ahead

In the timer ISR, this part is a bit doubtful

Code:
    if(display_string == 0)
        {
                        PORTB = seg[val[seg_counter]];
        }
        else
            PORTB = val[seg_counter];

If I do not check the "display_string" and directly pass value to PORTB, all's good.
 
All problems were resolved after using MPLAB-X IDE
Maybe I got it wrong but, have you solved a hardware problem using a different IDE?
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top