Ðề tài: LED trong PIC
View Single Post
Old 16-11-2013, 01:09 AM   #2
nguyenthanhquat
Nhập môn đệ tử
 
Tham gia ngày: Jul 2013
Bài gửi: 4
:
bạn có thể dùng PWM của pic, chương trính sau cho 2 con led ở 2 chân RC1 và RC2 sáng dần và tối dần

#include <16F877A.h>
#include <string.h>
#FUSES NOWDT //No Watch Dog Timer
#FUSES HS //High speed Osc (> 4mhz for PCM/PCH) (>10mhz for PCD)
#FUSES PUT //Power Up Timer
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for
#FUSES NOCPD //No EE protection
#FUSES NOWRT //Program memory not write protected
#FUSES NODEBUG //No Debug mode for ICD
#FUSES NOPROTECT //Code not protected from reading
#use delay(clock=8000000)


int8 run=0;



void main() //main program
{
set_tris_C(0b00000000);

output_low(PIN_C1); // Set CCP2 output low
output_low(PIN_C2); // Set CCP1 output low

//value = Fosc / (Fpwm * 4 * T2DIV) - 1
setup_timer_2(T2_DIV_BY_16, 124, 1); // 1000 Hz

setup_ccp1(CCP_PWM); // Configure CCP1 as a PWM
setup_ccp2(CCP_PWM); // Configure CCP2 as a PWM

/*duty cycle = value / [ 4 * (PR2 +1 ) ]
value = (Desired_Duty_Cycle% * Fosc) / (Fpwm * 4 * T2DIV) */



while(true)
{
for(run=0;run<124;run++)
{
set_pwm1_duty(run);
set_pwm2_duty(run);
delay_ms(20); // A pause at each increment.
}
for(run=124;run>1;run--)
{
set_pwm1_duty(run);
set_pwm2_duty(run);
delay_ms(20); // A pause at each increment.
}

}
}

}
nguyenthanhquat vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn