[SOLVED] PIC is not working under strange condition

Status
Not open for further replies.

Korawy

Member level 3
Joined
Sep 8, 2011
Messages
61
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Visit site
Activity points
1,776
Hi all,

i have a strange problem with PIC18F452
i have a program that uses all the timers(TMR0,1,2,3), and using RF Module and LCD to indicate things.

I was working with RF code with no problem (using two timers) and the program was more than 400 Lines (using mikroC), but when i add about 10 lines (dealing with the other two timers), The PIC doesnt work and the LCD shows number of blocks.

I think then that the PIC is damaged, but when i remove the 10 Lines that has been added, The PIC works fine with no problem...

Is it a problem with POWER or there are config bits that needs to be enabled (or disabled)?

The Config Bits that is activated :

WDT disabled
LVP disabled
HS_OSC Enabled
 

Post your code and a schematic of the circuit.

Is this a development board or a DIY design?

What is the source of power?


BigDog
 

kam1787.. Im using mikroc and when i compile the program it says "Used ROM 14%-Used RAM 7%".

bigdog .. I think the problem is not in the code, as i worked with the two codes seperately, but when i add them together in one code the pic doesnt work (also it's too long )

Im using bread board, but will switch to PCB today, and the source of power is two Sealed Lead acid batteries (2*6v=12v) that passes through LM317(that outputs ~ 5.1v).
 

OK.. Here's the code that works without any problems.

Code:
unsigned int count=0,count1=0,add=0,code=0,code1=0,zer=0,stop=0;
unsigned int co1,co2,co3,co4,co11,co22;
unsigned char y2[3],y3[3];

void check(){
  co1=TMR0L;
				Delay_ms(100);
				co2=TMR0L;
				co11=co2-co1;
				Inttostr(co11,y2);
				Lcd_Out(2,1,y2);

				TMR0L=0X00;

				co3=TMR1L;
				Delay_ms(100);
				co4=TMR1L;
				co22=co4-co3;
				Inttostr(co22,y3);
				Lcd_Out(2,8,y3);

        TMR1H=0x00;
        TMR1L=0x00;}

void receiver(){
TMR3H=0x00;
TMR3L=0X00;
T3CON.F0=1;
while(PORTB.F0==0){}
count=(TMR3H*256)+TMR3L;
count=count/5;
T3CON.F0=0;
if(count>300&&count<800)zer++;  }


void receiver0(){
TMR3H=0x00;
TMR3L=0X00;
T3CON.F0=1;
while(PORTB.F0==1){}
count=(TMR3H*256)+TMR3L;
count=count/5;
T3CON.F0=0;
if(count>300&&count<700)code++;



}

void receiver1(){
TMR3H=0x00;
TMR3L=0X00;
T3CON.F0=1;
while(PORTB.F0==1){}
count=(TMR3H*256)+TMR3L;
count=count/5;
T3CON.F0=0;
if(count>900&&count<1400)code1++;

//INttoStr(count1,count_dis);
//LCD_out(2,1,count_dis);
}

void main(){

unsigned int x,y,i,y1,h,h1,i11,i22;
unsigned int x1,i1;
unsigned short d1,d2;

TRISB=0b00000001;
TRISC=0;
TRISD=0;
PORTB=0;
PORTC=0;
T3CON.F7=0;
T3CON.F5=0;
T3CON.F4=0;
T3CON.F1=0;
T3CON.F0=0;
T2CON.F2=1;
T1CON=0b00110110;
T0CON=0b01100101;
TMR2=0;
TMR1H=0x00;
TMR1L=0x00;
TMR0L=0x00;
x=0,y=0,x1=0,y1=0,i=0,h=0,i11=0,i1=1,y1=1,h1=1,i22=1,d1=255,d2=255,co1=0,co2=0,co3=0,co4=0,co11=0,co22=0;
Lcd_Init(&PORTD);
Lcd_Cmd(Lcd_Clear);
LCD_Cmd(Lcd_Cursor_OFF);
LCD_Out(1,1,"robotic vechile");
PWM1_Init(20000);
PWM2_Init(20000);

PWM1_Start();
PWM2_Start();

PWM1_change_duty(d1);
PWM2_change_duty(d2);

T1CON.F0=1;
T0CON.F7=1;
Delay_ms(1000);
for(;;){

loop:
for(x=0;x<50;x++){
for(i=0;i<i1;i++){
code=0,code1=0,zer=0;
	 if(PORTB.F0==1){ //10010101-1236(527)
   receiver1();
   if(code1==1){
   if(PORTB.F0==0){
        receiver();
        if(zer==1){

        if(PORTB.F0==1){
         receiver0();
         if(code==1){
        if(PORTB.F0==0){
        receiver();
        if(zer==2){

        if(PORTB.F0==1){
         receiver0();
         if(code==2){
        if(PORTB.F0==0){
        receiver();
        if(zer==3){

        if(PORTB.F0==1){
         receiver1();
         if(code1==2){
        if(PORTB.F0==0){
        receiver();
        if(zer==4){

        if(PORTB.F0==1){
         receiver0();
         if(code==3){
        if(PORTB.F0==0){
        receiver();
        if(zer==5){

        if(PORTB.F0==1){
         receiver1();
         if(code1==3){
        if(PORTB.F0==0){
        receiver();
        if(zer==6){

        if(PORTB.F0==1){
         receiver0();
         if(code==4){
        if(PORTB.F0==0){
        receiver();
        if(zer==7){

        if(PORTB.F0==1){
         receiver1();
         if(code1==4){
        if(PORTB.F0==0){
        receiver();
        if(zer==8){



      d1=255;
      d2=255;
     PORTB.F4=1;
		 PORTB.F1=1;
		 PORTB.F2=0;
		 PORTB.F5=0;
		  PWM1_change_duty(d1);
		PWM2_change_duty(d2);

						i1=250;

           stop=100;

   }}} } }}}}}}}}}}}}}}}}}}}}}}}}}}}}}


for(y=0;y<y1;y++){
code=0,code1=0,zer=0;
	 if(PORTB.F0==1){ //11011100-1236(527)
   receiver1();
   if(code1==1){
   if(PORTB.F0==0){
        receiver();
        if(zer==1){

        if(PORTB.F0==1){
         receiver1();
         if(code1==2){
        if(PORTB.F0==0){
        receiver();
        if(zer==2){

        if(PORTB.F0==1){
         receiver0();
         if(code==1){
        if(PORTB.F0==0){
        receiver();
        if(zer==3){

        if(PORTB.F0==1){
         receiver1();
         if(code1==3){
        if(PORTB.F0==0){
        receiver();
        if(zer==4){

        if(PORTB.F0==1){
         receiver1();
         if(code1==4){
        if(PORTB.F0==0){
        receiver();
        if(zer==5){

        if(PORTB.F0==1){
         receiver1();
         if(code1==5){
        if(PORTB.F0==0){
        receiver();
        if(zer==6){

        if(PORTB.F0==1){
         receiver0();
         if(code==2){
        if(PORTB.F0==0){
        receiver();
        if(zer==7){

        if(PORTB.F0==1){
         receiver0();
         if(code==3){
        if(PORTB.F0==0){
        receiver();
        if(zer==8){

      d1=255;
      d2=255;
     PORTB.F2=1;
		 PORTB.F5=1;
		  PORTB.F4=0;
		 PORTB.F1=0;
		  PWM1_change_duty(d1);
		PWM2_change_duty(d2);
						y1=250;

           stop=100;

   }}} } }}}}}}}}}}}}}}}}}}}}}}}}}}}}}

   for(h=0;h<h1;h++){
code=0,code1=0,zer=0;
	 if(PORTB.F0==1){ //00100110-1236(527)
   receiver0();
   if(code==1){
   if(PORTB.F0==0){
        receiver();
        if(zer==1){

        if(PORTB.F0==1){
         receiver0();
         if(code==2){
        if(PORTB.F0==0){
        receiver();
        if(zer==2){

        if(PORTB.F0==1){
         receiver1();
         if(code1==1){
        if(PORTB.F0==0){
        receiver();
        if(zer==3){

        if(PORTB.F0==1){
         receiver0();
         if(code==3){
        if(PORTB.F0==0){
        receiver();
        if(zer==4){

        if(PORTB.F0==1){
         receiver0();
         if(code==4){
        if(PORTB.F0==0){
        receiver();
        if(zer==5){

        if(PORTB.F0==1){
         receiver1();
         if(code1==2){
        if(PORTB.F0==0){
        receiver();
        if(zer==6){

        if(PORTB.F0==1){
         receiver1();
         if(code1==3){
        if(PORTB.F0==0){
        receiver();
        if(zer==7){

        if(PORTB.F0==1){
         receiver0();
         if(code==5){
        if(PORTB.F0==0){
        receiver();
        if(zer==8){


      d2=175;
     PORTB.F4=1;
		 PORTB.F1=1;
		  PORTB.F2=0;
		 PORTB.F5=0;
     PWM2_change_duty(d2);
						h1=250;

           stop=100;

   }}} } }}}}}}}}}}}}}}}}}}}}}}}}}}}}}


    for(i11=0;i11<i22;i11++){
code=0,code1=0,zer=0;
	 if(PORTB.F0==1){ //11010101-1236(527)
   receiver1();
   if(code1==1){
   if(PORTB.F0==0){
        receiver();
        if(zer==1){

        if(PORTB.F0==1){
         receiver1();
         if(code1==2){
        if(PORTB.F0==0){
        receiver();
        if(zer==2){

        if(PORTB.F0==1){
         receiver0();
         if(code==1){
        if(PORTB.F0==0){
        receiver();
        if(zer==3){

        if(PORTB.F0==1){
         receiver1();
         if(code1==3){
        if(PORTB.F0==0){
        receiver();
        if(zer==4){

        if(PORTB.F0==1){
         receiver0();
         if(code==2){
        if(PORTB.F0==0){
        receiver();
        if(zer==5){

        if(PORTB.F0==1){
         receiver1();
         if(code1==4){
        if(PORTB.F0==0){
        receiver();
        if(zer==6){

        if(PORTB.F0==1){
         receiver0();
         if(code==3){
        if(PORTB.F0==0){
        receiver();
        if(zer==7){

        if(PORTB.F0==1){
         receiver1();
         if(code1==5){
        if(PORTB.F0==0){
        receiver();
        if(zer==8){


     d1=175;
    PORTB.F4=1;
		 PORTB.F1=1;
		  PORTB.F2=0;
		 PORTB.F5=0;
		 PWM1_change_duty(d1);


						i22=250;

          stop=100;

   }}} } }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}






if(stop==0){PORTB.F1=0;
PORTB.F2=0;
PORTB.F4=0;
PORTB.F5=0;}
code=0,code1=0,zer=0;
stop--;

i1=1,h1=1,i22=1,y1=1;



		}
   }

"void check();" is the other code that works in a seperate program, The code above works without any problem(even with the presence of "void check();"), But when i add this "check();" in the main program , The PIC doesnt work !!!!

Note: mikroc compiles the program with no problems.
 

@Korawy

Wow.

Your decision to avoid the use of interrupts has produced a serious case of spaghetti code.

A little judicious use of the interrupts and Interrupt Service Routines (ISR) could greatly simply your code.

Or possibly the use of the devices UART?

Are these RF modules to which you are referring, 315MHz or 434MHz Amplitude Shift Keying (ASK) modules similar to those in the photo below?





Where in the code is the check() routine normally being called? Can you post the version which utilizes the check() routine and has the issues?



@jayanth.devarayanadurga

There is nothing wrong in the code.

I believe that statement is a bit premature.


BigDog
 

I believe that statement is a bit premature.

What I meant is there is nothing suspicious in check() function that would make PIC not to work.

I think these are causing the problem.

unsigned char y2[3],y3[3];

Change it to

unsigned char y2[17],y3[17];


--------------------------------------------------------------
void IntToStr(int input, char *output);

Returns Nothing.

Description Converts input signed integer number to a string. The output string has fixed width of 7 characters including null character at the end (string termination). The output string is right justified and the remaining positions on the left (if any) are filled with blanks.

Parameters :

input: signed integer number to be converted
output: destination string


Requires Destination string should be at least 7 characters in length.

Example int j = -4220;
char txt[7];
...
IntToStr(j, txt); // txt is " -4220" (one blank here)

-----------------------------------------------------------------------------------------
 
Last edited:

The code above works without any problem(even with the presence of "void check();"), But when i add this "check();" in the main program , The PIC doesnt work !!!!

What I meant is there is nothing suspicious in check() function that would make PIC not to work.


Actually, my next question was exactly that, define "doesn't work."

@Korawy

Are you stating there is no longer any change of output on LCD?

What about changes to the PWM duty cycle, do they cease as well?



Yes, that would certainly account for LCD display issues.


BigDog
 

BigDog... im using 434 MHz ASK module and it's similar to the picture, UART has a problem that will not make RF operate at a long distances.

AT the end of the program there's if statement (if stop==0), the "check();" is placed just before it.

jayanth... as i said the PIC works perfectly when i remove "check();" only. im declaring width of 3 characters only because i need to place two readings in one line, which cant be done when declaring 7 characters.

- - - Updated - - -

BigDog... The pic doesnt work means that the program is not occurred (it's not just for LCD), Just blocks are appeared.
 

This is the strangest way of writing the code I have seen. What is the purpose of so many if conditions? Post the full code which you say doesn't work.




Code C - [expand]
1
2
3
4
5
6
7
unsigned char myStr[7];
unsigned int myInt = 0;
 
//............
 
IntToStr(myInt, myStr);
myStr[3] = '\0';






Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
if(PORTB.F0==1){ //10010101-1236(527)
   receiver1();
   if(code1==1){
   if(PORTB.F0==0){
        receiver();
        if(zer==1){
 
        if(PORTB.F0==1){
            receiver0();
            if(code==1){
                if(PORTB.F0==0){
                    receiver();
                    if(zer==2){
 
                        if(PORTB.F0==1){
                            receiver0();
                            if(code==2){
                                if(PORTB.F0==0){
                                    receiver();
                                    if(zer==3){
 
                                        if(PORTB.F0==1){
                                            receiver1();
                                            if(code1==2){
                                                if(PORTB.F0==0){
                                                    receiver();
                                                    if(zer==4){
 
                                                        if(PORTB.F0==1){
                                                            receiver0();
                                                            if(code==3){
                                                                if(PORTB.F0==0){
                                                                    receiver();
                                                                    if(zer==5){
 
                                                                        if(PORTB.F0==1){
                                                                            receiver1();
                                                                            if(code1==3){
                                                                                if(PORTB.F0==0){
                                                                                    receiver();
                                                                                    if(zer==6){
 
                                                                                        if(PORTB.F0==1){
                                                                                            receiver0();
                                                                                                if(code==4){
                                                                                                    if(PORTB.F0==0){
                                                                                                        receiver();
                                                                                                        if(zer==7){
 
                                                                                                            if(PORTB.F0==1){
                                                                                                                    receiver1();
                                                                                                                    if(code1==4){
                                                                                                                        if(PORTB.F0==0){
                                                                                                                            receiver();
                                                                                                                            if(zer==8){
 
 
 
                                                                                                                                d1=255;
                                                                                                                                d2=255;
                                                                                                                                PORTB.F4=1;
                                                                                                                            PORTB.F1=1;
                                                                                                                            PORTB.F2=0;
                                                                                                                            PORTB.F5=0;
                                                                                                                            PWM1_change_duty(d1);
                                                                                                                            PWM2_change_duty(d2);
 
                                                                                                                            i1=250;
 
                                                                                                                                stop=100;
 
                                            }
                                                                }
                                                                    }
                                                                        }
                                                                                }
                                                                        }
                                                                                        }
                                                    }
                                                        }
                                                }
                                                                }
                                                        }
                                                        }
                                                            }
                                                }
                                            }
                                            }
                                            }
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}

 

BigDog... im using 434 MHz ASK module and it's similar to the picture, UART has a problem that will not make RF operate at a long distances.

Due largely by the fact, ASK RF modules typically require some method of encoding/decoding to be effective, like Manchester coding. If I remember correctly, mikroC Pro offers a Manchester coding library routines for just such a purpose.

AT the end of the program there's if statement (if stop==0), the "check();" is placed just before it.

The pic doesnt work means that the program is not occurred (it's not just for LCD), Just blocks are appeared.

What PIC programmer are you using? Is it compatible with the mikroC IDE for debugging?

The use of some simple in-circuit debugging could certainly provide some incite.

I have seen issues concerning the void IntToStr(int input, char *output) as Jayanth pointed out.

It may have inadvertently caused an improper memory access during code execution, although I cannot be certain without installing mikroC and debugging your app.

jayanth... as i said the PIC works perfectly when i remove "check();" only. im declaring width of 3 characters only because i need to place two readings in one line, which cant be done when declaring 7 characters.

If this is the case, simply use the sprintf() routine or write your own routine to compose the display string you require.

At 14% Flash usage, it appears you have more than enough flash available, temporarily using the sprintf() routine to compose your LCD output string would certainly narrow down the possible cause of the issue.

BigDog
 

Can't you write like this?


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
if(PORTB.F0==1){ //10010101-1236(527)
   receiver1();
}
 
if(code1==1){
   if(PORTB.F0==0){
        receiver();
}
 
if(zer==1){
 
    if(PORTB.F0==1){
          receiver0();
}
 
if(code==1){
        if(PORTB.........

 

BigDog... u r right about the RF module, that what i have done in the transmitter code, where the "1" pulse consists of number of pulses (if "1" period is 1000us, i have to transmit a set of pulses of frequency ~38KHz for a duration of 1000us), that's also for the "0" pulse but with a little time period.

This is the programmer :


I will try to deal with sprintf()..

- - - Updated - - -

jayanth.. i cant do this as i need to get out of the loop until it take the action, also if i do this the program loop will go to the begining again which is not acceptable.

- - - Updated - - -

- - - Updated - - -

i now discover another problem, as u see in the code there are two PWMs i use, when i test the circuit, PORTB (LEDs) are having a little light, although PORTB state is "0".

i remove the declaraion of the second PWM and i found that the LEDs are not lightning when their state is"0".
 


Yes, essentially it appears you are attempting to implement your own form of encoding.

It is possible to use the devices UART in conjunction with an encoding scheme.

I have implement several such routines appropriate for use with ASK RF TX/RX pairs.

You might find the following paper informative if you care to "roll-your-own" rather than use the library routines offered by mikroC.

**broken link removed**


This is the programmer :
View attachment 93829

Unfortunately, it appears to be a PICkit clone, which if I remember correctly is not compatible with the mikroC IDE for in-circuit debugging.

While the PICkit and ICD programmer/debuggers are compatible with Microchip MPLAB/X IDEs, mikroE IDEs require the following or its clone:

mikroProg

I'm actually not sure if there are mikroProg clones available as I rarely use mikroE products, perhaps jayanth could shed some light on that subject.

In-circuit debugging allows you to single step through the code as it executes, examining the values contained in variables, registers, and other memory locations.

You can also set breakpoints which temporarily pause code execution, allowing you to examine the values contained in variables, registers, and other memory locations and then continuing code execution until the next breakpoint is encountered.

These are very powerful tools when debugging an applications such as yours.




I will try to deal with sprintf()..

Doing so should at least provide some method of debugging your code during execution.


First, let see if we can remedy the issue with the LCD display, which will provide some method of debugging during code execution.



BigDog
 

jayanth... I would like to thank u a lot, i change the version of the mikroC to PRO and it works.

I first think that the program will be compiled as it is, but i find that there are differences when calling LCD LIbrary, PWM, and the variable "code".
Code:
sbit LCD_RS at RD4_bit;
sbit LCD_EN at RD5_bit;
sbit LCD_D4 at RD0_bit;
sbit LCD_D5 at RD1_bit;
sbit LCD_D6 at RD2_bit;
sbit LCD_D7 at RD3_bit;

sbit LCD_RS_Direction at TRISD4_bit;
sbit LCD_EN_Direction at TRISD5_bit;
sbit LCD_D4_Direction at TRISD0_bit;
sbit LCD_D5_Direction at TRISD1_bit;
sbit LCD_D6_Direction at TRISD2_bit;
sbit LCD_D7_Direction at TRISD3_bit;

I change all that to fit mikroC PRO, and when testing it it works perfectly.

Thank u jayanth, and also bigdog for presenting all possible solutions.

- - - Updated - - -

i now have a problem of ccp2, i check ccp2mux and found that it's enabled, so i disable it to see if this will fix the problem, but the ccp2 is off and the LEDs also are off, i recognize then that disabling ccp2 will change ccp2 to RB3 instead of RC1(am i right?), if this is right, how it works with RB3 and dont work with RC1??

Unfortunately i build the PCB to deal with RC1 as CCP2, but the problem is the LEDs that have a little light, and that may affect the PIC!!!!
 

I'm actually not sure if there are mikroProg clones available as I rarely use mikroE products, perhaps jayanth could shed some light on that subject.

Till now I haven't found a clone or Circuit diagram of mikroProg. I use PICKit2 with MPLAB. I have used PICKit2 to program chips with hex file generated by mikroC PRO PIC Compilers.

These two

PWM1_change_duty(d1);
PWM2_change_duty(d2);

doesn't exist in mikroC PRO PIC. You have to use

PWM1_set_duty(d1);
PWM2_set_duty(d2);

Zip and post your mikroC PRO Project files. It will be easy for someone to help you. Also post you Circuit.

I had done a 433 MHz RF module based remote control. There was no issue of encoding or decoding data. Here is the video of it.

 
Last edited:

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…