Thông báo

Collapse
No announcement yet.

putchar ( idata) tronh USART bị reset port?

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

  • putchar ( idata) tronh USART bị reset port?

    Kính chào các bác.

    Em sử dụng USART để bật tắt LED.

    truyền ký tự từ PC-> AVR thì bật tắt ngon.

    Tuy nhiên vấp phải vấn đề là sau khi sử dụng hàm putchar() thì các PORT reset về 0x00 hết.

    Code bật tắt LED:

    switch(idata)
    {
    case 'A':
    PORTA.0=1;
    PORTA.1=0;
    break;

    case 'B':
    PORTA.0=0;
    PORTA.1=1;
    break;
    }
    Tuy nhiên nếu thêm dòng sau vào thì LED nháy 1 phát rồi tắt ngấm;

    switch(idata)
    {
    case 'A':
    PORTA.0=1;
    PORTA.1=0;
    break;

    case 'B':
    PORTA.0=0;
    PORTA.1=1;
    break;
    }

    delay_ms(500);
    putchar(ichar);/// -> Thủ phạm là dòng này.

    Kính mong các bác chỉ giúp.

  • #2
    Bác post lên cả đoạn code xem. có thể có phần khác chứ không phải do hàm putchar đâu.
    n
    ĐT: 0986 492 489

    Tham khảo:

    Comment


    • #3
      thanhks Hiếu. Mình post code đoạn code mình viết lên đây, xem hộ cái nhé,

      //


      char idata=0;
      // USART Receiver interrupt service routine
      interrupt [USART_RXC] void usart_rx_isr(void)
      {
      idata=UDR;
      }

      ///
      Đoạn code mặc định của CVA.. Bỏ đi cho đỡ dài
      //
      while (1)
      {
      // Place your code here

      if(idata)
      {
      switch(idata)
      {
      case 'A':
      PORTA.0=1;
      PORTA.1=0;
      break;

      case 'B':
      PORTA.0=0;
      PORTA.1=1;
      break;
      }

      delay_ms(200);

      putchar(idata); // -> thủ phạm gây tắt led

      delay_ms(200);


      idata=0;
      }// end if

      if(PINC.0==0)
      {
      PORTA.0=1;
      PORTA.1=0;

      putchar('0');// -> thủ phạm gây tắt led

      }
      if(PINC.1==0)
      {
      DDRA=0xFF;
      PORTA.0=0;
      PORTA.1=1;

      putchar('1');// -> thủ phạm gây tắt led
      }

      }// End while


      Nếu bỏ putchar; thì chân PORTA.0 và PORTA.1 luôn giữ giá trị mình set, khi cho hàm này vào thì Led sáng lên rồi tắt. Đo chân PORTA đều V=0;
      Last edited by lomo; 28-03-2010, 16:08.

      Comment


      • #4
        Do phần khởi tạo PORTA của bác. Phần bác đã xóa đi rồi.
        Đây là đoạn code em copy vào project mới giống hệt như của bác. Bác so sánh với code của bác đúng chưa nha.
        Em có kèm theo cả file mô phỏng bằng proteus đấy -> chạy ngon lành.
        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    : 3/28/2010
        Author  : minhhieu
        Company : miniavr
        Comments: 
        
        
        Chip type               : ATmega16
        Program type            : Application
        AVR Core Clock frequency: 8.000000 MHz
        Memory model            : Small
        External RAM size       : 0
        Data Stack size         : 256
        *****************************************************/
        
        #include <mega16.h>
        #include <delay.h>
        
        #ifndef UDRE
        #define UDRE 5
        #endif
        #define DATA_REGISTER_EMPTY (1<<UDRE);
        // Standard Input/Output functions
        #include <stdio.h>
        void init();
        char idata=0;
        // USART Receiver interrupt service routine
        interrupt [USART_RXC] void usart_rx_isr(void)
        {
            idata=UDR;  
            //putchar(idata);
        }
        
        
        // Declare your global variables here
        
        void main(void)
        {
            init();  
            while (1)
            {
                 if(idata)
                {
                    switch(idata)
                    {
                        case 'A':
                        PORTA.0=1;
                        PORTA.1=0;
                        break;
        
                        case 'B':
                        PORTA.0=0;
                        PORTA.1=1;
                        break;
                    }
        
                    delay_ms(200);
                    putchar(idata); // -> thu? pha.m gây ta('t led    
                    delay_ms(200);
                    idata=0;
                }// end if
        
                if(PINC.0==0)
                {
                    PORTA.0=1;
                    PORTA.1=0;
        
                    putchar('0');// -> thu? pha.m gây ta('t led
        
                }
                if(PINC.1==0)
                {
                    DDRA=0xFF;
                    PORTA.0=0;
                    PORTA.1=1;  
                    putchar('1');// -> thu? pha.m gây ta('t led
                }
            };
        }
         void init()
         {
          // Declare your local variables here
        
        // Input/Output Ports initialization
        // Port A initialization
        // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=Out Func0=Out 
        // State7=T State6=T State5=T State4=T State3=T State2=T 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=P State0=P 
        PORTC=0x03;
        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: 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
        // INT2: Off
        MCUCR=0x00;
        MCUCSR=0x00;
        
        // Timer(s)/Counter(s) Interrupt(s) initialization
        TIMSK=0x00;
        
        // USART initialization
        // Communication Parameters: 8 Data, 1 Stop, No Parity
        // USART Receiver: On
        // USART Transmitter: On
        // USART Mode: Asynchronous
        // USART Baud Rate: 9600
        UCSRA=0x00;
        UCSRB=0x98;
        UCSRC=0x86;
        UBRRH=0x00;
        UBRRL=0x33;
        
        // Analog Comparator initialization
        // Analog Comparator: Off
        // Analog Comparator Input Capture by Timer/Counter 1: Off
        ACSR=0x80;
        SFIOR=0x00;
        
        // Global enable interrupts
        #asm("sei")
         }
        Attached Files
        n
        ĐT: 0986 492 489

        Tham khảo:

        Comment


        • #5
          Đây là toàn bộ code.

          #include <mega16.h>

          #ifndef UDRE
          #define UDRE 5
          #endif

          #define DATA_REGISTER_EMPTY (1<<UDRE);



          char idata=0;
          // USART Receiver interrupt service routine
          interrupt [USART_RXC] void usart_rx_isr(void)
          {
          idata=UDR;
          }



          // Standard Input/Output functions
          #include <stdio.h>
          #include <delay.h>
          // Declare your global variables here

          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=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0
          PORTA=0x00;
          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=0xFF;
          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;

          // USART initialization
          // Communication Parameters: 8 Data, 1 Stop, No Parity
          // USART Receiver: On
          // USART Transmitter: On
          // USART Mode: Asynchronous
          // USART Baud Rate: 4800
          UCSRA=0x00;
          UCSRB=0xD8;
          UCSRC=0x86;
          UBRRH=0x00;
          UBRRL=0x0C;

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

          // Global enable interrupts
          #asm("sei")



          while (1)
          {
          // Place your code here

          if(idata)
          {
          switch(idata)
          {
          case 'A':
          PORTA.0=1;
          PORTA.1=0;
          break;

          case 'B':
          PORTA.0=0;
          PORTA.1=1;
          break;
          };

          delay_ms(200);


          idata=0;
          };

          if(PINC.0==0)
          {
          PORTA.0=1;
          PORTA.1=0;

          putchar('0');

          };
          if(PINC.1==0)
          {
          DDRA=0xFF;
          PORTA.0=0;
          PORTA.1=1;

          putchar('1');
          };
          };
          }
          Last edited by lomo; 29-03-2010, 10:33.

          Comment


          • #6
            bạn xem hộ mình chỗ này cái nhá :
            oldPortA=PORTA;
            while(1)
            {
            PORTA=oldPortA;
            ...............................
            }
            mình nghĩ cái này là nguyên nhân đấy

            Comment


            • #7
              Đúng vậy bác xem lại chỗ oldPortA; Không thấy bác khai báo ở đâu cả. Bác copy code vẫn không đầy đủ code. Bác dùng thử code của em xem chạy ko. Thạch anh là 8M.
              n
              ĐT: 0986 492 489

              Tham khảo:

              Comment


              • #8
                Nguyên văn bởi enter88 Xem bài viết
                bạn xem hộ mình chỗ này cái nhá :
                oldPortA=PORTA;
                while(1)
                {
                PORTA=oldPortA;
                ...............................
                }
                mình nghĩ cái này là nguyên nhân đấy
                Sorry tôi quên không xóa dòng có cái oldPortA. cái này để check xem thế nào thôi. Tôi đã post lại toàn bộ code lên rồi đấy. Bác xem thế nào.

                Comment


                • #9
                  Nguyên văn bởi minhhieu Xem bài viết
                  Đúng vậy bác xem lại chỗ oldPortA; Không thấy bác khai báo ở đâu cả. Bác copy code vẫn không đầy đủ code. Bác dùng thử code của em xem chạy ko. Thạch anh là 8M.
                  cái oldPortA để test thôi.
                  Cái của hiếu send cho tớ chạy với Protues thì rất OK, còn chưa thử trên mạch.
                  Hì, điện tử tưởng đơn giản mà khoai phết nhẩy.

                  Comment


                  • #10
                    Copy code của Hiếu đè lên code của mình thì chạy lại OK, để tớ soi các code xem có chỗ nào khác nhau không.

                    Thanks các bác.

                    Comment


                    • #11
                      Tìm ra lỗi rồi,

                      Chào các bác,

                      Cuối cùng thì mạch đã chạy trên Test Board, LED bật tắt theo quy tắc.

                      SO code của minhhieu thì thấy chỗ này khác nhau:

                      // USART initialization
                      // Communication Parameters: 8 Data, 1 Stop, No Parity
                      // USART Receiver: On
                      // USART Transmitter: On
                      // USART Mode: Asynchronous
                      // USART Baud Rate: 9600
                      UCSRA=0x00;
                      UCSRB=0x98;
                      UCSRC=0x86;
                      UBRRH=0x00;
                      UBRRL=0x33;

                      còn của mình là:


                      UCSRA=0x00;
                      UCSRB=0xD8;
                      UCSRC=0x86;
                      UBRRH=0x00;
                      UBRRL=0x33;


                      Hì.
                      Last edited by lomo; 29-03-2010, 22:29.

                      Comment


                      • #12
                        Vâng em đoán chỗ khai báo phần cứng của bác mà. Phần quan trọng nhất là chỗ khai báo thì code bác up lên lại xóa mất. hiiiiiiii, chúc bác sớm thành công!!!!!
                        n
                        ĐT: 0986 492 489

                        Tham khảo:

                        Comment

                        Về tác giả

                        Collapse

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

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

                        Collapse

                        Đang tải...
                        X