of 21 /21
1 BÙI QUỐC BẢO LẬP TRÌNH HỆ THỐNG NHÚNG BM Kỹ Thuật ðiện Tử - ðH Bách Khoa TP.HCM 2 Vi ñiều khiển 8051

slide 8051

Embed Size (px)

Text of slide 8051

  • 1BI QUC BO

    LP TRNH H THNG NHNG

    BM K Thut in T - H Bch Khoa TP.HCM 2

    Vi iu khin 8051

  • 2BM K Thut in T - H Bch Khoa TP.HCM 3

    Vi iu khin 8051

    BM K Thut in T - H Bch Khoa TP.HCM 4

    Phn mm

    KEIL

    PROTEUS

  • 3BM K Thut in T - H Bch Khoa TP.HCM 5

    Cu trc b nh ca 8051

    BM K Thut in T - H Bch Khoa TP.HCM 6

  • 4BM K Thut in T - H Bch Khoa TP.HCM 7

    Cu trc b nh ca 8051

    8051 c 3 vng nh ring bit (b nhchng trnh, b nh d liu trong, bnh d liu ngoi). Mi vng nh ctruy cp bng cc cu lnh khc nhau.

    Khi khai bo cc bin, ta phi xc nhvng nh cho cc bin .

    BM K Thut in T - H Bch Khoa TP.HCM 8

    Cu trc b nh ca ARM7 LPC2000

    Cc vi x l c kin trc Von-Newman (VD: ARM) dngchung 1 khng gian nh chob nh chng trnh v dliu.

  • 5BM K Thut in T - H Bch Khoa TP.HCM 9

    Khng gian b nh chng trnh

    c gi l code space Cha chng trnh v cc hng s VD: khai bo hng s cha bnh phng

    cc s t 0 n 3:code char square[4] = {0,1,4,9};

    Lnh assembly: MOVC A, @A+DPTR

    BM K Thut in T - H Bch Khoa TP.HCM 10

    Khng gian b nh d liu ni

    Vng nh ni truy xut trc tip (00H-7FH) c khiu l data Lnh assembly: MOV A, 7FH

    Vng nh ni truy xut gin tip (00H-FFH), c khiu idata Lnh assembly:

    MOV R0, #80H MOV A, @R0

    Hai vng nh ny dng cha cc bin thngdng v bin tm

    VD: khai bo mt bin integer: idata unsigned int temp;

  • 6BM K Thut in T - H Bch Khoa TP.HCM 11

    Khng gian b nh d liu ngoi

    c gi l external data space (xdata)

    Lnh assembly: MOV DPTR, #2000H

    MOVX A,@DPTR

    Cha cc bin t dng, cc khi d liu lnhoc ngoi vi theo kiu memory-mapped

    VD: xdata unsigned int[1000] ADC_value;

    BM K Thut in T - H Bch Khoa TP.HCM 12

    VD:

    Vit hm c prototype nh sau:

    unsigned int checksum (unsigned int

    start, unsigned int end)

    tnh checksum ca vng nhch*+ng trnh t a ch start n end (code)

  • 7BM K Thut in T - H Bch Khoa TP.HCM 13

    unsigned int checksum (unsigned int start, unsigned int end) { unsigned int code *codeptr, *codeend; unsigned int data checksum = 0;

    codeptr = (unsigned int code *)start; codeend = (unsigned int code *)end;

    while (codeptr

  • 8BM K Thut in T - H Bch Khoa TP.HCM 15

    Ki float v double

    Trong C51, kiu float v double gingnhau, gm 4 byte theo chun EEE-754

    BM K Thut in T - H Bch Khoa TP.HCM 16

    Cc kiu c bit ca C51

    0 to 65,535 216sf16

    0 to 25518sfr

    0 or 11sbit

    0 or 11bit

    RangeBytesBitsType

  • 9BM K Thut in T - H Bch Khoa TP.HCM 17

    Thanh ghi

    Keil cho php khai bo cc thanh ghibng t kha sfr

    VD: sfr P0 = 0x80; //khai bo thanh ghi P0

    //a ch 80H

    P0 = 0x0F;

    BM K Thut in T - H Bch Khoa TP.HCM 18

    Tc ng n bit trong SFR

    8051 c cc lnh tc ng n bit. tc ng n bit trong thanh ghi ta dngkhai bo sbit

    sbit name = sfr-name ^ bit-position;

    sbit name = sfr-address ^ bit-position; sbit name = sbit-address;

  • 10

    BM K Thut in T - H Bch Khoa TP.HCM 19

    Tc ng n bit trong SFR

    sfr PSW = 0xD0;

    sbit CY = PSW^7;

    sbit CY = 0xD0^7;

    sbit CY = 0xD7;

    BM K Thut in T - H Bch Khoa TP.HCM 20

    Tc ng n bit trong SFR

    sfr char P0 = 0x80; sbit SW = P0^0; void main(void) { while(1) { SW = !SW; } }

    a ch ca cc bin sfr, sfr16, sbit phi nm bnngoi hm.

  • 11

    BM K Thut in T - H Bch Khoa TP.HCM 21

    Vng nh c nh a ch bit

    8051 c vng nh c nh a ch bit. tc ng n vng nh ny ta dng khai bobdata

    bdata char temp _at_ 0x2F;

    bit flag = temp ^0;

    void main(void)

    {

    flag = 1;

    }

    BM K Thut in T - H Bch Khoa TP.HCM 22

    Lu :

    Cc khai bo code, idata, xdata, bdata, sfr, sbit l c bit cho trng hp lptrnh 8051.

  • 12

    BM K Thut in T - H Bch Khoa TP.HCM 23

    VD:

    Vit chng trnh cho 8051 to hm nhsau:

    P1.0 = P1.1 AND P1.2

    BM K Thut in T - H Bch Khoa TP.HCM 24

    Cu trc vng lp v tn (endless loop)

    void main(void)

    {Task1_init();

    Task2_init();

    While(1) //m/t vng l2p v t4n th5c hin task1 v task2

    {

    Task1();

    Task2();

    }

    }

  • 13

    BM K Thut in T - H Bch Khoa TP.HCM 25

    Endless loop

    u im: n gin.

    D thc hin, sa li v bo tr

    Khng cn s dng nhiu ti nguyn hthng (VD: timer)

    D dng thay i chy vi cc vi x lkhc (portable)

    BM K Thut in T - H Bch Khoa TP.HCM 26

    Endless loop

    Nhc im: Khng ph hp vi cc ng dng i hi

    thi gian chnh xc (VD: ng h s).

    Tiu tn nng lng v CPU hot ng ch bnh thng trong ton b thi gian

  • 14

    BM K Thut in T - H Bch Khoa TP.HCM 27

    Look-up table

    unsigned char code square[]={0,1,4,9,16,25,36,49,64,81};

    unsigned char cal_square(unsigned char num){

    return square[num];}

    Assembly:

    MOV A,R7MOV DPTR, #SQUAREMOVC A,@A+DPTRMOV R7,A

    BM K Thut in T - H Bch Khoa TP.HCM 28

  • 15

    BM K Thut in T - H Bch Khoa TP.HCM 29

    Question

    a ch ca LED 7 on l bao nhiu?

    lm LED hin th s 8, ta dng lnhcho 8051 nh th no? (dng assembly v C)

    Xy dng bng m 7 on cho LED (chocc s ln hn 9 tt tt c on LED).

    Vit chng trnh C c gi tr t 4 SW v hin th ln LCD. (Dng bng tra ly m 7 on).

    BM K Thut in T - H Bch Khoa TP.HCM 30

    Giao tip switch

    Switch l 1 trong nhng phng phpgiao tip chnh ca h thng nhng

    Cc contact c kh s b rung khi nghoc m.

  • 16

    BM K Thut in T - H Bch Khoa TP.HCM 31

    Chng rung

    Not pressedPress

    detected

    delay

    Read againConfirmed

    Press detected

    delay

    Read againJust a bounce

    BM K Thut in T - H Bch Khoa TP.HCM 32

    Giao tip switch

    Vi 8051, trc khi cswitch ta phighi 1 ra bcht ca chnport ang giaotip switch

  • 17

    BM K Thut in T - H Bch Khoa TP.HCM 33

    Vit chng trnh C c gi tr switch (cchng rung) v xut ra LED.

    BM K Thut in T - H Bch Khoa TP.HCM 34

    Giao tip keypad

  • 18

    BM K Thut in T - H Bch Khoa TP.HCM 35

    Giao tip keypad

    Chng trnh getkey():

    Gi tr tr v: m phm (0-15) hoc 255 nu khng c phm nhn.

    V lu

    Vit code C

    BM K Thut in T - H Bch Khoa TP.HCM 36

    Giao tip keypad

    Chng trnh getkey_Debound()

    Gi tr tr v: m phm (0-15) hoc 255 nu khng c phm nhn.

    Cch thc hin:

    Nu trong 20 ln lin tip gi chngtrnh getkey() tr v cng 1 gi tr, th l gi tr tr v ca getkey_Debound().

  • 19

    BM K Thut in T - H Bch Khoa TP.HCM 37

    Giao tip keypad

    V lu cho chng trnhgetkey_Debound()

    Vit code C

    BM K Thut in T - H Bch Khoa TP.HCM 38

  • 20

    BM K Thut in T - H Bch Khoa TP.HCM 39

    Giao tip keypad

    Vit chng trnh getCalKey_Debound():

    Gi tr tr v l 1 trong cc k t:

    0, 1, 2, 3, 4, 5, 6, 7, 8, 9, C, =, +, -, x, / hoc N nu khng cphm c nhn

    BM K Thut in T - H Bch Khoa TP.HCM 40

    Hm printf()

    Hm printf() rt c ch trong qu trnh vit vdebug chng trnh.

    Mc nh, hm printf xut 1 chui k t raserial port ca 8051

    Cu trc:

    int printf ( const char * format, ... );

    Chi tit tham kho ti:

    http://www.cplusplus.com/reference/clibrary/cstdio/printf/

  • 21

    BM K Thut in T - H Bch Khoa TP.HCM 41

    Th vin ca Keil C

    Keil C h tr rt nhiu th vin nh thvin xut nhp (stdio), ton (math),

    Cc th vin v hm ca Keil C c ththam kho ti:

    http://www.keil.com/support/man/docs/c51/c51_library.htm