Please Help me on Blinking LED Project. (MicroC and ISIS)

Status
Not open for further replies.

Ishah

Member level 1
Joined
Mar 13, 2013
Messages
33
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Visit site
Activity points
1,502
Hi everyone,

I still got problems in my project, which I want to control LED with switch. But here nothing happen to PORTB.
Can anyone check my circuit in ISIS and my code.





Code:
/*
    Project Title: Controled LED by switches

*/

void main()
{
  TRISA=0XFF; //Configure all bit of PORTA as input
  TRISB=0X00; //Configure all bit of PORTB as output
  PORTA=0X00;       // Clear bits to zero
  PORTB=0X00;       // Clear bits to zero

  CMCON=0X07; //      or CMCON register = 0X00000111

  ADCON1=0X07; //     Configure ADCON1, or ADCON1=0X06;

  
  


while (1);
{
    if( TRISA.F0 == 1)   //If the switch is pressed
       {
          {
          Delay_ms(30);
          if( TRISA.F0 == 1)   //If the switch is pressed

          PORTB.F0 = ~PORTB.F0;   // Toggle LEDs on PORTB.0
          Delay_ms(500);         // Delay 1000 ms
          }
       }
    else
    if (TRISA.F1 == 1)   //If the switch is pressed
       {
          {
          Delay_ms(30);
          if( TRISA.F1 == 1)   //If the switch is pressed

          PORTB.F1 = ~PORTB.F1;   // Toggle LEDs on PORTB.1
          Delay_ms(500);         // Delay 1000 ms
          }
       }
    else
    if(TRISA.F2 == 1)   //If the switch is pressed
       {
          {
          Delay_ms(30);
          if( TRISA.F2 == 1)   //If the switch is pressed

          PORTB.F2 = ~PORTB.F2;   // Toggle LEDs on PORTB.2
          Delay_ms(1000);         // Delay 1000 ms
          }
       }
    else
    if(TRISA.F3 == 1)   //If the switch is pressed
       {
          {
          Delay_ms(30);
          if( TRISA.F3 == 1)   //If the switch is pressed

          PORTB.F3 = ~PORTB.F3;   // Toggle LEDs on PORTB.3
          Delay_ms(1000);         // Delay 1000 ms
          }
       }
    else
    if(TRISA.F4 == 1)   //If the switch is pressed
       {
          {
          Delay_ms(30);
          if( TRISA.F4 == 1)   //If the switch is pressed

          PORTB.F4 = ~PORTB.F4;   // Toggle LEDs on PORTB.4
          Delay_ms(1000);         // Delay 1000 ms
          }
       }
    else
    if(TRISA.F5 == 1)   //If the switch is pressed
       {
          {
          Delay_ms(30);
          if( TRISA.F5 == 1)   //If the switch is pressed

          PORTB.F5 = ~PORTB.F5;   // Toggle LEDs on PORTB.0
          Delay_ms(1000);         // Delay 1000 ms
          }
       }

 }


}

Thanks.
 

Use a flag like sw1.

unsigned sw1 = 0;

and replace if condition like this


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
if( TRISA.F0)   //If the switch is pressed
       {
          {
          Delay_ms(30);
          if( TRISA.F0)   //If the switch is pressed
 
               sw1 = 1;
          }
       }
 
 
if(sw1){
      PORTB.F0 = ~PORTB.F0;   // Toggle LEDs on PORTB.0
          Delay_ms(500);         // Delay 1000 ms
}
else{
        PORTB.F0 = 0;
}

 


I got error in my code. sorry im newbie in this field. Please guide me and give some example..

 

