Chào các bạn trong bài đăng này mình sẽ giới thiệu các bạn về RC Servo SG90-9G sử dụng PIC16F877A.
Ảnh mô phỏng protues.
Đây là code chương trình.
#include <16F877A.h>
#device ADC=10
#FUSES NOWDT //No Watch Dog Timer
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used FOR I/O
#use delay(crystal=12MHz)
#define LED PIN_C1
#define DELAY 10
#define RC PIN_E0
#include <lcd_lib_4bit.c>
int i;
INT16 adc;
INT8 mode = 0;
#INT_EXT
void ngat_ngoai()
{
mode++;
IF(mode==1)
{
printf (LCD_PutChar, "\fMicro Servo SG90\nManual mode ");
}
IF(mode==2)
{
printf (LCD_PutChar, "\fMicro Servo SG90\nAuto mode ");
mode=0;
}
}
void main()
{
LCD_Init ();
printf (LCD_PutChar, " CHIASEDIENTU");
enable_interrupts (INT_EXT) ;
//enable_interrupts (INT_EXT_H2L);
delay_ms (2000);
setup_adc_ports (AN0);
setup_adc (ADC_CLOCK_INTERNAL);
set_adc_channel (0);
delay_us (10);
enable_interrupts (GLOBAL);
WHILE (true)
{
IF (mode == 1)
{
adc = read_adc ();
IF (adc > 1000) adc = 1000;
output_high (RC);
delay_ms (1);
delay_us (adc);
output_low (RC);
delay_us (1000 - adc) ;
delay_ms (18);
}
IF (mode == 0)
{
for( i=0;i<=50;i++ )
{
adc=1000;
output_high (RC);
delay_ms (1);
delay_us (adc);
output_low (RC);
delay_us (1000 - adc) ;
delay_ms (18);
}
for(i=0;i<=50;i++ )
{
adc=0;
output_high (RC);
delay_ms (1);
delay_us (adc);
output_low (RC);
delay_us (1000 - adc) ;
delay_ms (18);
}
}
}
}
Ảnh mô phỏng protues.
Đây là code chương trình.
#include <16F877A.h>
#device ADC=10
#FUSES NOWDT //No Watch Dog Timer
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used FOR I/O
#use delay(crystal=12MHz)
#define LED PIN_C1
#define DELAY 10
#define RC PIN_E0
#include <lcd_lib_4bit.c>
int i;
INT16 adc;
INT8 mode = 0;
#INT_EXT
void ngat_ngoai()
{
mode++;
IF(mode==1)
{
printf (LCD_PutChar, "\fMicro Servo SG90\nManual mode ");
}
IF(mode==2)
{
printf (LCD_PutChar, "\fMicro Servo SG90\nAuto mode ");
mode=0;
}
}
void main()
{
LCD_Init ();
printf (LCD_PutChar, " CHIASEDIENTU");
enable_interrupts (INT_EXT) ;
//enable_interrupts (INT_EXT_H2L);
delay_ms (2000);
setup_adc_ports (AN0);
setup_adc (ADC_CLOCK_INTERNAL);
set_adc_channel (0);
delay_us (10);
enable_interrupts (GLOBAL);
WHILE (true)
{
IF (mode == 1)
{
adc = read_adc ();
IF (adc > 1000) adc = 1000;
output_high (RC);
delay_ms (1);
delay_us (adc);
output_low (RC);
delay_us (1000 - adc) ;
delay_ms (18);
}
IF (mode == 0)
{
for( i=0;i<=50;i++ )
{
adc=1000;
output_high (RC);
delay_ms (1);
delay_us (adc);
output_low (RC);
delay_us (1000 - adc) ;
delay_ms (18);
}
for(i=0;i<=50;i++ )
{
adc=0;
output_high (RC);
delay_ms (1);
delay_us (adc);
output_low (RC);
delay_us (1000 - adc) ;
delay_ms (18);
}
}
}
}
Link download project.
EmoticonEmoticon