Hello,
There's an expansion board attached to the LPC2478 which contains a motor and a lightgate which can count the revolutions of the motor, connected to a counter (timer1).
My program is written to enable the user to select various motor speeds and receive feedback as to what the actual motor speed is, which is printed on the display.
The display contains various lines of text explaining the program and various speeds the user can select.
Essentially the problem I have is that for every line of 'lcd_putString' that is used, it multiplies the reading of the motor revolutions. There's 12 lines of lcd_putString which are used in the initialisation code of the program to print this text to the display and then never called upon again. If I comment these lines of text out, so that nothing other than the motor speed reading is display, the motor speed is displayed correctly on the display (tested using an oscilloscope).
To put it simply, if there is text printed to the display using lcd_putString, the counter reading is multiplied.
A lot of hours have gone into trying to sort this issue so any help is hugely appreciated.
Sorry noticed I had deleted the { from the toggle_trace_capture_pin function whilst copying.
Here's the correct code:
#include <lpc24xx.h> #include <textDisplay.h> #include <lcd_grph.h> #define TIMER3_PCLK 12000000 // clock frequency to timer 3 #define TIMER3_FREQ 1 // want 1 Hz interrupt volatile unsigned rps = 0; void initialization(void); void revolution_printer(void); void initialize_pwm(void); void initialize_revolution_counter(void); void initialize_display_settings(void); void print_menu_text(void); void initialize_revolution_count_interrupt(void); void initialize_interrupt_timer(void); void setup_screen(void); void pulse_count(void); void set_trace_print_pin(void); void clear_trace_print_pin(void); void toggle_trace_capture_pin(void); int main(void) { initialization(); T3TCR = 0x01; //Start Interrupt Timer while(1) { revolution_printer(); } } void initialization(void) //Configures All Counters/Timers/Interrupts And Sets Display Settings { FIO0DIR |= (3<<10); textInit(); lcd_init(); initialize_pwm(); initialize_revolution_counter(); initialize_interrupt_timer(); setup_screen(); print_menu_text(); initialize_revolution_count_interrupt(); } void initialize_pwm(void) { PINSEL2 |= (3<<6); //Set (P1.3PWM Pin Configuration PWM0PCR |= (1<<10); //PWM Set To PWM0[2] PWM0MR0 = 120000; //PWM Frequency PWM0MCR |= (1<<1); //Reset Count On Match PWM0MR2 = 120000; //PWM Pulse Width PWM0TCR = 0x00000009; //PWM Start } void initialize_revolution_counter(void) { PINSEL3 |= (3<<6); //Set (P1.19) to Counter Mode T1CTCR = 0x05; //Set Timer1 To Count Pulses T1TCR = 0x02; //Reset Count To 0 T1PR = 0; //Set Prescaler To 0 T1TCR = 0x01; //Enable Counter } void initialize_interrupt_timer(void) { PCONP |= (1<<23); //Set Timer3 On In PCONP T3TCR = 0x02; //Reset Timer T3IR = 1; //Clear Timer3 Interrupt Register T3MR0 = (TIMER3_PCLK/TIMER3_FREQ)-1; //Set Match Register Value T3MCR = 3; //On Match Reset Timer And Set Interrupt } void setup_screen(void) { lcd_fillScreen(WHITE); //Screen background = White lcd_fontColor(BLACK, WHITE); //Sets test colour/background colour } void print_menu_text(void) { lcd_putString(42, 7, "Exercise 2 - Motor Control"); lcd_putString(6, 37, " Use the joystick to navigate up and"); lcd_putString(5, 46, " down the menu, the centre button"); lcd_putString(5, 55, " toggles the selected motor speed on"); lcd_putString(5, 64, " and off."); lcd_putString(85, 90, "1. 1000 RPS"); lcd_putString(85, 120, "2. 2000 RPS"); lcd_putString(85, 150, "3. 3000 RPS"); lcd_putString(85, 180, "4. 4000 RPS"); lcd_putString(70, 210, "5. Custom Speed"); lcd_putString(80, 260, "Select a Speed RPS"); lcd_putString(170, 278, "RPS"); lcd_putString(5, 300, "***All speeds are approximate due to"); lcd_putString(20, 309, "varying performance of components***"); } void __attribute__((interrupt)) pulse_count(void) { T3IR = 1; //Clear Timer3 Interrupt Register static unsigned prev_count; unsigned curr_count; toggle_trace_capture_pin(); prev_count = curr_count; curr_count = T1TC; rps = curr_count-prev_count; VICVectAddr = 0; //Clear Vectored Interrupt Controller } void initialize_revolution_count_interrupt(void) { VICIntSelect &= ~(1<<27); //Set Interrupt To Timer3 Interrupt VICVectAddr27 = (unsigned int)pulse_count; //Assign Interrupt Service Routine VICIntEnable |= (1<<27); //Enable VIC } void revolution_printer(void) { unsigned prev_rpm = (unsigned)-1; unsigned curr_rpm; for (;;) { clear_trace_print_pin(); curr_rpm = rps; // snapshot volatile value if (prev_rpm != curr_rpm) { set_trace_print_pin(); prev_rpm = curr_rpm; textSetCursor(11, 30); simplePrintf("current rpm:%u",curr_rpm); } } } void set_trace_print_pin(void) //Pin 10 for Print Monitoring { FIO0SET |= (1<<10); } void clear_trace_print_pin(void) { FIO0CLR |= (1<<10); } void toggle_trace_capture_pin(void) //Pin 11 for Print Monitoring { unsigned int toggle; if (toggle == 0) { FIO0SET |= (1<<11); toggle = 1; } else { FIO0CLR |= (1<<11); toggle = 0; } }