Introduction Week1 4slidePerPage

download Introduction Week1 4slidePerPage

of 17

Transcript of Introduction Week1 4slidePerPage

  • 7/30/2019 Introduction Week1 4slidePerPage

    1/17

    B MN KTHUT IN T-H BK TP.HCM

    BI QUC BO

    LP TRNH H THNGNHNG

    Mc tiu mn hc

    T mnh thit k phn mm cho mt h thngnhng dng 1 hay nhiu CPU da trn vi iukhin (8051, ARM) da trn mt phn cng csn.

    Phn mm c thit k c cu trc, da trnngn ng cp cao (C) hoc s dng c hpng v C.

    Nm c l thuyt c bn v h iu hnhnhng; s dng c cc h iu hnh nhngc bn (RTX, uCOS).

    Ti liu tham kho

    C and the 8051, 3rd Edition, ThomasSchultz

    Embedded C, Michael J Pont

    nh gi kt qu

    Gia k: 20%

    Bi tp ln: 30%

    Cui k: 50%

  • 7/30/2019 Introduction Week1 4slidePerPage

    2/17

    Khi nim v h thng nhng

    H thng nhng l s kt hp gia phncng my tnh v phn mm, c thitk thc hin mt chc nng chuynbit. Ngi s dng mt h thng nhngkhng cn bit rng h thng c mtmy tnh bn trong

    VD: remote TV, l vi sng,

    Vi x l trong h thng nhng

    H 8086 PowerPC MIPS H 8051 PIC Ty thuc vo ng dng v gi thnh, ngi

    thit k quyt nh loi vi x l dng trong hthng nhng.

    Vi x l dng trong mn hc ny

    8051

    ARM

    Ngn ng lp trnh

    Assembler

    C

  • 7/30/2019 Introduction Week1 4slidePerPage

    3/17

    L do s dng ngn ng C

    C cho php lp trnh c cu trc.

    Ngn ng C cho php d dng truy cp ncu trc phn cng (v d thng qua con tr)

    C c dng rt ph bin.

    Hu ht cc h vi x l u c h tr trnhbin dch C.

    Rt nhiu ta nguyn tham kho (sch, v d,website ) v lp trnh C.

    L do dng hp ng (Assembler)

    Cho php vit nhng chng trnh ckch thc nh nht v chy vi tc nhanh nht.

    Thch hp cho nhng ng dng c yucu nghim ngt v thi gian.

    C operator

    = assignment statement @ address of ? selection < less than > greater than ! logical not (true to false, false to true) ~ 1's complement + addition - subtraction * multiply or pointer reference / divide % modulo, division remainder

    | logical or & logical and, or address of ^ logical exclusive or . used to access parts of a structure

    C operator

    == equal to comparison = greater than or equal to != not equal to > shift right ++ increment -- decrement && boolean and || boolean or

  • 7/30/2019 Introduction Week1 4slidePerPage

    4/17

    += add value to -= subtract value to *= multiply value to /= divide value to |= or value to &= and value to ^= exclusive or value to = shift value right %= modulo divide value to -> pointer to a structure

    Comments (ch thch)

    // This is a comment line

    /*

    This is a comment block

    */

    Preprocessor Directives (ch dntin x l)

    c x l u tin trong qu trnh bindch

    Bt u bng t kha #

    VD:

    #include LCD.h"

    #define LCD *(unsigned char volatile *)(0x1003)

    Key word (T kha)

    asm Insert assembly code auto Specifies a variable as automatic (created

    on the stack) break Causes the program control structure to finish case One possibility within a switch statement char 8 bit integer const Defines parameter as constant in ROM continue Causes the program to go to beginning of loop default Used in switch statement for all other cases do Used for creating program loops double Specifies variable as double precision floating point else Alternative part of a conditional

    extern Defined in another module float Specifies variable as single p recision floating point

  • 7/30/2019 Introduction Week1 4slidePerPage

    5/17

    Key word (T kha)

    for Used for creating program loops goto Causes program to jump to specified

    location if Conditional control structure int 16 bit integer long 32 bit integer register store the variable onto the CPU register if

    space on the register is availablereturn Leave function

    short 16 bit integer

    Key word (T kha)

    signed Specifies variable as signed (default) sizeof Built-in function returns the size of an

    object static Stored permanently in memory, accessed

    locally struct Used for creating data structures switch Complex conditional control structure typedef Used to create new data types unsigned Always greater than or equal to zero void Used in parameter list to mean no parameter volatile Can change implicitly while Used for creating program loops

    Du chm phy (semicolons)

    Chm dt cu lnh

    VD: For (i=0;i

  • 7/30/2019 Introduction Week1 4slidePerPage

    6/17

    Du phy (Commas)

    Dng ngn cch cc phn t.

    VD:

    unsigned short beginTime, endTime, elapsedTime;

    short add(short x, short y) ;

    Du nhy (Apostrophes)

    Dng xc nh k t

    VD: mych='a' ;

    Du nhy kp (Quotation marks )

    Dng xc nh chui (string)

    VD:

    unsigned char Name[] = embedded";

    Du ngoc nhn (braces)

    Dng bt u v kt thc mt khi cu lnh

    VD:

    For (i=0;i

  • 7/30/2019 Introduction Week1 4slidePerPage

    7/17

    Du ngoc vung (Brackets )

    Dng ch kch thc ca mng(array) v ch s

    VD:short fifo[100];

    I = fifo[0];

    Du ngoc trn (Parentheses )

    Bao quanh danh sch tham s

    VD:add(x,y);

    Xc nh mc u tin thc thi cacc biu thc

    VD:

    X = (17+3)/2 ;

    Bin tnh (Static)

    Bin tnh l bin m gi tr ca n cgi trong sut qu trnh chng trnhchy, nhng ch truy cp c bn trongchng trnh m n c nh ngha

    Bin tnh

    char sumIt(void) { static char sum = 0; sum = sum + 1; return sum; } void main(void) { char i; char result;

    for(i=0;i

  • 7/30/2019 Introduction Week1 4slidePerPage

    8/17

    T kha volatile

    T kha volatile m t 1 bin m gi trc th thay i khng on trc c.

    Bin volatile c dng m t: Cc ngoi vi c thit k theo kiu

    memory-mapped Bin ton cc m b thay i gi tr trong

    trnh phc v ngt Bin ton cc c truy cp bi nhiu tc

    v trong cc ng dng a tc v

    T kha volatile

    void main(void) { unsigned char xdata *p = (char xdata *) 0x8000; while (*p == 0); }

    void main(void) { volatile unsigned char xdata *p = (char xdata *) 0x8000; while (*p == 0); }

    T kha extern

    Dng ch bin c nh ngha trong1 module khc.

    VD:

    Trong module LCD.h, khai bo bin char LCD_value;

    Trong module main.h extern char LCD_value;

    Bin c khi to trc

    short Temperature = -55;

    const unsigned short Steps[4] = {10, 9,6, 5};

    char Name[4] = "Jon";

    char Name[6] = "Jon";

    char Name[] = "Jon";

    char *NamePt = "Jon";

  • 7/30/2019 Introduction Week1 4slidePerPage

    9/17

    Con tr (pointer)

    Con tr l 1 bin cha mt a ch

    Gi tr ca con tr c th thay i c

    Question: Khai bo bin a: int a;

    a ch ca bin a: &a c phi l con tr haykhng?

    Con tr

    int *p;

    p = 0x8000;

    int *p = (int *)0x8000;

    int k;

    p = &k;

    #define SW *(unsigned char volatile *)(0x2000)

    Php ton vi con tr

    Php cng: Con tr lun ch vo a ch u ca mt i

    tng (object). Cng 1 vo con tr lm nch n i tng tip theo.

    Php so snh: Khi so snh 2 con tr, gi tr chng ang

    mang c coi nh s khng du

    0x2000

    0x2001

    0x2002

    0x2003

    int *p;

    int *k;

    p = 0x2000;

    *p = 1;

    k = p+1;

    *k = *p+1;

    0x01

    0x00

    0x02

    0x00

    p

    k

  • 7/30/2019 Introduction Week1 4slidePerPage

    10/17

    Mng v chui (array and string)

    Mng l tp hp cc bin ging nhau c cngtn gi.

    Cc phn t ca mng c xc nh bngch s (bt u t 0).

    Kch thc ca mng l c nh.

    VD:

    int a[4] = {1,2,3,4};

    int b = a[0];

    Mng v chui (array and string)

    Chui tng t mng, ngoi tr:

    Chui c th c s lng phn t thay.

    Cc phn t ca chui l k t ASCII ktthc bng gi tr 0.

    Chui c khai bo nh 1 con tr kiuchar char * mystring = embedded;

    Kiu cu trc (structure)

    Mt structure l tp hp ca nhiu bin. Ccbin trong struct c th khc kiu nhau.

    struct theport

    {int mode; // 0 for I/O, 1 for in only -1 for out onlyunsigned char volatile *addr; // pointer to its addressunsigned char volatile *ddr; // pointer to its direction reg

    };

    struct theport PortA, PortB, PortC;

    Structure

    struct theport

    {int mode ; // 0 for I/O, 1 for in only -1 for out only

    unsigned char volatile *addr ; // pointer to its address

    unsigned char volatile *ddr ; // pointer to its direction//reg

    } PortA, PortB, PortC;

  • 7/30/2019 Introduction Week1 4slidePerPage

    11/17

    struct theport

    {int mode; // 0 for I/O, 1 for in only -1 for out onlyunsigned char volatile *addr; // pointer to its addressunsigned char volatile *ddr; // pointer to its direction

    reg

    };

    typedef struct theport port;

    port PortA, PortB, PortC;

    Struct

    typedef struct

    {int mode ; // 0 for I/O, 1 for in only -1 for out onlyunsigned char volatile *addr ; // pointer to its addressunsigned char volatile *ddr ; // pointer to its direction

    //reg

    } port;

    port PortA, PortB, PortC;

    Truy cp phn t con ca struct

    PortC.mode=0;

    PortC.addr=(unsigned char volatile *)(0x1003);PortC.ddr=(unsigned char volatile *)(0x1007);(*PortC.ddr)=0;

    (*PortB.addr)=(*PortC.addr);

    Truy cp struct thng qua pointer

    port PORTA;

    port *p;

    p = &PORTA; p->mode=0; (dng du -> truy cp phn

    t con ca 1 con tr kiu struct)

  • 7/30/2019 Introduction Week1 4slidePerPage

    12/17

    Khi to struct

    port PortE={1,(unsigned char volatile *)(0x100A),(unsigned char volatile *)(0)};

    Function (Hm, chng trnh con)

    Mt chng trnh ln thng c chiathnh nhiu khi (module)

    Module l 1 tc v nhn d liu vo(input), x l v xut ra kt qu (output).

    Cc module c to ra nh l cc hm(function).

    Function

    Mt module (Ngun: EmbeddedProg/Developing Embedded Software in C)

    Note:Hn ch truy cp n bin ton cc v I/O trong 1 function

    int FtoC(int TempF){int TempC;TempC=(5*(TempF-32))/9; // conversion

    return TempC;}

    M t hm (function declaration)

    M t hm cho ta bit tn hm, kiu ca cctham s v kiu kt qu tr v.

    // declaration input outputvoid Ritual(void); // none nonechar InChar(void); // none 8-bit void OutChar(char le tte r); // 8-bit noneshort InSDec(void); // none 16-bit void OutSDec(short i); // 16-bit nonechar Max(char in1,char in2); // two 8-bit 8-bit int EMax(int i n1,int in2); // two 16-bit 16-bit void OutString(char* mystring); // pointer to 8-bit nonechar *alloc(int size); // 16-bit pointer to

    8-bitint Exec(void(*fnctPt)(void)); // function pointer 16-bit

  • 7/30/2019 Introduction Week1 4slidePerPage

    13/17

    nh ngha hm (function definition)

    Mt nh ngha ca hm l cch m hm thc thi.

    type Name(parameter list){CompoundStatement

    };

    int FtoC(int TempF){

    int TempC;TempC=(5*(TempF-32))/9; // conversion

    return TempC;}

    M t hm

    Mt module thng bao gm 2 file:

    Header file (dng cha cc m t hm).

    Source file (dng cha cc nh nghahm)

    Extern function

    Khi ta cn khai bo mt hm c nhngha mt module khc, ta dng tkha extern

    extern void InitSCI(void);extern char InChar(void);extern void OutChar(char character);

    Truyn tham s (Argumentspassing)

    C hai phng php truyn tham s chochng trnh con: Truyn tham chiu (call by reference)

    Truyn gi tr (call by value)

  • 7/30/2019 Introduction Week1 4slidePerPage

    14/17

    Truyn tham chiu (call byreference) Tham chiu (con tr) n tham s c a

    vo chng trnh con. Cc tnh ton trong chng trnh con s nh

    hng n bin c c truyn vo. int increment(int*num) {

    *num++; return *num;

    } Void main(void) { int number = 1; int temp; temp = increment(& number ); }

    Truyn gi tr (call by value)

    Gi tr ca bin s c sao chp v truynvo chng trnh con.

    Cc php tnh trn tham s trong chng trnhcon khng nh hng bin ban u. int increment(int num) {

    num++; return num;

    } Void main(void) { int number = 1; int temp; temp = increment(number ); }

    Struct lm tham s ca chngtrnh con

    struct theport {

    int mode; // 0 for I/O, 1 for in only -1 for out onlyunsigned char volatile *addr; // pointer to its addressunsigned char volatile *ddr; // pointer to its direction reg

    }; typedef struct theport port;

    unsigned char Input(port thePort) {

    return (*thePort.addr); }

    unsigned char Input(port *thePort)

    { return (*thePort->addr); } Function no l tt hn?

    Con tr hm (function pointer)

    Con trhm lcon trch n1 hm.

    int (*fp)(int); // pointer to a function with input and output

    int fun1(int input) {

    return(input+1); // this adds 1};int fun2(int input)

    {return(input+2); // this adds 2

    };void Setp(void)

    { int data;

    fp=&fun1; // fp points to fun1data=(*fp)(5); // data=fun1(5);

    fp=&fun2; // fp points to fun2data=(*fp)(5); // data=fun2(5);};

  • 7/30/2019 Introduction Week1 4slidePerPage

    15/17

    Question:

    Gii thch s khc nhau gia: int (*fp)(int);

    int *fp(int);

    Con tr hm l tham s ca 1 hmkhc

    int fun1(int input){return(input+1); // this adds 1

    };

    int fun2(int input){return(input+2); // this adds 2

    };

    int execute(int (*fp)(int)) { int data;

    data=(*fp)(5); // data=fun1(5);return (data);

    };

    void main(void) { int result;

    result=execute(&fun1); // result=fun1(5);result=execute(&fun2); // result=fun2(5);

    };

    Hm callback

    Hm callback l hm c gi khi c 1s kin xy ra.

    MyButton

    void create_button( int x, int y, const char *text, function callback_func );

    MyButton

    click

    ButtonClickedEvent callback_func

    V d v hm callback

    void cbfunc(){

    printf("called");}int main () {/* function pointer */void (*callback)(void);

    /* point to your callback function */callback=(void *)cbfunc;

    /* perform callback */callback();

    return 0;}

  • 7/30/2019 Introduction Week1 4slidePerPage

    16/17

    Truyn tham s cho hm callback

    typedef struct _myst { int a; char b[10]; }myst; void cbfunc(myst *mt) { fprintf(stdout,"called %d %s.",mt->a,mt->b); } int main() { /* func pointer */ void (*callback)(void *); //param myst m; m.a=10; strcpy(m.b,"123"); /* point to callback function */ callback = (void*)cbfunc; /* perform callback and pass in the param */ callback(&m); return 0; }

    Ch dn tin bin dch(Preprocessor Directives )

    #define size 100

    int data[size];

    #define LCD *(unsigned char *)0x2000

    Ch dn tin bin dch(Preprocessor Directives )

    #ifdef A

    #endif

    #ifndef B

    #endif

    Ch dn tin bin dch(Preprocessor Directives )

    #define Debug 1#define LCD *(unsigned char *)0x2000

    void LCD_write(char data) {

    #ifdef Debugprintf(entering LCD_write);

    #endif

    LCD = data;

    #ifdef Debugprintf(exitting LCD_write);

    #endif return; }

  • 7/30/2019 Introduction Week1 4slidePerPage

    17/17

    Debug macro

    #ifdef PRGDEBUG

    #define DBG(x) printf(x)

    #else

    #define DBG(x) /* nothing */

    #endif

    Ch dn tin bin dch(Preprocessor Directives )

    #include "Filename"

    #include