Thông báo

Collapse
No announcement yet.

Lập trình AVR bằng CodeVisionAVR C Compiler cho người mới bắt đầu

Collapse
X
 
  • Lọc
  • Giờ
  • Show
Clear All
new posts

  • Nguyên văn bởi lethediep Xem bài viết
    cac bnac ah minh moi hoc ve cip avr minh hoi cac bac ka muon tao tre khong 10 s ch con at90s2313 o chan pỏt bo qua trinh viet nhu nao cac bac chi minh voi
    bác khai báo hàm delay như sau #include <delay.h> bên trong thư viện delay có sẵn 2 chương trình delay:

    void delay_us(unsigned int n);
    void delay_ms(unsigned int n);
    ------------------------------------------------------------ tôi viết trên CodeVisionAVR

    Comment


    • chào các bạn mình mới bắt đầu học avr . chưa hiểu được phần mềm nào có thể viết chương trình cho avr. và cách làm mạch nạp cho nó.boar mạch nữa vì thế bạn nào pro thi cho mình xin file để học và làm cho riêng mình 1 cái xin cảm on trước nhe!
      |

      Comment


      • bạn ơi cho mình hỏi, cơ chế định thời của TIMER0 của AVR như thế nào.
        mình lập trình để timer đếm 200 nhịp rùi vào ngắt.
        Nhưng TCNT0 đêm vượt giá trị 0, cụ thể là 04h rồi mới vào ngắt, và ở trong ngắt thì TCNT0 vẵn tiếp tục đếm.
        Mong các pro chỉ giá giúp.

        bạn nào có chương trình nào viết bằng CodevisionAVR cho mình xin tham khảo với
        nick yahoo cua mình : boyhaychat
        thanks mọi người

        Comment


        • /
          ************************************************** ***/



          #include <mega16.h>
          #include <delay.h>
          // Declare your global variables here
          unsigned char i;
          void hienthiled(unsigned char x)
          {
          // Declare your local variables here

          // Input/Output Ports initialization
          // Port A initialization
          // Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out
          // State7=1 State6=1 State5=1 State4=1 State3=1 State2=1 State1=1 State0=1
          PORTA=0xFF;
          DDRA=0xFF;

          // Port B initialization
          // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
          // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
          PORTB=0x00;
          DDRB=0x00;

          // Port C initialization
          // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
          // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
          PORTC=0x00;
          DDRC=0x00;

          // Port D initialization
          // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
          // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
          PORTD=0x00;
          DDRD=0x00;

          // Timer/Counter 0 initialization
          // Clock source: System Clock
          // Clock value: Timer 0 Stopped
          // Mode: Normal top=FFh
          // OC0 output: Disconnected
          TCCR0=0x00;
          TCNT0=0x00;
          OCR0=0x00;

          // Timer/Counter 1 initialization
          // Clock source: System Clock
          // Clock value: Timer 1 Stopped
          // Mode: Normal top=FFFFh
          // OC1A output: Discon.
          // OC1B output: Discon.
          // Noise Canceler: Off
          // Input Capture on Falling Edge
          // Timer 1 Overflow Interrupt: Off
          // Input Capture Interrupt: Off
          // Compare A Match Interrupt: Off
          // Compare B Match Interrupt: Off
          TCCR1A=0x00;
          TCCR1B=0x00;
          TCNT1H=0x00;
          TCNT1L=0x00;
          ICR1H=0x00;
          ICR1L=0x00;
          OCR1AH=0x00;
          OCR1AL=0x00;
          OCR1BH=0x00;
          OCR1BL=0x00;

          // Timer/Counter 2 initialization
          // Clock source: System Clock
          // Clock value: Timer 2 Stopped
          // Mode: Normal top=FFh
          // OC2 output: Disconnected
          ASSR=0x00;
          TCCR2=0x00;
          TCNT2=0x00;
          OCR2=0x00;

          // External Interrupt(s) initialization
          // INT0: Off
          // INT1: Off
          // INT2: Off
          MCUCR=0x00;
          MCUCSR=0x00;

          // Timer(s)/Counter(s) Interrupt(s) initialization
          TIMSK=0x00;

          // Analog Comparator initialization
          // Analog Comparator: Off
          // Analog Comparator Input Capture by Timer/Counter 1: Off
          ACSR=0x80;
          SFIOR=0x00;
          switch(x)
          {
          case 0: {PORTD=0x40; break;}
          case 1: {PORTD=0xF9; break;}
          case 2: {PORTD=0x24; break;}
          case 3: {PORTD=0x30; break;}
          case 4: {PORTD=0x19; break;}
          case 5: {PORTD=0x12; break;}
          case 6: {PORTD=0x02; break;}
          case 7: {PORTD=0xF8; break;}
          case 8: {PORTD=0x00; break;}
          case 9: {PORTD=0x10; break;}
          } }
          void main(void){
          while (1)
          {
          {

          for(i=0;i<10;i++)
          {
          hienthiled (i);
          delay_us(1500);

          }

          };
          }
          }

          sao không chạy nhỉ ?
          anh em chỉ giúp lỗi sai cái .
          Last edited by quytho; 07-01-2010, 02:49.

          Comment


          • Nguyên văn bởi quytho Xem bài viết
            /
            ************************************************** ***/



            #include <mega16.h>
            #include <delay.h>
            // Declare your global variables here
            unsigned char i;
            void hienthiled(unsigned char x)
            {
            // Declare your local variables here

            // Input/Output Ports initialization
            // Port A initialization
            // Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out
            // State7=1 State6=1 State5=1 State4=1 State3=1 State2=1 State1=1 State0=1
            PORTA=0xFF;
            DDRA=0xFF;

            // Port B initialization
            // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
            // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
            PORTB=0x00;
            DDRB=0x00;

            // Port C initialization
            // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
            // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
            PORTC=0x00;
            DDRC=0x00;

            // Port D initialization
            // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
            // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
            PORTD=0x00;
            DDRD=0x00;

            // Timer/Counter 0 initialization
            // Clock source: System Clock
            // Clock value: Timer 0 Stopped
            // Mode: Normal top=FFh
            // OC0 output: Disconnected
            TCCR0=0x00;
            TCNT0=0x00;
            OCR0=0x00;

            // Timer/Counter 1 initialization
            // Clock source: System Clock
            // Clock value: Timer 1 Stopped
            // Mode: Normal top=FFFFh
            // OC1A output: Discon.
            // OC1B output: Discon.
            // Noise Canceler: Off
            // Input Capture on Falling Edge
            // Timer 1 Overflow Interrupt: Off
            // Input Capture Interrupt: Off
            // Compare A Match Interrupt: Off
            // Compare B Match Interrupt: Off
            TCCR1A=0x00;
            TCCR1B=0x00;
            TCNT1H=0x00;
            TCNT1L=0x00;
            ICR1H=0x00;
            ICR1L=0x00;
            OCR1AH=0x00;
            OCR1AL=0x00;
            OCR1BH=0x00;
            OCR1BL=0x00;

            // Timer/Counter 2 initialization
            // Clock source: System Clock
            // Clock value: Timer 2 Stopped
            // Mode: Normal top=FFh
            // OC2 output: Disconnected
            ASSR=0x00;
            TCCR2=0x00;
            TCNT2=0x00;
            OCR2=0x00;

            // External Interrupt(s) initialization
            // INT0: Off
            // INT1: Off
            // INT2: Off
            MCUCR=0x00;
            MCUCSR=0x00;

            // Timer(s)/Counter(s) Interrupt(s) initialization
            TIMSK=0x00;

            // Analog Comparator initialization
            // Analog Comparator: Off
            // Analog Comparator Input Capture by Timer/Counter 1: Off
            ACSR=0x80;
            SFIOR=0x00;
            switch(x)
            {
            case 0: {PORTD=0x40; break;}
            case 1: {PORTD=0xF9; break;}
            case 2: {PORTD=0x24; break;}
            case 3: {PORTD=0x30; break;}
            case 4: {PORTD=0x19; break;}
            case 5: {PORTD=0x12; break;}
            case 6: {PORTD=0x02; break;}
            case 7: {PORTD=0xF8; break;}
            case 8: {PORTD=0x00; break;}
            case 9: {PORTD=0x10; break;}
            } }
            void main(void){
            while (1)
            {
            {

            for(i=0;i<10;i++)
            {
            hienthiled (i);
            delay_us(1500);

            }

            };
            }
            }

            sao không chạy nhỉ ?
            anh em chỉ giúp lỗi sai cái .
            BAN khai PORTD là đầu vào nên ko chạy.Thay Portd bằng porta trong chương trình con và test lại.
            |

            Comment


            • Mình đã thử thay PORTD=A rùi nhưng sao ko được nhỉ?
              Giúp mình rõ chút nhé !!!

              Comment


              • chưa tìm tòi đã hỏi
                ngon rùi bạn ah ?

                Comment


                • Nguyên văn bởi quytho Xem bài viết
                  /
                  ************************************************** ***/
                  #include <mega16.h>
                  #include <delay.h>
                  // Declare your global variables here
                  unsigned char i;
                  void hienthiled(unsigned char x)
                  {
                  ..............................
                  switch(x)
                  {
                  case 0: {PORTD=0x40; break;}
                  case 1: {PORTD=0xF9; break;}
                  case 2: {PORTD=0x24; break;}
                  case 3: {PORTD=0x30; break;}
                  case 4: {PORTD=0x19; break;}
                  case 5: {PORTD=0x12; break;}
                  case 6: {PORTD=0x02; break;}
                  case 7: {PORTD=0xF8; break;}
                  case 8: {PORTD=0x00; break;}
                  case 9: {PORTD=0x10; break;}
                  } }


                  void main(void){
                  while (1)
                  {
                  {

                  for(i=0;i<10;i++)
                  {
                  hienthiled (i);
                  delay_us(1500);

                  }

                  };
                  }
                  }

                  sao không chạy nhỉ ?
                  anh em chỉ giúp lỗi sai cái .

                  1 – đoạn chương trình này bạn đặt ở trong void hienthiled(unsigned char x) tại sao lại có thêm phần thiết lập ngõ ra vào nữa.

                  2- Theo tôi bạn không hiểu cấu truc của 1 chương trình C. Bạn nên tham khảo ở các bài viết khác như:
                  http://my.opera.com/quilong/blog/huo...rinh-c-cho-avr
                  http://hocavr.com/index.php/software/c-cho-avr
                  nó có ích cho bạn đó.


                  fix

                  #include <mega16.h>
                  #include <delay.h>
                  // Declare your global variables here
                  unsigned char i;
                  void hienthiled(unsigned char x)
                  {
                  /* copy toàn bộ phần code phát sinh này vào sau hàm main*/
                  switch(x)
                  {
                  case 0: {PORTD=0x40; break;}
                  case 1: {PORTD=0xF9; break;}
                  case 2: {PORTD=0x24; break;}
                  case 3: {PORTD=0x30; break;}
                  case 4: {PORTD=0x19; break;}
                  case 5: {PORTD=0x12; break;}
                  case 6: {PORTD=0x02; break;}
                  case 7: {PORTD=0xF8; break;}
                  case 8: {PORTD=0x00; break;}
                  case 9: {PORTD=0x10; break;}
                  } }


                  void main(void){

                  /*phân code phát sinh đạt ở dây*/
                  while (1)
                  {


                  for(i=0;i<10;i++)
                  {
                  hienthiled (i);
                  delay_us(1500);
                  }

                  }
                  }

                  Comment


                  • Có lên mạng mới thấy hay.
                    Thank you !
                    Cả trang web bạn nữa.

                    Comment


                    • Có lên mạng mới thấy hay.
                      Thank you !
                      Cả trang web bạn nữa.

                      Comment


                      • --------------*-------------*----------
                        #include <mega16.h>
                        #include <delay.h>

                        // Declare your global variables here
                        void daydulieu(unsigned char x)// Ham dua du lieu ra PORT
                        {
                        switch(x) // Tuy thuoc vao bien dau vao ma dua du lieu
                        { //logic 1 tat led, logic 0 bat led
                        case 0: { PORTB=0xC0; break; } // So 0
                        case 1: { PORTB=0xF9; break; } // So 1
                        case 2: { PORTB=0xA4; break; } // So 2
                        case 3: { PORTB=0xB0; break; } // So 3
                        case 4: { PORTB=0x99; break; } // So 4
                        case 5: { PORTB=0x92; break; } // So 5
                        case 6: { PORTB=0x82; break; } // So 6
                        case 7: { PORTB=0xF8; break; } // So 7
                        case 8: { PORTB=0x80; break; } // So 8
                        case 9: { PORTB=0x90; break; } // So 9
                        }
                        }
                        void hienthi(int n)
                        {
                        int a,b;
                        a=n/10;
                        b=(n-a*10);
                        PORTD=0x00;
                        PORTD=0x01;
                        daydulieu(a);
                        delay_us(1000);
                        PORTB=0x00;
                        PORTD=0x02;
                        daydulieu(b);
                        delay_us(1000);
                        PORTB=0x00;
                        PORTD=0x00;
                        }
                        void doc(void)
                        {
                        int a,b;
                        if (++b==9)
                        { b=0,
                        a=1;}
                        if (++a==9)
                        {a=0;}
                        }
                        void main(void)
                        {
                        // Declare your local variables here

                        // Input/Output Ports initialization
                        // Port A initialization
                        // Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out
                        // State7=1 State6=1 State5=1 State4=1 State3=1 State2=1 State1=1 State0=1
                        PORTA=0xFF;
                        DDRA=0xFF;

                        // Port B initialization
                        // Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out
                        // State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0
                        PORTB=0xFF;
                        DDRB=0xFF;

                        // Port C initialization
                        // Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out
                        // State7=1 State6=1 State5=1 State4=1 State3=1 State2=1 State1=1 State0=1
                        PORTC=0xFF;
                        DDRC=0xFF;

                        // Port D initialization
                        // Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out
                        // State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0
                        PORTD=0xFF;
                        DDRD=0xFF;

                        // Timer/Counter 0 initialization
                        // Clock source: System Clock
                        // Clock value: Timer 0 Stopped
                        // Mode: Normal top=FFh
                        // OC0 output: Disconnected
                        TCCR0=0x00;
                        TCNT0=0x00;
                        OCR0=0x00;

                        // Timer/Counter 1 initialization
                        // Clock source: System Clock
                        // Clock value: Timer 1 Stopped
                        // Mode: Normal top=FFFFh
                        // OC1A output: Discon.
                        // OC1B output: Discon.
                        // Noise Canceler: Off
                        // Input Capture on Falling Edge
                        // Timer 1 Overflow Interrupt: Off
                        // Input Capture Interrupt: Off
                        // Compare A Match Interrupt: Off
                        // Compare B Match Interrupt: Off
                        TCCR1A=0x00;
                        TCCR1B=0x00;
                        TCNT1H=0x00;
                        TCNT1L=0x00;
                        ICR1H=0x00;
                        ICR1L=0x00;
                        OCR1AH=0x00;
                        OCR1AL=0x00;
                        OCR1BH=0x00;
                        OCR1BL=0x00;

                        // Timer/Counter 2 initialization
                        // Clock source: System Clock
                        // Clock value: Timer 2 Stopped
                        // Mode: Normal top=FFh
                        // OC2 output: Disconnected
                        ASSR=0x00;
                        TCCR2=0x00;
                        TCNT2=0x00;
                        OCR2=0x00;

                        // External Interrupt(s) initialization
                        // INT0: Off
                        // INT1: Off
                        // INT2: Off
                        MCUCR=0x00;
                        MCUCSR=0x00;

                        // Timer(s)/Counter(s) Interrupt(s) initialization
                        TIMSK=0x00;

                        // Analog Comparator initialization
                        // Analog Comparator: Off
                        // Analog Comparator Input Capture by Timer/Counter 1: Off
                        ACSR=0x80;
                        SFIOR=0x00;

                        while (1)
                        {
                        // Place your code here
                        doc();
                        };
                        }
                        Lại có bài này cần hỏi các bác chút đây. Hỏi nhiều ngại wa mà tìm mãi ko được ?
                        và khi báo lỗi có thêm dòng này ''---
                        Can't open file: c:\documents and settings\administrator\desktop\new folder . ""
                        Chương trình chạy tư 0-99!
                        Cho em hỏi chương trình như vậy có vấn đề gì ko và dòng lỗi nữa ?

                        Comment


                        • Chương trình chạy tư 0-99

                          Nếu kô lỗi chuơng trình của bạn kô đếm và số hiển thị có thể là ngâu nhiên. Bạn nên trau dồi thêm kiến thức c về cách dùng biến. Bạn có thể tham khảo chuơng trình này:
                          ----------------------------------------------------------------------------------------------------------------------------
                          unsigned char code ma7doan[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x9 0};
                          char dem=0;



                          void main (void)
                          {
                          /*code phat sinh*/
                          while(1)
                          {
                          PORTA=ma7doan[dem%10]; // hien thi hang don vi tren porta
                          PORTB=ma7doan[dem/10]; // hien thi hang chuc tren portb
                          dem++;
                          if(dem==100)dem=0;
                          delay_ms(1000); // khai bao #include<delay.h>
                          };

                          }

                          Comment


                          • Nhưng cái dòng báo lỗi là như thế nào vậy anh ?
                            Em thử 1 số cái nó cũng báo vậy .

                            Comment


                            • Code của mình không chạy? mong được giúp đỡ!

                              Code:
                              /*****************************************************
                              This program was produced by the
                              CodeWizardAVR V2.04.4a Advanced
                              Automatic Program Generator
                              © Copyright 1998-2009 Pavel Haiduc, HP InfoTech s.r.l.
                              http://www.hpinfotech.com
                              
                              Project : 
                              Version : 
                              Date    : 11/18/2009
                              Author  : NeVaDa
                              Company : Microsoft
                              Comments: 
                              
                              
                              Chip type               : ATmega8
                              Program type            : Application
                              AVR Core Clock frequency: 4.000000 MHz
                              Memory model            : Small
                              External RAM size       : 0
                              Data Stack size         : 256
                              *****************************************************/
                              
                              #include <mega8.h>
                              #include <delay.h>
                              
                              #define B5 PORTB.5
                              #define B4 PORTB.4
                              #define B3 PORTB.3
                              #define B2 PORTB.2
                              #define B1 PORTB.1
                              #define B0 PORTB.0
                              
                              #define C5 PORTC.5
                              #define C4 PORTC.4
                              #define C3 PORTC.3
                              #define C2 PORTC.2
                              #define C1 PORTC.1
                              #define C0 PORTC.0
                              
                              #define D7 PORTD.7
                              #define D6 PORTD.6
                              #define D5 PORTD.5
                              #define D4 PORTD.4
                              #define D3 PORTD.3
                              #define D2 PORTD.2
                              #define D1 PORTD.1
                              #define D0 PORTD.0
                              
                              unsigned char xung;
                              unsigned int pulse[]={0,1,2,3,4,5,6,7};
                              // Timer 0 overflow interrupt service routine
                              interrupt [TIM0_OVF] void timer0_ovf_isr(void)
                              {
                                  TCNT0=200;
                                  xung++;
                                  if(xung<pulse[0])//1
                                    {D0=1;}
                                  else
                                    {D0=0;}
                                  if(xung<pulse[1])//2
                                    {D1=1;}
                                  else
                                    {D1=0;}
                                  if(xung<pulse[2])//3
                                    {D2=1;}
                                  else
                                    {D2=0;}
                                  if(xung<pulse[3])//4
                                    {D3=1;}
                                  else
                                    {D3=0;}
                                  if(xung<pulse[4])//5
                                    {D4=1;}
                                  else
                                    {D4=0;}
                                  if(xung<pulse[5])//6
                                    {D5=1;}
                                  else
                                    {D5=0;}
                                  if(xung<pulse[6])//7
                                    {D6=1;}
                                  else
                                    {D6=0;}
                                  if(xung<pulse[7])//8
                                    {D7=1;}
                                  else
                                    {D7=0;}
                                    
                                       
                                  if(xung>255){xung=0;}//end of pulse
                              
                              }  
                              
                              // Declare your global variables here
                              
                              void pwm88RL(unsigned char m,unsigned char n){
                                  unsigned char i;
                                  unsigned int array[]={255,180,150,120,90,60,30,0};
                                  for(i=0;i<=n;i++){
                                      pulse[7]=array[0];
                                      pulse[6]=array[1];
                                      pulse[5]=array[2];
                                      pulse[4]=array[3];
                                      pulse[3]=array[4];
                                      pulse[2]=array[5];
                                      pulse[1]=array[6];
                                      pulse[0]=array[7];
                                      delay_ms(m);
                                      pulse[7]=array[7];
                                      pulse[6]=array[0];
                                      pulse[5]=array[1];
                                      pulse[4]=array[2];
                                      pulse[3]=array[3];
                                      pulse[2]=array[4];
                                      pulse[1]=array[5];
                                      pulse[0]=array[6];
                                      delay_ms(m);
                                      pulse[7]=array[6];
                                      pulse[6]=array[7];
                                      pulse[5]=array[0];
                                      pulse[4]=array[1];
                                      pulse[3]=array[2];
                                      pulse[2]=array[3];
                                      pulse[1]=array[4];
                                      pulse[0]=array[5];
                                      delay_ms(m);
                                      pulse[7]=array[5];
                                      pulse[6]=array[6];
                                      pulse[5]=array[7];
                                      pulse[4]=array[0];
                                      pulse[3]=array[1];
                                      pulse[2]=array[2];
                                      pulse[1]=array[3];
                                      pulse[0]=array[4];
                                      delay_ms(m);
                                      pulse[7]=array[4];
                                      pulse[6]=array[5];
                                      pulse[5]=array[6];
                                      pulse[4]=array[7];
                                      pulse[3]=array[0];
                                      pulse[2]=array[1];
                                      pulse[1]=array[2];
                                      pulse[0]=array[3];
                                      delay_ms(m);
                                      pulse[7]=array[3];
                                      pulse[6]=array[4];
                                      pulse[5]=array[5];
                                      pulse[4]=array[6];
                                      pulse[3]=array[7];
                                      pulse[2]=array[0];
                                      pulse[1]=array[1];
                                      pulse[0]=array[2];
                                      delay_ms(m);
                                      pulse[7]=array[2];
                                      pulse[6]=array[3];
                                      pulse[5]=array[4];
                                      pulse[4]=array[5];
                                      pulse[3]=array[6];
                                      pulse[2]=array[7];
                                      pulse[1]=array[0];
                                      pulse[0]=array[1];
                                      delay_ms(m);
                                      pulse[7]=array[1];
                                      pulse[6]=array[2];
                                      pulse[5]=array[3];
                                      pulse[4]=array[4];
                                      pulse[3]=array[5];
                                      pulse[2]=array[6];
                                      pulse[1]=array[7];
                                      pulse[0]=array[0];
                                      delay_ms(m);
                                        
                                  }
                              }
                              
                              void pwm88LR(unsigned char m,unsigned char n){
                                  unsigned char i;
                                  unsigned int array[]={255,180,150,120,90,60,30,0};
                                  for(i=0;i<=n;i++){
                                      pulse[7]=array[7];
                                      pulse[6]=array[6];
                                      pulse[5]=array[5];
                                      pulse[4]=array[4];
                                      pulse[3]=array[3];
                                      pulse[2]=array[2];
                                      pulse[1]=array[1];
                                      pulse[0]=array[0];
                                      delay_ms(m);
                                      pulse[7]=array[0];
                                      pulse[6]=array[7];
                                      pulse[5]=array[6];
                                      pulse[4]=array[5];
                                      pulse[3]=array[4];
                                      pulse[2]=array[3];
                                      pulse[1]=array[2];
                                      pulse[0]=array[1];
                                      delay_ms(m);
                                      pulse[7]=array[1];
                                      pulse[6]=array[0];
                                      pulse[5]=array[7];
                                      pulse[4]=array[6];
                                      pulse[3]=array[5];
                                      pulse[2]=array[4];
                                      pulse[1]=array[3];
                                      pulse[0]=array[2];
                                      delay_ms(m);
                                      pulse[7]=array[2];
                                      pulse[6]=array[1];
                                      pulse[5]=array[0];
                                      pulse[4]=array[7];
                                      pulse[3]=array[6];
                                      pulse[2]=array[5];
                                      pulse[1]=array[4];
                                      pulse[0]=array[3];
                                      delay_ms(m);
                                      pulse[7]=array[3];
                                      pulse[6]=array[2];
                                      pulse[5]=array[1];
                                      pulse[4]=array[0];
                                      pulse[3]=array[7];
                                      pulse[2]=array[6];
                                      pulse[1]=array[5];
                                      pulse[0]=array[4];
                                      delay_ms(m);
                                      pulse[7]=array[4];
                                      pulse[6]=array[3];
                                      pulse[5]=array[2];
                                      pulse[4]=array[1];
                                      pulse[3]=array[0];
                                      pulse[2]=array[7];
                                      pulse[1]=array[6];
                                      pulse[0]=array[5];
                                      delay_ms(m);
                                      pulse[7]=array[6];
                                      pulse[6]=array[5];
                                      pulse[5]=array[3];
                                      pulse[4]=array[2];
                                      pulse[3]=array[1];
                                      pulse[2]=array[0];
                                      pulse[1]=array[7];
                                      pulse[0]=array[6];
                                      delay_ms(m);
                                      pulse[7]=array[6];
                                      pulse[6]=array[5];
                                      pulse[5]=array[4];
                                      pulse[4]=array[3];
                                      pulse[3]=array[2];
                                      pulse[2]=array[1];
                                      pulse[1]=array[0];
                                      pulse[0]=array[7];
                                      delay_ms(m);
                                        
                                  }
                              }
                              
                              void pwm4RL(unsigned char m,unsigned char n){
                                  unsigned char i;
                                  unsigned int array[]={255,30};
                                  for(i=0;i<=n;i++){
                                      pulse[7]=array[0];
                                      pulse[6]=array[0];
                                      pulse[5]=array[0];
                                      pulse[4]=array[0];
                                      pulse[3]=array[1];
                                      pulse[2]=array[1];
                                      pulse[1]=array[1];
                                      pulse[0]=array[1];
                                      delay_ms(m);
                                      pulse[7]=array[1];
                                      pulse[6]=array[1];
                                      pulse[5]=array[1];
                                      pulse[4]=array[1];
                                      pulse[3]=array[0];
                                      pulse[2]=array[0];
                                      pulse[1]=array[0];
                                      pulse[0]=array[0];
                                      delay_ms(m);     
                                        
                                  }
                              }
                              
                              void pwm4LR(unsigned char m,unsigned char n){
                                  unsigned char i;
                                  unsigned int array[]={255,30};
                                  for(i=0;i<=n;i++){
                                      pulse[7]=array[1];
                                      pulse[6]=array[1];
                                      pulse[5]=array[1];
                                      pulse[4]=array[1];
                                      pulse[3]=array[0];
                                      pulse[2]=array[0];
                                      pulse[1]=array[0];
                                      pulse[0]=array[0];
                                      delay_ms(m);
                                      pulse[7]=array[0];
                                      pulse[6]=array[0];
                                      pulse[5]=array[0];
                                      pulse[4]=array[0];
                                      pulse[3]=array[1];
                                      pulse[2]=array[1];
                                      pulse[1]=array[1];
                                      pulse[0]=array[1];
                                      delay_ms(m);     
                                        
                                  }
                              }
                              
                              // 4 led mo dan
                              void pwm44RL(unsigned char m,unsigned char n){
                                  unsigned char i;
                                  unsigned int array[]={255,150,60,0};
                                  for(i=0;i<=n;i++){
                                      pulse[7]=array[0];
                                      pulse[6]=array[1];
                                      pulse[5]=array[2];
                                      pulse[4]=array[3];
                                      pulse[3]=array[0];
                                      pulse[2]=array[1];
                                      pulse[1]=array[2];
                                      pulse[0]=array[3];
                                      delay_ms(m);
                                      pulse[7]=array[1];
                                      pulse[6]=array[2];
                                      pulse[5]=array[3];
                                      pulse[4]=array[0];
                                      pulse[3]=array[1];
                                      pulse[2]=array[2];
                                      pulse[1]=array[3];
                                      pulse[0]=array[0];
                                      delay_ms(m);
                                      pulse[7]=array[2];
                                      pulse[6]=array[3];
                                      pulse[5]=array[0];
                                      pulse[4]=array[1];
                                      pulse[3]=array[2];
                                      pulse[2]=array[3];
                                      pulse[1]=array[0];
                                      pulse[0]=array[1];
                                      delay_ms(m);
                                      pulse[7]=array[3];
                                      pulse[6]=array[0];
                                      pulse[5]=array[1];
                                      pulse[4]=array[2];
                                      pulse[3]=array[3];
                                      pulse[2]=array[0];
                                      pulse[1]=array[1];
                                      pulse[0]=array[2];
                                      delay_ms(m);
                                      
                                        
                                  }
                              }
                              
                              void pwm44LR(unsigned char m,unsigned char n){
                                  unsigned char i;
                                  unsigned int array[]={255,150,60,0};
                                  for(i=0;i<=n;i++){
                                      pulse[7]=array[3];
                                      pulse[6]=array[2];
                                      pulse[5]=array[1];
                                      pulse[4]=array[0];
                                      pulse[3]=array[3];
                                      pulse[2]=array[2];
                                      pulse[1]=array[1];
                                      pulse[0]=array[0];
                                      delay_ms(m);
                                      pulse[7]=array[0];
                                      pulse[6]=array[3];
                                      pulse[5]=array[2];
                                      pulse[4]=array[1];
                                      pulse[3]=array[0];
                                      pulse[2]=array[3];
                                      pulse[1]=array[2];
                                      pulse[0]=array[1];
                                      delay_ms(m);
                                      pulse[7]=array[1];
                                      pulse[6]=array[0];
                                      pulse[5]=array[3];
                                      pulse[4]=array[2];
                                      pulse[3]=array[1];
                                      pulse[2]=array[0];
                                      pulse[1]=array[3];
                                      pulse[0]=array[2];
                                      delay_ms(m);
                                      pulse[7]=array[2];
                                      pulse[6]=array[1];
                                      pulse[5]=array[0];
                                      pulse[4]=array[3];
                                      pulse[3]=array[2];
                                      pulse[2]=array[1];
                                      pulse[1]=array[0];
                                      pulse[0]=array[3];
                                      delay_ms(m);
                                      
                                        
                                  }
                              }
                              
                              void pwm8RL(unsigned char m,unsigned char n){
                                  unsigned char i;
                                  unsigned int array[]={255,180,150,120,90,60,30,0};
                                  for(i=0;i<=n;i++){
                                      pulse[7]=array[0];
                                      pulse[6]=array[1];
                                      pulse[5]=array[2];
                                      pulse[4]=array[3];
                                      pulse[3]=array[4];
                                      pulse[2]=array[5];
                                      pulse[1]=array[6];
                                      pulse[0]=array[7];
                                      delay_ms(m);
                                      pulse[7]=array[1];
                                      pulse[6]=array[2];
                                      pulse[5]=array[3];
                                      pulse[4]=array[4];
                                      pulse[3]=array[5];
                                      pulse[2]=array[6];
                                      pulse[1]=array[7];
                                      pulse[0]=array[0];
                                      delay_ms(m);
                                      pulse[7]=array[2];
                                      pulse[6]=array[3];
                                      pulse[5]=array[4];
                                      pulse[4]=array[5];
                                      pulse[3]=array[6];
                                      pulse[2]=array[7];
                                      pulse[1]=array[0];
                                      pulse[0]=array[1];
                                      delay_ms(m);
                                      pulse[7]=array[3];
                                      pulse[6]=array[4];
                                      pulse[5]=array[5];
                                      pulse[4]=array[6];
                                      pulse[3]=array[7];
                                      pulse[2]=array[0];
                                      pulse[1]=array[1];
                                      pulse[0]=array[2];
                                      delay_ms(m);
                                      pulse[7]=array[4];
                                      pulse[6]=array[5];
                                      pulse[5]=array[6];
                                      pulse[4]=array[7];
                                      pulse[3]=array[0];
                                      pulse[2]=array[1];
                                      pulse[1]=array[2];
                                      pulse[0]=array[3];
                                      delay_ms(m);
                                      pulse[7]=array[5];
                                      pulse[6]=array[6];
                                      pulse[5]=array[7];
                                      pulse[4]=array[0];
                                      pulse[3]=array[1];
                                      pulse[2]=array[2];
                                      pulse[1]=array[3];
                                      pulse[0]=array[4];
                                      delay_ms(m);
                                      pulse[7]=array[6];
                                      pulse[6]=array[7];
                                      pulse[5]=array[0];
                                      pulse[4]=array[1];
                                      pulse[3]=array[2];
                                      pulse[2]=array[3];
                                      pulse[1]=array[4];
                                      pulse[0]=array[5];
                                      delay_ms(m);
                                      pulse[7]=array[7];
                                      pulse[6]=array[0];
                                      pulse[5]=array[1];
                                      pulse[4]=array[2];
                                      pulse[3]=array[3];
                                      pulse[2]=array[4];
                                      pulse[1]=array[5];
                                      pulse[0]=array[6];
                                      delay_ms(m);
                                        
                                  }
                              }
                              
                              void pwm8LR(unsigned char m,unsigned char n){
                                  unsigned char i;
                                  unsigned int array[]={255,180,150,120,90,60,30,0};
                                  for(i=0;i<=n;i++){
                                      pulse[7]=array[7];
                                      pulse[6]=array[6];
                                      pulse[5]=array[5];
                                      pulse[4]=array[4];
                                      pulse[3]=array[3];
                                      pulse[2]=array[2];
                                      pulse[1]=array[1];
                                      pulse[0]=array[0];
                                      delay_ms(m);
                                      pulse[7]=array[0];
                                      pulse[6]=array[7];
                                      pulse[5]=array[6];
                                      pulse[4]=array[5];
                                      pulse[3]=array[4];
                                      pulse[2]=array[3];
                                      pulse[1]=array[2];
                                      pulse[0]=array[1];
                                      delay_ms(m);
                                      pulse[7]=array[1];
                                      pulse[6]=array[0];
                                      pulse[5]=array[7];
                                      pulse[4]=array[6];
                                      pulse[3]=array[5];
                                      pulse[2]=array[4];
                                      pulse[1]=array[3];
                                      pulse[0]=array[2];
                                      delay_ms(m);
                                      pulse[7]=array[2];
                                      pulse[6]=array[1];
                                      pulse[5]=array[0];
                                      pulse[4]=array[7];
                                      pulse[3]=array[6];
                                      pulse[2]=array[5];
                                      pulse[1]=array[4];
                                      pulse[0]=array[3];
                                      delay_ms(m);
                                      pulse[7]=array[3];
                                      pulse[6]=array[2];
                                      pulse[5]=array[1];
                                      pulse[4]=array[0];
                                      pulse[3]=array[7];
                                      pulse[2]=array[6];
                                      pulse[1]=array[5];
                                      pulse[0]=array[4];
                                      delay_ms(m);
                                      pulse[7]=array[4];
                                      pulse[6]=array[3];
                                      pulse[5]=array[2];
                                      pulse[4]=array[1];
                                      pulse[3]=array[0];
                                      pulse[2]=array[7];
                                      pulse[1]=array[6];
                                      pulse[0]=array[5];
                                      delay_ms(m);
                                      pulse[7]=array[5];
                                      pulse[6]=array[4];
                                      pulse[5]=array[3];
                                      pulse[4]=array[2];
                                      pulse[3]=array[1];
                                      pulse[2]=array[0];
                                      pulse[1]=array[7];
                                      pulse[0]=array[6];
                                      delay_ms(m);
                                      pulse[7]=array[6];
                                      pulse[6]=array[5];
                                      pulse[5]=array[4];
                                      pulse[4]=array[3];
                                      pulse[3]=array[2];
                                      pulse[2]=array[1];
                                      pulse[1]=array[0];
                                      pulse[0]=array[7];
                                      delay_ms(m);
                                        
                                  }
                              }
                              
                              void pwm3LR(unsigned char m,unsigned char n){
                                  unsigned char i;
                                  unsigned int array[]={255,180,120,0};
                                  for(i=0;i<=n;i++){
                                      pulse[7]=array[3];
                                      pulse[6]=array[3];
                                      pulse[5]=array[3];
                                      pulse[4]=array[3];
                                      pulse[3]=array[3];
                                      pulse[2]=array[2];
                                      pulse[1]=array[1];
                                      pulse[0]=array[0];
                                      delay_ms(m);
                                      pulse[7]=array[0];
                                      pulse[6]=array[3];
                                      pulse[5]=array[3];
                                      pulse[4]=array[3];
                                      pulse[3]=array[3];
                                      pulse[2]=array[3];
                                      pulse[1]=array[2];
                                      pulse[0]=array[1];
                                      delay_ms(m);
                                      pulse[7]=array[1];
                                      pulse[6]=array[0];
                                      pulse[5]=array[3];
                                      pulse[4]=array[3];
                                      pulse[3]=array[3];
                                      pulse[2]=array[3];
                                      pulse[1]=array[3];
                                      pulse[0]=array[2];
                                      delay_ms(m);
                                      pulse[7]=array[2];
                                      pulse[6]=array[1];
                                      pulse[5]=array[0];
                                      pulse[4]=array[3];
                                      pulse[3]=array[3];
                                      pulse[2]=array[3];
                                      pulse[1]=array[3];
                                      pulse[0]=array[3];
                                      delay_ms(m);
                                      pulse[7]=array[3];
                                      pulse[6]=array[2];
                                      pulse[5]=array[1];
                                      pulse[4]=array[0];
                                      pulse[3]=array[3];
                                      pulse[2]=array[3];
                                      pulse[1]=array[3];
                                      pulse[0]=array[3];
                                      delay_ms(m);
                                      pulse[7]=array[3];
                                      pulse[6]=array[3];
                                      pulse[5]=array[2];
                                      pulse[4]=array[1];
                                      pulse[3]=array[0];
                                      pulse[2]=array[3];
                                      pulse[1]=array[3];
                                      pulse[0]=array[3];
                                      delay_ms(m);
                                      pulse[7]=array[3];
                                      pulse[6]=array[3];
                                      pulse[5]=array[3];
                                      pulse[4]=array[2];
                                      pulse[3]=array[1];
                                      pulse[2]=array[0];
                                      pulse[1]=array[3];
                                      pulse[0]=array[3];
                                      delay_ms(m);
                                      pulse[7]=array[3];
                                      pulse[6]=array[3];
                                      pulse[5]=array[3];
                                      pulse[4]=array[3];
                                      pulse[3]=array[2];
                                      pulse[2]=array[1];
                                      pulse[1]=array[0];
                                      pulse[0]=array[3];
                                      delay_ms(m);
                                        
                                  }
                              }
                              
                              void pwm3RL(unsigned char m,unsigned char n){
                                  unsigned char i;
                                  unsigned int array[]={255,180,120,30};
                                  for(i=0;i<=n;i++){
                                      pulse[7]=array[0];
                                      pulse[6]=array[1];
                                      pulse[5]=array[2];
                                      pulse[4]=array[3];
                                      pulse[3]=array[3];
                                      pulse[2]=array[3];
                                      pulse[1]=array[3];
                                      pulse[0]=array[3];
                                      delay_ms(m);
                                      pulse[7]=array[1];
                                      pulse[6]=array[2];
                                      pulse[5]=array[3];
                                      pulse[4]=array[3];
                                      pulse[3]=array[3];
                                      pulse[2]=array[3];
                                      pulse[1]=array[3];
                                      pulse[0]=array[0];
                                      delay_ms(m);
                                      pulse[7]=array[2];
                                      pulse[6]=array[3];
                                      pulse[5]=array[3];
                                      pulse[4]=array[3];
                                      pulse[3]=array[3];
                                      pulse[2]=array[3];
                                      pulse[1]=array[0];
                                      pulse[0]=array[1];
                                      delay_ms(m);
                                      pulse[7]=array[3];
                                      pulse[6]=array[3];
                                      pulse[5]=array[3];
                                      pulse[4]=array[3];
                                      pulse[3]=array[3];
                                      pulse[2]=array[0];
                                      pulse[1]=array[1];
                                      pulse[0]=array[2];
                                      delay_ms(m);
                                      pulse[7]=array[3];
                                      pulse[6]=array[3];
                                      pulse[5]=array[3];
                                      pulse[4]=array[3];
                                      pulse[3]=array[0];
                                      pulse[2]=array[1];
                                      pulse[1]=array[2];
                                      pulse[0]=array[3];
                                      delay_ms(m);
                                      pulse[7]=array[3];
                                      pulse[6]=array[3];
                                      pulse[5]=array[3];
                                      pulse[4]=array[0];
                                      pulse[3]=array[1];
                                      pulse[2]=array[2];
                                      pulse[1]=array[3];
                                      pulse[0]=array[3];
                                      delay_ms(m);
                                      pulse[7]=array[3];
                                      pulse[6]=array[3];
                                      pulse[5]=array[0];
                                      pulse[4]=array[1];
                                      pulse[3]=array[2];
                                      pulse[2]=array[3];
                                      pulse[1]=array[3];
                                      pulse[0]=array[3];
                                      delay_ms(m);
                                      pulse[7]=array[3];
                                      pulse[6]=array[0];
                                      pulse[5]=array[1];
                                      pulse[4]=array[2];
                                      pulse[3]=array[3];
                                      pulse[2]=array[3];
                                      pulse[1]=array[3];
                                      pulse[0]=array[3];
                                      delay_ms(m);
                                        
                                  }
                              }
                                
                              
                              void main(void)
                              {
                                  unsigned char i;
                              // Declare your local variables here
                              
                              // Input/Output Ports initialization
                              // Port B initialization
                              // Func7=In Func6=In Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out 
                              // State7=T State6=T State5=0 State4=0 State3=0 State2=0 State1=0 State0=0 
                              PORTB=0x00;
                              DDRB=0x3F;
                              
                              // Port C initialization
                              // Func6=In Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out 
                              // State6=T State5=0 State4=0 State3=0 State2=0 State1=0 State0=0 
                              PORTC=0x00;
                              DDRC=0x3F;
                              
                              // Port D initialization
                              // Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out 
                              // State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0 
                              PORTD=0x00;
                              DDRD=0xFF;
                              
                              // Timer/Counter 0 initialization
                              // Clock source: System Clock
                              // Clock value: 4000.000 kHz
                              TCCR0=0x01;
                              TCNT0=0x00;
                              xung=0;
                              
                              // Timer/Counter 1 initialization
                              // Clock source: System Clock
                              // Clock value: Timer1 Stopped
                              // Mode: Normal top=FFFFh
                              // OC1A output: Discon.
                              // OC1B output: Discon.
                              // Noise Canceler: Off
                              // Input Capture on Falling Edge
                              // Timer1 Overflow Interrupt: Off
                              // Input Capture Interrupt: Off
                              // Compare A Match Interrupt: Off
                              // Compare B Match Interrupt: Off
                              TCCR1A=0x00;
                              TCCR1B=0x00;
                              TCNT1H=0x00;
                              TCNT1L=0x00;
                              ICR1H=0x00;
                              ICR1L=0x00;
                              OCR1AH=0x00;
                              OCR1AL=0x00;
                              OCR1BH=0x00;
                              OCR1BL=0x00;
                              
                              // Timer/Counter 2 initialization
                              // Clock source: System Clock
                              // Clock value: Timer2 Stopped
                              // Mode: Normal top=FFh
                              // OC2 output: Disconnected
                              ASSR=0x00;
                              TCCR2=0x00;
                              TCNT2=0x00;
                              OCR2=0x00;
                              
                              // External Interrupt(s) initialization
                              // INT0: Off
                              // INT1: Off
                              MCUCR=0x00;
                              
                              // Timer(s)/Counter(s) Interrupt(s) initialization
                              TIMSK=0x01;
                              
                              // Analog Comparator initialization
                              // Analog Comparator: Off
                              // Analog Comparator Input Capture by Timer/Counter 1: Off
                              ACSR=0x80;
                              SFIOR=0x00;
                              
                              // Global enable interrupts
                              #asm("sei")
                              
                              PORTC=0XFF;
                              PORTB=0XFF;
                              while (1)
                                    {   
                                          TCNT0=0x00;
                                          xung=0;
                                          PORTB=0x00;
                                        for (i=0;i<=255;i++) {
                                              pulse[0]=pulse[1]=pulse[1]=pulse[3]=pulse[4]=pulse[5]=pulse[6]=pulse[7]=i;
                                              delay_ms(100);                
                                          } 
                                          PORTB=0xff;
                                          PORTC=0Xff;
                                          delay_ms(200);
                                          pwm4LR(100,8);
                                          pwm4RL(100,8);
                                          pulse[0]=pulse[1]=pulse[1]=pulse[3]=pulse[4]=pulse[5]=pulse[6]=pulse[7]=0;
                                          delay_ms(50);
                                          pulse[0]=pulse[1]=pulse[1]=pulse[3]=pulse[4]=pulse[5]=pulse[6]=pulse[7]=255;
                                          pwm8LR(100,10);
                                          pwm8RL(100,10);
                                          pwm44LR(100,10);
                                          pwm44RL(100,10);
                                          pwm88LR(100,10);
                                          pwm88RL(100,10);
                                          pulse[0]=pulse[1]=pulse[1]=pulse[3]=pulse[4]=pulse[5]=pulse[6]=pulse[7]=0;
                                          delay_ms(50);
                                          pulse[0]=pulse[1]=pulse[1]=pulse[3]=pulse[4]=pulse[5]=pulse[6]=pulse[7]=255;
                                          pwm3LR(100,10);
                                          pwm3RL(100,10);           
                                          
                                          
                                    };
                              }
                              Các bạn xem thử đoạn code của mình nhé! mình đã cho PortB ở mức cao rồi nhưng nó vẫn ở mức thấp khi chạy chuơng trình.
                              Khi mình bỏ các dòng lệnh liên quan tới mảng pulse[] thì PortB mới chạy bình thường.
                              Mình vừa thành công với những chương trình tương tụ như thế nhưng mảng mình khai báo chỉ có 4 phần tử thôi! Còn trong bài này mình dùng 8 phần tử.
                              Các bạn ơi! giúp mình nhé!

                              Comment


                              • các bác ơi cho em hỏi sao em cài codevision chạy trên win 7 ko đc nhỉ toàn báo lỗi thôi à

                                Comment

                                Về tác giả

                                Collapse

                                anhxtanh3087 Tìm hiểu thêm về anhxtanh3087

                                Bài viết mới nhất

                                Collapse

                                Đang tải...
                                X