Code:
void main()
{
    ADCON1=0x07;
    CMCON=0;
    TRISB = 0x00;                                // set Port B pin as output
    TRISD = 0xff;                                // set Port D pin as input

    while(1)                                    // Endless loop
    {
        if (PortD.F0==1)      // Detect logical one
        {
           PortB.F0=1;     // switch on led
           while(PortD.F0);
        }
        else if (PortD.F1==1)      // Detect logical one
        {
           PortB.F1=1;
           while(PortD.F1);
        }
        else if (PortD.F2==1)      // Detect logical one
        {
           PortB.F2=1;
           while(PortD.F2);
        }
        else if (PortD.F3==1)      // Detect logical one
        {
           PortB.F3=1;
           while(PortD.F3);
        }
        else if (PortD.F4==1)      // Detect logical one
        {
           PortB.F4=1;
           while(PortD.F4);
        }
        else if (PortD.F5==1)      // Detect logical one
        {
           PortB.F5=1;
           while(PortD.F5);
        }
       
        else
        {
            PortB=0x00;
        }
    }
}


Check This program.. Hope this may Help You
 
Last edited:

Try this...


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
void main(){
 
     TRISA = 0xFF;
     PORTA = 0x00;
     TRISB = 0x00;
     PORTB = 0x00;
     
     unsigned char sw1 = 0;
     
     if( TRISA.F0){
          Delay_ms(30);
          if( TRISA.F0){   //If the switch is pressed
               sw1 = ~sw1;
          }
     }
 
     if(sw1){
          PORTB.F0 = ~PORTB.F0;   // Toggle LEDs on PORTB.0
          Delay_ms(500);         // Delay 1000 ms
     }
     else{
          PORTB.F0 = 0;
     }
 
 
}

 

Check This program.. Hope this may Help You

Hi Aameer,
Thanks.
The code that you given is no error but I still didn't get a desired result. No LED will turn ON when i click the button.

Here is the result.



- - - Updated - - -

Try this...

Hi Jayanth,
Already try but still get error. Sorry Im quite slow to find out the error solution.
 

Use my code in post #5. It works but you have to add ADCON1 and CMCON settings to it before while(1) loop. You are using PORTA for switches and it has analog functions. To make it digital input pins Configure ADCON1 and CMCON registers. CMCON = 0x07 will do but for ADCON1 see the datasheet of PIC16F877A.

Edit:

Try this


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
void main(){
 
     TRISA = 0xFF;
     PORTA = 0x00;
     TRISB = 0x00;
     PORTB = 0x00;
     ADCON1 = 0b10000111;
     CMCON = 0x07;
     
     unsigned char sw1 = 0;
     
     if( TRISA.F0){
          Delay_ms(30);
          if( TRISA.F0){   //If the switch is pressed
               sw1 = ~sw1;
          }
     }
 
     if(sw1){
          PORTB.F0 = ~PORTB.F0;   // Toggle LEDs on PORTB.0
          Delay_ms(500);         // Delay 1000 ms
     }
     else{
          PORTB.F0 = 0;
     }
 
 
}

 



Code:
void main()
{
  TRISA=0XFF; //Configure all bit of PORTA as input
  TRISB=0X00; //Configure all bit of PORTB as output
  PORTA=0X00;       // Clear bits to zero
  PORTB=0X00;       // Clear bits to zero

  CMCON=0X07; //      or CMCON register = 0X00000111

  ADCON1=0X07; //     Configure ADCON1, or ADCON1=0X06;

while (1)

I already wrote the configuration for PIC16f877A as above.
But after i copy your code and run on my MicroC, i still have error (invalid expression, ; expected, but unsigned found etc)

 

What compiler are you using to build your code? Have you included the necessary files? that might be the problem.
 

@Ishah
i have connected Port D as input port and Port B as output... Try with that program
 

What compiler are you using to build your code? Have you included the necessary files? that might be the problem.

I am using the mikroC PRO for PIC from Mikroelektronika.

already configure the port A ADC , disable the comparator ...

Code:
CMCON=0X07; //      or CMCON register = 0X00000111

  ADCON1=0X07; //     Configure ADCON1, or ADCON1=0X06;

Thanks
 

@Ishah
i have connected Port D as input port and Port B as output... Try with that program

Thanks aameer, it works. After compare with yours, also work to my code at #1 so no need to change I/O, My mistake only at
Code:
while (1);
{
    if( TRISA.F0 == 1)   //If the switch is pressed
       {
i should change to if( PORTA.F0 == 1).


Thanks everyone.
 

k..
TRIS is to mention whether it is input/output
Don't repeat the mistakes

All the Best
 

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