Thông báo

Collapse
No announcement yet.

RC522 với 8051 qua UART

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

  • RC522 với 8051 qua UART

    Minh giao tiếp RC522 với 8051 qua UART
    mô phỏng thì ok còn thực tế thì ko như ý muốn
    mong ae giúp đỡ.

    #include <REGX51.H>
    unsigned char x;

    #define LCD_D4 P0_4
    #define LCD_D5 P0_5
    #define LCD_D6 P0_6
    #define LCD_D7 P0_7
    #define LINE_1 0x80
    #define LINE_2 0xC0
    #define CLEAR_LCD 0x01
    #define LCD_RS P2_0
    #define LCD_RW P2_1
    #define LCD_E P2_2
    unsigned char card_id[12];
    unsigned char current_byte = 0;
    //unsigned int a[12];

    //-----------------------------------------------
    void delay_ms(unsigned int Time)
    {
    unsigned int i,j,t;
    t=Time;
    for(i=0;i<t;i++)
    {
    for(j=0;j<125;j++);
    }
    }
    void delay_us(unsigned int Time)
    {
    unsigned int i,j;
    for(i=0;i<Time;i++)
    {
    for(j=0;j<2;j++);
    }
    }
    void delay(unsigned int count) //if count = 1000 --> delay 1s (milisecond)
    {
    int i,j;
    for(i=0;i<count;i++)
    for(j=0;j<1275;j++);
    }

    //-------------------------------------------------------------------------------------------
    void lcd1602_enable(void)
    {
    LCD_E=1;
    delay_us(3);
    LCD_E=0;
    delay_us(5);
    }
    // ---------------------------------
    // Ham Gui 4 Bit Du Lieu Ra LCD
    void lcd1602_send_4bit_data ( unsigned char cX )
    {
    LCD_D4 = cX & 0x01;
    LCD_D5 = (cX>>1)&1;
    LCD_D6 = (cX>>2)&1;
    LCD_D7 = (cX>>3)&1;
    }

    // ------------------------
    // Ham Gui 1 Lenh Cho LCD
    void lcd1602_send_command (unsigned char cX )
    {
    lcd1602_send_4bit_data ( cX >>4 ); // send 4 bit high
    lcd1602_enable() ;
    lcd1602_send_4bit_data ( cX ); // send 4 bit low
    lcd1602_enable() ;
    }

    // -------------------------
    // Ham Khoi Tao LCD
    void lcd1602_init ( void )
    {
    lcd1602_send_4bit_data ( 0x00 );
    delay_ms(200);

    LCD_RS=0;
    LCD_RW=0;
    LCD_E=0; // che do gui lenh

    lcd1602_send_4bit_data ( 0x03 ); // ket noi 8 bit
    lcd1602_enable() ;
    lcd1602_enable () ;
    lcd1602_enable () ;
    lcd1602_send_4bit_data ( 0x02 ); // ket noi 4 bit
    lcd1602_enable() ;

    lcd1602_send_command( 0x2C ); // giao thuc 4 bit, hien thi 2 hang, ki tu 5x8
    lcd1602_send_command( 0x80);
    lcd1602_send_command( 0x0C); // cho phep hien thi man hinh
    lcd1602_send_command( 0x06 ); // tang ID, khong dich khung hinh
    lcd1602_send_command( CLEAR_LCD ); // xoa toan bo khung hinh
    }

    // --------------------------------------------------
    // Ham Thiet Lap Vi Tri Con Tro
    void lcd1602_gotoxy(unsigned char x, unsigned char y)
    {
    unsigned char address;
    if(!y)
    address = (LINE_1+x);
    else
    address = (LINE_2+x);
    delay_ms(3);
    lcd1602_send_command(address);
    delay_ms(1);
    }

    // --------------------------------------------------
    // Ham Xoa Man Hinh LCD
    void lcd1602_clear(void)
    {
    lcd1602_send_command( CLEAR_LCD );
    //delay_us(300);
    }

    // --------------------------------------------------
    // Ham Gui 1 Ky Tu Len LCD
    void lcd1602_putchar ( unsigned int cX )
    {
    LCD_RS=1;
    lcd1602_send_command( cX );
    LCD_RS=0;
    }

    // --------------------------------------------------
    // Ham Gui 1 Chuoi Ky Tu Len LCD
    void lcd1602_puts(char *s)
    {
    while (*s)
    {
    lcd1602_putchar(*s);
    s++;
    }
    }
    //----------------------------------------------------------------------------------------------------

    void recieve() interrupt 4 // ngat truyen thông
    {
    card_id[current_byte]=SBUF;
    RI=0; // reset co RI
    current_byte++;
    }

    void main()
    {
    P2_7 = 0;
    TMOD=0x20; //Enable Timer 1
    TH1=0XFD;
    SCON=0x50;
    TR1=1;
    EA=1;
    ES=1;

    P1 = 0;
    lcd1602_init();
    lcd1602_clear();
    lcd1602_gotoxy(0,0);
    lcd1602_puts("Card ID:");
    lcd1602_gotoxy(0,1);

    while(1)
    {
    if(current_byte==12)
    {
    P2_7=1;
    lcd1602_gotoxy(0,1);
    for(x=0;x<12;x++)
    {
    lcd1602_putchar(card_id[x]);
    P1 = card_id[x];
    delay_ms(500);
    }
    break;
    }
    }
    }

    https://www.youtube.com/watch?v=Tb3YgvCbBxA
    |

Về tác giả

Collapse

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

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

Collapse

Đang tải...
X