Ishah
Member level 1
- Joined
- Mar 13, 2013
- Messages
- 33
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1,286
- Activity points
- 1,502
/*
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
}
}
}
}
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; }
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; }
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;
}
}
}
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
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; } }
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.
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)
What compiler are you using to build your code? Have you included the necessary files? that might be the problem.
CMCON=0X07; // or CMCON register = 0X00000111
ADCON1=0X07; // Configure ADCON1, or ADCON1=0X06;
@Ishah
i have connected Port D as input port and Port B as output... Try with that program
while (1);
{
if( TRISA.F0 == 1) //If the switch is pressed
{
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?