LCD:[#22] rewritten delayMicroseconds().

* Now it uses timer peripheral with interrupt.
* It sets interrupt to trigger after certain time and waits for it to clear the "interrupt pending" flag.
This commit is contained in:
RedHawk 2023-04-27 13:45:45 +03:00
parent 07e4083ab8
commit 4532191da6
2 changed files with 49 additions and 21 deletions

View File

@ -1,21 +1,62 @@
#include "LiquidCrystal.h" #include "LiquidCrystal.h"
// Remove this when code will be reworked.
#ifndef LiquidCrystal_NOT_FIXED
// Remove this when code will be reworked.
#include <cstring> #include <cstring>
#include "chip.h" #include "chip.h"
#include "board.h"
#define LOW 0 #define LOW 0
#define HIGH 1 #define HIGH 1
#define MHz1 1000000
/**
* @brief Handle interrupt from 32-bit timer 0
* @return Nothing
*/
void TIMER32_0_IRQHandler(void)
{
if (Chip_TIMER_MatchPending(LPC_TIMER32_0, 1)) {
Chip_TIMER_ClearMatch(LPC_TIMER32_0, 1);
}
}
#if 0 #if 1
void delayMicroseconds(unsigned int us) void delayMicroseconds(unsigned int us)
{ {
// implement with RIT /* Initialize 32-bit timer 0 clock */
Chip_TIMER_Init(LPC_TIMER32_0);
/* Timer setup for match and interrupt at TICKRATE_HZ */
Chip_TIMER_Reset(LPC_TIMER32_0);
/* Enable timer to generate interrupt when time matches */
Chip_TIMER_MatchEnableInt(LPC_TIMER32_0, 1);
/* Setup 32-bit timer's duration (32-bit match time) */
/* Once_per_microsecond * number_of_microseconds. */
Chip_TIMER_SetMatch(LPC_TIMER32_0, 1, (Chip_Clock_GetSystemClockRate() / MHz1 * us));
/* Setup timer to stop when match occurs */
Chip_TIMER_StopOnMatchEnable(LPC_TIMER32_0, 1);
/* Start timer */
Chip_TIMER_Enable(LPC_TIMER32_0);
/* Clear timer of any pending interrupts */
NVIC_ClearPendingIRQ(TIMER_32_0_IRQn);
/* Enable timer interrupt */
NVIC_EnableIRQ(TIMER_32_0_IRQn);
/* Wait for the interrupt to trigger */
while(Chip_TIMER_MatchPending(LPC_TIMER32_0, 1));
/*Disable the interrupt*/
Chip_TIMER_MatchDisableInt(LPC_TIMER32_0, 1);
/* Disable timer */
Chip_TIMER_Disable(LPC_TIMER32_0);
/* Deinitialise timer. */
Chip_TIMER_DeInit(LPC_TIMER32_0);
} }
#else #else
void delayMicroseconds(uint32_t delay) void delayMicroseconds(uint32_t delay)
@ -291,7 +332,3 @@ void LiquidCrystal::write4bits(uint8_t value) {
pulseEnable(); pulseEnable();
} }
// Remove this when code will be reworked.
#endif /* LiquidCrystal_NOT_FIXED */
// Remove this when code will be reworked.

View File

@ -1,11 +1,6 @@
#ifndef LiquidCrystal_h #ifndef LiquidCrystal_h
#define LiquidCrystal_h #define LiquidCrystal_h
// Remove this when code will be reworked.
#define LiquidCrystal_NOT_FIXED
#ifndef LiquidCrystal_NOT_FIXED
// Remove this when code will be reworked.
#include <cstddef> #include <cstddef>
#include <string> #include <string>
#include "chip.h" #include "chip.h"
@ -98,8 +93,4 @@ private:
uint8_t _numlines,_currline; uint8_t _numlines,_currline;
}; };
// Remove this when code will be reworked.
#endif /* LiquidCrystal_NOT_FIXED */
// Remove this when code will be reworked.
#endif /* LiquidCrystal_h */ #endif /* LiquidCrystal_h */