ben c dilinde biraz yeniyimde müsait isen bir örnekle gösterirmisin
Post automatically merged: 9 Tem 2023
#define F_CPU 16000000UL
#include <avr/io.h>
#include <util/delay.h>
int vites2_flag = 0; // bayrak değişkeni
int pwm = 0;
void InitPWM()
{
TCCR2 |= 1 << WGM21 | 1 << WGM20 | 1 << COM21;
TCCR2 |= 1 << CS20;
DDRD |= 1 << PIND7;
}
void SetPMOutput(int duty)
{
OCR2 = duty;
}
void Wait()
{
_delay_loop_2(5000);
}
void PWMStart()
{
for (pwm = 0; pwm < 255; pwm++)
{
SetPMOutput(pwm);
Wait();
}
}
void PWMStop()
{
for (pwm = 255; pwm > 0; pwm--)
{
SetPMOutput(pwm);
Wait();
}
}
int main(void)
{
DDRB|= (1 << PB0);
DDRB&= ~(1 << PB1);// pb1 portu ileri portuna eşittir
DDRB&= ~(1 << PB2);//pb2 portu geri portuna eşittir
DDRB&= ~(1 << PB3);//pb3 portu vites1 portuna eşittir
DDRB&= ~(1 << PB4);//pb4 portu vites2 portuna eşittir
DDRB&= ~(1 << PB5);//pb5 portu vites3 portuna eşittir
DDRB&= ~(1 << PB6);//pb portu vite4 portuna eşşitri
DDRA |= (1 << PA1) | (1 << PA2) | (1 << PA2) | (1 << PA3) | (1 << PA4) | (1 << PA5) | (1 << PA6); // PA1=geri_out | PA2=ileri_out | PA3= 1.bobin | PA4= 2.bobin | PA5= 3.bobin | PA6=4.bobin|;
while (1)
{
InitPWM();
if (PINB & (1 << PB2)) //ileri girişi okunur eğerki 1 olursa aşşadaki işlemler gerçekleşir
{
PORTB&= (1<<PB0);
PORTA|= (1<<PA2);
if ((PINB & (1 << PB3)) && ((PINB & (1 << PB4)) == 0) && ((PINB & (1 << PB5)) == 0) && ((PINB & (1 << PB6)) == 0)) {//vites1 içi olan koşul karşılanır ise aşşağıdaki şlemeler çalışır
PORTA|= (1<<PA3);
PWMStart();
}
else
{
PORTA&= ~(1<<PA3);
_delay_ms(25);
}
if ((PINB & (1 << PB3)) == 0 && (PINB & (1 << PB4)) == 0 && (PINB & (1 << PB5)) == 0 && (PINB & (1 << PB6)) == 0) {//vites 2 için olan koşul karşılanır ise aşşağıdaki şlemeler çalışır
PORTA |= (1 << PA4);
PWMStart();
break;
}
else
{
PORTA &= ~(1 << PA4);
PWMStop();
}
if ((PINB & (1 << PB3)) == 0 && (PINB & (1 << PB4)) == 0 && (PINB & (1 << PB5)) && (PINB & (1 << PB6)) == 0) {//vites 3 için olan koşul karşılanır ise aşşağıdaki şlemeler çalışır
PORTA |= (1 << PA5);
}
else
{
PORTA &= ~(1 << PA5);
_delay_ms(25);
}
if ((PINB & (1 << PB3))&& (PINB & (1 << PB4)) == 0 && (PINB & (1 << PB5)) && (PINB & (1 << PB6)) ) {//vites 4 için olan koşul karşılanır ise aşşağıdaki şlemeler çalışır
PORTA |= (1 << PA5);
}
else
{
PORTA &= ~(1 << PA6);
_delay_ms(25);
}
}
else
{
PORTA&= ~(1<< PA3);
PORTA&= ~(1<< PA4);
PORTA&= ~(1<< PA5);
PORTA&= ~(1<< PA6);
PORTA&= ~(1<< PA2);
PORTB|= (1<<PB0);
}
}
return 0;
}
yapmış olduğum kod bu