Well, you are coding before you have decided on the design (shift register, counter, etc.) How can that possibly work? If you are to be successful with this thing, you need to step back, add to what you have shown in #156 to indicate how you will track bad objects as they move along the belt. The code you show here only implements the encoder divider. What then?@JohnInTX This is my attempt to write a program
But you probably want to see something -anything!- work at this point so here we go.
First, your code doesn't compile. If you have C syntax errors, hit the books. Fix that stuff first. That includes compiler warnings too. Warnings are errors that don't happen until run-time.
Initialize ALL of your I/O pins, not just the ones you are using. Set any unconnected pins to output 0. Leaving uninitialized pins leaves you open to all kinds of problems.
Next, you have a delay inside your interrupt routine. That can't work. While you are merrily burning CPU cycles to show your LED, you will miss all of the encoder counts during that time. Interrupt routines in a PIC must be kept short and avoid doing I/O inside an interrupt routine. I show one way to beat that below.
You don't say what the code is supposed to do. How are we to know when it does or doesn't do it?
The code below actually compiles (XC8 Ver 2.00) and toggles the LED on each interrupt. Says me, I haven't tested it. Of interest is how I used a flag - gotIRQ - to indicate when an interrupt happens. The 'while' loop in main waits for this signal then complements the LED to flash it at 1/2 the interrupt rate. That's enough to prove out the timer/compare stuff. You also can experiment with different settings for the compare registers.
A final concern. I see that you have BOTH encoder inputs connected in this one. That's different. If what you really need is quadrature encoding (both directions of the belt) then this implementation won't work.
Good luck!
C:
// Daljeet: always describe what a program is supposed to do. That way,
// when it doesn't, you at least know what to look at
// This program uses TIMER1/CCP1 as a period counter that counts
// encoder pulses on T1CKI. When the count is equal to the value in CCP1RH/L,
// it toggles the LED on RD4
// PIC16F877A Configuration Bit Settings
// 'C' source line config statements
// CONFIG
#pragma config FOSC = XT // Oscillator Selection bits (XT oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = ON // Power-up Timer Enable bit (PWRT enabled)
#pragma config BOREN = OFF // Brown-out Reset Enable bit (BOR disabled)
#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.
#include <xc.h>
__bit gotIRQ; // signals main that an interrupt
void main(void)
{
//Make all PORTD pins low <- No, it doesn't
PORTA = 0b00000000;
PORTB = 0b00000000;
PORTC = 0b00000000;
PORTD = 0b00000000;
// Configured PORTD as Output
TRISD4 = 0;
// Connect the encoder to RC0 & RC1 sensor inputs
TRISC = 3;
// How about the REST of the IO pins??????
RD4 = 0; // init LED
//CCP1 MODULE INITIALIZATION
T1CON = 0b00000011 ; // 1:1 prescale, OSC disabled, Input Synchronized, External clock on T1CKI(RC0), Timer ON
CCP1CON = 0b00001011; // Compare mode using CCPR1H and CCPR1L as a 16 bit value
CCPR1H = 0x03;
CCPR1L = 0x1B;
CCP1IF = 0;
INTCON = 0xc0; //Enabled Global interrupts & Peripherals interrupt
CCP1IE = 1; //Enabled CCP1 interrupt
while(1){
if (gotIRQ)
RD4 = ~RD4; // toggle LED each interrupt
gotIRQ=0; // acknowledge the interrupt
}
}
void __interrupt() IRQ(void){
// CCP1 Interrupt
if(CCP1IF == 1) // if the CCP1 Interrupt flag is set...
{
CCP1IF = 0;
gotIRQ = 1; // 'Signal' main that an interrupt happened
// NEVER delay inside an interrupt routine
}
}