11.1 pic cd1.amobbs.com/bbs_upload782111/files_47/ourdev_689422b7xoxt.pdf · pic microchip pic c...

Download 11.1 PIC Cd1.amobbs.com/bbs_upload782111/files_47/ourdev_689422B7XOXT.pdf · PIC Microchip PIC C PIC C Hitech CCS IAR Bytecraft Hitech PICC PIC Hitech PIC PICC-Lite PIC ... struct

If you can't read please download the document

Upload: votu

Post on 06-Feb-2018

265 views

Category:

Documents


4 download

TRANSCRIPT

  • 2004-4-7 1 of 26

    PIC C

    11.1 PIC C

    CC

    PIC C

    C PC PCPC

    C

    CPIC

    C

    PIC Microchip PICC PIC C

    Hitech CCS IAR Bytecraft HitechPICC PIC

    Hitech PICPICC-Lite PIC

    PIC16F84 PIC16F877 PIC16F628 FlashPICC-Lite

    PIC C

    PIC CHitech-PICC www.htsoft.comIAR www.iar.comCCS www.ccsinfo.com/picc.shtmlByteCraft www.bytecraft.com/mpccaps.html

    Hitech-PICC CPIC

    11.2 Hitech-PICC

    PICC ANSIPIC PIC PIC

    PDF created with pdfFactory Pro trial version www.pdffactory.com

    http://www.htsoft.comhttp://www.iar.comhttp://www.ccsinfo.com/picc.shtmlhttp://www.bytecraft.com/mpccaps.htmlhttp://www.pdffactory.comhttp://www.pdffactory.com

  • 2004-4-7 2 of 26

    PIC PICCC

    4K C

  • 2004-4-7 3 of 26

    C

    11-2 CI2C

    11.4 PIC C

    PICC PIC C

    #includepic.h

    __CONFIGPICC

    main

    11-1 C

    #include //

    #include pc68.h //

    //

    __CONFIG (HS & PROTECT & PWRTEN & BOREN & WDTDIS);

    //

    void SetSFR(void);

    void Clock(void);

    void KeyScan(void);

    void Measure(void);

    void LCD_Test(void);

    void LCD_Disp(unsigned char);

    //

    unsigned char second, minute, hour;

    bit flag1,flag2;

    //

    11-2 C

    PDF created with pdfFactory Pro trial version www.pdffactory.com

    http://www.pdffactory.comhttp://www.pdffactory.com

  • 2004-4-7 4 of 26

    void main(void)

    {

    SetSFR();

    PORTC = 0x00;

    TMR1H += TMR1H_CONST;

    LED1 = LED_OFF;

    LCD_Test();

    //

    while(1) {

    asm(clrwdt); //

    Clock(); //

    KeyScan(); //

    Measure(); //

    SetSFR(); //

    }

    }

    11-1 C

    11.5 PICC

    11.5.1 PICC

    PICC 11-1

    bit 1 0 1

    char 8 PICC char

    unsigned char 8

    short 16

    unsigned short 16

    int 16

    unsigned int 16

    long 32

    unsigned long 32

    float 24

    double 24 32 PICC double 24

    32

    11-1 PICC

    PICC Little-endian

    PDF created with pdfFactory Pro trial version www.pdffactory.com

    http://www.pdffactory.comhttp://www.pdffactory.com

  • 2004-4-7 5 of 26

    11.5.2 PICC

    11-1 bit PICCC

    unsigned int data[10];

    struct commInData {

    unsigned char inBuff[8];

    unsigned char getPtr, putPtr;

    };

    union int_Byte {

    unsigned char c[2];

    unsigned int i;

    };

    11-2 C

    11.5.3 PICC bank

    PICC bankbank

    bank0unsigned char buffer[32];

    bit flag1,flag2;

    float val[8];

    bank0 bankbank

    bank1 unsigned char buffer[32]; // bank1

    bank2 bit flag1,flag2; // bank2

    bank3 float val[8]; // bank3

    PIC bank 128C bank RAM

    bankError[000] : Can't find 0x12C words for psect rbss_1 in segment BANK1

    0x12C 300 bank1 bank1bank1 bank

    bankbank C bank

    bankbank bank bank

    PDF created with pdfFactory Pro trial version www.pdffactory.com

    http://www.pdffactory.comhttp://www.pdffactory.com

  • 2004-4-7 6 of 26

    11.5.4 PICC

    PICC auto bank0

    PIC PICC

    bank0 bank0

    11.5.5 PICC

    bit PICC bank 8PIC

    PICC 0x000 0 0x00008

    flag1 0x123

    0x123/8 = 0x24

    0x123%8 = 3

    flag1 0x24 3 flag10x24 0x123

    PIC PICCtmp

    0x20tmp |= 0x80 => bsf 0x20,7

    tmp &= 0xf7 => bcf 0x20,3

    if (tmp&0xfe) => btfsc 0x20,0

    C

    C

    PDF created with pdfFactory Pro trial version www.pdffactory.com

    http://www.pdffactory.comhttp://www.pdffactory.com

  • 2004-4-7 7 of 26

    union {

    struct {

    unsigned b0: 1;

    unsigned b1: 1;

    unsigned b2: 1;

    unsigned b3: 1;

    unsigned b4: 1;

    unsigned b5: 1;

    unsigned : 2; //

    } oneBit;

    unsigned char allBits;

    } myFlag;

    11-3

    myFlag.oneBit.b3=1; //b3 1

    myFlag.allBits=0; // 0

    0 0 1 1ANSI-C 0

    11.5.6 PICC

    PICC IEEE-754 324 PICC

    24float 24 double

    24 double 32

    24

    11.5.7 PICC

    Cbank

    PICC PICCPICC

    PDF created with pdfFactory Pro trial version www.pdffactory.com

    http://www.pdffactory.comhttp://www.pdffactory.com

  • 2004-4-7 8 of 26

    unsigned char tmpData @ 0x20; //tmpData 0x20

    PICC tmpData0x20 0x20

    unsigned char tmpData @ 0x20; //tmpData 0x20

    bit tmpBit0 @ tmpData*8+0; //tmpBit0 tmpData 0

    bit tmpBit1 @ tmpData*8+1; //tmpBit0 tmpData 1

    bit tmpBit2 @ tmpData*8+2; //tmpBit0 tmpData 2

    tmpData

    11.5.8 PICC

    extern

    Cextern code1.c

    bank1 unsigned char var1, var2; // bank1

    code2.cextern bank1 unsigned char var1, var2; // bank1

    volatile

    PICC Cvolatile

    IO

    volatilevolatile unsigned char STATUS @ 0x03;

    volatile bit commFlag;

    volatile C

    volatile

    volatile

    PDF created with pdfFactory Pro trial version www.pdffactory.com

    http://www.pdffactory.comhttp://www.pdffactory.com

  • 2004-4-7 9 of 26

    volatile

    const

    const

    ROM ROMconst retlw

    PICC

    const unsigned char name[]=This is a demo; //

    const RAM

    persistent

    CPICC

    mainPICC persistent

    persistenpersistent unsigned char hour,minute,second; //

    persistentpersistent

    PIC

    11.5.9 PICC

    PICC C PIC

    RAM

    FSR PICCPICC C RAM FSR

    FSR 256 bank0/1bank2/3 512 PICC

    PDF created with pdfFactory Pro trial version www.pdffactory.com

    http://www.pdffactory.comhttp://www.pdffactory.com

  • 2004-4-7 10 of 26

    unsigned char *ptr0; // bank0/1

    bank2 unsigned char *ptr1; // bank2/3

    bank3 unsigned char *ptr2; // bank2/3

    bank bank0 bank1bank2 bank3

    bank bank0bank

    bank

    unsigned char *ptr0; // bank0/1

    bank2 unsigned char buff[8]; // bank2

    ptr0 = buff; // bank2 bank0/1

    PICCFixup overflow in expression (...)

    bank

    void SendMessage(unsigned char *);

    bank0 bank1 bank2 bank3

    void SendMessage_2(bank2 unsigned char *);

    Fixup overflow

    ROM

    ROMconst unsigned char company[]=Microchip; // ROM

    const unsigned char *romPtr; // ROM

    romPtr company; //

    data = *romPtr++; // 1

    *romPtr data; //

    C PICCPIC

    PICC RAM

    PDF created with pdfFactory Pro trial version www.pdffactory.com

    http://www.pdffactory.comhttp://www.pdffactory.com

  • 2004-4-7 11 of 26

    PC

    persistent volatilePICC

    bankbank0/1 bank2/3

    bank0bank

    // bank0/1 bank0

    unsigned char *ptr0;

    // bank2/3 bank0

    bank2 unsigned char *ptr0;

    // bank2/3 bank1

    bank2 unsigned char * bank1 ptr0;

    bank bank

    bank

    volatile persistent constbank volatile persistent const

    bank// bank0/1 bank0

    volatile unsigned char *ptr0;

    // bank2/3 bank1

    bank2 unsigned char * volatile bank1 ptr0;

    // ROM ROM

    const unsigned char * const ptr0;

    11.6 PICC

    PIC C

    11.6.1

    PDF created with pdfFactory Pro trial version www.pdffactory.com

    http://www.pdffactory.comhttp://www.pdffactory.com

  • 2004-4-7 12 of 26

    PICC CPIC 2K C

    2K2K

    11.6.2

    PIC 87

    PICC *.mapcall graph

    Call graph:

    *_main size 0,0 offset 0

    _RightShift_C

    * _Task size 0,1 offset 0

    lwtoft

    ftmul size 0,0 offset 0

    ftunpack1

    ftunpack2

    ftadd size 0,0 offset 0

    ftunpack1

    ftunpack2

    ftdenorm

    11-4 C

    mainC

    11.6.3

    PICC

    void Task(void);

    unsigned char Temperature(void);

    void BIN2BCD(unsigned char);

    void TimeDisplay(unsigned char, unsigned char);

    PDF created with pdfFactory Pro trial version www.pdffactory.com

    http://www.pdffactory.comhttp://www.pdffactory.com

  • 2004-4-7 13 of 26

    C

    11-1

    11.6.4

    PICC Cvoid interrupt ISR(void);

    ISRvoid interrupt

    interrupt PICC0x0004

    retfievoid interrupt ISR(void) //

    {

    if (T0IE && T0IF) // TMR0

    {

    T0IF = 0; // TMR0

    // TMR0

    }

    if (TMR1IE && TMR1IF) // TMR1

    {

    TMR1IF = 0; // TMR1

    // TMR1

    }

    } //

    11-5 C

    PICC

    C

    PICC

    PDF created with pdfFactory Pro trial version www.pdffactory.com

    http://www.pdffactory.comhttp://www.pdffactory.com

  • 2004-4-7 14 of 26

    PIC

    11.6.5

    PICC Cbank0

    #include #include

    PICC

    C printf/sprintfprintf/sprintf

    C scanf printf PICCgetch() putch() scanf/printf

    getch() putch()

    11.7 PICC

    PICC

    11.7.1

    PIC PICCC

    __CONFIG (HS & UNPROTECT & PWRTEN & BORDIS & WDTEN);

    __CONFIGpic.h

    HEX

    16F7x

    /* */

    #define RC 0x3FFF // RC

    #define HS 0x3FFE // HS

    PDF created with pdfFactory Pro trial version www.pdffactory.com

    http://www.pdffactory.comhttp://www.pdffactory.com

  • 2004-4-7 15 of 26

    #define XT 0x3FFD // XT

    #define LP 0x3FFC // LP

    /* */

    #define WDTEN 0x3FFF //

    #define WDTDIS 0x3FFB //

    /* */

    #define PWRTEN 0x3FF7 //

    #define PWRTDIS 0x3FFF //

    /* */

    #define BOREN 0x3FFF //

    #define BORDIS 0x3FBF //

    /* */

    #define UNPROTECT 0x3FFF //

    #define PROTECT 0x3FEF //

    11-6

    11.7.2

    PIC __IDLOC

    __IDLOC (1234);

    16 0x01020304

    11.8 MPLAB-IDE PICC

    11.3 PICC MPLAB-IDEMPLAB

    MPLAB

    Make MPLAB

    Build All

    Project Make Build All

    11.8.1

    PDF created with pdfFactory Pro trial version www.pdffactory.com

    http://www.pdffactory.comhttp://www.pdffactory.com

  • 2004-4-7 16 of 26

    PICC Configure Select DeviceMPLAB 11-1

    #include pic.h

    MPLABpic.h

    #include

    11.8.2 PICC General

    3.2.7 3-20PICC MPAMS 11-3 PICC

    Include Path

    11.8.3 PICC PICC Global

    C 11-4

    Compile for MPLAB ICD ICD CICD

    Treat char as signed PICC char

    11-3 PICC 11-4 PICC

    PDF created with pdfFactory Pro trial version www.pdffactory.com

    http://www.pdffactory.comhttp://www.pdffactory.com

  • 2004-4-7 17 of 26

    charFloating point double width PICCdouble 24 float

    32

    11.8.4 C PICC Compiler

    C C CGeneral Advanced

    11-5A 11-5B

    C9

    20% 2K PICC

    volatile

    PICC

    A B

    11-5 C

    PDF created with pdfFactory Pro trial version www.pdffactory.com

    http://www.pdffactory.comhttp://www.pdffactory.com

  • 2004-4-7 18 of 26

    C

    Generate assembly list file C *.lstC

    C

    CCompile to assembly only C

    *.as CC C

    11.8.5 PICC Linker

    PICC Linker11-6

    Generate map file

    *.map

    Display memory-segment usage

    //Psect Usage Map: //

    Psect | Contents | Memory Range

    ----------|------------------------------|--------------------

    powerup | Power on reset code | $0000 - $0003

    intentry | Interrupt service routine | $0004 - $000C

    intcode | Interrupt service routine | $000D - $002C

    intret | Interrupt service routine | $002D - $0035

    init | Initialization code | $0036 - $003D

    end_init | Initialization code | $003E - $0040

    11-6 PICC

    PDF created with pdfFactory Pro trial version www.pdffactory.com

    http://www.pdffactory.comhttp://www.pdffactory.com

  • 2004-4-7 19 of 26

    clrtext | Memory clearing code | $0041 - $0047

    const3 | Strings and constant data | $0048 - $0060

    const | Strings and constant data | $0061 - $0071

    const2 | Strings and constant data | $0072 - $0076

    text | Program and library code | $0576 - $0582

    text | Program and library code | $0583 - $07C7

    float_te | Arithmetic routine code | $07C8 - $07FF

    rbss_0 | Bank 0 RAM variables | $0021 - $0042

    temp | Temporary RAM data | $0043 - $0047

    nvram | Persistent RAM data | $0048 - $004A

    intsave | Registers saved on interrupt | $004B - $004D

    intsave | Registers saved on interrupt | $007F - $007F

    intsave_1 | Saved copy of W in bank 1 | $00FF - $00FF

    rbit_0 | Bank 0 bit variables | $0100 - $0104

    config | User-programmed CONFIG bits | $2007 - $2007

    Memory Usage Map: //

    //

    Program ROM $0000 - $0076 $0077 ( 119) words

    Program ROM $0576 - $07FF $028A ( 650) words

    $0301 ( 769) words total Program ROM

    //bank0

    Bank 0 RAM $0021 - $004D $002D ( 45) bytes

    Bank 0 RAM $007F - $007F $0001 ( 1) bytes

    $002E ( 46) bytes total Bank 0 RAM

    //bank1

    Bank 1 RAM $00FF - $00FF $0001 ( 1) bytes total Bank 1 RAM

    //bank0

    Bank 0 Bits $0100 - $0104 $0005 ( 5) bits total Bank 0 Bits

    //

    Config Data $2007 - $2007 $0001 ( 1) words total Config Data

    Program statistics: //

    Total ROM used 769 words (18.8%) //

    Total RAM used 48 bytes (25.0%) //

    11-7

    11.8.6 PICC Assembler

    PICC Microchip MPASMPICC

    *.as MPASM *.asm

    PDF created with pdfFactory Pro trial version www.pdffactory.com

    http://www.pdffactory.comhttp://www.pdffactory.com

  • 2004-4-7 20 of 26

    PICC PIC C

    11-7 Enable optimization ,

    11.9 C

    CC PIC

    clrwdt sleep ;

    C

    11.9.1

    CPICC

    asm(clrwdt);

    PICfor (;;) {

    asm("clrwdt"); //

    Task();

    ClockRun();

    asm("sleep"); //

    asm("nop"); //

    }

    11-8

    11-7 PICC

    PDF created with pdfFactory Pro trial version www.pdffactory.com

    http://www.pdffactory.comhttp://www.pdffactory.com

  • 2004-4-7 21 of 26

    PICC #asm#endasm 0x20~0x7F

    RAM#asm

    movlw 0x20

    movwf _FSR

    clrf _INDF

    incf _FSR,f

    btfss _FSR,7

    goto $-3

    #endasm

    11-9

    11.9.2 C

    CC C _

    C _11-9 FSR INDF

    C

    C _11-10

    volatile unsigned char tmp; // bank0

    void Test(void) //

    {

    #asm //

    clrf _STATUS // bank0

    movlw 0x10 //

    movwf _tmp //tmp=0x10

    #endasm //

    if (tmp==0x10) { // C

    ;

    }

    }

    11-10 C bank0

    C PICCC

    bank bank11-11

    PDF created with pdfFactory Pro trial version www.pdffactory.com

    http://www.pdffactory.comhttp://www.pdffactory.com

  • 2004-4-7 22 of 26

    volatile bank1 unsigned char tmpBank1; // bank1

    volatile bank2 unsigned char tmpBank2; // bank2

    volatile bank3 unsigned char tmpBank3; // bank3

    void Test(void) //

    {

    #asm //

    bcf _STATUS,6 // bank1

    bsf _STATUS,5

    movlw 0x10 //

    movwf _tmpBank1^0x80 //tmpBank1=0x10

    bsf _STATUS,6 // bank2

    bcf _STATUS,5

    movlw 0x20 //

    movwf _tmpBank1^0x100 //tmpBank2=0x20

    bsf _STATUS,6 // bank3

    bsf _STATUS,5

    movlw 0x30 //

    movwf _tmpBank1^0x180 //tmpBank1=0x30

    #endasm //

    }

    11-11 C bank0

    Cbank

    bank bank00x00 PIC bank

    PICCbank bank

    11.9.3 C

    PICCbank0

    C11-5(B) Compile to assembly only C PICC

    C11-12 C

    //C

    void Test(unsigned char inVar1, inVar2)

    PDF created with pdfFactory Pro trial version www.pdffactory.com

    http://www.pdffactory.comhttp://www.pdffactory.com

  • 2004-4-7 23 of 26

    {

    unsigned char tmp1, tmp2;

    inVar1++;

    inVar2--;

    tmp1 = 1;

    tmp2 = 2;

    }

    //

    _Test

    ; _tmp1 assigned to ?a_Test+0 //tmp1 ?a_Test+0

    _Test$tmp1 set ?a_Test

    ; _tmp2 assigned to ?a_Test+1 //tmp2 ?a_Test+1

    _Test$tmp2 set ?a_Test+1

    ; _inVar1 assigned to ?a_Test+2 //inVar1 ?a_Test+2

    _Test$inVar1 set ?a_Test+2

    line 44

    ;_inVar1 stored from w // W

    bcf 3,5

    bcf 3,6

    movwf ?a_Test+2

    ;ht16.c: 43: unsigned char tmp1, tmp2;

    incf ?a_Test+2

    line 45

    ;ht16.c: 45: inVar2--;

    decf ?_Test // inVar2 ?_Test

    line 46

    ;ht16.c: 46: tmp1 = 1;

    clrf ?a_Test

    incf ?a_Test

    line 47

    ;ht16.c: 47: tmp2 = 2;

    movlw 2

    movwf ?a_Test+1

    line 48

    ;ht16.c: 48: }

    return

    11-12 PICC

    PICC C11-13

    //C

    void Test(unsigned char inVar1, inVar2)

    {

    PDF created with pdfFactory Pro trial version www.pdffactory.com

    http://www.pdffactory.comhttp://www.pdffactory.com

  • 2004-4-7 24 of 26

    unsigned char tmp1, tmp2;

    #asm //

    incf ?a_Test+0,f //tmp1++;

    decf ?a_Test+1,f //tmp2--;

    movlw 0x10

    addwf ?a_Test+2,f //inVar1 += 0x10;

    rrf ?_Test,w //inVar2

    rrf ?_Test,f

    #endasm //

    }

    11-13

    PICCPICC Little endian

    11-14 CC

    //16

    unsigned int RR_Shift16(unsigned int var, unsigned char count)

    {

    while(count--) //

    {

    #asm //

    rrf ?_RR_Shift16+0,w // C

    rrf ?_RR_Shift16+1,f //var 1 C

    rrf ?_RR_Shift16+0,f //var 1

    #endasm //

    }

    return(var); //

    }

    11-14

    11.9.4

    C

    C

    C PICCC

    PICCPICC

    PDF created with pdfFactory Pro trial version www.pdffactory.com

    http://www.pdffactory.comhttp://www.pdffactory.com

  • 2004-4-7 25 of 26

    CC

    C

    11-14 C PIC

    countC

    C

    *.as

    PICC CPICC Hitech

    PICC

    C C

    CC

    11-15bit EvenParity(unsigned char data)

    {

    #asm

    swapf ?a_EvenParity+0,w // data ?a_EvenParity+0

    xorwf ?a_EvenParity+0,f

    rrf ?a_EvenParity+0,w

    xorwf ?a_EvenParity+0,f

    btfsc ?a_EvenParity+0,2

    incf ?a_EvenParity+0,f

    #endasm

    // data

    if (data&0x01) return(1);

    else return(0);

    }

    11-15 C

    C

    PDF created with pdfFactory Pro trial version www.pdffactory.com

    http://www.pdffactory.comhttp://www.pdffactory.com

  • 2004-4-7 26 of 26

    C

    PDF created with pdfFactory Pro trial version www.pdffactory.com

    http://www.pdffactory.comhttp://www.pdffactory.com