Microcontroller › PIC › whats wrong with my coding?
- This topic has 2 replies, 3 voices, and was last updated 13 years, 7 months ago by
romel emperado.
-
AuthorPosts
-
June 9, 2011 at 10:31 am #990
thanikai
Participantbelow is my coding, where i used to ON and OFF 2 LEDs using 2 push button… the conditions were as below:-
– when push button 1 presed, LED 1 ON after 2.5 sec and followed by LED 2 ON after another 2.5 sec.
– when push button 2 presed LED 2 OFF after 2.5 sec and followed by LED 1 OFF after another 2,5 sec…
when i simulate using proteus, there was no found problem and it function as the condition above.. but when i applied it through hardware, the LEDs ON and OFF automatically without press push button..
pls any1 help me to identify which part of my coding hs problem.. 10q
#include <htc.h>
#include <pic.h>
//#include <delay.h>#define _XTAL_FREQ 20000000
int count = 0;void main()
{
TRISA = 0x11100000; // RA0 to RA2 as input, rest as output
TRISB = 0x00000000; // port B output
PORTB = 0x00; //clear portBwhile (1)
{if (RA0 == 1)
{
__delay_ms(1000); //delay 2.5 sRB4 = 1; //LED on
__delay_ms(1000); //delay 2.5 sRB5 = 1; //Relay on
count = 1;
}if (RA1 == 1)
{
__delay_ms(1000); //delay 2.5 sRB5 = 0; //Relay off
__delay_ms(1000); //delay 2.5 sRB4 = 0; //LED off
count = 2;
}
}
}June 20, 2011 at 3:32 am #6362kari
ParticipantAdd pull down resistors to RA0 and RA1 in HW.
June 22, 2011 at 11:18 am #6370romel emperado
Participantfirst you need to enable PORTA as input by changing the value of CMCON register.. check the datasheet of your device.
your conditional statement is inside the while(1) loop, so if you release you button the value of the port will change and it will not satisfy your needs.. put also a pull down resistor to your input so that will toggle to 1 when the button is pressed.
just make a flag that the button is pressed.. do it like this
#include<htc.h> // you dont need to include pic.h when htc.h is there..
// dont forget to set the proper configuration bits.
void main()
{
bit x = 0, y= 0;;
//your I/0 initiallization here// dont forget to set CMCON
while(1)
{
if(RA0 == 1)
x = 1; //set as flag when RA0 is high
if(RA1 == 1)
y = 1;
if(x == 1)
{
//led is on
//relay is on
// put your delay here
x= 0; //clear your flag
}
if(y == 1)
{
//led is off
//relay is off
// put your delay here
y= 0; //clear your flag
}
}}
-
AuthorPosts
- You must be logged in to reply to this topic.