#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;
//unsigned long temp=0 ;
signed char index=0 ;
//unsigned char msec_complete;
//--------------------------------------------------------------------------------------------------------------------
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 eeprom_write1(unsigned char eeprom_add, unsigned char eeprom_data);
unsigned char eeprom_read1(unsigned char addr);
void eeprom_write1(unsigned char eeprom_add, unsigned char eeprom_data)
{
GIE = 0;
EEADR = eeprom_add; // set the address location in EEADR
EEDATA = eeprom_data; // data to be written is taken in EEDATA
WREN = 1; //enable write cycle
EECON2 = 0x55;
EECON2 = 0xAA;
WR = 1; //start of write cycle
WREN = 0; //write data to eeprom
while(WR == 1); // wait until write procedure gets completed.
GIE = 1;
}
unsigned char eeprom_read1(unsigned char addr)
{
unsigned char rd_data;
GIE = 0; //disable globle interrupts
EEADR = addr; //move address in eeadr reg
EEPGD = 0; //read eeprom pointer
RD = 1; //enable read cycle
while(RD == 1);//wait here till read cycle completes
rd_data = EEDATA; //read data from eeprom data reg
GIE = 1;
return rd_data;
}
*/
//--------------------------------------------------------------------------------------------------------
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 BCD_convert( unsigned long num )
{
unsigned char temp_Val[10];
for ( index=0; num>0; index++ ) {
temp = num / 10 ;
val[index] = num-10*temp ;
num = temp ;
}
for ( ; index>=0; index-- )
val[index] = 0 ;
}*/
//--------------------------------------------------------------------------------------------------------
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)
{
//if((Start_Blinking == 1))
{
PORTB = seg[val[seg_counter]];
}
// else
//if((Blink_Value == 1) && (seg_counter == Disp_Position)&&(Start_Blinking == 1))
// PORTB = seg[val[seg_counter]];
// else if((Blink_Value == 0) && (seg_counter == Disp_Position)&&(Start_Blinking == 1))
// PORTB = 0xFF;
// else
// 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<10000;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;
soft_delay();//soft_delay();//soft_delay();
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;
//unsigned long mem_va,mem_val1=0,mem_val2=0;//,mem_val3,mem_val4;
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;
/*FLASH_WRITE(0x00,0xFF);
soft_delay();
FLASH_WRITE(0x01,0xE0);
soft_delay();
FLASH_WRITE(0x02,0xF5);
soft_delay();
FLASH_WRITE(0x03,0x05);
soft_delay();
mem_val1 = FLASH_READ(0x00);
mem_val2 = FLASH_READ(0x01);
mem_val = mem_val2*0x100;
mem_val += mem_val1;
//mem_val = (mem_val2*100)+mem_val1;
mem_val1 = FLASH_READ(0x02);
mem_val2 = FLASH_READ(0x03);
mem_val1 *= 0x10000;
mem_val2 *= 0x1000000;
// mem_val1 = (((mem_val2*0x1000000)+(mem_val1*0x10000)));
mem_val += mem_val1+mem_val2;*/
//-----------------------------------------------------------------------------------------------------
//Set_Count_Value = mem_val;
//BCD_convert(mem_val);
while(1)
{
/* if(Start_Blinking == 1)
{
Blink_Count++;
if(Blink_Count > 300)
{
Blink_Count = 0;
Blink_Value ^=1;
RC4 ^= 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(); //display_setting_on_display(&set);
else if(position_couter == 1)
display_rst_on_display();// display_setting_on_display(&rst);
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;
//if(Start_Blinking == 0)
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 ;
}
//Start_Blinking = 1;
}//display_test_on_display();
}
break;
}
}
Key_Pressed = 0;
}
}
I presume you have a programming/debugging adapter like PICkit II. You can load and run the code in debug mode, setting breakpoints or stopping code execution manually, reading and possibly changing register content.You said that all kinds of software faults can be caught with MPLAB.
How to do that? ( First time working on a PIC)
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?
No, at that point of time, the temp variable would have been deleted and so the debugger say that it is out of scope, it is usual, it doesnt have any direct relation with that
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?