// PIC16F877A Configuration Bit Settings
// 'C' source line config statements
// CONFIG
#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = ON // Watchdog Timer Enable bit (WDT enabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
#define _XTAL_FREQ 16000000
#include <xc.h>
int main()
{
TRISB0 = 0; //RB0 as Output PIN
TRISD7 = 1; //RD7 as Input PIN
RB0 = 0; //LED Off
while(1)
{
if(RD7 == 0) //If Switch Pressed
{
RB0 = 1; //LED ON
__delay_ms(3000); //3 Second Delay
RB0 = 0; //LED OFF
}
}
return 0;
}
BUILD SUCCESSFUL (total time: 5s)
Loading code from E:/shyam/1. Embedded System/Project/PIC/Switch/Switch1/Switch1.X/dist/default/production/Switch1.X.production.hex...
Loading completed
That's exactly what you have coded! There's no delay after LED OFF, it's turned on immediately again.But when I press switch button my Led doesn't blink it stay only ON.
That's exactly what you have coded! There's no delay after LED OFF, it's turned on immediately again.
int main()
{
TRISB0 = 0; //RB0 as Output PIN
TRISD7 = 1; //RD7 as Input PIN
RB0 = 0; //LED Off
while(1)
{
if(RD7 == 0) //If Switch Pressed
{
RB0 = 1; //LED ON
__delay_ms(3000); //3 Second Delay
RB0 = 0; //LED OFF
__delay_ms(3000); //3 Second Delay
}
}
return 0;
}
int main()
{
TRISB0 = 0; //RB0 as Output PIN
TRISD7 = 1; //RD7 as Input PIN
RB0 = 0; //LED Off
while(1)
{
if(RD7 == 1) //If Switch Pressed
{
RB0 = 1; //LED ON
}
else
{
RB0 = 0; //LED OFF
}
}
return 0;
}
Is pin RD7 pulled the opposite way to the switch? In other words, if the switch goes to VSS is there a pull-up resistor to VDD or alternatively, if the switch goes to VDD is there a pull-down resistor to VSS? If you haven't used a resistor to make it revert to the opposite logic level, add one, I suggest 10K.
Brian.
I have only this connectionThe picture doesn't help I'm afraid, we would have to see the schematic for the board.
From the photograph it isn't possible to tell if there is already a resistor or whether it goes to VSS or VDD. Did the board come with a schematic?
Brian.
int main()
{
TRISB0 = 0; //RB0 as Output PIN
TRISD7 = 1; //RD7 as Input PIN
RB0 = 0; //LED Off
while(1)
{
if(RD7 == 1) //If Switch Pressed
{
RB0 = 1; //LED ON
}
else
{
RB0 = 0; //LED OFF
}
[COLOR="#FF0000"] CLRWDT();[/COLOR]
}
return 0;
}
thanks Brian. I tried that program but there is no change. I tried with different ports and pin but no luckThat helps!
so the watchdog is reset each time the loop executes.
Brian.
void main()
{
bit switch_bounce;
unsigned char lamp_status;
unsigned char input_internal = 1;
// unsigned char Switch;
TRISD = 0; //Set the 1st 7bits of PORTB as Output
TRISB4_bit = 1; //Set RB4 as Input
LATD = 0; //Turn off all LEDs
switch_bounce = 0;
lamp_status = 0x00;
do {
if (lamp_status == 0x00)
{
if ((switch_bounce == 0x00) && Button(&PORTB, 4, 37, 0 ))
{
lamp_status = 0xFF;
switch_bounce = 1;
delay_ms(5);
switch_bounce = 0;
}
if ((switch_bounce == 0) && Button(&PORTB, 4, 37, 0))
{
LATD = lamp_status;
//if (oldstate && Button(&PORTB, 4, 37, 1)){
}
}
if (lamp_status == 0xFF)
{
//input_internal = PORTB.RB4;
if((switch_bounce == 0) && Button(&PORTB, 4, 37, 1 ))
{
lamp_status = 0x00;
switch_bounce = 1;
delay_ms(5);
switch_bounce = 0;
}
if ((switch_bounce == 0) && Button(&PORTB, 4, 37, 1))
{
LATD = lamp_status;
}
}
}while(1);
}
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?