Bạn phải kích hoạt T2 bằng cách dùng hàm setup_timer_2(T2_DIV_BY_16,255,1) thì module PWM mới hoạt động. Các thông số trong hàm này bạn có thể thay đổi có tần số mong muốn.
Bạn nên dùng PWM 8 bit trước sau đó mới nghĩ đến 10bit. Theo tôi sử dụng 8 bit là đủ rồi.
Đoạn Code sửa lại như sau:
Code:
#include "16F877a.h"
#device *=16
#device adc=8
#FUSES NOWDT //No Watch Dog Timer
#FUSES RC //Resistor/Capacitor Osc with CLKOUT
#FUSES NOPUT //No Power Up Timer
#FUSES NOPROTECT //Code not protected from reading
#FUSES NODEBUG //No Debug mode for ICD
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD //No EE protection
#FUSES NOWRT //Program memory not write protected
#use delay(clock=20000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
//#use fast_io(b)
void main()
{
//int8 i;
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_psp(PSP_DISABLED);
setup_spi(FALSE);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DIV_BY_16,255,1); // KICH HOAT T2 TAI DAY, BAN CO THE THAY DOI CAC THONG SO TRONG HAM NAY DE CO TAN SO MONG MUON
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
while(1)
{
setup_ccp1(ccp_pwm);
set_pwm1_duty(0);
setup_ccp2(ccp_pwm);
set_pwm2_duty(50);
delay_ms(300);
setup_ccp1(ccp_pwm);
set_pwm1_duty(128);
setup_ccp2(ccp_pwm);
set_pwm2_duty(100);
delay_ms(300);
setup_ccp1(ccp_pwm);
set_pwm1_duty(255);
setup_ccp2(ccp_pwm);
set_pwm2_duty(200);
delay_ms(300);
}
}
Tôi đã mô phỏng trên Protues chay tốt, không biết trên mạch thì thế nào.
Mạch mô phỏng dưới đây:
Chúc thành công!