PIC单片机C语言教程
2004-4-7 1 of 26
PIC C
11.1 PIC C
C
C
PIC C
C PC PC
PC
C
C
PIC
C
PIC Microchip PIC
C PIC C
Hitech CCS IAR Bytecraft Hitech
PICC PIC
Hitech PIC
PICC-Lite PIC
PIC16F84 PIC16F877 PIC16F628 Flash
PICC-Lite
PIC C
PIC C
Hitech-PICC www.htsoft.com
IAR ...
2004-4-7 1 of 26
PIC C
11.1 PIC C
C
C
PIC C
C PC PC
PC
C
C
PIC
C
PIC Microchip PIC
C PIC C
Hitech CCS IAR Bytecraft Hitech
PICC PIC
Hitech PIC
PICC-Lite PIC
PIC16F84 PIC16F877 PIC16F628 Flash
PICC-Lite
PIC C
PIC C
Hitech-PICC www.htsoft.com
IAR www.iar.com
CCS www.ccsinfo.com/picc.shtml
ByteCraft www.bytecraft.com/mpccaps.html
Hitech-PICC C
PIC
11.2 Hitech-PICC
PICC ANSI
PIC PIC PIC
PDF created with pdfFactory Pro trial version www.pdffactory.com
2004-4-7 2 of 26
PIC PICC
C
4K C
<10% C
PIC
11.3 MPLAB-IDE PICC
PICC MPLAB-IDE
MPLAB-IDE ICE2000 ICD2
PICC
MPLAB-IDE IDE Project Set Language Tool
Locations… 11-1
HI-TECH PICC Toolsuite Executable
MPLAB-IDE
PICC Assembler C PICC Compiler PICC Linker
PICC.EXE
Location
Browse… PICC PICC.EXE
PICC.EXE
C .c .as
HI-TECH PICC
3.1.3 C
C C
11-1 MPLAB-IDE
PDF created with pdfFactory Pro trial version www.pdffactory.com
2004-4-7 3 of 26
C
11-2 C
I2C
11.4 PIC C
PICC PIC C
#include
pic.h
__CONFIG
PICC
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
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
2004-4-7 5 of 26
11.5.2 PICC
11-1 bit PICC
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
11.5.3 PICC bank
PICC bank
bank
bank0
unsigned char buffer[32];
bit flag1,flag2;
float val[8];
bank0 bank
bank
bank1 unsigned char buffer[32]; // bank1
bank2 bit flag1,flag2; // bank2
bank3 float val[8]; // 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
bank1 bank
bank
bank C bank
bank
bank bank bank
PDF created with pdfFactory Pro trial version 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 8
PIC
PICC 0x000 0 0x0000
8
flag1 0x123
0x123/8 = 0x24
0x123%8 = 3
flag1 0x24 3 flag1
0x24 0x123
PIC PICC
tmp
0x20
tmp |= 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
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 1
ANSI-C 0
11.5.6 PICC
PICC IEEE-754 32
4 PICC
24
float 24 double
24 double 32
24
11.5.7 PICC
C
bank
PICC PICC
PICC
PDF created with pdfFactory Pro trial version www.pdffactory.com
2004-4-7 8 of 26
unsigned char tmpData @ 0x20; //tmpData 0x20
PICC tmpData
0x20 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
C
extern code1.c
bank1 unsigned char var1, var2; // bank1
code2.c
extern bank1 unsigned char var1, var2; // bank1
volatile
PICC C
volatile
IO
volatile
volatile unsigned char STATUS @ 0x03;
volatile bit commFlag;
volatile C
volatile
volatile
PDF created with pdfFactory Pro trial version www.pdffactory.com
2004-4-7 9 of 26
volatile
const
const
ROM ROM
const retlw
PICC
const unsigned char name[]=”This is a demo”; //
const 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
FSR PICC
PICC C RAM FSR
FSR 256 bank0/1
bank2/3 512 PICC
PDF created with pdfFactory Pro trial version 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 bank1
bank2 bank3
bank bank0
bank
bank
unsigned char *ptr0; // bank0/1
bank2 unsigned char buff[8]; // bank2
ptr0 = buff; // bank2 bank0/1
PICC
Fixup overflow in expression (...)
bank
void SendMessage(unsigned char *);
bank0 bank1 bank2 bank3
void SendMessage_2(bank2 unsigned char *);
Fixup overflow
ROM
ROM
const unsigned char company[]=”Microchip”; // ROM
const unsigned char *romPtr; // ROM
romPtr company; //
data = *romPtr++; // 1
*romPtr data; //
C PICC
PIC
PICC RAM
PDF created with pdfFactory Pro trial version www.pdffactory.com
2004-4-7 11 of 26
PC
persistent volatile
PICC
bank
bank0/1 bank2/3
bank0
bank
// 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 const
bank 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
2004-4-7 12 of 26
PICC C
PIC 2K C
2K
2K
11.6.2
PIC 8
7
PICC *.map
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);
PDF created with pdfFactory Pro trial version www.pdffactory.com
2004-4-7 13 of 26
C
11-1
11.6.4
PICC C
void interrupt ISR(void);
ISR
void interrupt
interrupt PICC
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
C
PICC
PDF created with pdfFactory Pro trial version www.pdffactory.com
2004-4-7 14 of 26
PIC
11.6.5
PICC C
bank0
#include
#include
PICC
C printf/sprintf
printf/sprintf
C scanf printf PICC
getch() putch() scanf/printf
getch() putch()
11.7 PICC
PICC
11.7.1
PIC PICC
C
__CONFIG (HS & UNPROTECT & PWRTEN & BORDIS & WDTEN);
__CONFIG
pic.h
HEX
16F7x
/* */
#define RC 0x3FFF // RC
#define HS 0x3FFE // HS
PDF created with pdfFactory Pro trial version 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 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
PDF created with pdfFactory Pro trial version www.pdffactory.com
2004-4-7 16 of 26
PICC Configure Select Device
MPLAB 11-1
#include
pic.h
MPLAB
pic.h
#include
11.8.2 PICC General
3.2.7 3-20
PICC MPAMS 11-3 PICC
Include Path
11.8.3 PICC PICC Global
C 11-4
Compile for MPLAB ICD ICD C
ICD
Treat ‘char’ as signed PICC ‘char’
11-3 PICC 11-4 PICC
PDF created with pdfFactory Pro trial version www.pdffactory.com
2004-4-7 17 of 26
‘char’
Floating point ‘double’ width PICC
‘double’ 24 float
32
11.8.4 C PICC Compiler
C C C
General Advanced
11-5A 11-5B
C
9
20% 2K PICC
volatile
PICC
A B
11-5 C
PDF created with pdfFactory Pro trial version www.pdffactory.com
2004-4-7 18 of 26
C
Generate assembly list file C *.lst
C
C
C
Compile to assembly only C
*.as C
C C
11.8.5 PICC Linker
PICC Linker
11-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
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 MPASM
PICC
*.as MPASM *.asm
PDF created with pdfFactory Pro trial version www.pdffactory.com
2004-4-7 20 of 26
PICC PIC C
11-7 Enable optimization ,
11.9 C
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
11-7 PICC
PDF created with pdfFactory Pro trial version 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
C
C 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 PICC
C
bank bank
11-11
PDF created with pdfFactory Pro trial version 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
C
bank
bank bank0
0x00 PIC bank
PICC
bank bank
11.9.3 C
PICC
bank0
C
11-5(B) Compile to assembly only C PICC
C
11-12 C
//C
void Test(unsigned char inVar1, inVar2)
PDF created with pdfFactory Pro trial version 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 C
11-13
//C
void Test(unsigned char inVar1, inVar2)
{
PDF created with pdfFactory Pro trial version 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
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 // 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 PICC
C
PICC
PICC
PDF created with pdfFactory Pro trial version www.pdffactory.com
2004-4-7 25 of 26
C
C
C
11-14 C PIC
count
C
C
*.as
PICC C
PICC Hitech
PICC
C C
C
C
11-15
bit 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
2004-4-7 26 of 26
C
PDF created with pdfFactory Pro trial version www.pdffactory.com
本文档为【PIC单片机C语言教程】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑,
图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。