It's already explained in post#17, last sentence.but I don't understand why LED turn on for forever ?
That's because the processor does exactly what you command it, no what you think it should do. Do you reset MyTrigger any where in your code?I don't understand why LED turn on for forever ?
while (1)
{
if(MyTrigger == 1)
{
MyTrigger = 0; // this will prepare it for the next time it is triggered
// put the code to operate the air jet here
RD1 = 1; // Jet ON
__delay_ms(1000); //
RD1 = 0; // Jet OFF
__delay_ms(500); // 1 Second Delay
}
}
Similar applies to "MyCounter".if(INTF==1)
{
INTF = 0; // Clear interrupt flag
MyCounter++; // add 1 to the MyCounter
if(MyCounter == 10)
MyTrigger = 1;
}
simple - you never turn it off!
This is why I queried what you intended to do in an earlier post. There has to be some other condition that exists before you press the button and another condition that happens afterwards. In your code there is nothing to turn 'MyTrigger' off again. To 'UNtrigger' it add this extra line:
Brian.Code:while (1) { if(MyTrigger == 1) { MyTrigger = 0; // this will prepare it for the next time it is triggered // put the code to operate the air jet here RD1 = 1; // Jet ON __delay_ms(1000); // RD1 = 0; // Jet OFF __delay_ms(500); // 1 Second Delay } }
// CONFIG
#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#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)
#define _XTAL_FREQ 20000000 //Specify the XTAL crystal FREQ
#include <xc.h>
volatile unsigned int MyCounter;
char MyTrigger;
void main(void)
{
TRISB0=1; // Make RB0 pin as input
TRISD1= 0; // Make RD1 pin as output
OPTION_REG=1; // Set Rising Edge Trigger
INTCON = 1; // Enable The Global Interrupt
INTE = 1; // External Interrupt Enable (not INTF)
GIE=1;
MyCounter = 0; //initial values at start up
MyTrigger = 0;
while (1)
{
if(MyTrigger == 1)
{
MyTrigger = 0;
// put the code to operate the air jet here
RD1 = 1; // Jet ON
__delay_ms(1000); //
RD1 = 0; // Jet OFF
__delay_ms(500); // 1 Second Delay
}
}
}
void interrupt isr(void)
{
if(INTF==1)
{
INTF = 0; // Clear interrupt flag
MyCounter++; // add 1 to the MyCounter
if(MyCounter == 10)
MyTrigger = 1;
}
}
I suspect you have another problem, the bouncing switch contact as you assumed.
What value resistor do you use in series with the LED, if you drop below a few hundred Ohms the PIC pin may be overloaded and it can cause a software crash. I would advise using around 330 Ohms if possible.Brian.
I have single push button, some resitor and capcitor I will try to make it what you suggestedDo you have a capacitor (or two) across the VSS and VDD pins and are they mounted close to the PIC. They are not in your schematic but they are essential to reliable operation. Add at least a 100nF ceramic capacitor across VSS and VDD and preferably ALSO add a 10uF electrolytic capacitor across them too.
Brian.
I see no reason why it can't be reset within the ISR after the "IF(MyCounter......" line.Do not reset it inside the ISR or it will never reach 10.
Something else you might do:
Add a line (suggest after the 'MyTrigger = 0;' line) that resets the counter, otherwise it just keeps counting upwards. Do not reset it inside the ISR or it will never reach 10.
Brian.
done already and posted code but the problem is the same
Trying to keep it simple Klaus, if it goes any earlier in the ISR it wouldn't work. You are quite right though!I see no reason why it can't be reset within the ISR after the "IF(MyCounter......" line.
Something else you might do:
Add a line (suggest after the 'MyTrigger = 0;' line) that resets the counter, otherwise it just keeps counting upwards. Do not reset it inside the ISR or it will never reach 10.
Brian.
// CONFIG
#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#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)
#define _XTAL_FREQ 20000000 //Specify the XTAL crystal FREQ
#include <xc.h>
volatile unsigned int MyCounter;
char MyTrigger;
void main(void)
{
TRISB0=1; // Make RB0 pin as input
TRISD1= 0; // Make RD1 pin as output
OPTION_REG=1; // Set Rising Edge Trigger
INTCON = 1; // Enable The Global Interrupt
INTE = 1; // External Interrupt Enable (not INTF)
GIE=1;
MyCounter = 0; //initial values at start up
MyTrigger = 0;
while (1)
{
if(MyTrigger == 1)
{
MyTrigger = 0;
MyCounter = 0;
// put the code to operate the air jet here
RD1 = 1; // Jet ON
__delay_ms(1000); //
RD1 = 0; // Jet OFF
__delay_ms(500); // 1 Second Delay
}
}
}
void interrupt isr(void)
{
if(INTF==1)
{
INTF = 0; // Clear interrupt flag
MyCounter++; // add 1 to the MyCounter
if(MyCounter == 10)
MyTrigger = 1;
}
}
Looking back at post #16, can you confirm it really is a 16F877A you are using and that you are using the correct pins. The 'schematic' drawing of the pinouts is for a 20-pin device but the 16F877A has 40 pins.
Brian.
I see imagae posted in post#16 is used to show LED and Push buton connectionMyTrigger should be declared as volatile.
I too question the schematic in post #16. The data sheet does not show any package that has 5V on pin 1 (or 20, or 21, or 40 - i.e.on any pin at the end of the package) with Gnd opposite it. In fact all of the PIC16F877a packages have 2 Vdd and 2 Vss pins that are in the middle of the DIP package sides (similar fot the other packages). If that part of the schematic cannot be right, it makes me doubt the other connections as well. (RD1 (LED) is Pin 20 and RB0/INT is pin 33.)
Do you have the schematic for the board you bought? According the the web page you reference, the board also has an MAX232 on it - you need to make sure that the pins you are using are not connected to anything else on the development board or the additional connections will interfere with what you are trying to do.
yes When I press push button for counts LED goes turn on and It doesn't turn off but In code I have written it for blinkingwhat is the '...same problem'.
Are you still using a push button as the source?
Is the LED going on and staying on or not going on at all?
Susan
Hi,
This is a good solution if you want the software to count the pulses.
But you may use T1CKI to directly make the hardware to count pulses.
This is the fastest way to count pulses ... and it needs no interrupt, no ISR and no processing power for the counting.
Klaus
// CONFIG
#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = ON //
#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)
#define _XTAL_FREQ 8000000 //Specify the XTAL crystal FREQ
#include <xc.h>
void main(void)
{
TRISC1= 1; // Make RC1 pin as input sensor
TRISC1= 0; // Make RC2 pin as output LED
TMR1ON = 0; // Stops Timer1
TMR1CS = 1; // External clock from pin RC0/T1OSO/T1CKI (on the rising edge)
T1SYNC = 0; // Synchronize external clock input
T1OSCEN = 0; //Oscillator is shut-off
T1CKPS1 = T1CKPS0 = 0; // Assigned prescaler rate is 1:1
TMR1IF = 0; // TMR1 register overflow is cleared
TMR1H=0x82; // initial count values in timer1 register
TMR1L=0xff;
TMR1ON = 1; //Start Timer1
while(1)
{
if ( TMR1IF==1)
{
RC0 = 1; // LED on
__delay_ms(100);
RC0 = 0; // LED off
}
}
}
TRISC1= 1; // Make RC1 pin as input sensor
TRISC1= 0; // Make RC2 pin as output LED
TMR1ON = 0; // Stops Timer1
TMR1CS = 1; // External clock from pin RC0/T1OSO/T1CKI (on the rising edge)
T1SYNC = 0; // Synchronize external clock input
T1OSCEN = 0; //Oscillator is shut-off
T1CKPS1 = T1CKPS0 = 0; // Assigned prescaler rate is 1:1
TMR1IF = 0; // TMR1 register overflow is cleared
TMR1H=0x82; // initial count values in timer1 register, TMR1L=0xff;
while(1)
{
if ( TMR1IF==1)
{
RC0 = 1; // LED on
__delay_ms(100);
RC0 = 0; // LED off
}
}
I will say it again - the schematic in port #16 is totally useless. It does not show pin numbers and if you try to apply the 'standard' pin numbering then you cannot get anything that agrees with the package descriptions in the data sheet.I see imagae posted in post#16 is used to show LED and Push buton connection
I have posted link of real board that I am using right now
Is it asking too much for an error description?What's the wrong with this code because the code doesn't work with hardware ?
Note : My mistake I used wrong image I should not have done that, the picture posted in post#16 to show the connection of push button and LEDI will say it again - the schematic in port #16 is totally useless. It does not show pin numbers and if you try to apply the 'standard' pin numbering then you cannot get anything that agrees with the package descriptions in the data sheet.
you gave one idea to count pulses in post #13Hi,
Is it asking too much for an error description?
Klaus
I would anticipate the LED appearing to be on almost all of the time. Forget copying examples from web sites, work out the program flow yourself for your specific needs. I wrote my first computer program in 1973 and I still start out with a proper specification that caters for all situations then a flow chart to help to break the tasks into manageable routines. Programming should be 99% inspiration and 1% perspiration :smile:
Lets break your program down into small sections to see where it goes wrong:
Brian.
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?