دوستان عزیز کسی میتونه این برنامه زبان C رو واسم ترجمه کنه ؟
هر خط چکار میکنه؟ خیلی بش نیاز دارم
هر خط چکار میکنه؟ خیلی بش نیاز دارم
کد:
Chip type : ATmega16
Data Stack size : 256
*****************************************************/
#include <mega16.h>
// Alphanumeric LCD Module functions
#asm
.equ __lcd_port=0x1B ;PORTA
#endasm
#include <lcd.h>
#include<delay.h>
#include<math.h>
void display_no(int no);
void direction(char dir);
void lcd_direction(char lcd_dir);
int x=0,speed=0;
char lcd_dir;
// External Interrupt 0 service routine
interrupt [EXT_INT0] void ext_int0_isr(void)
{
speed++;
if(PIND.3==0)
lcd_dir=2;
if(PIND.3==1)
lcd_dir=1;
}
// External Interrupt 1 service routine
interrupt [EXT_INT1] void ext_int1_isr(void)
{
speed++;
}
// Timer 2 overflow interrupt service routine
interrupt [TIM2_OVF] void timer2_ovf_isr(void)
{
x++;
if (x==391)
{
lcd_clear();
lcd_putsf("Speed = ");
display_no(speed);
lcd_putsf("rpm");
lcd_direction(lcd_dir);
speed=0;
x=0;
}
}
void main(void)
{
// Declare your local variables here
// Input/Output Ports initialization
// Port A initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTA=0x00;
DDRA=0x00;
// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=Out Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=0 State2=T State1=T State0=T
PORTB=0x00;
DDRB=0x08;
// Port C initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTC=0x00;
DDRC=0x00;
// Port D initialization
// Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out
// State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0
PORTD=0x00;
DDRD=0x03;
// Timer/counter 0 initialization
// Clock source: System Clock
// Clock value: 125.000 kHz
// Mode: Fast PWM top=FFh
// OC0 output: Non-Inverted PWM
TCCR0=0x6A;
TCNT0=0x00;
OCR0=0xFF;
// Timer/counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// Timer/counter 2 initialization
// Clock source: System Clock
// Clock value: 1000.000 kHz
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x01;
TCNT2=0x00;
OCR2=0x00;
// External Interrupt(s) initialization
// INT0: On
// INT0 Mode: Falling Edge
// INT1: On
// INT1 Mode: Falling Edge
// INT2: Off
GICR|=0xC0;
MCUCR=0x0A;
MCUCSR=0x00;
GIFR=0xC0;
// Timer(s)/speeder(s) Interrupt(s) initialization
TIMSK=0x40;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/counter 1: Off
ACSR=0x80;
SFIOR=0x00;
// LCD module initialization
lcd_init(16);
// Global enable interrupts
#asm("sei")
PORTC=255;
direction(1);
while (1)
{
if (PINC.2==0 || PINC.3==0 || PINC.4==0 )
{
delay_ms(10);
if (PINC.2==0)
direction(1);
if (PINC.3==0)
direction(2);
if (PINC.4==0)
direction(3);
}
//lcd_gotoxy(0,1);
//lcd_putsf(" ");
//lcd_gotoxy(0,1);
//lcd_putsf("OCR0: ");
//display_no(OCR0);
if (PINC.0==0 || PINC.1==0 )
{
delay_ms(10);
if (PINC.0==0)
{
if (OCR0> 250)
OCR0=255;
else
OCR0=OCR0+5;
}
else if (PINC.1==0)
{
if(OCR0<5)
OCR0=0;
else
OCR0=OCR0-5;
}
}
else
{
}
}
}
/////////////////////////
void direction(char dir)
{
switch(dir)
{
case 1:
PORTD.0=1;
PORTD.1=0;
//lcd_clear();
//lcd_putsf("Direction: Right ");
break;
case 2:
PORTD.0=0;
PORTD.1=1;
//lcd_clear();
//lcd_putsf("Direction: Left ");
break;
case 3:
PORTD.0=0;
PORTD.1=0;
//lcd_clear();
//lcd_putsf("Direction: Brake ");
break;
}
}
/////////////////////////
/////////////////////////
void lcd_direction(char lcd_dir)
{
switch(lcd_dir)
{
case 1:
lcd_gotoxy(0,1);
lcd_putsf("Direction: Left ");
break;
case 2:
lcd_gotoxy(0,1);
lcd_putsf("Direction: Right ");
break;
case 3:
lcd_gotoxy(0,1);
lcd_putsf("Direction: Brake ");
break;
}
}
void display_no(int no)
{
int array[5];
int i=0,j;
/*if( no < 0)
{
lcd_putchar('-');
no=-1*no;
}
else
lcd_putchar('+');*/
while(no > 9)
{
array[i++]=no % 10;
no/=10;
}
array[i]=no;
for(j=i;j >=0 ;j--)
{
lcd_putchar(48+array[j]);
delay_us(100);
}
}
/////////////////////////
/////////////////////////
