GunkutA
Junior Member
- Total Posts : 120
- Reward points : 0
- Joined: 2019/01/10 00:09:38
- Location: 0
- Status: offline
PIC18F26K83 LED does not work according to debugger
Hello, I am using PIC18F26K83 and I am trying to read temperature value. I use LED as a debug tool. However, it behaves strangely. I have 2 arrays 1 for comparing ADC values and other one is for finding out the temperature from ADC value array. Here is the simplified code that supposed to work: int i; int k; int temperature; unsigned int temp_data; int temp_ADC_array[34]= { 259,293,332,377,428,487,555,632,720,821,934,1062,1203,1360,1531,1715,1910,2113,2320,2528,2731,2926,3108,3274,3422,3552,3663,3756,3833,3895,3945,3983,4013,4036}; int temp_array[34]= {125,120,115,110,105,100,95,90,85,80,75,70,65,60,55,50,45,40,35,30,25,20,15,10,5,0,-5,-10,-15,-20,-25,-30,-35,-40 };
void main(){ //Set the pins of the MCU TRISA.B2=0; //LED LATA.B2=0;
while(1){
temp_data=2000;
for(i =0;i<34; i++){ if(temp_data>temp_ADC_array[33-i]) { temperature = temp_array[33-i]; break;}} if(temperature>50){ led=1;} else{ led=0;}
delay_ms(5000);
}
} In the code above since the temp_data is 2000, from the for loop temperature must be 45. And that is the case in debugger it shows it 45. That means LED must be off because temperature is less than 50 and also in debugger I can see that LED pin is zero all the time. However, when I try it with my circuit LED gets ON. In the other cases when I test LED it works well. Where might be the problem in for loop? Or maybe the problem is in the LED? Thanks beforehand. Edit: I also tried it with: if(temperature<50){ led=1;} else{ led=0;} So LED is ON when temperature<50 and also temperature >50 ? How can it be possible? Note that I use MicroC.
post edited by GunkutA - 2019/08/05 06:10:04
|
ric
Super Member
- Total Posts : 24581
- Reward points : 0
- Joined: 2003/11/07 12:41:26
- Location: Australia, Melbourne
- Status: online
Re: PIC18F26K83 LED does not work according to debugger
2019/08/05 06:04:40
(permalink)
The first time through your loop, the first 17 comparisons will fail, so you do not copy anything to the temperature variable, so it will contain the initial value of zero.(All global variables are cleared on startup).This is because you never initialise that variable. Edit: Sorry, ignore this post. The simulator in my head was playing up!
post edited by ric - 2019/08/05 13:17:39
To get a useful answer, always state which PIC you are using!
|
GunkutA
Junior Member
- Total Posts : 120
- Reward points : 0
- Joined: 2019/01/10 00:09:38
- Location: 0
- Status: offline
Re: PIC18F26K83 LED does not work according to debugger
2019/08/05 06:07:37
(permalink)
But in the first 17 trials in the for loop it will not enter the if condition and in the 18. it will enter and copy the value to the temperature. Am I wrong?
|
ric
Super Member
- Total Posts : 24581
- Reward points : 0
- Joined: 2003/11/07 12:41:26
- Location: Australia, Melbourne
- Status: online
Re: PIC18F26K83 LED does not work according to debugger
2019/08/05 06:38:56
(permalink)
Yes, you are wrong.It fails the first if().It does do the second if().Why do you think it doesn't? Edit: Please ignore. Sorry for the confusion.
post edited by ric - 2019/08/05 13:18:15
To get a useful answer, always state which PIC you are using!
|
GunkutA
Junior Member
- Total Posts : 120
- Reward points : 0
- Joined: 2019/01/10 00:09:38
- Location: 0
- Status: offline
Re: PIC18F26K83 LED does not work according to debugger
2019/08/05 06:41:45
(permalink)
|
ric
Super Member
- Total Posts : 24581
- Reward points : 0
- Joined: 2003/11/07 12:41:26
- Location: Australia, Melbourne
- Status: online
Re: PIC18F26K83 LED does not work according to debugger
2019/08/05 06:46:15
(permalink)
Start by explaining what you want the code to do. It appears right now it is doing what you have written but not what you want.
To get a useful answer, always state which PIC you are using!
|
GunkutA
Junior Member
- Total Posts : 120
- Reward points : 0
- Joined: 2019/01/10 00:09:38
- Location: 0
- Status: offline
Re: PIC18F26K83 LED does not work according to debugger
2019/08/05 06:53:59
(permalink)
Okay, I have some ADC values that I use as lookup table. (temp_ADC_array). In there every index responds to some temperature. (temp_array) For example, temp_ADC_array[5] means the temperature is temp_array[5]. temp_ADC_array[33] has the highest value which is 4036 and the ADC value gets decreased when the index getting lower. I also have some value from ADC which is temp_data (I use it constant right now for testing). I want to compare temp_data with temp_ADC_array so that I will now the temperature. So in the for loop I am comparing temp_data with temp_ADC_array till temp_data is greater than temp_ADC_array( with each iteration temp_ADC_array is getting decreased so eventually temp_data will be greater). When temp_data is greater the index of temp_array will be the temperature.
|
1and0
Access is Denied
- Total Posts : 9982
- Reward points : 0
- Joined: 2007/05/06 12:03:20
- Location: Harry's Gray Matter
- Status: offline
Re: PIC18F26K83 LED does not work according to debugger
2019/08/05 06:55:58
(permalink)
GunkutA But in the first 17 trials in the for loop it will not enter the if condition and in the 18. it will enter and copy the value to the temperature. Am I wrong?
You're not wrong. In the first 17 iterations thru the for() loop, i.e. i = 0 to 16, the if() body is skipped. Then on the 18th iteration when i = 17 the if() body is executed, copying 45 to temperature and exit the for() loop; so led = 0.
|
GunkutA
Junior Member
- Total Posts : 120
- Reward points : 0
- Joined: 2019/01/10 00:09:38
- Location: 0
- Status: offline
Re: PIC18F26K83 LED does not work according to debugger
2019/08/05 06:58:00
(permalink)
1and0
GunkutA But in the first 17 trials in the for loop it will not enter the if condition and in the 18. it will enter and copy the value to the temperature. Am I wrong?
You're not wrong. In the first 17 iterations thru the for() loop, i.e. i = 0 to 16, the if() body is skipped. Then on the 18th iteration when i = 17 the if() body is executed, copying 45 to temperature and exit the for() loop; so led = 0.
But the LED is ON how can it be possible? Because of ANSEL registers maybe? But other than that case LED is working perfectly. Maybe a bug?
|
1and0
Access is Denied
- Total Posts : 9982
- Reward points : 0
- Joined: 2007/05/06 12:03:20
- Location: Harry's Gray Matter
- Status: offline
Re: PIC18F26K83 LED does not work according to debugger
2019/08/05 07:05:16
(permalink)
GunkutA But the LED is ON how can it be possible? Because of ANSEL registers maybe? But other than that case LED is working perfectly. Maybe a bug?
What is the symbol name 'led'? How is your LED connected?
|
GunkutA
Junior Member
- Total Posts : 120
- Reward points : 0
- Joined: 2019/01/10 00:09:38
- Location: 0
- Status: offline
Re: PIC18F26K83 LED does not work according to debugger
2019/08/05 07:11:35
(permalink)
1and0
GunkutA But the LED is ON how can it be possible? Because of ANSEL registers maybe? But other than that case LED is working perfectly. Maybe a bug?
What is the symbol name 'led'? How is your LED connected?
I defined the pin led as : #define led (LATA.B2) And that is how LED connected: (added attachment) ( sorry I do not have soft version of it so took a picture from a paper it is not that visible.) LED_RED is connected to the RA2 pin of PIC.
Attached Image(s)
|
GunkutA
Junior Member
- Total Posts : 120
- Reward points : 0
- Joined: 2019/01/10 00:09:38
- Location: 0
- Status: offline
Re: PIC18F26K83 LED does not work according to debugger
2019/08/05 07:15:38
(permalink)
This is the full test code: ( I tried to add it to my main post but site give me access denied error. So I am putting it in here) #include <stdint.h> int i; int k; int temperature; unsigned int temp_data; short transmit_data1; short transmit_data2; uint16_t data_transmit; int temp_ADC_array[34]= { 259,293,332,377,428,487,555,632,720,821,934,1062,1203,1360,1531,1715,1910,2113,2320,2528,2731,2926,3108,3274,3422,3552,3663,3756,3833,3895,3945,3983,4013,4036}; int temp_array[34]= {125,120,115,110,105,100,95,90,85,80,75,70,65,60,55,50,45,40,35,30,25,20,15,10,5,0,-5,-10,-15,-20,-25,-30,-35,-40 };
#define led (LATA.B2)
void Clk_62kHz (){ NOSC2_BIT =1; NOSC1_BIT=1; //HFINTOSC NOSC0_BIT=0; FRQ3_BIT =0; FRQ2_BIT=0; //1 MHz FRQ1_BIT=0; FRQ0_BIT=0; NDIV3_BIT =0; NDIV2_BIT=1; NDIV1_BIT=0; //Divide 16 =62.5 kHz. NDIV0_BIT=0;
}
void main(){ //Set the pins of the MCU TRISA.B2=0; //LED LATA.B2=0; TRISA.B3=1; //Case Temp A ANSELA.B3=1; TRISC.B5=0; //SCLK TRISC.B6=0; //CS LATC.B6=1; //Deselect slave TRISC.B7=0; //DIN ANSELC.B5=0; ANSELC.B6=0; ANSELC.B7=0; temperature=0; Clk_62kHz(); // Clk_8Mhz() ; //PPS Mapping RC7PPS= 0b00011111 ; //DIN, RC7 = SDIPPS RC6PPS= 0b00100000; //CS, RC6= SSPPS RC5PPS= 0b00011110; //SCLK, RC5=SCKPPS //transmit_data= 58112;
transmit_data1=0b11100011; //buradan assagisi while loopun icindeydi!!!!! LATC.B6=0; //Select the slave
transmit_data2=0b01111111;
LATC.B6=1; //deselect the slave && update the data
while(1) { temp_data=2000; for(i =0;i<34; i++) { if (temp_data>temp_ADC_array[33-i]) { temperature = temp_array[33-i]; if (temperature>50) { led=1; } else { led=0; } } } delay_ms(5000); } }
|
1and0
Access is Denied
- Total Posts : 9982
- Reward points : 0
- Joined: 2007/05/06 12:03:20
- Location: Harry's Gray Matter
- Status: offline
Re: PIC18F26K83 LED does not work according to debugger
2019/08/05 07:24:22
(permalink)
I'm sure you can provide a clearer picture. :( And I think you meant LED_RED is connected to RB2, not RA2. Give us the respect by providing better information! Anyway, get back to basic. Replace the entire while(1) body with led=1 and see what happen; then replace led=0 and see what happen. Edit: GunkutA Because of ANSEL registers maybe? But other than that case LED is working perfectly. Maybe a bug?
I don't think that is the problem, but why don't you just clear bit 2 in ANSELB ANSELA?
post edited by 1and0 - 2019/08/05 07:34:57
|
GunkutA
Junior Member
- Total Posts : 120
- Reward points : 0
- Joined: 2019/01/10 00:09:38
- Location: 0
- Status: offline
Re: PIC18F26K83 LED does not work according to debugger
2019/08/05 07:27:47
(permalink)
1and0 I'm sure you can provide a clearer picture. :( And I think you meant LED_RED is connected to RB2, not RA2. Give us the respect by providing better information! Anyway, get back to basic. Replace the entire while(1) body with led=1 and see what happen; then replace led=0 and see what happen.
No the LED is connected to RA2 see LATA.B2 from the code and also in hardware it is connectedo to RA2. :( LED is working fine. When I try to make it on and off without that if(temperature>50) statement it works fine. Maybe there is a variable conflict? But both of them are int so
|
1and0
Access is Denied
- Total Posts : 9982
- Reward points : 0
- Joined: 2007/05/06 12:03:20
- Location: Harry's Gray Matter
- Status: offline
Re: PIC18F26K83 LED does not work according to debugger
2019/08/05 07:33:34
(permalink)
GunkutA No the LED is connected to RA2 see LATA.B2 from the code and also in hardware it is connectedo to RA2. :(
My apology. I hate the bit struct of that compiler. I see B2 and think RB2. LED is working fine. When I try to make it on and off without that if(temperature>50) statement it works fine. Maybe there is a variable conflict? But both of them are int so
If that is the case, then the if(temperature) statement is not working correctly. Can you post the disassembly listing of it? Edit: I don't think that is the problem, but why don't you just clear bit 2 in ANSELA?
post edited by 1and0 - 2019/08/05 07:38:36
|
GunkutA
Junior Member
- Total Posts : 120
- Reward points : 0
- Joined: 2019/01/10 00:09:38
- Location: 0
- Status: offline
Re: PIC18F26K83 LED does not work according to debugger
2019/08/05 07:36:58
(permalink)
1and0
GunkutA No the LED is connected to RA2 see LATA.B2 from the code and also in hardware it is connectedo to RA2. :(
My apology. I hate the bit struct of that compiler. I see B2 and think RB2.
LED is working fine. When I try to make it on and off without that if(temperature>50) statement it works fine. Maybe there is a variable conflict? But both of them are int so
It that is the case, then the if(temperature) statement is not working correctly. Can you post the disassembly listing of it?
I am not sure which one is it. But here is the list for all main : ; L_main0: ;BRE70111_gosterge.c,68 :: temp_data=2000; MOVLW 208 MOVWF _temp_data+0 MOVLW 7 MOVWF _temp_data+1 ;BRE70111_gosterge.c,69 :: for(i =0;i<34; i++) CLRF _i+0 CLRF _i+1 L_main2: MOVLW 128 XORWF _i+1, 0 MOVWF R0 MOVLW 128 SUBWF R0, 0 BTFSS STATUS+0, 2 GOTO L__main11 MOVLW 34 SUBWF _i+0, 0 L__main11: BTFSC STATUS+0, 0 GOTO L_main3 ;BRE70111_gosterge.c,71 :: if (temp_data>temp_ADC_array[33-i]) MOVF _i+0, 0 SUBLW 33 MOVWF R3 MOVF _i+1, 0 MOVWF R4 MOVLW 0 SUBFWB R4, 1 MOVF R3, 0 MOVWF R0 MOVF R4, 0 MOVWF R1 RLCF R0, 1 BCF R0, 0 RLCF R1, 1 MOVLW _temp_ADC_array+0 ADDWF R0, 0 MOVWF FSR2L+0 MOVLW hi_addr(_temp_ADC_array+0) ADDWFC R1, 0 MOVWF FSR2L+1 MOVF POSTINC2+0, 0 MOVWF R1 MOVF POSTINC2+0, 0 MOVWF R2 MOVF _temp_data+1, 0 SUBWF R2, 0 BTFSS STATUS+0, 2 GOTO L__main12 MOVF _temp_data+0, 0 SUBWF R1, 0 L__main12: BTFSC STATUS+0, 0 GOTO L_main5 ;BRE70111_gosterge.c,73 :: temperature = temp_array[33-i]; MOVF _i+0, 0 SUBLW 33 MOVWF R3 MOVF _i+1, 0 MOVWF R4 MOVLW 0 SUBFWB R4, 1 MOVF R3, 0 MOVWF R0 MOVF R4, 0 MOVWF R1 RLCF R0, 1 BCF R0, 0 RLCF R1, 1 MOVLW _temp_array+0 ADDWF R0, 0 MOVWF FSR0L+0 MOVLW hi_addr(_temp_array+0) ADDWFC R1, 0 MOVWF FSR0L+1 MOVF POSTINC0+0, 0 MOVWF _temperature+0 MOVF POSTINC0+0, 0 MOVWF _temperature+1 ;BRE70111_gosterge.c,74 :: } L_main5: ;BRE70111_gosterge.c,69 :: for(i =0;i<34; i++) INFSNZ _i+0, 1 INCF _i+1, 1 ;BRE70111_gosterge.c,75 :: } GOTO L_main2 L_main3: ;BRE70111_gosterge.c,77 :: if (temperature<0) MOVLW 128 XORWF _temperature+1, 0 MOVWF R0 MOVLW 128 SUBWF R0, 0 BTFSS STATUS+0, 2 GOTO L__main13 MOVLW 0 SUBWF _temperature+0, 0 L__main13: BTFSC STATUS+0, 0 GOTO L_main6 ;BRE70111_gosterge.c,79 :: led=1; BSF LATA+0, 2 ;BRE70111_gosterge.c,80 :: } GOTO L_main7 L_main6: ;BRE70111_gosterge.c,83 :: led=0; BCF LATA+0, 2 ;BRE70111_gosterge.c,84 :: } L_main7: ;BRE70111_gosterge.c,85 :: delay_ms(5000); MOVLW 110 MOVWF R12, 0 MOVLW 146 MOVWF R13, 0 L_main8: DECFSZ R13, 1, 1 BRA L_main8 DECFSZ R12, 1, 1 BRA L_main8 NOP NOP ;BRE70111_gosterge.c,86 :: } GOTO L_main0 ;BRE70111_gosterge.c,87 :: } L_end_main: GOTO $+0 ; end of _main
post edited by GunkutA - 2019/08/05 07:38:44
|
1and0
Access is Denied
- Total Posts : 9982
- Reward points : 0
- Joined: 2007/05/06 12:03:20
- Location: Harry's Gray Matter
- Status: offline
Re: PIC18F26K83 LED does not work according to debugger
2019/08/05 07:41:09
(permalink)
GunkutA
BRE70111_gosterge.c,77 :: if (temperature<0)
Oh, com'on! <0? NOT >50?
|
GunkutA
Junior Member
- Total Posts : 120
- Reward points : 0
- Joined: 2019/01/10 00:09:38
- Location: 0
- Status: offline
Re: PIC18F26K83 LED does not work according to debugger
2019/08/05 07:49:39
(permalink)
Ahh sorry no.I was trying if the LED will work correctly. Normally it was 50 :( my bad. Actually it does not matter 50 or 100 it is on all the time till I set it 130 or higher
|
1and0
Access is Denied
- Total Posts : 9982
- Reward points : 0
- Joined: 2007/05/06 12:03:20
- Location: Harry's Gray Matter
- Status: offline
Re: PIC18F26K83 LED does not work according to debugger
2019/08/05 07:54:33
(permalink)
GunkutA Ahh sorry no.I was trying if the LED will work correctly. Normally it was 50 :( my bad. Actually it does not matter 50 or 100 it is on all the time till I set it 130 or higher
Then post the disassembly listing for >50. FYI, that disassembly listing for comparing <0 is lousy and very inefficient.
|
1and0
Access is Denied
- Total Posts : 9982
- Reward points : 0
- Joined: 2007/05/06 12:03:20
- Location: Harry's Gray Matter
- Status: offline
Re: PIC18F26K83 LED does not work according to debugger
2019/08/05 07:59:17
(permalink)
GunkutA
;BRE70111_gosterge.c,77 :: if (temperature<0) MOVLW 128 XORWF _temperature+1, 0 MOVWF R0 MOVLW 128 SUBWF R0, 0 BTFSS STATUS+0, 2 GOTO L__main13 MOVLW 0 SUBWF _temperature+0, 0 L__main13: BTFSC STATUS+0, 0 GOTO L_main6
That is, all that could have been just TWO instructions.
|