I would anticipate the LED appearing to be on almost all of the time.
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
volatile unsigned int Counter;
char Trigger;
void main(void)
{
TRISB0=1; // encoder as input
TRISB1=1; // Sensor as input
TRISD1= 0; // LED as output
OPTION_REG=1; // Set Rising Edge Trigger
INTCON = 1; // Enable The Global Interrupt
INTE = 1; // External Interrupt Enable (not INTF)
GIE=1;
Counter = 0; //initial values at start up
Trigger = 0;
while (1)
{
if(Trigger == 1)
{
Trigger = 0;
Counter = 0;
RD1 = 1; // LED ON
__delay_ms(1000); //
RD1 = 0; // LED OFF
__delay_ms(500);
}
}
}
void interrupt isr(void)
{
if(INTF==1)
{
INTF = 0; // Clear interrupt flag
Counter++; // add 1 to the Counter
if(Counter == 30)
Trigger = 1;
}
}
so the programming is workin now I am trying to add sensor triggreTidied up a little and with the counting and checking added:
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 Counter;
char Trigger;
void interrupt isr(void)
{
if(INTF==1)
{
INTF = 0; // Clear interrupt flag
Counter++; // add 1 to the Counter
if(Counter == 600)
Trigger = 1;
}
}
void main(void)
{
TRISB0=1; // encoder as input
TRISB1=1; // Sensor as input
TRISD1= 0; // LED as output
OPTION_REG=1; // Set Rising Edge Trigger
INTCON = 1; // Enable The Global Interrupt
INTE = 1; // External Interrupt Enable (not INTF)
GIE=1;
Counter = 0; //initial values at start up
Trigger = 0;
while (1)
{
if (RB1 == 0)
{
if(Trigger == 1)
{
Trigger = 0;
Counter = 0;
}
INTE = 0; //Disables the RB0/INT external interrupt
}
else if (RB1 == 1)
{
if(Trigger == 1)
{
Trigger = 0;
Counter = 0;
RD1 = 1; // LED ON
__delay_ms(500);
RD1 = 0; // LED OFF
}
INTE = 1; //Enables the RB0/INT external interrupt
}
}
}
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 /* * File: main.c * Author: Jayanth D * * Created on 30 April, 2019, 5:58 AM */ #define _XTAL_FREQ 20000000 // #pragma config statements should precede project file includes. // Use project enums instead of #define for ON and OFF. #pragma config FOSC = HS // Oscillator Selection bits (HS oscillator) #pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT enabled) #pragma config PWRTE = ON // 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> #include <stdio.h> #include <stdlib.h> #define Use_External_Interrupt #ifndef Use_External_Interrupt #define Use_Timer_Counter #endif #define Trigger_Value 1 #define Pulse_Count 10 #define Trigger PORTBbits.RB1 #define LED PORTDbits.RD1 #define LED_On 1 #define LED_Off 0 typedef struct { unsigned char B0:1; unsigned char B1:1; unsigned char B2:1; unsigned char B3:1; unsigned char B4:1; unsigned char B5:1; unsigned char B6:1; unsigned char B7:1; }BIT_FIELDS_TYPE; BIT_FIELDS_TYPE flags; //#define triggerDetect flags.B0 #define toggleLed flags.B1 unsigned int pulseCount = 0; void __interrupt () _isr_routine (void) { #ifdef Use_External_Interrupt if((INTCONbits.INTE) && (INTCONbits.INTF) /*&& (triggerDetect)*/) { INTCONbits.INTF = 0; if(++pulseCount >= Pulse_Count) { INTCONbits.INTE = 0; toggleLed = 1; pulseCount = 0; } } #endif #ifdef Use_Timer_Counter #endif } int main(int argc, char** argv) { TRISB = 0x03; TRISD = 0x00; PORTB = 0x00; PORTD = 0x00; __delay_ms(100); toggleLed = 0; OPTION_REGbits.INTEDG = 0; INTCONbits.INTF = 0; INTCONbits.PEIE = 1; INTCONbits.GIE = 1; while(1) { if(Trigger == Trigger_Value) { __delay_ms(30); if(Trigger == Trigger_Value) { //triggerDetect = 1; INTCONbits.INTE = 1; } } if(toggleLed) { toggleLed = 0; LED = LED_On; __delay_ms(500); LED = LED_Off; } } return (EXIT_SUCCESS); }
if(++pulseCount >= Pulse_Count)
If you use my code then you can make a little improvement to it that is,
Just turn On the LED in ISR that is in
above condition and turn it off in while(1) loop.Code:if(++pulseCount >= Pulse_Count)
Can't help more without knowing what exactly is your sensor? Also, need to know what encoder you are using to help better.
That is poorly structured, do the check for Trigger==1 once then check the RB1 state, it makes the code much simpler. Why do you change the INTE bit?
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 Counter;
char Trigger;
void main(void)
{
TRISB= 0b00000001; // encoder as input
TRISD= 0b00000000; // LED as output
OPTION_REG = 0b01000000; // Set Rising Edge Trigger
INTCON = 1; // Enable The Global Interrupt
while (1)
{
if(INTF==1)
{
RD4 = 1; // LED ON
__delay_ms(1000); //
RD4 = 0; // LED OFF
INTF = 0; // Clear interrupt flag
}
}
}
That is poorly structured, do the check for Trigger==1 once then check the RB1 state, it makes the code much simpler. Why do you change the INTE bit?
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 Counter;
char Trigger;
void main(void)
{
TRISB= 0b00000001; // encoder as input
TRISD= 0b00000000; // LED as output
OPTION_REG = 0b01000000; // Set Rising Edge Trigger
INTCON = 1; // Enable The Global Interrupt
Counter = 0; //initial values at start up
Trigger = 0;
while (1)
{
if(INTF == 1)
{
Counter++; // add 1 to the Counter
if(Counter == 100)
Trigger = 1;
INTF = 0; // Clear interrupt flag
}
if(Trigger == 1)
{
RD4 = 1; // LED ON
__delay_ms(1000); //
RD4 = 0; // LED OFF
Trigger = 0;
Counter = 0;
}
}
}
What's wrong in following code because LED doesn't flash when 30 counts happen
If this isn´t obvious, then I recommend to start with simpler projects.if(Counter == 100)
Hi,
If this isn´t obvious, then I recommend to start with simpler projects.
Be thankful to all those, who do your job with great patience.
Klaus
because LED doesn't flash when 30 counts happen
RD4 = 1; // LED ON
__delay_ms(1000); //
RD4 = 0; // LED OFF
__delay_ms(1000); //
At the following snippet:
Code:RD4 = 1; // LED ON __delay_ms(1000); // RD4 = 0; // LED OFF
It is missing an additional delay:
Code:__delay_ms(1000); //
I don't think it's issue of additional delay
Did you at least tried to fix it as suggested ?
pulseCount = 0;
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 /* * File: main.c * Author: Jayanth D * * Created on 30 April, 2019, 5:58 AM */ #define _XTAL_FREQ 20000000 // #pragma config statements should precede project file includes. // Use project enums instead of #define for ON and OFF. #pragma config FOSC = HS // Oscillator Selection bits (HS oscillator) #pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT enabled) #pragma config PWRTE = ON // 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> #include <stdio.h> #include <stdlib.h> #define Use_External_Interrupt #ifndef Use_External_Interrupt #define Use_Timer_Counter #endif #define Trigger_Value 1 #define Pulse_Count 10 #define Trigger PORTBbits.RB1 #define LED PORTDbits.RD1 #define LED_On 1 #define LED_Off 0 typedef struct { unsigned char B0:1; unsigned char B1:1; unsigned char B2:1; unsigned char B3:1; unsigned char B4:1; unsigned char B5:1; unsigned char B6:1; unsigned char B7:1; }BIT_FIELDS_TYPE; BIT_FIELDS_TYPE flags; #define turnOffLED flags.B1 unsigned int pulseCount = 0; void __interrupt () _isr_routine (void) { #ifdef Use_External_Interrupt if((INTCONbits.INTE) && (INTCONbits.INTF)) { INTCONbits.INTF = 0; if(++pulseCount >= Pulse_Count) { INTCONbits.INTE = 0; turnOffLED = 1; pulseCount = 0; LED = LED_On; } } #endif #ifdef Use_Timer_Counter #endif } int main(int argc, char** argv) { TRISB = 0x03; TRISD = 0x00; PORTB = 0x00; PORTD = 0x00; __delay_ms(100); turnOffLED = 0; OPTION_REGbits.INTEDG = 0; INTCONbits.INTF = 0; INTCONbits.INTE = 0; INTCONbits.PEIE = 1; INTCONbits.GIE = 1; while(1) { if(Trigger == Trigger_Value) { __delay_ms(30); if(Trigger == Trigger_Value) { pulseCount = 0; if(INTCONbits.INTE == 0) INTCONbits.INTE = 1; } } if(turnOffLED) { __delay_ms(500); turnOffLED = 0; LED = LED_Off; } } return (EXIT_SUCCESS); }
What's wrong in following code because LED doesn't flash when 30 counts happen
if(Counter == 30)
Trigger = 1;
#pragma config FOSC = XT // Oscillator Selection bits (XT oscillator)
#define _XTAL_FREQ 4000000 //Specify the XTAL crystal FREQ
while (1)
{
if(INTF == 1)
{
[COLOR=#ff0000] __delay_ms(10); // [/COLOR]
Counter++; // add 1 to the Counter
if(Counter == 10)
Trigger = 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?