PIC Vietnam

PIC Vietnam (http://www.picvietnam.com/forum/index.php)
-   Cơ bản về vi điều khiển và PIC (http://www.picvietnam.com/forum/forumdisplay.php?f=8)
-   -   LED nháy theo nhạc dùng pic (http://www.picvietnam.com/forum/showthread.php?t=30771)

chiviyeu_ltk91 17-10-2012 10:58 PM

LED nháy theo nhạc dùng pic
 
Chào cáo bác. Em có một ý tưởng thế này. Em muốn làm cái mạch LED nháy theo nhạc dùng pic18fxxx nhưng chưa biết phai bắt đầu từ đâu. Bác nào có kinh nghiệm trong vụ này chỉ em và mọi người biết với.
Thank các bác...!!!

thegioithongmin 18-10-2012 12:57 AM

Mình có mạch nháy theo nhạc đơn giản lắm chỉ khoảng 10ngan linh kiện thôi
bạn qua minh cho nè cafe thôi thuc hanh tại chỗ

chiviyeu_ltk91 18-10-2012 07:15 PM

bác post mạch lên cho anh em cùng tham khảo cái nào.

tungtot_vl 20-10-2012 08:51 PM

cái này viết code cũng đơn giản lắm chỉ vài dòng thôi. mình đã làm cách đây 3 năm rồi như code bỏ ở đâu rồi ko nhớ nữa. bạn sử dụng chức ADC trong PIC là dùng được

isad2008 22-10-2012 10:48 AM

bac cho em dia chi voi, hom nao em qua moi bac cafe va hoc hoi luon. cho em so dien thoai nua nhe. mail em la hoang.ksd@gmail.com

chiviyeu_ltk91 24-10-2012 01:02 PM

bác tungtot_vl ơi giúp em vụ này với.thank bác nhiều

nhanh0112 29-10-2012 09:26 PM

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 ;))

nhanh0112 29-10-2012 09:42 PM

Còn nếu chỉ đơn thuần chỉ để nháy nhạc thì mua con LM3914 (15-16) (ở HN thì mua ở chợ zời, led thanh thì mua ở 17a Hàn Thuyên, xanh đỏ có cả 6k/nhót - 10bar). Tải datasheet của LM3914 sẽ có đủ sơ đồ (app note) cho lắp ráp, đơn giản dễ chạy...

tungtot_vl 01-11-2012 09:12 PM

tt
 
1 Attachment(s)
chương trình Equalizer cho các bác đây
// Equalizer do nhay 16 bit
#include<16f877.h>
#fuses NOWDT,PUT,hs,NOPROTECT,NOBROWNOUT NOLVP
#device 16f877*=16 ADC=8
#use delay(clock=20000000)
#use fast_io(a)
#use fast_io(b)
#byte portb = 0x06
#use fast_io(d)
#byte portd=0x08

void main()
{ int l,i,a;
int ADC;
set_tris_A(0x0f);
set_tris_b(0x00);
set_tris_d(0x00);

enable_interrupts(global);
setup_ADC(ADC_clock_internal);
setup_adc_ports( ALL_ANALOG );
set_ADC_channel(0);
portb = 0x00;portd = 0x00;

while(1)
{ ADC = read_ADC();

if((ADC>4)&&(ADC<8)){portb = 0x01;portd = 0x00;} // tan so lay mau
if((ADC>8)&&(ADC<16)){portb = 0x03;portd = 0x00;}
if((ADC>16)&&(ADC<24)){portb = 0x07;portd = 0x00;}
if((ADC>24)&&(ADC<32)){portb = 0x0f;portd = 0x00;}
if((ADC>32)&&(ADC<40)){portb = 0x1f;portd = 0x00;}
if((ADC>40)&&(ADC<48)){portb = 0x3f;portd = 0x00;}
if((ADC>48)&&(ADC<56)){portb = 0x7f;portd = 0x00;}
if((ADC>56)&&(ADC<64)){portb = 0xff; portd = 0x00;}

if((ADC>64)&&(ADC<72)){portd = 0x01; portb = 0xff;}
if((ADC>72)&&(ADC<80)){portd = 0x03; portb = 0xff;}
if((ADC>80)&&(ADC<96)){portd = 0x07; portb = 0xff;}
if((ADC>96)&&(ADC<100)){portd = 0x0f; portb = 0xff;}
if((ADC>104)&&(ADC<112)){portd = 0x1f; portb = 0xff;}
if((ADC>112)&&(ADC<120)){portd = 0x3f; portb = 0xff;}
if((ADC>120)&&(ADC<128)){portd = 0x7f; portb = 0xff;}
if(ADC>128){portd = 0xff; portb = 0xff;}
delay_ms(5); // do nhay nhay theo dieu nhac
}
}

//the end
có kèm theo chương trình test protues các chú chạy thử để xem nhé
các này đã chạy thự tế rồi không phải lo gì
trong PIC có bao nhiêu chân ADC thì có thể làm được bấy nhiêu kênh. chương trình này mình làm có 1 kênh. các kênh khách các chú tự làm thử nhé

chiviyeu_ltk91 02-11-2012 09:26 PM

thank bác nhiều. mà bác tungtot_vl có viết code băng C được không CSS em chả hiểu gì cả.hì.

dientu90 05-11-2012 04:27 PM

CCS không là C thì là cái gì?????????

legiap 07-11-2012 09:27 PM

while(1)
{ ADC = read_ADC();

if((ADC>4)&&(ADC<8)){portb = 0x01;portd = 0x00;} // tan so lay mau
if((ADC>8)&&(ADC<16)){portb = 0x03;portd = 0x00;}
if((ADC>16)&&(ADC<24)){portb = 0x07;portd = 0x00;}
if((ADC>24)&&(ADC<32)){portb = 0x0f;portd = 0x00;}
if((ADC>32)&&(ADC<40)){portb = 0x1f;portd = 0x00;}
if((ADC>40)&&(ADC<48)){portb = 0x3f;portd = 0x00;}
if((ADC>48)&&(ADC<56)){portb = 0x7f;portd = 0x00;}
if((ADC>56)&&(ADC<64)){portb = 0xff; portd = 0x00;}

if((ADC>64)&&(ADC<72)){portd = 0x01; portb = 0xff;}
if((ADC>72)&&(ADC<80)){portd = 0x03; portb = 0xff;}
if((ADC>80)&&(ADC<96)){portd = 0x07; portb = 0xff;}
if((ADC>96)&&(ADC<100)){portd = 0x0f; portb = 0xff;}
if((ADC>104)&&(ADC<112)){portd = 0x1f; portb = 0xff;}
if((ADC>112)&&(ADC<120)){portd = 0x3f; portb = 0xff;}
if((ADC>120)&&(ADC<128)){portd = 0x7f; portb = 0xff;}
if(ADC>128){portd = 0xff; portb = 0xff;}
delay_ms(5); // do nhay nhay theo dieu nhac
}


Cảm ơn bạn tungtot_vl!
Từ đoạn code trên ta thấy
Nếu a = phần nguyên của ADC/8
((2 mu a) -1) là giá trị 16 bit có byte thấp là giá trị của Port B và byte cao là giá trị của Port D.
Vì vậy có thể rút gọn lại được.

gnuhuuh 08-11-2012 01:20 AM

cám ơn !! đang quan tâm cái này !! để làm thử

tien168bn 07-01-2013 05:12 PM

Trích:

Nguyên văn bởi tungtot_vl (Post 78967)
chương trình Equalizer cho các bác đây
// Equalizer do nhay 16 bit
#include<16f877.h>
#fuses NOWDT,PUT,hs,NOPROTECT,NOBROWNOUT NOLVP
#device 16f877*=16 ADC=8
#use delay(clock=20000000)
#use fast_io(a)
#use fast_io(b)
#byte portb = 0x06
#use fast_io(d)
#byte portd=0x08

void main()
{ int l,i,a;
int ADC;
set_tris_A(0x0f);
set_tris_b(0x00);
set_tris_d(0x00);

enable_interrupts(global);
setup_ADC(ADC_clock_internal);
setup_adc_ports( ALL_ANALOG );
set_ADC_channel(0);
portb = 0x00;portd = 0x00;

while(1)
{ ADC = read_ADC();

if((ADC>4)&&(ADC<8)){portb = 0x01;portd = 0x00;} // tan so lay mau
if((ADC>8)&&(ADC<16)){portb = 0x03;portd = 0x00;}
if((ADC>16)&&(ADC<24)){portb = 0x07;portd = 0x00;}
if((ADC>24)&&(ADC<32)){portb = 0x0f;portd = 0x00;}
if((ADC>32)&&(ADC<40)){portb = 0x1f;portd = 0x00;}
if((ADC>40)&&(ADC<48)){portb = 0x3f;portd = 0x00;}
if((ADC>48)&&(ADC<56)){portb = 0x7f;portd = 0x00;}
if((ADC>56)&&(ADC<64)){portb = 0xff; portd = 0x00;}

if((ADC>64)&&(ADC<72)){portd = 0x01; portb = 0xff;}
if((ADC>72)&&(ADC<80)){portd = 0x03; portb = 0xff;}
if((ADC>80)&&(ADC<96)){portd = 0x07; portb = 0xff;}
if((ADC>96)&&(ADC<100)){portd = 0x0f; portb = 0xff;}
if((ADC>104)&&(ADC<112)){portd = 0x1f; portb = 0xff;}
if((ADC>112)&&(ADC<120)){portd = 0x3f; portb = 0xff;}
if((ADC>120)&&(ADC<128)){portd = 0x7f; portb = 0xff;}
if(ADC>128){portd = 0xff; portb = 0xff;}
delay_ms(5); // do nhay nhay theo dieu nhac
}
}

//the end
có kèm theo chương trình test protues các chú chạy thử để xem nhé
các này đã chạy thự tế rồi không phải lo gì
trong PIC có bao nhiêu chân ADC thì có thể làm được bấy nhiêu kênh. chương trình này mình làm có 1 kênh. các kênh khách các chú tự làm thử nhé

có thể chia nhỏ các khoảng adc nữa ra để làm thêm led không bác nhỉ. mà cái này là nháy theo biên độ phải ko bác


Múi giờ GMT. Hiện tại là 08:35 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