![]() |
LED trong PIC
Xin hỏi các bạn trong PIC chúng ta có thể viết code để cho 1 LED sang dần lên hoặc mờ dần và tắt hẳn đi không ? và code nó như thể nào? cám ơn các bạn nghe.
|
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. } } } } |
Múi giờ GMT. Hiện tại là 07:24 AM. |
Tên diễn đàn: vBulletin Version 3.8.11
Được sáng lập bởi Đoàn Hiệp.
Copyright © PIC Vietnam