2019-12-01 10:19 AM
GPIO PA15 input connnected to infrared receiver, I am using a NEC standard remote.
This code work with 2 different NEC remote command.
DelayUs function found on the post "Delay in us".
//***************************************************************************************
//* Infrared on PA15
//***************************************************************************************
uint32_t getUs(void)
{
uint32_t usTicks = HAL_RCC_GetSysClockFreq() / 1000000;
register uint32_t ms, cycle_cnt;
do {
ms = HAL_GetTick();
cycle_cnt = SysTick->VAL;
} while (ms != HAL_GetTick());
return (ms * 1000) + (usTicks * 1000 - cycle_cnt) / usTicks;
}
void delayUs(uint16_t micros)
{
uint32_t start = getUs();
while (getUs()-start < (uint32_t) micros)
{
__asm("nop");
}
}
void EXTI15_10_IRQHandler(void)
{
#define MAX_NEG_START (10000)
#define MIN_NEG_START (8000)
#define MAX_POS_START (5000)
#define MIN_POS_START (3500)
#define MAX_POS_REP (2500)
#define MIN_POS_REP (1500)
#define MIN_POS_BIT1 (1200)
#define DELAY_STEP (250)
int IR;
int l, k, counter;
unsigned char data1, data2, addr1, addr2;
//HAL_SuspendTick();
HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_15);
for (k = 0; k < MAX_NEG_START && !HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_15); k += DELAY_STEP)
{
delayUs(DELAY_STEP);
}
if ((k < MAX_NEG_START) && (k > MIN_NEG_START))
{
for (l = 0; l < MAX_POS_START && HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_15); l += DELAY_STEP)
{
delayUs(DELAY_STEP);
}
if (l < MAX_POS_REP && l > MIN_POS_REP)
{
while(!HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_15));
if (save_addr1 > 0)
{
IR_addr1 = save_addr1;
IR_addr2 = save_addr2;
}
time1 = k; // only for debug time
time2 = l;
return;
}
if ((l < MAX_POS_START) && (l > MIN_POS_START))
{
while (!HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_15));
addr1 = IR_readByte(0, MIN_POS_BIT1);
addr2 = IR_readByte(1, MIN_POS_BIT1);
data1 = IR_readByte(0, MIN_POS_BIT1);
data2 = IR_readByte(1, MIN_POS_BIT1);
#if OLD_REMOTE
if (addr1 != addr2) return;
#endif
if (data1 != data2) return;
if (addr1 == 0) return;
if (data1 == 0) return;
save_addr1 = addr1;
save_addr2 = addr2;
IR_addr1 = addr1;
IR_addr2 = addr2;
IR_data1 = data1;
IR_data2 = data2;
time1 = k; // only for debug time
time2 = l;
}
}
}
unsigned char IR_readByte(int inv, int CheckTime)
{
unsigned char out, tmp[10];
int i, k, counter;
for (k = 0; k < 8; k++)
{
for (i = 0; HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_15); i += DELAY_STEP)
{
delayUs(DELAY_STEP);
}
while (!HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_15));
if (i > CheckTime)
{
if (inv == 0 )
tmp[k] = '1';
else
tmp[k] = '0';
}
else
{
if (inv == 0 )
tmp[k] = '0';
else
tmp[k] = '1';
}
}
out = 0;
for (k = 7; k >= 0; k--)
{
if (tmp[k] == '1')
{
out <<= 1; out |= 1;
}
else
{
out <<= 1;
}
}
return out;
}
void BSP_IR_Init()
{
GPIO_InitTypeDef gpio_init_structure;
__HAL_RCC_GPIOA_CLK_ENABLE();
gpio_init_structure.Pin = GPIO_PIN_15;
gpio_init_structure.Mode = GPIO_MODE_IT_FALLING;
gpio_init_structure.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOA, &gpio_init_structure);
HAL_NVIC_SetPriority(EXTI15_10_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
}
//***************************************************************************************
//* end - Infrared
//***************************************************************************************