Microcontroller › PIC › How to read switch in XC8 compiler
- This topic has 1 reply, 2 voices, and was last updated 10 years, 6 months ago by Ashutosh Bhatt.
-
AuthorPosts
-
March 30, 2014 at 5:50 am #2997yaseen. d. makandarParticipant
Hello .. I’m using pic16f684 microcontroller, pickit3 and XC8 compiler, I written program to flash led for 10 seconds after switch button press but when i burn program on pic it start flashing led continuesly before i pressed button, i used all control instructions such as if, while, switch… even i tryed using pointer which is used in mikroC pro compiler which is also not working, Please can you tell me how to read switch. below is the progam which i burned on pic16f684.
#include<xc.h> // XC general include file
#include<pic16f684.h>#pragma config FOSC = INTOSCIO // Oscillator Selection bits (INTOSCIO oscillator: I/O function on RA6/OSC2/
CLKOUT pin, I/O function on RA7/OSC1/CLKIN)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled and can be enabled by SWDTEN bit ofthe WDTCON register)
#pragma config PWRTE = ON // Power-up Timer Enable bit (PWRT enabled)
#pragma config MCLRE = ON // RE3/MCLR pin function select bit (RE3/MCLR pin function is MCLR)
#pragma config CP = OFF // Code Protection bit (Program memory code protection is disabled)
#pragma config CPD = OFF // Data Code Protection bit (Data memory code protection is disabled)
#pragma config BOREN = OFF // Brown Out Reset Selection bits (BOR disabled)
#pragma config IESO = OFF // Internal External Switchover bit (Internal/External Switchover mode is disabled)
#pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is disabled)
//#define Switch PORTCbits.RC0#define _XTAL_FREQ 4000000 // Set xtal frequency for time delay
unsigned int i;
void main()
{CMCON0 = 0x07; // turn off comparator
ANSEL = 0b00000000; //All I/O pins are configured as digital
TRISC = 0b00000111; // PORTC All Outputs
TRISA = 0b00001000; // PORTA All Outputs, Except RA3,
while(1)
{if(switch==1)
{
for(i=0;i<=10;i++)
{
PORTA = 1;__delay_ms(1000);
PORTA = 0;
__delay_ms(1000);
}
}
PORTA =0;
}
}March 31, 2014 at 4:46 am #11426Ashutosh BhattParticipantu r reading “switch” but what is switch?
u hv commented it out in your program. see ur code.
another thing
u r not polling pin status in continuous loop the actual loop should be
loop:while(switch==0);
for(i=0;i<=10;i++)
{
PORTA = 1;__delay_ms(1000);
PORTA = 0;
__delay_ms(1000);
}
switch=0;
goto loop;
-
AuthorPosts
- You must be logged in to reply to this topic.