มีๆ...ผมยังใช้หากินอยู่เลยครับ
This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.
Show posts MenuQuote from: firmware.c on February 02, 2013, 05:35:58 PM
โอ้วให้ credit ผมด้วยขอบคุณครับ
Quote from: sitthisak2024 on February 02, 2013, 08:56:27 AM
ว้าว ขอบคุณคร้าบ
เอาไฟล์ไปเปิดกับ Protel 99 se ได้บ่ สอนหนูหน่อย หนูยังไม่เคย
//--------------------------------------------------
//Program :Control Servo motor
//Author :Somlak Mangnimit
//Date :01/02/2013
//MCU :ATmega8A
//XTAL :16Mhz
//Compiler :WinAVR20100110
//Version :-
//Note :-
//--------------------------------------------------
//use analog build in library driver
#define use_analog
//my library
#include "../m8lib.c"
//define equ name
#define servo_min 1500
#define servo_max 4500
//main program
int main(void){
unsigned int ch1,ch2;
//configure timer 1 for fast pwm mode via ICR1, with no prescaling
ICR1 = 0x9c3f; //period 20ms
TCCR1A |= (1<<WGM11);
TCCR1B |= (1<<WGM13)|(1<<WGM12)|(1<<CS11);
//enable PWM on port B1 B2 in non-inverted compare mode 2
TCCR1A |= (1<<COM1A1)|(1<<COM1B1);
//1ms pulse
OCR1A = ch1 = servo_min;
OCR1B = ch2 = servo_min;
//set digital 9,10 as outputs
ddr_digital_9 = output;
ddr_digital_10 = output;
[color=red]for(;;){
_delay_us(500);
if(!pin_analog_1){
if(ch1>1500){ch1--;}
OCR1A = ch1;
}
else if(!pin_analog_0){
if(ch1<4500){ch1++;}
OCR1A = ch1;
}
if(!pin_analog_3){
if(ch2>1500){ch2--;}
OCR1B = ch2;
}
else if(!pin_analog_2){
if(ch2<4500){ch2++;}
OCR1B = ch2;
}
}[/color]
return 0;
}
//--------------------------------------------------
//Program :Control Servo motor
//Author :Somlak Mangnimit
//Date :26/01/2013
//MCU :ATmega8A
//XTAL :16Mhz
//Compiler :WinAVR20100110
//Version :-
//Note :-
//--------------------------------------------------
//use analog build in library driver
#define use_analog
//my library
#include "../m8lib.c"
//define equ name
#define servo_min 1500
#define servo_max 4500
//main program
int main(void){
unsigned long x;
analog_initial();
unsigned int ch1,ch2;
//configure timer 1 for fast pwm mode via ICR1, with no prescaling
ICR1 = 0x9c3f; //period 20ms
TCCR1A |= (1<<WGM11);
TCCR1B |= (1<<WGM13)|(1<<WGM12)|(1<<CS11);
//enable PWM on port B1 B2 in non-inverted compare mode 2
TCCR1A |= (1<<COM1A1)|(1<<COM1B1);
//1ms pulse
OCR1A = ch1 = servo_min;
OCR1B = ch2 = servo_min;
//set digital 9,10 as outputs
ddr_digital_9 = output;
ddr_digital_10 = output;
for(;;){
x = analog_read(0);
OCR1A = (int)((x*(servo_max-servo_min))/1023)+servo_min;
x = analog_read(1);
OCR1B = (int)((x*(servo_max-servo_min))/1023)+servo_min;
}
return 0;
}