Thông báo

Collapse
No announcement yet.

Cần giúp đỡ đếm xung encoder sử dụng ngắt ! bị mất xung !

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

  • Cần giúp đỡ đếm xung encoder sử dụng ngắt ! bị mất xung !

    Xin chào diễn đàn !
    Rất mong mọi người giúp đỡ mình. Mình không hiểu sao code rất đơn giản của mình bị mất xung encoder !
    Miinhf sử dụng ATmega32A 16Mhz, encoder 1000 xung/ vòng.
    Phase A mình cho vào ngắt ngoài 1 để đếm xung mỗi khi có ngắt, pha B mình nối với 1 chân bất kì portD.5, mỗi lần ngắt INT1 mình kiểm tra thanh ghi PINC.5 để xác định chiều quay.. và mình hiển thị số xung lên LCD thì thấy bị mất xung, chỉ được cỡ 860-900xung (quay thử cỡ 1 vòng).
    code của mình như sau:

    #include <mega32a.h>
    #include <delay.h>
    // I2C Bus functions
    #asm
    .equ __i2c_port=0x15 ;PORTC
    .equ __sda_bit=1
    .equ __scl_bit=0
    #endasm
    #include <i2c.h>
    // DS1307 Real Time Clock functions
    #include <ds1307.h>
    // Alphanumeric LCD Module functions
    #include <alcd.h>
    // Declare your global variables here
    unsigned char h,m,s;
    unsigned char date,month,year;
    unsigned int xung_thuan=0, xung_nghich=0;
    char flag_int0,t=0;
    //=====================================
    void lcd_ds1307() //doc du lieu tu ds1307 hien thi ra LCD
    {
    rtc_get_time(&h,&m,&s);
    rtc_get_date(&date,&month,&year);
    }
    //=============================== hien thi so nguyen duong ===========================================
    void lcd_putnum(unsigned long int z) //hien thi toi da 5 so (bien 4 byte , max 4,294,967,295 int 2 byte 65535)
    {
    unsigned long z1,z2,z3,z4,z5;
    z1=z/10000;
    z2=(z-10000*z1)/1000;
    z3=(z-10000*z1-1000*z2)/100;
    z4=(z-10000*z1-1000*z2-100*z3)/10;
    z5= z-10000*z1-1000*z2-100*z3-10*z4;
    if(z1>0)
    {
    lcd_putchar(z1+48);
    lcd_putchar(z2+48);
    lcd_putchar(z3+48);
    lcd_putchar(z4+48);
    lcd_putchar(z5+48);
    }
    else
    if (z2>0)
    {
    lcd_putchar(z2+48);
    lcd_putchar(z3+48);
    lcd_putchar(z4+48);
    lcd_putchar(z5+48);
    }
    else
    if (z3>0)
    {
    lcd_putchar(z3+48);
    lcd_putchar(z4+48);
    lcd_putchar(z5+48);
    }
    else
    if (z4>0)
    {
    lcd_putchar(z4+48);
    lcd_putchar(z5+48);
    }
    else
    {
    lcd_putchar(z5+48);
    };
    }
    //=====================================
    void lcd_hienthi()
    {
    lcd_ds1307();
    lcd_clear();
    lcd_gotoxy(0,0); lcd_putnum(xung_thuan);
    lcd_gotoxy(0,1); lcd_putnum(xung_nghich);
    lcd_gotoxy(0,2); lcd_putnum(flag_int0); lcd_gotoxy(10,2); lcd_putnum(t);
    lcd_gotoxy(0,3);
    lcd_putchar(h/10+48);
    lcd_putchar(h%10+48);
    lcd_putsf(":");
    lcd_putchar(m/10+48);
    lcd_putchar(m%10+48);
    lcd_putsf(":");
    lcd_putchar(s/10+48);
    lcd_putchar(s%10+48);
    lcd_gotoxy(10,3);
    lcd_putchar(date/10+48);
    lcd_putchar(date%10+48);
    lcd_putsf(":");
    lcd_putchar(month/10+48);
    lcd_putchar(month%10+48);
    lcd_putsf(":20");
    lcd_putchar(year/10+48);
    lcd_putchar(year%10+48);
    }
    //=====================================
    // External Interrupt 0 service routine
    interrupt [EXT_INT0] void ext_int0_isr(void)
    {
    // Place your code here
    flag_int0++;
    }
    //=====================================
    // External Interrupt 1 service routine
    interrupt [EXT_INT1] void ext_int1_isr(void)
    {
    // Place your code here
    if(PIND.5==0) //kiem tra chan PD.5
    {
    xung_thuan++;
    }
    else
    {
    xung_nghich++;
    };
    }
    //=====================================
    // Timer1 overflow interrupt service routine
    interrupt [TIM1_OVF] void timer1_ovf_isr(void)
    {
    // Place your code here
    t++; //tang 1s
    //reinstall timer
    TCNT1H=0xF0;
    TCNT1L=0x00;
    }
    //=====================================
    void main(void)
    {
    // Declare your local variables here

    // 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=In Func0=In
    // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
    PORTA=0x00;
    DDRA=0x00;

    // 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=P
    PORTB=0x01;
    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=P State6=T State5=T State4=T State3=T State2=P State1=T State0=T
    PORTD=0x84;
    DDRD=0x00;

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

    // Timer/Counter 1 initialization
    // Clock source: T1 pin Rising Edge
    // Mode: Normal top=0xFFFF
    // 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=0x07;
    TCNT1H=0xF0;
    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=0xFF
    // OC2 output: Disconnected
    ASSR=0x00;
    TCCR2=0x00;
    TCNT2=0x00;
    OCR2=0x00;

    // External Interrupt(s) initialization
    // INT0: On
    // INT0 Mode: Rising Edge
    // INT1: On
    // INT1 Mode: Rising Edge
    // INT2: Off
    GICR|=0xC0;
    MCUCR=0x0F;
    MCUCSR=0x00;
    GIFR=0xC0;

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

    // USART initialization
    // USART disabled
    UCSRB=0x00;

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

    // ADC initialization
    // ADC disabled
    ADCSRA=0x00;

    // SPI initialization
    // SPI disabled
    SPCR=0x00;

    // TWI initialization
    // TWI disabled
    TWCR=0x00;

    // I2C Bus initialization
    i2c_init();

    // DS1307 Real Time Clock initialization
    // Square wave output on pin SQW/OUT: On
    // Square wave frequency: 4096Hz
    rtc_init(1,1,0);

    // Alphanumeric LCD initialization
    // Connections specified in the
    // Project|Configure|C Compiler|Libraries|Alphanumeric LCD menu:
    // RS - PORTA Bit 0
    // RD - PORTA Bit 1
    // EN - PORTA Bit 2
    // D4 - PORTA Bit 4
    // D5 - PORTA Bit 5
    // D6 - PORTA Bit 6
    // D7 - PORTA Bit 7
    // Characters/line: 20
    lcd_init(20);
    rtc_set_time(10,10,20);
    rtc_set_date(30,04,15);
    #asm("sei")
    while (1)
    {
    // Place your code here
    if(t==1)
    {
    t=0;
    lcd_hienthi();
    };
    }
    }

  • #2
    Thanks các bác đã xem !
    Code chả có gì sai. do hệ thống cơ khí . Cảm ơn các bác ! Làm thật mới thấy thực tế và lý thuyết k hề đơn giản, rõ ràng code đúng cứ lo code sai, sử dụng cả timer cả ngắt để đếm đều cho kết quả giống nhau, nhưng hơi xoay xoay 1 chút encoder là thấy kết quả quay 10 vòng lệch nhau cỡ 500-1000 xung....

    Giờ lại nhờ các bác có kinh nghiệm chế tạo gắn encoder vào trục bánh xe như thế nào để đếm chính xác???

    Comment


    • #3
      Biết đâu encoder lởm ?

      Comment


      • #4
        Không phải encoder lởm, mình mua mới tính loại của hàn quốc mà ! haitiyong HB50 gì đấy? đã test lúc mua,. hôm nay tháo ra xem là do bị trượt giữa trục bánh với khớp mềm - đi kèm encoder lúc mua...
        Làm thật có thêm nhiều kinh nghiệm ...
        Thank các bác !

        Comment


        • #5
          Cái encoder quay rất nhẹ ... nối khớp mềm cũng bị trượt nữa ??? chắc không bắt vít ?
          Module RF chuyên dụng điều khiển, truyền dữ liệu, thiết kế đề tài, dự án điện tử - chuyển giao công nghệ... ĐT: 0904964977 - email: dientuqueduong@yahoo.com

          Comment


          • #6
            Giả sử encoder rung lắc tại vị trí này mcu vẫn đếm, nhưng thực tế encoder không quay,
            Code của bác không đọc hết tín hiệu của encoder nhé.

            .Click image for larger version

Name:	A.jpg
Views:	1942
Size:	17.1 KB
ID:	1652570

            Comment


            • #7
              mình đả làm nhiều cách , vđk này sẻ không đọc chuẩn encoder . mình đả chuyển qua xài ic số và đọc ngon . ví dụ cd4029
              linh kiện máy ép nhựa : điện , thủy lực secon-hand

              Comment

              Về tác giả

              Collapse

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

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

              Collapse

              Đang tải...
              X