0509592
27-08-2008, 01:12 AM
/*chào các bác!
em viết đoạn code pwm này nhưng gặp một vấn đề là em thử đưa ra led thì chỉ thấy led ở kênh hai sáng dần lên, còn led ở kênh một thì vẫn sáng mà không hề tối dần. Em hiển thị lên lcd thì thấy giá trị hiển thị đúng! không hiểu tại sao?? hay là em làm sai??? các bác giúp em với???
thanks!
/truongtv0510@gmail.com/
*/
Code:
#include <16f877a.h>
#include <def_877a.h>
#FUSES NOWDT, HS, NOPUT, NOPROTECT, NODEBUG, NOBROWNOUT, NOLVP, NOCPD, NOWRT
#use delay(clock=8000000)
#include <lcd_lib_4bit.c>
#bit TMR2ON =0x12.2
int16 i,j;
int16 n;
int a1,b1,c1,a2,b2,c2;
void main()
{
trisc=0x00;
lcd_init();
LCD_PutCmd (0x82 );
lcd_putchar("DKH_4X");
LCD_PutCmd (0xC3 );
lcd_putchar("robocon");
delay_ms(500);
LCD_PutCmd (0x01 );
n=0;
while(n<300)
{
setup_timer_2(T2_DIV_BY_16,255,1);
setup_ccp1(CCP_PWM);
setup_ccp2(CCP_PWM);
set_pwm1_duty(299 - n);
set_pwm2_duty(n);
a1=n/100;
b1=(n-100*a1)/10;
c1=n%10;
a2=(299-n)/100;
b2=((299-n)-100*a2)/10;
c2=(299-n)%10;
lcd_init();
LCD_PutCmd (0x80);
lcd_putchar(a1+48);
LCD_PutCmd (0x81);
lcd_putchar(b1+48);
LCD_PutCmd (0x82);
lcd_putchar(c1+48);
LCD_PutCmd (0xC0 );
lcd_putchar(a2+48);
LCD_PutCmd (0xC1 );
lcd_putchar(b2+48);
LCD_PutCmd (0xC2);
lcd_putchar(c2+48);
delay_ms(200);
LCD_PutCmd (0x01 );
n++;
if(n==300)
goto thoat;
}
thoat:
TMR2ON=0;
setup_ccp1 (CCP_OFF);
setup_ccp2 (CCP_OFF);
}
em viết đoạn code pwm này nhưng gặp một vấn đề là em thử đưa ra led thì chỉ thấy led ở kênh hai sáng dần lên, còn led ở kênh một thì vẫn sáng mà không hề tối dần. Em hiển thị lên lcd thì thấy giá trị hiển thị đúng! không hiểu tại sao?? hay là em làm sai??? các bác giúp em với???
thanks!
/truongtv0510@gmail.com/
*/
Code:
#include <16f877a.h>
#include <def_877a.h>
#FUSES NOWDT, HS, NOPUT, NOPROTECT, NODEBUG, NOBROWNOUT, NOLVP, NOCPD, NOWRT
#use delay(clock=8000000)
#include <lcd_lib_4bit.c>
#bit TMR2ON =0x12.2
int16 i,j;
int16 n;
int a1,b1,c1,a2,b2,c2;
void main()
{
trisc=0x00;
lcd_init();
LCD_PutCmd (0x82 );
lcd_putchar("DKH_4X");
LCD_PutCmd (0xC3 );
lcd_putchar("robocon");
delay_ms(500);
LCD_PutCmd (0x01 );
n=0;
while(n<300)
{
setup_timer_2(T2_DIV_BY_16,255,1);
setup_ccp1(CCP_PWM);
setup_ccp2(CCP_PWM);
set_pwm1_duty(299 - n);
set_pwm2_duty(n);
a1=n/100;
b1=(n-100*a1)/10;
c1=n%10;
a2=(299-n)/100;
b2=((299-n)-100*a2)/10;
c2=(299-n)%10;
lcd_init();
LCD_PutCmd (0x80);
lcd_putchar(a1+48);
LCD_PutCmd (0x81);
lcd_putchar(b1+48);
LCD_PutCmd (0x82);
lcd_putchar(c1+48);
LCD_PutCmd (0xC0 );
lcd_putchar(a2+48);
LCD_PutCmd (0xC1 );
lcd_putchar(b2+48);
LCD_PutCmd (0xC2);
lcd_putchar(c2+48);
delay_ms(200);
LCD_PutCmd (0x01 );
n++;
if(n==300)
goto thoat;
}
thoat:
TMR2ON=0;
setup_ccp1 (CCP_OFF);
setup_ccp2 (CCP_OFF);
}