Viết hơi lủng củng tý, nhưng đảm bảo chạy. 8 kênh (active high), chân cẳng tùy bạn định nghĩ lại (mặc định là portB). Tín hiệu audio vào AN0 (biên độ max là 5V).
Chúc thành công!
Code:
/**********************************************
***********************************************/
#include <16F716.h>
#device adc=8
#FUSES WDT //No Watch Dog Timer
#FUSES HS //High speed Osc (> 4mhz for PCM/PCH) (>10mhz for PCD)
#FUSES NOPUT //No Power Up Timer
#FUSES PROTECT //Code not protected from reading
#FUSES BORV40 //Brownout reset at 4.0V
#FUSES NOBROWNOUT //Reset when brownout detected
#FUSES RESERVED //Used to set the reserved FUSE bits
#use delay(clock=12000000)
#define ch1 PIN_B7
#define ch2 PIN_B6
#define ch3 PIN_B4
#define ch4 PIN_B5
#define ch5 PIN_B2
#define ch6 PIN_B3
#define ch7 PIN_B1
#define ch8 PIN_B0
//#include "module.c"
char value;
char PWM[8];
//#INT_TIMER2
void time()
{
static int16 mode=0;
static char i=0,temp,j,flag[8];
static char led1=0,led2=1,led3=2,led4;
static int1 init=0,clear;
/*
*/
temp=READ_ADC();
if(temp>27)
{temp=temp-27;}
if(temp>67)
{mode=1500;}
if(mode)
{
mode--;
if(init>0)
{
init=0;
PWM[0]=0;
PWM[1]=0;
PWM[2]=0;
PWM[3]=0;
PWM[4]=0;
PWM[5]=0;
PWM[6]=0;
PWM[7]=0;
}
if(value<temp){value=temp;}
if(value>31)
{
if(PWM[0]<100)PWM[0]+=10;flag[0]=50;
if(value>40)
{
if(PWM[1]<100)PWM[1]+=10;flag[1]=50;
if(value>49)
{
if(PWM[2]<100)PWM[2]+=10;flag[2]=50;
if(value>58)
{
if(PWM[3]<100)PWM[3]=10;flag[3]=50;
if(value>67)
{
if(PWM[4]<100)PWM[4]+=10;flag[4]=50;
if(value>76)
{
if(PWM[5]<100)PWM[5]+=10;flag[5]=50;
if(value>85)
{
if(PWM[6]<100)PWM[6]+=10;flag[6]=50;
if(value>94)
{if(PWM[7]<100)PWM[7]+=10;flag[7]=50;}
else{if(flag[7]==0)PWM[7]=0;}
}
else{if(flag[6]==0)PWM[6]=0;}
}
else{if(flag[5]==0)PWM[5]=0;}
}
else{if(flag[4]==0)PWM[4]=0;}
}
else{if(flag[3]==0)PWM[3]=0;}
}
else{if(flag[2]==0)PWM[2]=0;}
}
else{if(flag[1]==0)PWM[1]=0;}
}
else{if(flag[0]==0)PWM[0]=0;}
if(value>10){value-=5;}
clear=0;
for(j=0;j<8;j++)
{
if(clear)
{
flag[7-j]=0;
}
if(flag[7-j]>0)
{
flag[7-j]=flag[7-j]-1;
if(flag[7-j]==0)
{PWM[7-j]=0;}
clear=1;
}
else{flag[7-j]=0;}
}
init=0;
}
else
{
if(init<=0)
{
init=1;
PWM[0]=25;
PWM[1]=50;
PWM[2]=100;
PWM[3]=0;
PWM[4]=0;
PWM[5]=0;
PWM[6]=0;
PWM[7]=0;
i=0;led1=0;led2=1;led3=2;
}
PWM[led1]=PWM[led1]-1;
PWM[led2]=PWM[led2]-1;
PWM[led3]=PWM[led3]-2;
led4=led3+1;if(led4>7){led4=led4-8;}
PWM[led4]=PWM[led4]+4;
if(PWM[led1]==0)
{
i++;
if(i>7){i=0;}
led1=i;
led2=i+1;
if(led2>7){led2=led2-8;}
led3=i+2;
if(led3>7){led3=led3-8;}
PWM[led3]=100;
}
}
}
void SPWM()
{
static char count,counttime;
count++;
if(count>100)
{
count=0;
if(counttime++>10)
{
counttime=0;
time();
}
if(PWM[0]>0){output_high(ch1);}
if(PWM[1]>0){output_high(ch2);}
if(PWM[2]>0){output_high(ch3);}
if(PWM[3]>0){output_high(ch4);}
if(PWM[4]>0){output_high(ch5);}
if(PWM[5]>0){output_high(ch6);}
if(PWM[6]>0){output_high(ch7);}
if(PWM[7]>0){output_high(ch8);}
}
if(count==PWM[0])
{output_low(ch1);}
if(count==PWM[1])
{output_low(ch2);}
if(count==PWM[2])
{output_low(ch3);}
if(count==PWM[3])
{output_low(ch4);}
if(count==PWM[4])
{output_low(ch5);}
if(count==PWM[5])
{output_low(ch6);}
if(count==PWM[6])
{output_low(ch7);}
if(count==PWM[7])
{output_low(ch8);}
}
void main()
{
//setup_timer_0(RTCC_INTERNAL);
//enable_interrupts(INT_timer0);
//enable_interrupts(GLOBAL);
setup_adc_ports(AN0_AN1_AN3);
setup_adc(ADC_CLOCK_DIV_2);
//setup_timer_2(T2_DIV_BY_16,249,16); //20kHz
//enable_interrupts(INT_TIMER2);
//enable_interrupts(GLOBAL);
PWM[0]=0;
PWM[1]=0;
PWM[2]=0;
PWM[3]=0;
PWM[4]=0;
PWM[5]=0;
PWM[6]=0;
PWM[7]=0;
set_adc_channel(0);
setup_wdt(WDT_18MS);
while(TRUE)
{
restart_wdt();
SPWM();
}
}
PS: Nếu chạy đc nhớ quảng cáo giùm nha

)