[SOLVED] my pic18F4520 is not responding for any thing

Status
Not open for further replies.
In the code

Code:
if(PORTA.F0 == 1) {
                      Delay_ms(30);
                      if((PORTA.F0 == 1) && (sw0 == 0)) {          //here sw=0 means switch off knw ??
                           PORTB.F0 = 1;
                           sw0 = 1;
                      }
                      else if ((PORTA.F0 == 1) && (sw0 == 1)) {
                           PORTB.F0 = 0;
                           sw0 = 0;
                      }

It checks if PORTA.F0 == 1 i.e., it checks if RA0 is 1, then it gives a delay of 30 ms then again it checks if RA0 and sw0 both == to 1.
If yes, sw0 == 0 means that switch was not previously pressed. So, It makes RB0 = 1 and also sw0 = 1. sw0 = 1 means switch has been pressed.

In the else if condition... It checks if RA0 == 1 then gives a delay of 30 ms. Then again it checks if RA0 == 1 and sw0 == 1. If it is true, i.e., if switch was previously pressed and RB0 was ON, then it turns off RB0 and also makes switch pressed flag i.e., sw0 = 0.

Instead, you can also use PORTB.F0 = ~PORTB.F0 for button press.

The code will be

Code:
if(PORTA.F0 == 1) {
       Delay_ms(30);
       if((PORTA.F0 == 1) {
		PORTB.F0 = ~PORTB.F0;
       }
 
Last edited:

but in program itz checking sw0==0;and RA0==1;and inside the loop sw0=1....that makes me confusion....
 

but in program itz checking sw0==0;and RA0==1;and inside the loop sw0=1....that makes me confusion....

Everytime the button is pressed it has to check if RA0 == 1. sw0 is a variable just to hold the last state of the pin.

where is sw0 inside the loop? After the button is pressed for the first time, RB0 is made 1 and sw0 is also made one, meaning switch was pressed. So, when it checks for if(PORTA.F0 == 1) && (sw0 ==0) it becomes false. It then checks if If(PORTA.F0 == 1) && (sw0 == 1). It becomes true because sw0 was set 1 when first pressed. So it then makes RB0 = 0 and sw0 = 0.
 
Last edited:

Not getting control on LED's all of them are blinking when am giving one input..!
 

It is the problem with your PIC or hardware. Where did you get the PIC from?

remove the code in the while loop. Put this code in the while loop and compile and check.

Code:
if(PORTA.F0 == 1) {
       Delay_ms(30);
       if((PORTA.F0 == 1) {
		PORTB.F0 = ~PORTB.F0;
       }

if(PORTA.F1 == 1) {
       Delay_ms(30);
       if((PORTA.F1 == 1) {
		PORTB.F1 = ~PORTB.F1;
       }

if(PORTA.F2 == 1) {
       Delay_ms(30);
       if((PORTA.F2 == 1) {
		PORTB.F2 = ~PORTB.F2;
       }

if(PORTA.F3 == 1) {
       Delay_ms(30);
       if((PORTA.F3 == 1) {
		PORTB.F3 = ~PORTB.F3;
       }

if(PORTA.F4 == 1) {
       Delay_ms(30);
       if((PORTA.F4 == 1) {
		PORTB.F4 = ~PORTB.F4;
       }

if(PORTA.F5 == 1) {
       Delay_ms(30);
       if((PORTA.F5 == 1) {
		PORTB.F5 = ~PORTB.F5;
       }

Have you set the osc as internal and have you set the freq as 4 MHz in mikroC? Have you set I/O function on pin RA6 and RA7?

Check your circuit with this hex file.
 

Attachments

  • 6_led.rar
    562 bytes · Views: 52
Last edited:

it will not be problem with pic i think i have three pic which i getting frm my office..dnt knw whether its other h/w problem...yes i have choosed internal osc with 4 mhz,RA5,RA6 port fun
 

Check you circuit with the hex file in my last post. It it doesn't work then the problem might be in the programmer. may be it is not writing the hex file properly.
 

ok ...i have orderd a new one...but how come we will knw that programmer not writing the config bits???
 

Before that check if you mikroC compiler is compiling the right hex file. Go to mikroC folder and backup the file mikroCPIC1618.exe and copy the file I have posted to that folder. Then compile your code and check. You can scan the file at virustotal.com. It works for mikroC PRO PIC 5.6.0. Which version of mikroC do you have?

To check if programmer is writing properly are not, make a circuit with 6 leds blinking every 1 sec. you can use the same circuit. Try to blink the led. If even that code doesn't work then the problem might be your compiler is creating wrong hex file or the programmer is not writing properly.

Dowload the hex for led blinking and also video. check the led blink hex with your circuit.
 

Attachments

  • mikroCPIC1618.rar
    523.1 KB · Views: 64
  • sim2.rar
    14.2 KB · Views: 47
  • 6_led_blink.rar
    272 bytes · Views: 48
Last edited:

Then use this file. Also use this key with it.

Does you mikroC about box say that it is registered to P3rtic@n?
 

Attachments

  • mikroCPIC1618 v5.6.1.rar
    523.1 KB · Views: 54
  • mikroC_PRO_PIC.rar
    181 bytes · Views: 71

any way to found out the programmer is working proprly?
 

any way to found out the programmer is working proprly?

I don't know any other method. Just burn the led blink hex to your PIC and see if it works. If it doesn't work then the problem is your compiler or programmer.
 


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void main() {
 
  TRISB = 0b00000000;
  OSCCON=0b01101111;
  while(1)
  {
    PORTB.RB7=1;
    delay_ms(1000);
    PORTB.RB7=0;
    PORTB.RB6=1;
    delay_ms(1000);
    PORTB.RB6=0;
  
  }
}



this pgram i have tried now itz working properly blinking two led's with 1sec delay....!
 
Last edited by a moderator:

Then it means that your programmer is ok. Did you change your mikroc files?
 

what should i do now???,any other compiler avilable???
 
Last edited:

Did you check the buttons program with your circuit? Does the leds turn on / off on pressing buttons? If yes, you don't have to change the compiler.
 
Last edited:

YES led's are working when am checking with 1 led and 1 switch itz working( code used will given below) but when am using more than one instead of corresponding led it glows many led's




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
void main() {
       int sw0;
       TRISA = 0b01111111;
       TRISB = 0b00000000;
       PORTB = 0b00000000;
       ADCON1 = 0b00001111;
       CMCON = 0b00000111;
       CVRCON.CVREN = 0;
       CVRCON.CVROE = 0;
 
       while(1) {
               if(PORTA.F0 == 1) {
                      Delay_ms(30);
                      if((PORTA.F0 == 1) && (sw0 == 0)) {
                           PORTB.F0 = 1;
                           sw0 = 1;
                      }
                      else if ((PORTA.F0 == 1) && (sw0 == 1)) {
                           PORTB.F0 = 0;
                           sw0 = 0;
                      }
               }
}
}


- - - Updated - - -

this is working perfectly.........!
 
Last edited by a moderator:

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…