PIC shutting down

Status
Not open for further replies.

superbrew

Member
Hello again, I have strange problem. My PIC shuts down when I try to run certain sections of my code. Here is the code:
Code:
                SetChanADC( ADC_CH0 );
		ConvertADC(); 			//ADC conversion
		while( BusyADC() ); 		
  		result = ReadADC();
  		
  		if(result > max) max = result;
  		
  		itoa(result,output);		//Convert ADC result to string
  		reading1(output, 5);

  		//This is the section that causes problems
  		itoa(max,output);		//Converts max to string
  		reading1(output, 14);

If I run the code without the max reading included the PIC shuts down after a few seconds. I know this because I am using RB3 to switch on the LCD, and it shuts off after a few seconds. The code runs fine without the second reading1 function call. I left it on overnight without any trouble.
 
Based on the supplied information, the best advice i can give you is "don't do that". The amount of info you gave is like telling your mechanic "the car doesn't work right, what's wrong?"

However, I am sure you want to get it working so you will need to provide more information.
- what does "shut down" mean. Does the PIC lose power? Does the display turn off?
- if you don't make the second reading1() call does it not "shut down".
- show us the reading1 code as well as your LCD code
- what compiler? what programmer/debugger? What processor?


Phil
 
Last edited:
Reactions: 3v0
Sorry, I guess I should have given more information. When I say shut down, I mean that the display stops working, RB3, which I use to turn the display on, turns off. All the other pins appear to be off as well. The PIC should still have power because the backlight stays on on the LCD. If I comment out the second reading1() call, then the progrma functions as expected. It is only that one line that causes the problem, if I leave the itoa function in there is no problem. The code for reading one is as follows (not very pretty, I know):
Code:
void reading1(char* reading1, char pos){ 	
	int i,j = 0;
	char temp[6];
	
	for(i = strlen(reading1);i >= 0;i--){  //Copy reading1 into a temp array
		temp[i] = reading1[i];
	}
	
	for(i = 0;i <= 4-strlen(temp);i++){   //Add spaces
		reading1[i] = 32;
	}
	
	for(i = 4-strlen(temp); i <= 5; i++){ //Copy temp back into reading1
		reading1[i] = temp[j];
		j++;
	}
	
	str_decimal(reading1,1);                 //Format number with 1 dec place
	LCD_pos(1,pos);                           //set cursor position
	LCD_text(reading1);                      //Output to LCD
}

The uC is an 18F2520. I am using C18 and a MikroE EasyPIC5 to program. Thanks
 
Last edited:
I got it working, I needed to initialize my 'max' variable to 0. I still don't know why I was having all the problems that I was having. Sorry to bother anyone.
 
I suspect you overwrote something because max was uninitialized

A cleaner way to right justify numbers with blanks is to fill a buffer with blanks and then copy the string backwards (right to left). Even better would be to write your own itoa, plunk each digit down backwards in the buffer and finish off the buffer with blanks. itoa works from smallest to largest.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…