วันอาทิตย์ที่ 3 ตุลาคม พ.ศ. 2553

Lab9: การควบคุม RC SERVO MOTOR


การใช้งาน RC SERVO MOTOR นั้นสามารถเขียนโปรแกรมได้หลายวิธี ยกตัวอย่างเช่น ใช้ Library “SERVOS.c” , การใช้งาน Power PWM โดยกำหนดค่า Frequency = 50 Hz และการใช้งานฟังก์ชัน Delay ง่ายๆ ในที่นี้ขอนำเสนอวิธีใช้งานฟังก์ชัน Delay

#include<18F4431.h>
#define  CLOCK_SP    20000000 // ความเร็วสัญญาณนาฬิกา
#fuses HS                     //   โหมดการทำงานแบบ High Speed
#fuses NOLVP,NOWDT           // No Low Voltage Program, No Watchdog timer
#fuses NOPROTECT              // Code no protection
#use delay (clock=CLOCK_SP)   // ใช้งานฟังก์ชัน delay_ms() & delay_us()
#use rs232(baud=9600,xmit= PIN_C6,rcv= PIN_C7 ) // ใช้งาน module RS232

void main(void){

while(true){

if(!input(pin_a0)){                      //ตรวจสอบ input pin a0 ถ้า Low (กด switch)
output_high(pin_b0);    
delay_us(2000);
output_low(pin_b0);
delay_ms(20);
//RC SERVO MOTOR หมุนที่ +90 องศา
}

if(!input(pin_a1)){          //ตรวจสอบ input pin a1 ถ้า Low (กด switch)
output_high(pin_b0);
delay_us(1000);
output_low(pin_b0);
delay_ms(20);
}
//RC SERVO MOTOR หมุนที่ -90 องศา

}
}