burrow
Full Member level 2
Hai.. i was writing a program for pic18f4520, i just finished few lines, iam not using too much Nested functions. I guess pic18f supports upto 31 nested calls or so.. But mine seems far less still iam getting stack overflow resets. I have no idea whats happening.. Yeah i tried changing to different version of Mikroc too but got same error.
I tried in proteus and real hardware too but its giving the same result
Code:
sbit LCD_RS at LATD0_bit;
sbit LCD_EN at LATA2_bit;
sbit LCD_D4 at LATA1_bit;
sbit LCD_D5 at LATC5_bit;
sbit LCD_D6 at LATC2_bit;
sbit LCD_D7 at LATA0_bit;
short garbage;
sbit LCD_RS_Direction at TRISD0_bit;
sbit LCD_EN_Direction at TRISA2_bit;
sbit LCD_D4_Direction at TRISA1_bit;
sbit LCD_D5_Direction at TRISC5_bit;
sbit LCD_D6_Direction at TRISC2_bit;
sbit LCD_D7_Direction at TRISA0_bit;
int datas=0;
char output[100];
short menu=0;
short submenu=0;
short refresh=0;
char *string[100]="Hello";
const char *son="Turning on";
const char *shello="Hello...";
const char *sinit="Inititializing....";
const char *swifi="...WiFi";
const char *smux="Multiplexers";
const char *suart="...Uart.....";
const char *sready="Ready.....:)";
const char *shoip="Hdd";
short A0,A1,A2,DATA,WE1,WE2; // Used to control multiplex;
short quotient;
short binarystore[4];
short binarypointer=0;
bit bin1,bin2,bin3,bin0;
short pointerr3;
//I am using CD4099 ic to multiplex my output pins. AND Function multiplexer Does the multiplexing
void multiplexer(int addres,int dat) //Parameters are the pin number ( from 0 to 15, 2 cd 4099 used and the DATA to be placed)
{
LATA.B3 =1; //Write enable pin of mux 1 is kept high (to turn it off)
LATE.B1=1; //Write enable pin of mux 2 is kept high (to turn it off)
delay_ms(03);
if(dat==0) //Data placed in Data pin
LATE.B2=0; //Data pin is connected to PORT E PIN2
else
LATE.B2=1;
quotient=addres;
binarypointer=0;
binarystore[0]=9;
binarystore[1]=9;
binarystore[2]=9;
binarystore[3]=9;
while(quotient>0) {
//The passed pin number is split down to binary form to place address is A0 A1 and A2 of mux
binarystore[binarypointer]=quotient%2;
binarypointer++;
quotient=quotient/2;
}
//Appropriate bits are placed in address pins
if(binarystore[3]!=9)
bin3=binarystore[3] ;
else
bin3=0;
if(binarystore[2]!=9)
bin2=binarystore[2] ;
else
bin2=0;
if(binarystore[1]!=9)
bin1=binarystore[1] ;
else
bin1=0;
if(binarystore[0]!=9)
bin0=binarystore[0] ;
else
bin0=0;
if(bin0==1)
LATE.B0=1 ;
else
LATE.B0=0 ;
if(bin1==1)
LATA.B5=1 ;
else
LATA.B5=0 ;
if(bin2==1)
LATA.B4=1 ;
else
LATA.B4=0 ;
//Msb decides which MUX is to be turned on
if(bin3==1)
{
LATA.B3=1 ;
LATE.B1=0 ;
}
else
{
LATA.B3=0;
LATE.B1=1 ;
}
delay_ms(03);
return ;
}
//HT12E is connected to output of the mux and function sendrf takes 5 parameters, 4 DATA bits and 1 address bit
short rfbinarystore[6];
short rfbinarypointer=0;
short rfquotient;
bit rfbin0,rfbin1,rfbin2,rfbin3,rfbin4,rfbin5;
void strConstCpy(char *dest, const char *source) {
while(*source)
*dest++ = *source++ ;
*dest = 0 ;
}
//Function to send RF commands
void sendrf(short addres, short d1,short d2,short d3,short d4)
{
rfquotient=addres;
rfbinarypointer=0;
rfbinarystore[0]=9;
rfbinarystore[1]=9;
rfbinarystore[2]=9;
rfbinarystore[3]=9;
rfbinarystore[4]=9;
rfbinarystore[5]=9;
rfbinarypointer=0;
//address is converted to bit form
while(rfquotient>0)
{
rfbinarystore[rfbinarypointer]=rfquotient%2;
rfbinarypointer++;
rfquotient=rfquotient/2;
}
//each bit is found out
if(rfbinarystore[5]!=9)
rfbin5=rfbinarystore[5] ;
else
rfbin5=0;
if(rfbinarystore[4]!=9)
rfbin4=rfbinarystore[4] ;
else
rfbin4=0;
if(rfbinarystore[3]!=9)
rfbin3=rfbinarystore[3] ;
else
rfbin3=0;
if(rfbinarystore[2]!=9)
rfbin2=rfbinarystore[2] ;
else
rfbin2=0;
if(rfbinarystore[1]!=9)
rfbin1=rfbinarystore[1] ;
else
rfbin1=0;
if(rfbinarystore[0]!=9)
rfbin0=rfbinarystore[0] ;
else
rfbin0=0;
multiplexer(11,1); //Turn of Transmission Enable which is connected to mux out 11
//depending on the bits mux is called to turn on that particular pin
if(rfbin0==1) //Set address bit of HT12E
multiplexer(0,1) ;
else
multiplexer(0,0) ;
if(rfbin1==1) //Set address bit of HT12E
multiplexer(1,1) ;
else
multiplexer(1,0) ;
if(rfbin2==1) //Set address bit of HT12E
multiplexer(2,1) ;
else
multiplexer(2,0) ;
if(rfbin3==1) //Set address bit of HT12E
multiplexer(3,1) ;
else
multiplexer(3,0) ;
if(rfbin4==1) //Set address bit of HT12E
multiplexer(4,1) ;
else
multiplexer(4,0) ;
if(rfbin5==1) //Set address bit of HT12E
multiplexer(5,1) ;
else
multiplexer(5,0) ;
//Place Data
if(d1==1)
multiplexer(9,1);
else
multiplexer(9,0);
if(d2==1)
multiplexer(8,1);
else
multiplexer(8,0);
if(d3==1)
multiplexer(10,1);
else
multiplexer(10,0);
if(d4==1)
multiplexer(6,1);
else
multiplexer(6,0);
//Enable transmission
multiplexer(11,0);
// transmission enable pin is tied to 11th pin of mux
Delay_ms(03);
return ;
;
}
void interrupt(void)
{
if(INTCON.INT0IF)
{
INTCON.INT0IF = 0; // clear the interrupt flag
}
if(INTCON.RBIF==1)
{
if(PORTB.f7 == 1){
datas=datas+10;
}
garbage=PORTB;
INTCON.RBIF = 0;
}
return;
}
void main()
{
TRISA=0b00000000;
ADCON0.ADON=0;
TRISE=0;
TRISE=0b00000000;
TRISC=0; //Seeting Port c as output
PORTC=0; //Setting PORT C as zero
TRISD=0;
PORTD.B7=1;
INTCON.TMR0IE=0;
T0CON.TMR0ON=0;
T0CON.T08BIT=1;
T0CON.T0CS=0;
T0CON.PSA=0;
T0CON.T0CS=0;
CMCON.B3=0;
CMCON.B2=1; //mAKING All pins Digital by disabling comparators
CMCON.B1=1;
CMCON.B0=1;
TRISB=0b11111111; //setting B as input
PORTB=0;
INTCON.RBIE = 0; //Port b interrutpt enable
//Global Interrupt enable
//All interrupt turned off
INTCON.INT0IE= 1; //enable Interrupt on timer zero
INTCON.PEIE = 1; //Periphral interup enable
INTCON.GIEL=1;
INTCON.GIEH=1;
INTCON.IPEN = 1;
INTCON.GIE = 1;
INTCON2.INTEDG0 = 0; //Interrupt on rising edge of timer2 Clock
INTCON2.RBIP = 1;
TRISD=0b00000000; //Setting port d as output
TRISC=0b11000000 ;
ADCON1.pcfg3=1;
ADCON1.pcfg2=1;
ADCON1.pcfg1=1;
ADCON1.pcfg0=1;
PORTD.B6 =0;
UART1_Init(9600);
Delay_ms(100);
Lcd_Init();
//Some leds are connected to following pins,turning them off and on
multiplexer(13,0);
multiplexer(12,0);
LATD.B7=1;
LATD.B4=0;
LATD.B5=0;
Lcd_Cmd(_LCD_CLEAR); // Clear display
strConstCpy(string,shoip);
Lcd_Out(1,7,string);
strConstCpy(string,shello);
Lcd_Out(2,1,string);
delay_ms(1000);
Lcd_Cmd(_LCD_CLEAR); // Clear display
strConstCpy(string,sinit);
Lcd_Out(1,1,string);
// i need some leds to blink in a fashion thats why using off and on
multiplexer(13,1);
delay_ms(100); delay_ms(1000); refresh=0;
multiplexer(13,0);
strConstCpy(string,swifi);
Lcd_Out(2,1,string);
multiplexer(12,1);
delay_ms(1000) ;
refresh=0;
LATD.B5=1; //pOWER led
delay_ms(1000); refresh=0;
strConstCpy(string,smux);
Lcd_Out(2,1,string); refresh=0;
multiplexer(13,1); //HOME led
delay_ms(500); refresh=0;
LATD.B4=1; //WARNING led
refresh=0;
strConstCpy(string,suart);
Lcd_Out(2,1,string);
refresh=0;
delay_ms(1000);
delay_ms(1000); refresh=0;
multiplexer(13,0); //HOME
delay_ms(1000); refresh=0;
strConstCpy(string,sready);
Lcd_Out(2,1,string); refresh=0;
multiplexer(13,1); //HOME
delay_ms(1000); refresh=0;
multiplexer(12,0); //led1
delay_ms(1000); refresh=0;
LATD.B4=0; //WARNING
while(1)
//
{
sendrf(66,1,1,1,1);
sendrf(64,1,1,1,1);
sendrf(65,1,1,1,1);
sendrf(65,0,0,0,0);
sendrf(64,0,0,0,0);
UART1_Write_Text("H;");
UART1_Read_Text(output, "end", 19); // reads text until 'OK' is found
// if data isreceived
refresh=0;
multiplexer(12,1);
refresh=0;
delay_ms(10);
multiplexer(12,0);
refresh=0;
menu=0;
submenu=0;
datas=datas+1;
if(menu==0 && submenu==0)
{
Lcd_Cmd(_LCD_CLEAR); // Clear display
strConstCpy(string,shoip);
Lcd_Out(1,7,string);
Lcd_Out(2,1,output);
}
else
if(menu==1 && submenu==0)
{
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Out(1,1,"s");
InttoStr(datas,string);
Lcd_Out(2,3,string);
}
else
if(menu==1 && submenu==1)
{
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Out(1,1,"s");
InttoStr(datas,string);
Lcd_Out(2,3,string);
}
else
if(menu==2 && submenu==0)
{
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Out(1,1,"T");
InttoStr(datas,string);
Lcd_Out(2,3,string);
}
else
if(menu==3 && submenu==0)
{
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Out(1,1,"Fo");
InttoStr(datas,string);
Lcd_Out(2,3,string);
}
//i AM JUST pasting above codes again to show you the error.. irrespective of the code, when i cross certain limits in program error starts appearing
if(menu==2 && submenu==0)
{
Lcd_Cmd(_LCD_CLEAR); // Clear display
strConstCpy(string,shoip);
Lcd_Out(1,7,string);
Lcd_Out(2,1,output);
}
else
if(menu==4 && submenu==0)
{
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Out(1,1,"s");
InttoStr(datas,string);
Lcd_Out(2,3,string);
}
else
if(menu==6 && submenu==1)
{
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Out(1,1,"s");
InttoStr(datas,string);
Lcd_Out(2,3,string);
}
else
if(menu==8 && submenu==0)
{
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Out(1,1,"T");
InttoStr(datas,string);
Lcd_Out(2,3,string);
}
else
if(menu==3 && submenu==0)
{
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Out(1,1,"Fo");
InttoStr(datas,string);
Lcd_Out(2,3,string);
}
}
}
I tried in proteus and real hardware too but its giving the same result