I have recently started reading on interrupts properly to optimize my code and to prepare for my next project on DC motor control.
To start with, I thought I'd write a small piece of code which increments a counter by 1 every time the motor completes a revolution. I am using the INT interrupt feature of the PIC16F877A and while the interrupt works correctly (I assume. I had an LED glow ON and OFF on each call), it would be nice if I could display the value of the counter on an LCD for debugging/measurement.
I had written a LCD module for a previous project and it worked fine then. However, in conjugation with the interrupt the LCD doesn't seem to be able to display anything yet (in Proteus).
As the LCD routines are working fine (I checked again) and so does the interrupt, could there be any speed issues (is the interrupt occuring too fast to allow the LCD to display/update any values?
Here's the code:
Thanks!
To start with, I thought I'd write a small piece of code which increments a counter by 1 every time the motor completes a revolution. I am using the INT interrupt feature of the PIC16F877A and while the interrupt works correctly (I assume. I had an LED glow ON and OFF on each call), it would be nice if I could display the value of the counter on an LCD for debugging/measurement.
I had written a LCD module for a previous project and it worked fine then. However, in conjugation with the interrupt the LCD doesn't seem to be able to display anything yet (in Proteus).
As the LCD routines are working fine (I checked again) and so does the interrupt, could there be any speed issues (is the interrupt occuring too fast to allow the LCD to display/update any values?
Here's the code:
Rich (BB code):
//main.c
//Measure speed of a DC motor using a position encoder
#include <htc.h>
#include "lcd.h"
#define _XTAL_FREQ 200000
__CONFIG (FOSC_HS & WDTE_OFF & PWRTE_OFF & BOREN_OFF & LVP_OFF & CPD_OFF & CP_OFF & WRT_OFF);
#define MOTOR_POS RD3
#define MOTOR_NEG RD4
#define LED RD5
#define COMPLEMENT(x) x=~x
volatile double counter;
void interrupt ISR()
{
if(INTF)
{
__delay_us(500);
COMPLEMENT(LED);
counter++;
INTF=0;
}
}
void init()
{
LCD_Init();
INTCON=0x10; //set INT interrupt enable bit
OPTION_REG=0x80; //Pullups enabled and rising edge triggering
TRISB=0xFF; //configure as inputs
TRISD=0x00;
TRISC=0x00;
}
void move_forward()
{
MOTOR_POS=1;
MOTOR_NEG=0;
}
void main()
{
counter=0;
init();
ei(); //Global interrupt enable macro
while(1)
{
move_forward();
LCD_Write((char)counter+48); //For debug only; write a proper integer to ascii routine later
__delay_ms(300);
}
}
Attachments
-
9.1 KB Views: 11