Jump to content
ELFORUM - Forumul electronistilor
criss

Comanda motor

Recommended Posts

Buna,

 

Lucrez la un controler pt un motor de joasa turatie(stergator de parbriz masini) si am 2 probleme.

1. temporizarea il controlez printr-un potentiometru de 10k(pana la 1 minut) si imi merge super pana nu il ametesc de cap, insemnand ca reglez aleator potentiometrul .. inainte si inapoi, atunci se opreste temporizarea dar ADCul inca functioneaza ..rezultand ca uCul nu s-a blocat.dupa un timp de stat isi da drumul si funtioneaza, dar acesta nu ma incanta prea mult.

Atasez sursa:

#include <avr/io.h>	void WiperTime(void);	void ADCPoz(void);    unsigned char ElapsedSeconds = 0;  unsigned char NeededTime;int main (void){   DDRB |= (1 << 7);    DDRD |= (1 << 5);    DDRD |= (1 << 6);    DDRD |= (1 << 7);    DDRB |= (1 << 0);     DDRB |= (1 << 2); 		DDRD |= (0 << 0); 	DDRD |= (0 << 1); 	DDRD |= (0 << 2); 	DDRD |= (0 << 3); 	DDRD |= (0 << 4); 	DDRC |= (0 << 1);    DDRB |= (1 << 1);    TCCR1B |= ((1 << CS10) | (1 << CS11));    ADCSRA |= (1 << ADPS1) | (1 << ADPS0);    ADMUX |= (1 << REFS0);    ADMUX |= (1 << ADLAR);    ADCSRA |= (1 << ADFR);     ADCSRA |= (1 << ADEN);     ADCSRA |= (1 << ADSC);   for(;;)  	{if ((PIND & (1<<1)) && (~PIND & (1<<2)) && (~PIND & (1<<3)) && (~PIND & (1<<4)))  {	WiperTime();	ADCPoz();  }else if ( (PIND & (1<<2)) && (~PIND & (1<<1)) && (~PIND & (1<<3)) && (~PIND & (1<<4)) ){ PORTD |= (1 << 5); // Turn on LED2PORTB &= ~(1 << 7); // Turn off LED1PORTD &= ~(1 << 6); // Turn off LED3PORTD &= ~(1 << 7); // Turn off LED4PORTB &= ~(1 << 1); // Turn off LED1 } else if ( (PIND & (1<<3)) && (~PIND & (1<<1)) && (~PIND & (1<<2)) && (~PIND & (1<<4)) ){ PORTD |= (1 << 6); // Turn on LED3PORTB &= ~(1 << 7); // Turn off LED1PORTD &= ~(1 << 5); // Turn off LED2PORTD &= ~(1 << 7); // Turn off LED4PORTB &= ~(1 << 1); // Turn off LED1 PORTB |= (1 << 1); } else if ( (PIND & (1<<4)) && (~PIND & (1<<1)) && (~PIND & (1<<2)) &&  (~PIND & (1<<3))){ PORTD |= (1 << 7); // Turn on LED4PORTB &= ~(1 << 7); // Turn off LED1PORTD &= ~(1 << 5); // Turn off LED2PORTD &= ~(1 << 6); // Turn off LED3PORTB &= ~(1 << 1); // Turn off LED1 } else if ( (~PIND & (1<<1)) && (~PIND & (1<<2)) &&  (~PIND & (1<<3)) && (~PIND & (1<<4)) ){PORTB &= ~(1 << 7); // Turn off LED1PORTD &= ~(1 << 5); // Turn off LED2PORTD &= ~(1 << 6); // Turn off LED3PORTD &= ~(1 << 7); // Turn off LED4 }}		}void WiperTime(void){ if (TCNT1 >= 15624)      	{        	 TCNT1 = 0;          	ElapsedSeconds++;				 	PORTB &= ~(1 << 1); 	         		if (ElapsedSeconds == NeededTime)           		{            		ElapsedSeconds = 0;            		PORTB |= (1 << 1);							}									}			}void ADCPoz(void){      if(ADCH < 51)      {	    PORTB |= (1 << 7); 		 PORTD &= ~(1 << 5); // Turn off LED2		 PORTD &= ~(1 << 6); // Turn off LED3         PORTD &= ~(1 << 7); // Turn off LED4		 PORTB &= ~(1 << 0); // Turn off LED5			NeededTime = 6; // Set time	      }	else if((ADCH > 51) && (ADCH < 102))      {         PORTD |= (1 << 5); // Turn on LED2		 	 	PORTB &= ~(1 << 7); // Turn off LED1		 PORTD &= ~(1 << 6); // Turn off LED3         PORTD &= ~(1 << 7); // Turn off LED4		 PORTB &= ~(1 << 0); // Turn off LED5		 NeededTime = 10; // Set time		 		}	else if((ADCH > 102) && (ADCH < 153))      {         PORTD |= (1 << 6); // Turn on LED3		 	 	PORTB &= ~(1 << 7); // Turn off LED1		 PORTD &= ~(1 << 5); // Turn off LED2         PORTD &= ~(1 << 7); // Turn off LED4		 PORTB &= ~(1 << 0); // Turn off LED5		 NeededTime = 20; // Set time		 		}		else if((ADCH > 153) && (ADCH < 204))      {         PORTD |= (1 << 7); // Turn on LED4		 	 	PORTB &= ~(1 << 7); // Turn off LED1		 PORTD &= ~(1 << 5); // Turn off LED2		 PORTD &= ~(1 << 6); // Turn off LED3		 PORTB &= ~(1 << 0); // Turn off LED5		 NeededTime = 40; // Set time		}		else if((ADCH > 204) && (ADCH < 255))      {         PORTB |= (1 << 0); // Turn on LED5	 	 	PORTB &= ~(1 << 7); // Turn off LED1		 PORTD &= ~(1 << 5); // Turn off LED2		 PORTD &= ~(1 << 6); // Turn off LED3         PORTD &= ~(1 << 7); // Turn off LED4		NeededTime = 60; 	}}

