Skocz do zawartości

[STM32] [C]Problem z czujnikiem QTR-8RC


Pomocna odpowiedź

Napisano

Po dłuższej przerwie od programowania i robotyki, zabrałem się za forbotowy kurs stm32, i budowę prostego LFa na bazie nucleo i QTR_8RC. Mam jednak problem z oprogramowaniem tych czujników, gdyż są dość nietypowe w obsłudze. Są one zasilane z 3,3v. Program ma sprawdzać czujniki i jeżeli pierwszy z nich coś wykryje załączyć na 5s silniki. Kod wygląda następująco:

/**
Spis pinow
A_PHASE - PC_0
B_PHASE - PC_1
A_ENABLE - PB_8
B_ENABLE - PB_9
MODE - PC_2
LED_ON - PC_3
1_qtr - PC_4
2_qtr - PC_5
3_qtr - PC_6
4_qtr - PC_7
5_qtr - PC_8
6_qtr - PC_9
7_qtr - PC_10
8_qtr - PC_11
PB6, PB7, PB8, PB9 - PWM - TIM4
*/

#include "stm32f10x.h"
#include "stm32f1xx_nucleo.h"

volatile uint32_t timer_us = 0;
short signal [8];

void SysTick_Handler()
{
if (timer_us)
{
	timer_us--;
}
}

void delay_us(int time)
{
timer_us = time;
while (timer_us) {};
}

void sensor()
{
GPIO_InitTypeDef gpio;

GPIO_StructInit(&gpio);
gpio.GPIO_Pin = GPIO_Pin_3;
gpio.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOC, &gpio);

GPIO_SetBits(GPIOC, GPIO_Pin_3); // LEDON ON

gpio.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5| GPIO_Pin_6| GPIO_Pin_7| GPIO_Pin_8| GPIO_Pin_9| GPIO_Pin_10| GPIO_Pin_11;
gpio.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOC, &gpio);

GPIO_SetBits(GPIOC, GPIO_Pin_4 | GPIO_Pin_5| GPIO_Pin_6| GPIO_Pin_7| GPIO_Pin_8| GPIO_Pin_9| GPIO_Pin_10| GPIO_Pin_11); // Ladowanie kondensatorow
delay_us(12); //12us na naladowanie
GPIO_ResetBits(GPIOC, GPIO_Pin_4 | GPIO_Pin_5| GPIO_Pin_6| GPIO_Pin_7| GPIO_Pin_8| GPIO_Pin_9| GPIO_Pin_10| GPIO_Pin_11); // koniec ladowania

gpio.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5| GPIO_Pin_6| GPIO_Pin_7| GPIO_Pin_8| GPIO_Pin_9| GPIO_Pin_10| GPIO_Pin_11;
gpio.GPIO_Mode = GPIO_Mode_IN_FLOATING; // zmiana trybu na wejscie bez podciagniecia
GPIO_Init(GPIOC, &gpio);

signal [0] = 0; // zerowanie odzczytow
signal [1] = 0;
signal [2] = 0;
signal [3] = 0;
signal [4] = 0;
signal [5] = 0;
signal [6] = 0;
signal [7] = 0;

while(1)
{
	if(GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_4) == 1)
	{
		signal[0] +=1;
	}
	if(GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_5) == 1)
	{
		signal[1] +=1;
	}
	if(GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_6) == 1)
	{
		signal[2] +=1;
	}
	if(GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_7) == 1)
	{
		signal[3] +=1;
	}
	if(GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_8) == 1)
	{
		signal[4] +=1;
	}
	if(GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_9) == 1)
	{
		signal[5] +=1;
	}
	if(GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_10) == 1)
	{
		signal[6] +=1;
	}
	if(GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_11) == 1)
	{
		signal[7] +=1;
	}
	if(GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_4) == 0 && GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_5) == 0 && GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_6) == 0 && GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_7) == 0 && GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_8) == 0 && GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_9) == 0 && GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_10) == 0 && GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_11) == 0)
	{
		break; // przerwanie pętli po rozladowaniu
	}
	delay_us (1);
}
GPIO_ResetBits(GPIOC, GPIO_Pin_3); // LEDON OFF
}

int main(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);

GPIO_InitTypeDef gpio;
TIM_TimeBaseInitTypeDef tim;
TIM_OCInitTypeDef  channel;

GPIO_StructInit(&gpio);
gpio.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9;
gpio.GPIO_Speed = GPIO_Speed_50MHz;
gpio.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &gpio);

TIM_TimeBaseStructInit(&tim);
tim.TIM_CounterMode = TIM_CounterMode_Up;
tim.TIM_Prescaler = 20 - 1;
tim.TIM_Period = 100 - 1; //32kHz
TIM_TimeBaseInit(TIM4, &tim);

TIM_OCStructInit(&channel);
channel.TIM_OCMode = TIM_OCMode_PWM1;
channel.TIM_OutputState = TIM_OutputState_Enable;
channel.TIM_Pulse = 0;
TIM_OC3Init(TIM4, &channel);
channel.TIM_Pulse = 0;
TIM_OC4Init(TIM4, &channel);

TIM_Cmd(TIM4, ENABLE);

GPIO_StructInit(&gpio);
gpio.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2;
gpio.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOC, &gpio);

gpio.GPIO_Pin = GPIO_Pin_13;
gpio.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOC, &gpio);

SysTick_Config(SystemCoreClock / 1000000); // automatycznie wywoluje systick handler co 1us

int main_counter = 0;

while(1)
{
	sensor();
	if (signal [0] != 0)
	{
		GPIO_SetBits(GPIOC, GPIO_Pin_2); // MODE << 1
		GPIO_SetBits(GPIOC, GPIO_Pin_0 | GPIO_Pin_1); // kierunek do przodu
		channel.TIM_Pulse = 50; //50% PWM
		TIM_OC3Init(TIM4, &channel);
		channel.TIM_Pulse = 50; //50% PWM
		TIM_OC4Init(TIM4, &channel);
		while(main_counter < 5000)//5s
		{
			main_counter += 1;
			delay_us(1000);
		}
		channel.TIM_Pulse = 0;
		TIM_OC3Init(TIM4, &channel);
		channel.TIM_Pulse = 0;
		TIM_OC4Init(TIM4, &channel);
		main_counter = 0;
	}
}
}

Siedzę nad tym już parę dni i dalej nie wiem co jest nie tak 😐

Odczyt odbywa się poprzez pomiar czasu rozładowania kondensatora. Należy podać stan wysoki na wyjściu czujnika, odczekać 10us, ustawić pin na wejście bez rezystora pull-up i mierzyć czas rozładowania. Przynajmniej tak to rozumiem. Jakbym się przed zakupem bardziej w to zagłębił to pewnie wybrał bym wersje analogową... Ale cóż.

Nie wiem dlaczego nie działa. Samo sterowanie silnikami działa jeśli if jest prawdziwy.

Bądź aktywny - zaloguj się lub utwórz konto!

Tylko zarejestrowani użytkownicy mogą komentować zawartość tej strony

Utwórz konto w ~20 sekund!

Zarejestruj nowe konto, to proste!

Zarejestruj się »

Zaloguj się

Posiadasz własne konto? Użyj go!

Zaloguj się »
×
×
  • Utwórz nowe...