首页资源分类嵌入式开发单片机 > PIC单片机C语言教程

PIC单片机C语言教程

已有 456470个资源

下载专区

上传者其他资源

文档信息举报收藏

标    签: PICC语言

分    享:

文档简介

PIC单片机C语言教程

文档预览

11.1 PIC C C PIC C PIC C C C PC PC PC C C PIC C C PICC PIC Hitech CCS IAR Microchip Bytecraft PIC PIC PIC C Hitech Hitech PIC PICC-Lite PIC16F84 PIC16F877 PIC16F628 PIC C Flash PIC PICC-Lite Hitech-PICC IAR CCS ByteCraft PIC C www.htsoft.com www.iar.com www.ccsinfo.com/picc.shtml www.bytecraft.com/mpccaps.html Hitech-PICC C PIC 11.2 Hitech-PICC PICC ANSI PIC PIC PIC 2004-4-7 1 of 26 PDF created with pdfFactory Pro trial version www.pdffactory.com PIC C PICC 4K C <10% C PIC 11.3 MPLAB-IDE PICC PICC MPLAB-IDE MPLAB-IDE ICE2000 ICD2 MPLAB-IDE Locations… PICC IDE 11-1 Project Set Language Tool 11-1 MPLAB-IDE HI-TECH PICC Toolsuite MPLAB-IDE PICC Assembler C PICC Compiler Browse… PICC.EXE Location PICC C .c Executable PICC Linker PICC.EXE PICC.EXE .as 3.1.3 HI-TECH PICC C C C 2004-4-7 2 of 26 PDF created with pdfFactory Pro trial version www.pdffactory.com C 11-2 C I2C 11.4 PIC C 11-2 C PICC PIC pic.h __CONFIG #include C PICC 11-1 C main #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; // 2004-4-7 3 of 26 PDF created with pdfFactory Pro trial version www.pdffactory.com 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 char unsigned char short unsigned short int unsigned int long unsigned long float double 1 01 PICC 8 8 16 16 16 16 32 32 24 24 32 PICC 32 11-1 PICC double PICC Little-endian char 24 2004-4-7 4 of 26 PDF created with pdfFactory Pro trial version www.pdffactory.com 11.5.2 PICC 11-1 bit C 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 PICC 11.5.3 PICC bank PICC unsigned char buffer[32]; bit flag1,flag2; float val[8]; bank0 bank bank bank0 bank bank1 unsigned char buffer[32]; // bank2 bit flag1,flag2; // bank3 float val[8]; // bank bank1 bank2 bank3 PIC bank 128 C bank RAM bank Error[000] : Can't find 0x12C words for psect rbss_1 in segment BANK1 0x12C 300 bank1 bank1 bank bank1 bank bank C bank bank bank bank bank 2004-4-7 5 of 26 PDF created with pdfFactory Pro trial version www.pdffactory.com 11.5.4 PICC PICC auto bank0 PIC 11.5.5 PICC bit bank0 PICC PICC bank0 bank 8 PIC PICC 8 0x000 0 0x0000 flag1 0x123 flag1 0x24 0x123/8 = 0x24 0x123%8 = 3 0x24 3 0x123 flag1 PIC 0x20 tmp |= 0x80 tmp &= 0xf7 if (tmp&0xfe) PICC => => => C bsf bcf btfsc 0x20,7 0x20,3 0x20,0 tmp C 2004-4-7 6 of 26 PDF created with pdfFactory Pro trial version www.pdffactory.com 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 1 ANSI-C 0 11.5.6 PICC PICC IEEE-754 4 24 float 24 24 double 32 PICC double 32 24 11.5.7 PICC C bank PICC PICC PICC 2004-4-7 7 of 26 PDF created with pdfFactory Pro trial version www.pdffactory.com unsigned char tmpData @ 0x20; //tmpData PICC 0x20 0x20 0x20 tmpData unsigned char tmpData @ 0x20; bit tmpBit0 @ tmpData*8+0; bit tmpBit1 @ tmpData*8+1; bit tmpBit2 @ tmpData*8+2; //tmpData //tmpBit0 //tmpBit0 //tmpBit0 0x20 tmpData 0 tmpData 1 tmpData 2 tmpData 11.5.8 PICC extern C extern bank1 unsigned char var1, var2; // code2.c extern bank1 unsigned char var1, var2; // code1.c bank1 bank1 volatile PICC C volatile IO volatile unsigned char STATUS @ 0x03; volatile bit commFlag; volatile volatile C volatile volatile 2004-4-7 8 of 26 PDF created with pdfFactory Pro trial version www.pdffactory.com volatile const const ROM const PICC ROM const unsigned char name[]=”This is a demo”; // const retlw RAM persistent C PICC main PICC persistent persisten persistent unsigned char hour,minute,second; // persistent persistent PIC 11.5.9 PICC PICC C PIC RAM bank2/3 PICC 512 C FSR FSR RAM PICC FSR 256 bank0/1 PICC 2004-4-7 9 of 26 PDF created with pdfFactory Pro trial version www.pdffactory.com unsigned char *ptr0; // bank0/1 bank2 unsigned char *ptr1; // bank2/3 bank3 unsigned char *ptr2; // bank2/3 bank bank2 bank3 bank bank bank0 bank1 bank0 bank unsigned char *ptr0; bank2 unsigned char buff[8]; // bank0/1 // bank2 ptr0 = buff; // bank2 PICC Fixup overflow in expression (...) bank0/1 void SendMessage(unsigned char *); bank0 bank1 void SendMessage_2(bank2 unsigned char *); bank bank2 bank3 Fixup overflow ROM ROM const unsigned char company[]=”Microchip”; const unsigned char *romPtr; // ROM // ROM romPtr company; // data = *romPtr++; // 1 *romPtr data; // C PIC PICC RAM 2004-4-7 10 of 26 PICC PDF created with pdfFactory Pro trial version www.pdffactory.com PC PICC persistent volatile bank bank0 bank bank0/1 // bank0/1 bank0 unsigned char *ptr0; // bank2/3 bank0 bank2 unsigned char *ptr0; // bank2/3 bank1 bank2 unsigned char * bank1 ptr0; bank bank2/3 bank bank volatile persistent const bank // bank0/1 bank volatile unsigned char *ptr0; // bank2/3 bank2 unsigned char * volatile bank1 ptr0; // ROM ROM const unsigned char * const ptr0; volatile bank0 bank1 persistent const 11.6 PICC PIC C 11.6.1 2004-4-7 11 of 26 PDF created with pdfFactory Pro trial version www.pdffactory.com PICC C PIC 2K C 2K 2K 11.6.2 PIC 8 7 PICC call 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 main C 11.6.3 PICC void Task(void); unsigned char Temperature(void); void BIN2BCD(unsigned char); void TimeDisplay(unsigned char, unsigned char); 2004-4-7 12 of 26 PDF created with pdfFactory Pro trial version www.pdffactory.com *.map C 11-1 11.6.4 PICC C void interrupt ISR(void); ISR void 0x0004 retfie void interrupt ISR(void) // { if (T0IE && T0IF) // TMR0 { T0IF = 0; // TMR0 // TMR0 } if (TMR1IE && TMR1IF) // TMR1 { TMR1IF = 0; // TMR1 // TMR1 } } // 11-5 C PICC interrupt interrupt PICC C PICC 2004-4-7 13 of 26 PDF created with pdfFactory Pro trial version www.pdffactory.com PIC 11.6.5 PICC C printf/sprintf C bank0 #include #include PICC printf/sprintf C getch() putch() scanf getch() printf scanf/printf putch() PICC 11.7 PICC PICC 11.7.1 PIC C __CONFIG (HS & UNPROTECT & PWRTEN & BORDIS & WDTEN); __CONFIG HEX 16F7x /* */ #define RC #define HS 0x3FFF 0x3FFE // RC // HS 2004-4-7 14 of 26 PDF created with pdfFactory Pro trial version www.pdffactory.com PICC pic.h #define XT #define LP 0x3FFD 0x3FFC // XT // LP /* */ #define WDTEN #define WDTDIS 0x3FFF // 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 0x 01020304 11.8 MPLAB-IDE PICC 11.3 PICC MPLAB-IDE MPLAB MPLAB Make MPLAB Build All Project Make Build All 11.8.1 2004-4-7 15 of 26 PDF created with pdfFactory Pro trial version www.pdffactory.com PICC MPLAB #include MPLAB pic.h pic.h Configure Select Device 11-1 #include 11.8.2 PICC General PICC 3.2.7 3-20 MPAMS 11-3 PICC Include Path 11-3 PICC 11-4 PICC 11.8.3 PICC PICC Global C Compile for MPLAB ICD ICD C ICD Treat ‘char’ as signed PICC 2004-4-7 16 of 26 11-4 ‘char’ PDF created with pdfFactory Pro trial version www.pdffactory.com Floating point ‘double’ width ‘double’ 32 ‘char’ 24 PICC float 11.8.4 C C 11-5A 11-5B C 9 PICC Compiler C General C Advanced 20% 2K PICC A B 11-5 C volatile PICC 2004-4-7 17 of 26 PDF created with pdfFactory Pro trial version www.pdffactory.com C Generate assembly list file C C C C Compile to assembly only C *.as C *.lst C C 11.8.5 PICC Linker 11-6 PICC Linker Generate map file *.map Display memory-segment usage 11-6 PICC // 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 2004-4-7 18 of 26 PDF created with pdfFactory Pro trial version www.pdffactory.com 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 *.as MPASM PICC Assembler Microchip PICC *.asm MPASM 2004-4-7 19 of 26 PDF created with pdfFactory Pro trial version www.pdffactory.com PICC 11-7 PIC C Enable optimization , 11.9 C 11-7 PICC C C PIC clrwdt sleep ; C 11.9.1 C PICC asm(“clrwdt”); PIC for (;;) { asm("clrwdt"); Task(); ClockRun(); asm("sleep"); asm("nop"); } // // // 11-8 2004-4-7 20 of 26 PDF created with pdfFactory Pro trial version www.pdffactory.com RAM #endasm #asm movlw movwf clrf incf btfss goto #endasm 0x20 _FSR _INDF _FSR,f _FSR,7 $-3 PICC 11-9 #asm 0x20~0x7F 11.9.2 C C C C C 11-9 C FSR INDF _ _ C _ 11-10 volatile unsigned char tmp; // bank0 void Test(void) { #asm clrf _STATUS movlw 0x10 movwf _tmp #endasm if (tmp==0x10) { ; } } 11-10 // // // bank0 // //tmp=0x10 // // C C bank0 C PICC C bank bank 11-11 2004-4-7 21 of 26 PDF created with pdfFactory Pro trial version www.pdffactory.com volatile bank1 unsigned char tmpBank1; // volatile bank2 unsigned char tmpBank2; // volatile bank3 unsigned char tmpBank3; // bank1 bank2 bank3 void Test(void) { #asm bcf _STATUS,6 bsf _STATUS,5 movlw 0x10 movwf _tmpBank1^0x80 // // // bank1 // //tmpBank1=0x10 bsf bcf movlw movwf _STATUS,6 _STATUS,5 0x20 _tmpBank1^0x100 // bank2 // //tmpBank2=0x20 bsf _STATUS,6 bsf _STATUS,5 movlw movwf 0x30 _tmpBank1^0x180 #endasm } 11-11 // bank3 // //tmpBank1=0x30 // C bank0 bank 0x00 bank bank 11.9.3 C PICC bank bank0 PIC bank0 PICC C bank C 11-5(B) Compile to assembly only C C 11-12 C //C void Test(unsigned char inVar1, inVar2) 2004-4-7 22 of 26 PICC PDF created with pdfFactory Pro trial version www.pdffactory.com { unsigned char tmp1, tmp2; inVar1++; inVar2--; tmp1 = 1; tmp2 = 2; } // _Test ; _tmp1 assigned to ?a_Test+0 _Test$tmp1 set ?a_Test //tmp1 ; _tmp2 assigned to ?a_Test+1 _Test$tmp2 set ?a_Test+1 //tmp2 ; _inVar1 assigned to ?a_Test+2 _Test$inVar1 set ?a_Test+2 line 44 //inVar1 ;_inVar1 stored from 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 line 46 // inVar2 ;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 ?a_Test+0 ?a_Test+1 ?a_Test+2 W ?_Test PICC //C void Test(unsigned char inVar1, inVar2) { C 11-13 2004-4-7 23 of 26 PDF created with pdfFactory Pro trial version www.pdffactory.com unsigned char tmp1, tmp2; #asm incf ?a_Test+0,f decf ?a_Test+1,f movlw 0x10 addwf ?a_Test+2,f rrf ?_Test,w rrf ?_Test,f #endasm } 11-13 // //tmp1++; //tmp2--; //inVar1 += 0x10; //inVar2 // PICC PICC Little endian 11-14 C C //16 unsigned int RR_Shift16(unsigned int var, unsigned char count) { while(count--) // { #asm // rrf ?_RR_Shift16+0,w rrf ?_RR_Shift16+1,f // //var C 1C rrf ?_RR_Shift16+0,f //var 1 #endasm // } return(var); // } 11-14 11.9.4 C C PICC C C PICC 2004-4-7 24 of 26 PICC PDF created with pdfFactory Pro trial version www.pdffactory.com C 11-14 count C C C C C PIC PICC C PICC Hitech PICC C 11-15 bit EvenParity(unsigned char data) { #asm swapf ?a_EvenParity+0,w // 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 *.as C C C data ?a_EvenParity+0 C 2004-4-7 25 of 26 PDF created with pdfFactory Pro trial version www.pdffactory.com C 2004-4-7 26 of 26 PDF created with pdfFactory Pro trial version www.pdffactory.com

Top_arrow
回到顶部
EEWORLD下载中心所有资源均来自网友分享,如有侵权,请发送举报邮件到客服邮箱bbs_service@eeworld.com.cn 或通过站内短信息或QQ:273568022联系管理员 高进,我们会尽快处理。