#define _XTAL_FREQ 20000000 //Specify the XTAL crystall FREQ
#define LED RB2
#define ON 1
void main()
{
TRISB=0X00; //PORTB pins are used as Output.
TRISC=0Xff; //PORTC pins are used as Input.
TMR1H=0x82; // initial count values in timer1 register
TMR1L=0xff;
TMR1IE=1; //Enable timer interrupt bit in PIE1 register
GIE=1; //Enable Global Interrupt
PEIE=1; //Enable the Peripheral Interrupt
TMR1ON = 1; //Start Timer1
T1CON=0x01; //Prescale value = 1:1, It using Internal clock, Timer 1 ON
}
void interrupt timer_isr()
{
if(TMR0IF==1) // Timer flag has been triggered
{
LED = on
TMR0IF=0; // Clear timer interrupt flag
}
}
Comments:
You don't start with the LED turned off.
Nothing counts the pulses.
I suspect you want to configure the INT0 interrupt rather than TMR0 so it triggers an interrupt on transitions of the INT pin. From there is a simple case of counting the interrupts (count up a variable inside the ISR) and when it reaches 32000 turn the LED on. Have a think about what happens next though, do you want some way to turn the LED off again?
Brian.
air jet will be turn on only for 50msFirst you need to tell a little more specification:
Does the jet stay on for ever or does something turn it off?
What turns it off?
Brian.
Hi,
I wonder why you first talked about a LED, then about airjet.
Why not about airjet from the beginning? ... this just brings confusion.
Technically I see no difference.
Klaus
#include<pic16f877a.h>
void main()
{
TRISB=0X00; //PORTB pins are used as Output.
TRISC=0Xff; //PORTC pins are used as Input.
TMR1H=0x82; // initial count values in timer1 register
TMR1L=0xff;
TMR1IE=1; //Enable timer interrupt bit in PIE1 register
GIE=1; //Enable Global Interrupt
PEIE=1; //Enable the Peripheral Interrupt
TMR1ON = 1; //Start Timer1
}
void interrupt timer_isr()
{
if(TMR1IF==1)
{
TMR1IF=0; // Clear timer interrupt flag
}
}
Susan,
Speaking as a moderator, we do see many "I found it on the internet, it was for EL34 beam tetrodes but I converted it to work on a PIC" postings. For you and I, we can see they are heading off on the wrong course and ignoring the direction signs but for a complete novice who expects to be led by the hand they find it difficult to see the destination, let alone the way to reach it.
Brian.
This is a good solution if you want the software to count the pulses.The INT pin on the PIC is the safest way to count, it uses the external interrupt functionality.
The INT pin on the PIC is the safest way to count, it uses the external interrupt functionality.
You might be able to use an ordinary input/output pin if the count pulses are relatively slow. To do that all you have to do is continuously loop while reading the pin (polling) and look for it's logic state changing. However, using external interrupts is always safe and can respond faster because the detection is in hardware rather than software.
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)
#include <xc.h>
#define _XTAL_FREQ 20000000 //Specify the XTAL crystall FREQ
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
INTF = 1; // External Interrupt Enable
while (1)
{
}
return;
}
void interrupt isr(void)
{
if(INTF==1)
{
// Clear interrupt flag
}
}
// 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
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)
MyCounter = 0; //initial values at start up
MyTrigger = 0;
while (1)
{
if(MyTrigger == 1)
{
// put the code to operate the air jet here
}
}
return;
}
void interrupt isr(void)
{
if(INTF==1)
{
INTF = 0; // Clear interrupt flag
MyCounter++; // add 1 to the MyCounter
if(MyCounter == 32000) MyTrigger = 1;
}
}
Tidied up a little and with the counting and checking added:
I have not tested the code or even tried to compile it!
Brian.
// 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>
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)
MyCounter = 0; //initial values at start up
MyTrigger = 0;
while(1)
{
if(MyTrigger == 1)
{
// put the code to operate the air jet here
RD1 = 1; // Jet ON
__delay_ms(100); //
RD1 = 0; // Jet OFF
__delay_ms(50); // 1 Second Delay
}
}
return;
}
void interrupt isr(void)
{
if(INTF==1)
{
INTF = 0; // Clear interrupt flag
MyCounter++; // add 1 to the MyCounter
if(MyCounter == 10)
MyTrigger = 1;
}
}
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
// 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>
void main(void)
{
TRISB=0X00; //PORTB pins are used as Output.
TRISC=0Xff; //PORTC pins are used as Input.
TMR1H=0x82; // initial count values in timer1 register
TMR1L=0xff;
PEIE=1; //Enable the Peripheral Interrupt
T1CON=0x01; //Prescale value = 1:1, It using Internal clock, Timer 1 ON
TMR1IE=1; //Enable timer interrupt bit in PIE1 register
GIE=1; //Enable Global Interrupt
PEIE=1; //Enable the Peripheral Interrupt
while(1)
{
}
return;
}
Go back to the previous code, as pointed out, if you just add a switch to ground it will behave unpredictably if at all. Closing the switch makes the pin go to 0V but there is nothing to make the voltage go up again when the switch is opened. In your schematic you need a resistor (~10K) from the input pin to VDD but note that your switch is wired to make the pin LOW when pressed but you are probably expecting to count HIGH transitions. This means you probably should wire the switch to VDD and the resistor to VSS.
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)
{
// 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
}
}
return;
}
void interrupt isr(void)
{
if(INTF==1)
{
INTF = 0; // Clear interrupt flag
MyCounter++; // add 1 to the MyCounter
if(MyCounter == 10)
MyTrigger = 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?