2. tot in cadrul acestui proiect am o alta problema si mai precis cu capatul de cursa.cand ajunge motorul in pozitia de unde a pornit sa se opreasca(are un sezor HALL ce intrerupe pe masa) sau daca temporizatorul a pornit motorul el isi continua cursa pana ajunge la capat:

 

void Limit(void){if (~PIND & (1<<0))	 { 	PORTB &= ~(1 << 1); // Turn off LED1	 } 	else 	{	 PORTB |= (1 << 1); 	}}
merge totul bine pana nu actionez temporizarea! de aici nu mai vrea sa porneasca, impulsul dat de temporizator este de scurta durata si doar nici nu misca motorul deoarece intra in funtiune limitarea(considerand ca motorul a ajuns la capat de cursa si intrerupe semnalul pe PB1)

 

cum as putea executa partea de cod anterior dupa ce s-a terminat temporizarea adica dupa ce comanda a fost data: "WiperTime();"

For(;;){if ((PIND & (1<<1)) && (~PIND & (1<<2)) && (~PIND & (1<<3)) && (~PIND & (1<<4)))  {	WiperTime();	ADCPoz();         Limit(); }}

Multumesc

Link to comment
Share on other sites

Bun, codul e scris pentru AT90CAN128, poti sa adaptezi pe orice atmega,

Trebuie sa faci ceva de genu ca sa mearga treaba bine

(iar cand faci calculul turatiei in functie de adc trebuie sa te asiguri ca faci vreo impartire la 0 sau ceva de genul, se blocheaza)

 

ISR_intrerupere_de 5ms ///5ms e suficient

{

////la fiecare interupere citesti adc-ul

ADC_read

setezi pwm-ul daca valoarea de la adc se modifica

set_PWM

}

void main()

{

initializare timere

init interrupts

while(1);

}

 

 

////////////////////////////////////////////////////////////////////////////////// TIMER1 - generare de PWM (fast PWM)void FastPWM_Init_T1(void){	// pinii de iesire al semnalului	DDRB |= _BV(PB5) | _BV(PB6);		// Control Register - ambele canale fast pwm pe 10 biti ---> 1024	TCCR1A |= _BV(COM1A1) | _BV(COM1B1) | _BV(WGM10) | _BV(WGM11);		// prescaler de 8	TCCR1B |= _BV(WGM12) | _BV(CS11);		// pwm initial 0	OCR1A = 0;	OCR1B = 0;	}//////////////////////////////intrerupere de 5ms pentru quart de 16Mhzvoid Timer2_Init(void){	// setam prescaler la 1024	TCCR2A |= _BV(CS22) | _BV(CS21) | _BV(CS20);		// timer-ul este intrerupt "la comparatie"	TIMSK2 |= _BV(OCIE2A);		// setare valoare de comparatie -> 5ms	OCR2A = 80;		sei();}// intrerupere la 5 msISR(TIMER2_COMP_vect){////code}////////////////////////////////////////
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.Terms of Use si Guidelines