PIC Vietnam

Go Back   PIC Vietnam > Microchip PIC > Cơ bản về vi điều khiển và PIC

Tài trợ cho PIC Vietnam
Trang chủ Đăng Kí Hỏi/Ðáp Thành Viên Lịch Bài Trong Ngày Vi điều khiển

Cơ bản về vi điều khiển và PIC Những bài hướng dẫn cơ bản nhất để làm quen với vi điều khiển PIC

 
 
Ðiều Chỉnh Xếp Bài
Prev Previous Post   Next Post Next
Old 29-10-2012, 09:26 PM   #7
nhanh0112
Đệ tử 4 túi
 
Tham gia ngày: May 2007
Bài gửi: 81
:
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 vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn
 


Quyền Sử Dụng Ở Diễn Ðàn
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is Mở
Smilies đang Mở
[IMG] đang Mở
HTML đang Tắt

Chuyển đến


Múi giờ GMT. Hiện tại là 07:32 PM.


Được sáng lập bởi Đoàn Hiệp
Powered by vBulletin®
Page copy protected against web site content infringement by Copyscape
Copyright © PIC Vietnam