h hii-tech picc-16 -tech picc-16 c compiler workshop€¦ · /* * header file for the microchip *...

116
© 2003 Microchip Taiwan. All Rights Reserved. HI-TECH PICC-16 C Compiler Workshop 1 1 HI-TECH PICC-16 HI C Compiler Workshop HI - - TECH PICC TECH PICC - - 16 16 C Compiler Workshop C Compiler Workshop

Upload: others

Post on 22-Oct-2019

16 views

Category:

Documents


1 download

TRANSCRIPT

© 2003 Microchip Taiwan. All Rights Reserved. HI-TECH PICC-16 C Compiler Workshop 11

HI-TECH PICC-16 HIC Compiler WorkshopHI--TECH PICCTECH PICC--16 16

C Compiler WorkshopC Compiler Workshop

© 2003 Microchip Taiwan. All Rights Reserved. HI-TECH PICC-16 C Compiler Workshop 22

課程說明課程說明課程說明

C

C

MPLAB C18

ANSI C C

C

Hi-Tech PICC

PIC16F877A

C

C

MPLAB C18

ANSI C C

C

Hi-Tech PICC

PIC16F877A

© 2003 Microchip Taiwan. All Rights Reserved. HI-TECH PICC-16 C Compiler Workshop 33

Compiler OverviewCompiler OverviewCompiler Overview

© 2003 Microchip Taiwan. All Rights Reserved. HI-TECH PICC-16 C Compiler Workshop 44

Compiler OverviewCompiler OverviewCompiler Overview

21Compiler Activation

21Compiler Activation

© 2003 Microchip Taiwan. All Rights Reserved. HI-TECH PICC-16 C Compiler Workshop 55

MPLAB IDE 使用 Hi-Tech PICC MPLAB IDE MPLAB IDE 使用使用 HiHi--Tech PICC Tech PICC 1. “Project Select Language Toolsuite”

HI-TECH PICC

C:\HT-PIC

1. “Project Select Language Toolsuite”

HI-TECH PICC

C:\HT-PIC

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

66

MPLAB IDE 使用

Hi-Tech PICC

MPLAB IDE

MPLAB IDE 使用

使用

HiHi-- Tech PICC

Tech PICC

2.

“P

roje

ct

Set

Lan

gu

ag

e T

oo

l L

ocati

on

s”

HI-

TE

CH

PIC

C

MP

LA

B ID

E

C:\

HT

-PIC

\BIN

\PIC

C.e

xe

2.

“P

roje

ct

Set

Lan

gu

ag

e T

oo

l L

ocati

on

s”

HI-

TE

CH

PIC

C

MP

LA

B ID

E

C:\

HT

-PIC

\BIN

\PIC

C.e

xe

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

77

PICC 介紹

(一)

PICC

PI

CC 介紹

介紹

(( 一一))

Hi-T

ech

PIC

C -

co

mm

an

d lin

e d

rive

r (C

LD

)

CL

D: M

PLA

B ID

E

HP

DP

IC -

Hi-T

ech ID

E

PIC

C C–

.c

–.a

s

Hi-T

ech

PIC

C -

co

mm

an

d lin

e d

rive

r (C

LD

)

CL

D: M

PLA

B ID

E

HP

DP

IC -

Hi-T

ech ID

E

PIC

C C–

.c

–.a

s

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

88

PICC 介紹

(二)

PICC

PI

CC 介紹

介紹

(( 二二))

PIC

C Re

loca

tab

leo

bje

ct

file

s –

.ob

j

Lib

rary

file

s –

.lib

Hi-T

ech P

ICC

A

NS

I C

PIC

C Re

loca

tab

leo

bje

ct

file

s –

.ob

j

Lib

rary

file

s –

.lib

Hi-T

ech P

ICC

A

NS

I C

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

99

PICC 執行檔的說明(一

)PICC

PICC

執行檔的說明

執行檔的說明

(( 一一))

Hi-T

ech P

ICC

:

CP

P -

Th

e p

re-p

roce

sso

r

P1

-T

he

pa

rse

r

Hi-T

ech P

ICC

:

CP

P -

Th

e p

re-p

roce

sso

r

P1

-T

he

pa

rse

r

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

10

10

PICC 執行檔的說明(二

)PICC

PICC

執行檔的說明

執行檔的說明

(( 二二))

CG

PIC

-T

he

co

de

ge

ne

rato

r

AS

PIC

-T

he

asse

mb

ler

(Re

loca

tab

leo

bje

ct file

)

HL

INK

-T

he

lin

ke

r

RA

M

CG

PIC

-T

he

co

de

ge

ne

rato

r

AS

PIC

-T

he

asse

mb

ler

(Re

loca

tab

leo

bje

ct file

)

HL

INK

-T

he

lin

ke

r

RA

M

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

11

11

PICC 執行檔的說明(三

)PICC

PICC

執行檔的說明

執行檔的說明

(( 三三))

OB

JT

OH

EX

-T

he o

utp

ut file

convert

er

Hex

CR

OM

WE

LL

-T

he

re

-fo

rma

tte

r

OB

JT

OH

EX

-T

he o

utp

ut file

convert

er

Hex

CR

OM

WE

LL

-T

he

re

-fo

rma

tte

r

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

12

12

編譯流程

(一)

編譯流程

編譯流程

(( 一一))

使用著的

輸入檔案

.c

file

s

.as

file

s

.obj

file

s

.lib

file

s.h

file

s

clis

t

cpp

p1

cgp

ic

cpp

-pre

(pre

-pro

ce

ss)

-p (

pre

-pro

ce

ss a

ssem

ble

r file

s)

-g (

genera

te d

ebug info

)

-s (

com

pile

to a

ssem

ble

r file

s)

編譯器

輸出檔案

編譯器支援

的輸入檔案

.lst

file

s

.h

file

s.p

re

file

s

.sd

b

file

s

.in

i

file

s

.as

file

s

Ch

ip I

nfo

fil

e

(\L

IB\p

icin

fo.i

ni)

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

13

13

編譯流程

(二)

編譯流程

編譯流程

(( 二二))

asp

ic

hli

nk

編譯器

輸出檔案

Ru

n-t

ime

file

ob

jtoh

ex

cro

mw

ell

.obj

.lib

Cas

-m (

ma

p f

ile)

-c (

com

pile

to o

bj)

Ou

tpu

t hex f

orm

at

MP

LA

B I

DE

debug info

.lst

file

s

-asm

list

(genera

te a

sm

lis

ting)

編譯器支援

的輸入檔案

.ob

j

file

s.o

bj

file

s

.map

file

s

.sy

m

file

s

.hex

file

s

.lib

file

sd

ebu

g

file

sli

bra

ry f

ile

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

14

14

Hi-Tech PICC 輸入檔案

HiHi-- Tech PICC

Tech PICC 輸入檔案

輸入檔案

HI-

Te

ch

Lib

rary

file

.lib

Hi-T

EC

H P

ICC

ob

ject

Obje

ct file

.ob

j

Hi-T

ech

PIC

C

Asse

mb

ler

file

.as

CH

eader

file

.h

Hi-T

ech

PIC

C

AN

SI C

C

sourc

e file

.c

檔案內容說明

名稱

附加檔

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

15

15

其它檔案說明

其它檔案說明

其它檔案說明

ab

so

lute

ad

dre

ss o

f p

rog

ram

sym

bo

lsS

ym

bo

l file

.sym

ob

ject

na

me

s a

nd

typ

es f

or

mo

du

leS

ym

bo

licdebug f

ile.s

db

C s

ou

rce

with

co

rre

sp

on

din

g

asse

mb

ler

instr

uctio

nA

sse

mb

ler

listin

g.lst

sym

bo

l a

nd

psectre

loca

tio

nin

form

atio

nm

ap

file

.ma

p

C s

ou

rce

co

de

with

lin

e

nu

mb

ers

C lis

tin

g f

ile.lst

檔案內容說明

名稱

附加檔

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

16

16

特殊功能暫存器

(SFR

)特殊功能暫存器

特殊功能暫存器

(( SFR

)SF

R)

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

17

17

特殊功能暫存器

特殊功能暫存器

特殊功能暫存器

CS

FR

C@

SF

R

C

SF

R

PIC

mic

ro

he

ad

er

file

s

<p

ic.h

>

PIC

mic

ro

He

ad

er

File

CS

FR

C@

SF

R

C

SF

R

PIC

mic

ro

he

ad

er

file

s

<p

ic.h

>

PIC

mic

ro

He

ad

er

File

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

18

18

PIC.

hPI

C.h

PIC.

hp

ic.h

? (

C:\

HT

-PIC

\in

clu

de

\pic

.h )

#ifndef

_P

IC_H

#define

_P

IC_H

#if

defined(_

12C

508)

||defined(_

12C

509)

||\

defined(_

12C

508A

)|| d

efined(_

12C

509A

)||\

defined(_

12C

E518)

|| d

efined(_

12C

E519)

||\

defined(_

12C

509A

G)

|| d

efined(_

12C

509A

F)

||\

defined(_

12C

R509A

)||

defined(_

RF

509A

G)

||\

defined(_

RF

509A

F)

#in

clu

de

<pic

125xx.h

>

#endif

#if

defined(_

16C

432)

||defined(_

16C

433)

#in

clu

de

<pic

1643x.h

>

#endif

#if

defined(_

16C

52)

|| d

efined(_

16C

54)

|| d

efined(_

16C

54A

)||\

defined(_

16C

54B

)|| d

efined(_

16C

54C

)|| d

efined(_

16C

R54A

)||\

defined(_

16C

R54B

)|| d

efined(_

16C

R54C

)|| d

efined(_

16C

55)

||\

defined(_

16C

55A

)|| d

efined(_

16C

56)

|| d

efined(_

16C

56A

)||\

defined(_

16C

R56A

)|| d

efined(_

16C

57)

|| d

efined(_

16C

57C

)||\

defined(_

16C

R57B

)|| d

efined(_

16C

R57C

)|| d

efined(_

16C

58A

)||\

defined(_

16C

58B

)|| d

efined(_

16C

R58A

)|| d

efined(_

16C

R58B

)||\

defined(_

16C

58)

|| d

efined(_

16H

V540)

#in

clu

de

<pic

165x.h

>

#endif

pic

.h?

( C

:\H

T-P

IC\in

clu

de

\pic

.h )

#ifndef

_P

IC_H

#define

_P

IC_H

#if

defined(_

12C

508)

||defined(_

12C

509)

||\

defined(_

12C

508A

)|| d

efined(_

12C

509A

)||\

defined(_

12C

E518)

|| d

efined(_

12C

E519)

||\

defined(_

12C

509A

G)

|| d

efined(_

12C

509A

F)

||\

defined(_

12C

R509A

)||

defined(_

RF

509A

G)

||\

defined(_

RF

509A

F)

#in

clu

de

<pic

125xx.h

>

#endif

#if

defined(_

16C

432)

||defined(_

16C

433)

#in

clu

de

<pic

1643x.h

>

#endif

#if

defined(_

16C

52)

|| d

efined(_

16C

54)

|| d

efined(_

16C

54A

)||\

defined(_

16C

54B

)|| d

efined(_

16C

54C

)|| d

efined(_

16C

R54A

)||\

defined(_

16C

R54B

)|| d

efined(_

16C

R54C

)|| d

efined(_

16C

55)

||\

defined(_

16C

55A

)|| d

efined(_

16C

56)

|| d

efined(_

16C

56A

)||\

defined(_

16C

R56A

)|| d

efined(_

16C

57)

|| d

efined(_

16C

57C

)||\

defined(_

16C

R57B

)|| d

efined(_

16C

R57C

)|| d

efined(_

16C

58A

)||\

defined(_

16C

58B

)|| d

efined(_

16C

R58A

)|| d

efined(_

16C

R58B

)||\

defined(_

16C

58)

|| d

efined(_

16H

V540)

#in

clu

de

<pic

165x.h

>

#endif

PIC.h 依據所選

定的元件來決定使

用那一個元件的定

義檔案

PIC.h 依據所選

定的元件來決定使

用那一個元件的定

義檔案

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

19

19

元件的

PIC168xA.h

元件的

元件的

PIC168xA.h

PIC168xA.h

/* *H

eader

file

for

the M

icro

chip

*P

IC 1

6F

873A

chip

*P

IC 1

6F

874A

chip

*P

IC 1

6F

876A

chip

*P

IC 1

6F

877A

chip

*M

idra

nge M

icro

contr

olle

r

*/ #if d

efined(_

16F

874A

)|| d

efined(_

16F

877A

)

#define

__P

INS

_40

#endif

sta

tic v

ola

tile

unsig

ned c

har

IND

F@

0x00;

sta

tic v

ola

tile

unsig

ned c

har

TM

R0

@ 0

x01;

sta

tic v

ola

tile

unsig

ned c

har

PC

L@

0x02;

sta

tic v

ola

tile

unsig

ned c

har

ST

AT

US

@ 0

x03;

sta

tic

unsig

ned c

har

FS

R@

0x04;

sta

tic v

ola

tile

unsig

ned c

har

PO

RT

A@

0x05;

sta

tic v

ola

tile

unsig

ned c

har

PO

RT

B@

0x06;

sta

tic v

ola

tile

unsig

ned c

har

PO

RT

C@

0x07;

#ifdef__P

INS

_40

sta

tic v

ola

tile

unsig

ned c

har

PO

RT

D@

0x08;

sta

tic v

ola

tile

unsig

ned c

har

PO

RT

E@

0x09;

#endif

/* *H

eader

file

for

the M

icro

chip

*P

IC 1

6F

873A

chip

*P

IC 1

6F

874A

chip

*P

IC 1

6F

876A

chip

*P

IC 1

6F

877A

chip

*M

idra

nge M

icro

contr

olle

r

*/ #if d

efined(_

16F

874A

)|| d

efined(_

16F

877A

)

#define

__P

INS

_40

#endif

sta

tic v

ola

tile

unsig

ned c

har

IND

F@

0x00;

sta

tic v

ola

tile

unsig

ned c

har

TM

R0

@ 0

x01;

sta

tic v

ola

tile

unsig

ned c

har

PC

L@

0x02;

sta

tic v

ola

tile

unsig

ned c

har

ST

AT

US

@ 0

x03;

sta

tic

unsig

ned c

har

FS

R@

0x04;

sta

tic v

ola

tile

unsig

ned c

har

PO

RT

A@

0x05;

sta

tic v

ola

tile

unsig

ned c

har

PO

RT

B@

0x06;

sta

tic v

ola

tile

unsig

ned c

har

PO

RT

C@

0x07;

#ifdef__P

INS

_40

sta

tic v

ola

tile

unsig

ned c

har

PO

RT

D@

0x08;

sta

tic v

ola

tile

unsig

ned c

har

PO

RT

E@

0x09;

#endif

選定PIC16F877A的元

件後,暫存器的定義

將會使用PIC168xA.h

的宣告

選定PIC16F877A的元

件後,暫存器的定義

將會使用PIC168xA.h

的宣告

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

20

20

練習一

Hi-T

ech PICC 工作環境設定

練習一

練習一

HiHi-- Tech PICC

Tech PICC 工作環境設定

工作環境設定

Hi-

TE

CH

PIC

C

MP

LA

B I

DE

Hi-T

ech

PIC

C

(Pro

ject)

: L

ab

1.c

He

ad

file

s : p

ic.h

, c

on

fig

87

7a

.h

PIC

17

F8

77

A

MP

LA

B IC

D2

Hi-

TE

CH

PIC

C

MP

LA

B I

DE

Hi-T

ech

PIC

C

(Pro

ject)

: L

ab

1.c

He

ad

file

s : p

ic.h

, c

on

fig

87

7a

.h

PIC

17

F8

77

A

MP

LA

B IC

D2

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

21

21

練習一

Hi-Tech PICC思考問題

練習一

練習一

HiHi-- Tech PICC

Tech PICC 思考問題

思考問題

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

22

22

資料型態

變數種類

I/O 的定址

資料型態

資料型態

變數種類

變數種類

I/O

I/O 的定址

的定址

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

23

23

資料型態

資料型態

資料型態

(b

its)

bit

1

bo

ole

an

ch

ar

8-1

28

to

12

7 a

un

sig

ne

d c

ha

r 8

0 t

o 2

55

s

ho

rt

16

-3

27

68

to3

27

67

un

sig

ne

d s

ho

rt16

0 t

o 6

55

35

int

16

-3

27

68

to3

27

67

un

sig

ne

d i

nt

16

0 t

o 6

55

35

lo

ng

32

-2,1

47

,48

3,6

48

to

+2

,14

7,4

83

,64

7u

ns

ign

ed

lo

ng

3

20

to

4,2

94

,96

7,9

25

flo

at

24

real

do

ub

le2

4 o

r 3

2b

rea

l

A.

Ach

ar

is u

nsig

ne

d b

yd

efa

ult

.B

.A

do

ub

led

efa

ult

s t

o 2

4-b

it.

Easil

y c

ha

ng

ed

to

32 b

its

wit

hin

th

e H

PD

PIC

, P

ICC

co

mm

an

d l

ine

in

terf

ac

e o

r w

ith

inM

PL

AB

-ID

E.

Flo

ati

ng

po

int

is i

mp

lem

en

ted

usin

g t

he I

EE

E754 3

2-b

it.

(b

its)

bit

1

bo

ole

an

ch

ar

8-1

28

to

12

7 a

un

sig

ne

d c

ha

r 8

0 t

o 2

55

s

ho

rt

16

-3

27

68

to3

27

67

un

sig

ne

d s

ho

rt16

0 t

o 6

55

35

int

16

-3

27

68

to3

27

67

un

sig

ne

d i

nt

16

0 t

o 6

55

35

lo

ng

32

-2,1

47

,48

3,6

48

to

+2

,14

7,4

83

,64

7u

ns

ign

ed

lo

ng

3

20

to

4,2

94

,96

7,9

25

flo

at

24

real

do

ub

le2

4 o

r 3

2b

rea

l

A.

Ach

ar

is u

nsig

ne

d b

yd

efa

ult

.B

.A

do

ub

led

efa

ult

s t

o 2

4-b

it.

Easil

y c

ha

ng

ed

to

32 b

its

wit

hin

th

e H

PD

PIC

, P

ICC

co

mm

an

d l

ine

in

terf

ac

e o

r w

ith

inM

PL

AB

-ID

E.

Flo

ati

ng

po

int

is i

mp

lem

en

ted

usin

g t

he

IEE

E 7

54

32-b

it.

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

24

24

變數的等級

變數的等級

變數的等級

1.Au

to–

2. Static

-AN

SI-C

3. Global–

main( )

4. Extern–

Extern

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

25

25

變數的型態

變數的型態

變數的型態

1. Const

-const

2. Volatile–

volatile

/ (volatile)

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

26

26

特殊的變數型態

特殊的變數型態

特殊的變數型態

1. Persisten

t–

(startup)

( for example, the nvram

or nvram_1

psects

)

2. Bank1, Bank2 and Bank3–

RAM Bank 1, RAM

Bank 2

RAM Bank 3

RAM Bank

RAM Bank 0

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

27

27

絕對位址變數

絕對位址變數

絕對位址變數

1.Absolute–

“@

address”

(

)

:volatile unsigned char PORTB @ 0x06

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

28

28

絕對定址使用範例

絕對定址使用範例

絕對定址使用範例

定義特殊暫存器

(FSR)的絕對位址的範例

(PIC168xA.H)

/*bank 0

regis

ters

*/

sta

tic v

ola

tile

unsig

ned c

har

IND

F@

0x00;

sta

tic v

ola

tile

unsig

ned c

har

TM

R0

@ 0

x01;

sta

tic v

ola

tile

un

sig

ne

d c

ha

rP

CL

@ 0

x0

2;

sta

tic v

ola

tile

un

sig

ne

d c

ha

rS

TA

TU

S@

0x0

3;

sta

tic

unsig

ned

char

FS

R@

0x04;

:

/*bank 1

regis

ters

*/

sta

tic u

nsig

ned c

har

bank1

OP

TIO

N @

0x81;

sta

tic v

ola

tile

un

sig

ne

d c

ha

r b

an

k1

TR

ISA

@ 0

x8

5;

sta

tic v

ola

tile

un

sig

ne

d c

ha

r bank1

TR

ISA

@ 0

x85;

sta

tic v

ola

tile

un

sig

ne

d c

ha

r b

an

k1

TR

ISB

@ 0

x8

6;

sta

tic v

ola

tile

un

sig

ne

d c

ha

r bank1

TR

ISB

@ 0

x86;

sta

tic v

ola

tile

un

sig

ne

d c

ha

r b

an

k1

TR

ISC

@ 0

x8

7;

sta

tic v

ola

tile

un

sig

ne

d c

ha

r bank1

TR

ISC

@ 0

x87;

:

定義特殊暫存器(FSR)的絕對位址的範例

(PIC168xA.H)

/*bank 0

regis

ters

*/

sta

tic v

ola

tile

unsig

ned c

har

IND

F@

0x00;

sta

tic v

ola

tile

unsig

ned c

har

TM

R0

@ 0

x01;

sta

tic v

ola

tile

un

sig

ne

d c

ha

rP

CL

@ 0

x0

2;

sta

tic v

ola

tile

un

sig

ne

d c

ha

rS

TA

TU

S@

0x0

3;

sta

tic

unsig

ned

char

FS

R@

0x04;

:

/*bank 1

regis

ters

*/

sta

tic

unsig

ned c

har

bank1

OP

TIO

N

@ 0

x81;

:

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

29

29

位元

(bit) 變數定址能力

位元

位元

(( bit)

bit) 變數定址能力

變數定址能力

Hi-

Te

ch

C

bit

Hi-

Te

ch

C

AN

SI C

Hi-

Te

ch

C

bit

Hi-

Te

ch

C

AN

SI C

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

30

30

位元

(bit) 變數定址

-與絕對位址變數共用

位元

位元

(( bit)

bit) 變數定址

變數定址

--與絕對位址變數共用

與絕對位址變數共用

0

: (

8 b

its *

SF

R

) +

範例

: 存取

Z 旗號

sta

tic u

nsig

ned c

har

ST

AT

US

@

0x03 ;

sta

tic b

it Z

ER

O @

(u

nsig

ned)

& S

TA

TU

S*8

+ 2

;

存取

PORT

A 的

RA5

sta

tic v

ola

tile

unsig

ned c

har

PO

RT

A @

0x05 ;

sta

tic v

ola

tile

bit R

A5 @

(unsig

ned)

& P

OR

TA

*8 +

5 ;

0

: (

8 b

its *

SF

R

) +

範例

: 存取

Z 旗號

sta

tic u

nsig

ned c

har

ST

AT

US

@

0x03 ;

sta

tic b

it Z

ER

O @

(u

nsig

ned)

& S

TA

TU

S*8

+ 2

;

存取

PORT

A 的

RA5

sta

tic v

ola

tile

unsig

ned c

har

PO

RT

A @

0x05 ;

sta

tic v

ola

tile

bit R

A5 @

(unsig

ned)

& P

OR

TA

*8 +

5 ;

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

31

31

位元

(bit) 變數定址

-與絕對位址變數共用

位元

位元

(( bit)

bit) 變數定址

變數定址

-- 與絕對位址變數共用

與絕對位址變數共用

HI-

TE

CH

C

(PIC

16

8xA

.h)

/*S

TA

TU

S b

its

*/

sta

tic v

ola

tile

bit

IRP

@ (

un

sig

ned

)&S

TA

TU

S*8

+7;

sta

tic v

ola

tile

bit

RP

1@

(u

nsig

ned

)&S

TA

TU

S*8

+6;

sta

tic v

ola

tile

bit

RP

0@

(u

nsig

ned

)&S

TA

TU

S*8

+5;

sta

tic v

ola

tile

bit

TO

@ (

un

sig

ned

)&S

TA

TU

S*8

+4;

sta

tic v

ola

tile

bit

PD

@ (

un

sig

ned

)&S

TA

TU

S*8

+3;

sta

tic v

ola

tile

bit

ZE

RO

@

(u

nsig

ned

)&S

TA

TU

S*8

+2;

sta

tic v

ola

tile

bit

DC

@ (

un

sig

ned

)&S

TA

TU

S*8

+1;

sta

tic v

ola

tile

bit

CA

RR

Y @

(u

ns

ign

ed

)&S

TA

TU

S*8

+0;

Z,

If (

ZE

RO

)…

……

……

……

……

……

HI-

TE

CH

C

(PIC

16

8xA

.h)

/*S

TA

TU

S b

its

*/

sta

tic v

ola

tile

bit

IRP

@ (

un

sig

ned

)&S

TA

TU

S*8

+7;

sta

tic v

ola

tile

bit

RP

1@

(u

nsig

ned

)&S

TA

TU

S*8

+6;

sta

tic v

ola

tile

bit

RP

0@

(u

nsig

ned

)&S

TA

TU

S*8

+5;

sta

tic v

ola

tile

bit

TO

@ (

un

sig

ned

)&S

TA

TU

S*8

+4;

sta

tic v

ola

tile

bit

PD

@ (

un

sig

ned

)&S

TA

TU

S*8

+3;

sta

tic v

ola

tile

bit

ZE

RO

@

(u

nsig

ned

)&S

TA

TU

S*8

+2;

sta

tic v

ola

tile

bit

DC

@ (

un

sig

ned

)&S

TA

TU

S*8

+1;

sta

tic v

ola

tile

bit

CA

RR

Y @

(u

ns

ign

ed

)&S

TA

TU

S*8

+0;

Z,

If (

ZE

RO

)…

……

……

……

……

……

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

32

32

位元

(bit) 變數定址

-使用

定義位元變數

位元

位元

(( bit)

bit) 變數定址

變數定址

--使用

使用

定義位元變數

定義位元變數

bit

Hi-T

ech C

B

yte

範例

: 利用

bit 定義位元變數

sta

tic b

it C

ount_

Fla

g ;

sta

tic b

it B

uzzer_

1_F

lag ;

Bu

zze

r_1

_F

lag

= 1

;

if (

Count_

Fla

g)

Count_

Fla

g =

0 ;

bit

Hi-T

ech C

B

yte

範例

: 利用

bit 定義位元變數

sta

tic b

it C

ount_

Fla

g ;

sta

tic b

it B

uzzer_

1_F

lag ;

Bu

zze

r_1

_F

lag

= 1

;

if (

Cou

nt_

Fla

g)

Count_

Fla

g =

0 ;

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

33

33

準備練習二

關於

LCD 顯示函數

準備練習二

準備練習二

關於

關於

LCD

LCD 顯示函數

顯示函數

LC

D F

unctions (

mid

_lc

d.c

)

vo

idO

pe

nL

CD

(vo

id)

;

void

puts

LC

D(

char

* )

;

void

putr

sLC

D(

const char

* )

;

vo

idp

utc

LC

D(

un

sig

ne

d c

ha

r )

;

vo

idp

uth

exL

CD

( u

nsig

ne

d c

ha

r )

;

vo

idS

etC

urs

orL

CD

( u

nsig

ne

d c

ha

r , u

nsig

ne

d c

ha

r )

;

//(

X,Y

)

LC

D F

unctions (

mid

_lc

d.c

)

vo

idO

pe

nL

CD

(vo

id)

;

void

puts

LC

D(

char

* )

;

void

putr

sLC

D(

const char

* )

;

vo

idp

utc

LC

D(

un

sig

ne

d c

ha

r )

;

vo

idp

uth

exL

CD

( u

nsig

ne

d c

ha

r )

;

vo

idS

etC

urs

orL

CD

( u

nsig

ne

d c

ha

r , u

nsig

ne

d c

ha

r )

;

//(

X,Y

)

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

34

34

練習二

基本I/O的設定

練習二

練習二

基本

基本

I/O

I/O 的設定

的設定

lab2.m

cp

lab2.m

cp

Hi-T

ech P

ICC

Ex2

Up: 01 D

ow

n: F

EH

i-T

ech P

ICC

Ex2

Up: 01 D

ow

n: F

E

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

35

35

結構變數

共用變數

位元結構

結構變數

結構變數

共用變數

共用變數

位元結構

位元結構

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

36

36

結構型態

(Structures)

結構型態

結構型態

(( Structures)

Structures)

“.

&

()

“.

&

()

struct

struct-name

{type

member1;

type

member2;

. . .

}variable-name;

}

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

37

37

使用基本的結構變數

使用基本的結構變數

使用基本的結構變數

“.”

:variable-name.memberx

struct Comm_protocol

{

char ID[6];

char Data[10];

char

Message[20];

unsigned int CRC;

unsigned char Repeat;

} Rec_Fram;

: :

unsigned char j;

for(j=0;j<20;j++)

{ writeUSART(Rec_Fram.Message[j]);

}

Comm_protocol

RA

MComm_protocol

RA

MR

AM

ID6

Byte

s

Data

10

Byte

s

Mess

age

20

Byte

s

CR

C2

Byte

s

Rep

eat

1 B

yte

s

Rec_F

ram

ID6

Byte

s

Data

10

Byte

s

Mess

age

20

Byte

s

CR

C2

Byte

s

Rep

eat

1 B

yte

s

Rec_F

ram

ID6

Byte

s

Data

10

Byte

s

Mess

age

20

Byte

s

CR

C2

Byte

s

Rep

eat

1 B

yte

s

Rec_F

ram

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

38

38

存取結構中的陣列

(一)

存取結構中的陣列

存取結構中的陣列

(( 一一))

範例

1 :

str

uct

filt

ere

d_d

ata

{

ch

ar

Fb

an

dg

ap

[4];

ch

ar

Fre

fhi[

4];

ch

ar

Fre

flo

[4];

ch

ar

Fte

mp

[4];

} F

co

un

t;

“. “

:

Fco

un

t.F

ban

dg

ap

[1]

= 0

x34;

“Fbandgap”

0x34

.

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

39

39

存取結構中的陣列

(二)

使用結構指標

存取結構中的陣列

存取結構中的陣列

(( 二二))

使用結構指標

使用結構指標

範例

2 :

1 :

.

2 :

->

1“fi

ltere

d_d

ata

: str

uct

filt

ere

d_d

ata

*p

tr;

ptr

= &

Fco

un

t;

ptr

->F

refh

i[0]

= 0

x87;

“F

co

un

t.Freghi”

0x87

.

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

40

40

在宣告同時指定結構變數的初始值

在宣告同時指定結構變數的初始值

在宣告同時指定結構變數的初始值

範例

3 :

str

uct

tem

p

{

int

co

un

t;

flo

at

freq

;

ch

ar

sig

n;

} p

wm

= {

0x1234,

1850.5

,

0x

FF

};

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

41

41

以結構為元素的陣列

以結構為元素的陣列

以結構為元素的陣列

範例

4 :

str

uct

co

ntr

ol {

ch

ar

mo

de

;

ch

ar

sta

te;

ch

ar

sig

n;

} d

rive[3

];

“mode”

:

dri

ve[0

].m

od

e =

0x33;

“sign”

:

dri

ve[2

].sig

n =

0xF

F;

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

42

42

共用型態(union)

共用型態(

共用型態(

unio

n)un

ion)

(un

ion

)

(unio

n)

(Str

uctu

re)

(unio

n)

(un

ion

)

(unio

n)

(Str

uctu

re)

(unio

n)

union union-name

{type member1;

type member2;

. . .

} variable-name;

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

43

43

共用型態的資料架構

共用型態的資料架構

共用型態的資料架構

unionEE_tag

{

int Word;

charByte[2];

} TC_74;

unionEE_tag

{

int

Word;

charByte[2];

} TC_74;

union

{

int

Word;

char

Bytes[2];

} TC_74;

for (i=0;i<5;i++)

{

EE_Addr=i;

TC_74.Word = EERandomRead(0xA0,EE_Addr);

if ( TC_74.Word >= 0 )

Secu_Code[i]= TC_74.Bytes[0];

}

union

{

int

Word;

char

Bytes[2];

} TC_74;

for(i=0;i<5;i++)

{

EE_Addr=i;

TC_74.Word = EERandomRead(0xA0,EE_Addr);

if ( TC_74.Word >= 0 )

Secu_Code[i]=

TC_74.Bytes[0];

}

EE

Ra

nd

om

Rea

d(

)

“in

t”“u

nio

n”

“ch

ar”

“L

ow

-by

te”

**

EE

Ra

nd

om

Rea

d(

)

>=

0

!!

EE

Ra

nd

om

Rea

d(

)

“in

t”“u

nio

n”

“ch

ar”

“L

ow

-by

te”

**

EE

Ra

nd

om

Rea

d(

)

>=

0

!!

Wo

rd

Byte

[0]

Byte

[1]

RA

M0

RA

M1

EE

_ta

gBytes

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

44

44

共用型態和成員的存取

(一)

共用型態和成員的存取

共用型態和成員的存取

(( 一一))

範例

1 : u

nio

n u

_ta

g {

ch

ar

ab

c;

// 8 b

its

int

va

lue

; // 1

6 b

its

} u

tem

p; ‘.’ -utemp.abc

= ‘A

’;

-utemp.value

= 0

x3456;

範例

2 : u

nio

n u

_ta

g

*up

tr;

up

tr=

&u

tem

p;

‘->

’-uptr->value

= 0

x5678;

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

45

45

共用型態和成員的存取

(二)

共用型態和成員的存取

共用型態和成員的存取

(( 二二))

範例

: 在整數

(16-

bit)中單獨存取其中一個

8-bi

t 值

un

ion

{ un

sig

ned

in

t v

ar;

str

uct

{ ch

ar

var_

lo;

ch

ar

var_

hi;

} h

ilo

;

} m

ix;

ch

ar

a,

b;

vo

id m

ain

( v

oid

)

{

mix

.var

= 0

x1234;

a =

mix

.hil

o.v

ar_

lo;

b =

mix

.hil

o.v

ar_

hi;

}

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

46

46

合併使用結構型態

& 共用型態

合併使用

合併使用結構型態

結構型態

& & 共用型態

共用型態

union

FPvar

{

float

FPNum;

//floating point access

struct

{

unsigned char Arg0;

//argument byte 0 access

unsigned char Arg1;

//argument byte 1 access

unsigned char Arg2;

//argument byte 2 access

unsigned char Exp;

//exponent byte access

}Bytes;

} Foo;

Foo.FPNum

= 3.14159;

Exponent = Foo.Bytes.Exp -

0x7F;

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

47

47

位元結構

位元結構

位元結構

1 :

Byte

str

uct

{

un

sig

ned

in

t:

3;

// b

it p

ad

din

g

un

sig

ned

in

tb

3:

1;

// b

it 3

un

sig

ned

in

tb

4:

1;

// b

it 4

un

sig

ned

in

t:

3;

// b

it p

ad

din

g

} P

CL

AT

Hb

it @

0x

0A

;

C“b

3”

0x0A

RA

M

PC

LA

TH

bit

.b3 =

1;

:

(@

)

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

48

48

位元結構

位元結構

位元結構

2 :

Wo

rd

str

uct

sta

tus {

un

sig

ned

in

t h

igh

:

1;

// L

Sb

un

sig

ned

in

t l

ow

: 1;

un

sig

ned

in

t:

5;

//

bit

pad

din

g

un

sig

ned

in

t d

ir:

1;

un

sig

ned

in

t r

ate

:

1;

un

sig

ned

in

t:

6;

//

bit

pad

din

g

un

sig

ned

in

t f

au

lt :

1;

// M

Sb

} p

ressu

re;

C“dir”:

pre

ssu

re.d

ir =

1;

:

16

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

49

49

位位元的使用範例(一

)位元的使用範例

位元的使用範例

(( 一一))

1:

un

ion

{

un

sig

ned

ch

ar

var;

str

uc

t{

un

sig

ned

in

t b

it0

: 1;

un

sig

ned

in

t :

6;

//p

ad

din

g

un

sig

ned

in

t b

it7

: 1;

} b

its;

} u

var;

:

uvar.

var

8 b

its

uvar.

bit

s.b

it7

bit

7

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

50

50

位元的使用範例(二

)位元的使用範例

位元的使用範例

(( 二二))

2:

: sta

tic b

it F

low

@ (u

nsig

ned

)&P

OR

TB

*8+

4;

PO

RT

Bb

it4

“Flow”

:

Flo

w =

1;

3:

: sta

tic b

it M

oto

r @

(u

nsig

ned

)&P

OR

TC

*8+

3;

“Motor”

PO

RT

Cb

it3 :

if (

Mo

tor)

{

// S

tate

men

ts w

hen

Mo

tor

==

1

}

els

e

{

// S

tate

men

ts w

hen

Mo

tor

!= 1

}

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

51

51

位元的使用範例(三

)位元的使用範例

位元的使用範例

(( 三三))

4:

:

#d

efi

ne P

ort

Bit

(po

rt,b

it)

((u

nsig

ned

)&(p

ort

)*8+

(bit

))

:

sta

tic b

it led

8 @

Po

rtB

it(P

OR

TB

,8);

sta

tic b

it p

uls

e @

Po

rtB

it(P

OR

TC

,7);

#d

efi

ne

#d

efi

ne S

1_B

utt

on

RA

4;

#d

efi

ne S

2_B

utt

on

RB

0;

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

52

52

位元的使用範例(四

)位元的使用範例

位元的使用範例

(( 四四))

5:

bit

fla

g1;

// g

lob

ally v

isib

le

bit

fla

g2;

// g

lob

ally v

isib

le

vo

id m

ain

( v

oid

)

{

sta

tic b

it f

lag

3;

// lo

cally v

isib

le

fla

g3

= 1

;

. . .

wh

ile

(1);

}

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

53

53

準備練習三

重要函數說名

準備練習三

準備練習三

重要函數說名

重要函數說名

vo

id I

nit_

IO(v

oid

)

I/O

vo

id I

nit_

Ad

c(

vo

id )

A/D

vo

id A

2D

( u

nsig

ne

d c

ha

r )

Ch

an

ne

lA

/D

vo

id L

CD

_It

oA

(un

sig

ne

d in

t)

10

AS

CII

LC

D

un

sig

ne

d c

ha

r S

et_

BC

D_

AS

CII

(un

sig

ne

d c

ha

r)

vo

id I

nit_

IO(v

oid

)

I/O

vo

id I

nit_

Ad

c(

vo

id )

A/D

vo

id A

2D

( u

nsig

ne

d c

ha

r )

Ch

an

ne

lA

/D

vo

id L

CD

_It

oA

(un

sig

ne

d in

t)

10

AS

CII

LC

D

un

sig

ne

d c

ha

r S

et_

BC

D_

AS

CII

(un

sig

ne

d c

ha

r)

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

54

54

練習三

練習三

練習三

10-b

it A

/D

unio

nstr

uct

10

-bit

10

LC

D M

od

ule

A/D

8-b

it

LC

D M

od

ule

A/D

A/D

Module

Data

Sheet

bit

10-b

it A

/D

unio

nstr

uct

10

-bit

10

LC

D M

od

ule

A/D

8-b

it

LC

D M

od

ule

A/D

A/D

Module

Data

Sheet

bit

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

55

55

10-b

it A

/D 轉換器

10

10

-- bit

A/D

b

it A

/D 轉換器

轉換器

81

0 b

its

20

µS

(<

10

K)

19

.2 µ

S (

12

TA

D)

10

-bit

VR

EF

+ &

VR

EF

-

39.2

µs

29

.2 µ

s

81

0 b

its

20

µS

(<

10

K)

19

.2 µ

S (

12

TA

D)

10

-bit

VR

EF

+ &

VR

EF

-

39.2

µs

29

.2 µ

s

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

56

56

10-bit A/D 方塊圖

1010-- bit A

/D

bit

A/D 方塊圖

方塊圖

CH

S2:C

HS

0

VD

D

AN

2

AN

1

AN

0

VS

S

VR

EF

+

PC

FG

0

VR

EF

-

10

10

-- bit

A/D

bit

A/D

Co

nv

ert

er

Co

nv

ert

er

Refe

ren

ce

Vo

ltag

e

VS

S

RIN

CH

OL

D

120P

F

AN

7

Sam

pli

ng S

WA

N6

AN

5

AN

4

AN

3

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

57

57

See

Dat

a B

ook

A/D 控制暫存器

A/D

A/D 控制暫存器

控制暫存器

AD

CO

N0 R

egis

ter

AD

CS

1

AD

CS

0

C

HS

2

C

HS

1

CH

S0

GO

/DO

NE

---

AD

ON

AD

FM

AD

CS

2

-

----

----

-P

CF

G3

P

CF

G2

P

CF

G1

PC

FG

1

AD

CO

N1 R

egis

ter

bit

7bit

0

10-b

it R

esult

AD

RE

SH

A

DR

ES

L

10-b

it

向右靠齊

00

00

00

7 2

10 7

0

XX

X

XX

XX

XX

X

AD

MF

=0

AD

MF

=1

向左靠齊

AD

RE

SH

A

DR

ES

L

10-b

it

00 0

000

XX

XX

XX

XX

XX

7 0 7

6 5

4 3210

A/D轉換結果

向左、向右調整

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

58

58

A/D 轉換基本流程

A/D

A/D 轉換基本流程

轉換基本流程

A/D

?N

o

Yes

A/D

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

59

59

RAM & ROM 形態

RAM & ROM

的指標標和陣列

RAM & ROM 形態

形態

的指標和陣列

的指標和陣列

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

60

60

使用指標

使用指標

使用指標

??

<O

bje

ct’s

type

and q

ual

ifie

rs>

* <

poin

ter’

s qual

ifie

rs>

<

poin

ter’

s nam

e> ;

ch

ar *

cp;

const

char

*

cp;

const

char

*

c

onst

cp ;

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

61

61

ROM & RAM 指標

ROM & RAM

ROM & RAM 指標

指標

co

nst

RO

M

Ba

nkn

RA

M b

an

ks

(n

1 –

2 )

Po

inte

rs to

vo

latile

-qu

alif

ied

ob

jects

ma

y u

se

d

iffe

ren

t se

qu

en

ce

s o

f co

de

to

acce

ss o

bje

cts

co

nst

RO

M

Ba

nkn

RA

M b

an

ks

(n

1 –

2 )

Po

inte

rs to

vo

latile

-qu

alif

ied

ob

jects

ma

y u

se

d

iffe

ren

t se

qu

en

ce

s o

f co

de

to

acce

ss o

bje

cts

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

62

62

常見錯誤指標用法

(一)

常見錯誤指標用法

常見錯誤指標用法

(( 一一)

1 :

RO

M

const

char

arr

ay[

] =

{0, 2, 4, 6};

char

* c

p, c;

cp =

ar

ray;

c =

* c

p;

//

*cp

wil

l ac

cess

RA

M n

ot

RO

M

)

constch

ar *cp;

char c;

正確宣告是將指標宣告為止到

ROM 的指標

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

63

63

常見錯誤指標用法

(二)

常見錯誤指標用法

常見錯誤指標用法

(( 二二))

2 :

ban

k

void

pro

cess

( in

t* i

p);

//

ban

k2

int

val

ue;

//v

alu

eB

ank2

pro

cess

( &

val

ue

); //

&val

ue

Ban

k0

void

process( bank2int* ip

)正確宣告是需加入相對的

Bank2

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

64

64

指標存放資料格式

指標存放資料格式

指標存放資料格式

(little e

ndia

nfo

rmat)

Ch

ar

mu

lti-b

yte

byte

(little e

ndia

nfo

rmat)

Ch

ar

mu

lti-b

yte

byte :

long i

nput;

char

* c

p;

*((

char

*)&

input

+ 3

) =

0x55;

//

wri

te c

har

acte

r to

MS

B o

f in

put

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

65

65

RAM 的指標

-Midrange PICmicro

RAM

RAM 的指標

的指標

--Midrange

Midrange P

ICmicro

PICmicro

RA

M8-b

its

RA

M b

an

ks (

ba

nk0

+b

an

k1

or

ba

nk2

+b

an

k3

)

ba

nk1

ba

nk 0

bank 1

ba

nk2

ba

nks 2

an

d 3

RA

M8-b

its

RA

M b

an

ks (

ba

nk0

+b

an

k1

or

ba

nk2

+b

an

k3

)

ba

nk1

ba

nk 0

bank 1

ba

nk2

ba

nks 2

an

d 3

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

66

66

RAM 的指標

-Midrange PICmicro

RAM

RAM 的指標

的指標

--Midrange

Midrange P

ICmicro

PICmicro

RA

M ch

ar

*ra

mp

tr;

ba

nk0

8-b

it

ba

nk2

ch

ar

* ra

mp

tr;

8-b

itb

an

k2

ch

ar

* b

an

k2

ra

mp

tr;

ba

nk2

8-b

itb

an

k1

ba

nk2

ch

ar

* b

an

k1

ra

mp

tr;

?

RA

M ch

ar

*ra

mp

tr;

ba

nk0

8-b

it

ba

nk2

ch

ar

* ra

mp

tr;

8-b

itb

an

k2

ch

ar

* b

an

k2

ra

mp

tr;

ba

nk2

8-b

itb

an

k1

ba

nk2

ch

ar

* b

an

k1

ra

mp

tr;

?

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

67

67

RAM 的指標

-Midrange PICmicro

RAM

RAM 的指標

的指標

--Midrange

Midrange P

ICmicro

PICmicro

RA

M–

PIC

mic

ro (

PIC

16

C5

x,

16

Fxx)

far

?

….

RA

M–

PIC

mic

ro (

PIC

16

C5

x,

16

Fxx)

far

?

….

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

68

68

RAM 的指標

-Midrange PICmicro

RAM

RAM 的指標

的指標

--Midrange

Midrange P

ICmicro

PICmicro

Use

Ba

nk2

or

Ba

nk3

Po

inte

rs

ba

nk2

ch

ar

*ra

mp

tr;

ba

nk3

ch

ar

*ra

mp

tr;

ba

nk3

ch

ar

* b

an

k2

ram

ptr

;

?

ram

ptr

BA

NK

2

BA

NK

3

Use

Ba

nk2

or

Ba

nk3

Po

inte

rs

ba

nk2

ch

ar

*ra

mp

tr;

ba

nk3

ch

ar

*ra

mp

tr;

ba

nk3

char

* b

an

k2

ram

ptr

;

?

ram

ptr

BA

NK

2

BA

NK

3

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

69

69

RAM 用指標方式存取

RAM

RAM 用指標方式存取

用指標方式存取

#in

clu

de <

pic

.h>

ch

ar

RA

Marr

ay1[

]= “

Hi-

Tech

PIC

C”;

vo

id w

rite

_L

CD

( ch

ar

data

)

{

PO

RT

D =

data

;

} vo

id U

pd

ate

_L

CD

( vo

id )

{ ch

ar

*ra

mp

tr;

//d

efi

ne

au

to t

yp

e p

oin

ter

ram

ptr

= R

AM

arr

ay

1;

// i

nit

. p

oin

ter

wh

ile

( *r

am

ptr

)

{

wri

te_

LC

D(

*ra

mp

tr);

ram

ptr

++

;

}

} vo

id m

ain

( v

oid

)

{

Up

date

_L

CD

();

wh

ile

(1);

}

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

70

70

RAM 用陣列方式存取

RAM

RAM 用陣列方式存取

用陣列方式存取

#in

clu

de <

pic

.h>

ch

ar

RA

Ma

rra

y[

] =

“W

elc

om

e t

o M

as

ter’

s 2

00

0”;

vo

id w

rite

_L

CD

( ch

ar

data

)

{

PO

RT

D =

data

;

} vo

id U

pd

ate

_L

CD

( vo

id )

{

ch

ar

ch

;//

au

to v

ari

ab

le

ch

ar

i =

0;

// a

uto

va

ria

ble

wh

ile

(c

h=

RA

Ma

rra

y[i

]){ wri

te_L

CD

(ch

);

i++

;

}

} vo

id m

ain

( v

oid

)

{

Up

date

_L

CD

();

wh

ile

(1);

}

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

71

71

練習四

RAM 指標

練習四

練習四

RAM

RAM 指標

指標

lab

4.c

RA

M B

ank (

0 –

3)

RA

M

De

bu

gg

er

Cle

ar

Me

mo

ry

File

Re

gis

ter

aptr

= IR

P=

00x20 ,

0x6

1 (

‘a’)

bptr

= IR

P=

00xA

0 ,

0x6

2 (

‘b’)

cptr

= IR

P:1

0x10 ,

0x6

3 (

‘c’)

dptr

= IR

P:1

0x90 ,

0x6

4 (

‘d’)

lab

4.c

RA

M B

ank (

0 –

3)

RA

M

De

bu

gg

er

Cle

ar

Me

mo

ry

File

Re

gis

ter

aptr

= IR

P=

00x20 ,

0x6

1 (

‘a’)

bptr

= IR

P=

00xA

0 ,

0x6

2 (

‘b’)

cptr

= IR

P:1

0x10 ,

0x6

3 (

‘c’)

dptr

= IR

P:1

0x90 ,

0x6

4 (

‘d’)

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

72

72

常數型態的指標

-Mi

drange PICmicro

常數型態的指標

常數型態的指標

--Midrange

Midrange P

ICmi

cro

PICm

icro

16-b

its

RA

M

RO

M

RA

M

?

16-b

its

RA

M

RO

M

RA

M

?

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

73

73

#in

clu

de <

pic

.h>

const

ch

ar

RO

Ma

rra

y1

[ ]=

“C

Co

mp

ile

r fo

r P

ICm

icro

”;

const

ch

ar

*ro

mp

tr;

// p

oin

ter

de

fin

ed

vo

id w

rite

_L

CD

( ch

ar

data

)

{ PO

RT

D =

da

ta;

} vo

id U

pd

ate

_L

CD

( vo

id )

{

rom

ptr

= R

OM

arr

ay

1;

wh

ile

(*ro

mp

tr)

{//

te

st

for

nu

ll c

ha

r

wri

te_

LC

D(

*ro

mp

tr);

rom

ptr

++

;

}

} vo

id m

ain

( v

oid

)

{

Up

date

_L

CD

();

wh

ile

(1);

}

ROM 指標的查表法

ROM

ROM 指標的查表法

指標的查表法

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

74

74

ROM 陣列的查表法

ROM

ROM 陣列的查表法

陣列的查表法

#in

clu

de <

pic

.h>

const

ch

ar

RO

Ma

rra

y[

] =

“W

elc

om

e t

o M

as

ter’

s 2

00

0”;

vo

id w

rite

_L

CD

( ch

ar

data

)

{

PO

RT

D =

da

ta;

} vo

id U

pd

ate

_L

CD

( vo

id )

{

ch

ar

ch

;//

au

to v

ari

ab

le

ch

ar

i =

0;

// a

uto

va

ria

ble

wh

ile

(ch

= R

OM

arr

ay

[i])

{//

tes

t fo

r n

ull

ch

ar

wri

te_L

CD

(ch

);

i++

;

}

} vo

id m

ain

( v

oid

)

{

Up

date

_L

CD

();

wh

ile

(1);

}

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

75

75

練習五

取得

ROM 陣列資料

練習五

練習五

取得

取得

ROM

ROM 陣列資料

陣列資料

lab

5.c

RO

M

RA

M

RA

M

De

bu

gg

er

Cle

ar

Me

mo

ry

File

Re

gis

ter

RA

Marr

ay1[ ] :

0x2

3

RA

Marr

ay2[ ] :

0x1

10

(b

an

k2

)

lab

5.c

RO

M

RA

M

RA

M

De

bu

gg

er

Cle

ar

Me

mo

ry

File

Re

gis

ter

RA

Marr

ay1[ ] :

0x2

3

RA

Marr

ay2[ ] :

0x1

10

(b

an

k2

)

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

76

76

Hi-Tech PICC

重置後的初始設定

Hi-Tech PI

CC

重置後的初始設定

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

77

77

Reset 動作流程

Reset

Reset 動作流程

動作流程

Po

wer

-On

Res

etpowerup

Ru

n p

ow

eru

p.a

sU

aer

Use

r def

ine

Po

wer

_u

p?

Ru

n U

ser’

s

my_st

artu

p(

)

Yes

Uae

r

Run p

icrt

66x.a

s

goto

_m

ain

goto

start

No

C

mai

n(

)

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

78

78

啟動程式

啟動程式

啟動程式

0x00

powerup

MC

U R

ese

t

C:\

HT

-PIC

\SO

UR

CE

\po

we

rup

.as

I/O

reset

RC

(R

C)

bss p

se

cts

(u

nin

itia

lise

dd

ata

)

data

psects

ma

in(

)

: C

:\H

T-P

IC\S

OU

RC

ES

\pic

rt66x.a

s

0x00

powerup

MC

U R

ese

t

C:\

HT

-PIC

\SO

UR

CE

\po

we

rup

.as

I/O

reset

RC

(R

C)

bss p

se

cts

(u

nin

itia

lise

dd

ata

)

data

psects

ma

in(

)

: C

:\H

T-P

IC\S

OU

RC

ES

\pic

rt66x.a

s

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

79

79

自行設定的啟動工作

自行設定的啟動工作

自行設定的啟動工作

pow

eru

p(p

ow

eru

p.a

s)

I/O

po

we

rup

.as

C

PIC

mic

ro

pow

eru

p(p

ow

eru

p.a

s)

I/O

po

we

rup

.as

C

PIC

mic

ro

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

80

80

I/O Initialization on Powerup

I/O Initialization on

-PIC16CXXX

I/O Initialization on

Powerup

Powerup

--PIC16CXXX

PIC16CXXX

C:Assembly:

. .

.void mystartup( void )

{PORTC = 0x00;

TRISC = 0x00;

#asmclrf _STATUS

clrf _PORTB

bsf _STATUS,5

clrf _TRISB

movlw high start

movwf _PCLATH

goto ( start &

0x7FF )

; asm( “ljmp

start“

);

#endasm

}

#include “

sfr.h”

extrn _mystartup

global powerup, start

psect powerup,class=CO

DE,delta=2

powerup

#if defined (_12C508)

|| defined(_12C509)

movwf 5

#endif

#if defined(_PIC14)

movlw high _mystartup

movwf PCLATH

goto ( _

mystartup

& 0x7FF )

#endif

#if

defined(_PIC16) .

. .

.#endif

end powerup

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

81

81

Program Sections -

(cont.)

Progra

m Se

ctio

ns

Program Sections --

(cont.)

(cont.)

Lin

ker

will

gro

up a

ll data

obje

cts

for

a g

lobal

psectin

to a

sin

gle

segm

ent

Op

tio

ns p

asse

d to

lin

ke

r w

ith

-L

op

tio

n

-L-P

de

co

de

r_ra

m=

B0

h

Co

mp

iler

dri

ve

r w

ill n

ot

ge

ne

rate

me

mo

ry

cle

ari

ng

co

de

sin

ce

de

co

de

r_ra

m p

se

ctis

no

t sta

nd

ard

-L-N

..

(so

rt m

ap

file

sym

bo

l ta

ble

by a

dd

ress

rath

er

tha

n s

ym

bo

l n

am

e.

Lin

ker

will

gro

up a

ll data

obje

cts

for

a g

lobal

psectin

to a

sin

gle

segm

ent

Op

tio

ns p

asse

d to

lin

ke

r w

ith

-L

op

tio

n

-L-P

de

co

de

r_ra

m=

B0

h

Co

mp

iler

dri

ve

r w

ill n

ot

ge

ne

rate

me

mo

ry

cle

ari

ng

co

de

sin

ce

de

co

de

r_ra

m p

se

ctis

no

t sta

nd

ard

-L-N

..

(so

rt m

ap

file

sym

bo

l ta

ble

by a

dd

ress

rath

er

tha

n s

ym

bo

l n

am

e.

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

82

82

Program Sections -

(cont.)

Progra

m Se

ctio

ns

Program Sections --

(cont.)

(cont.)

Pse

ctU

sag

e M

ap:

Pse

ct| C

on

ten

ts| M

emo

ry R

ang

e

----

----

----

--|-

----

----

----

----

----

----

----

----

----

---|

----

----

----

----

----

--

dec

od

er_

r |

Use

r d

efin

ed p

sect

| $

00

B0

-$

00

C6

po

wer

up

| P

ow

er o

n r

eset

co

de

| $

00

00

-$

00

03

init

|

Init

iali

zati

on c

ode

|

$0004 -

$000F

end_in

it |

Init

iali

zati

on c

ode

| $

0010 -

$0013

clrt

ext

|

Mem

ory

cle

arin

g c

ode

| $

0014 -

$0027

stri

ngta

| S

trin

gs

and c

onst

ant

dat

a | $

0028 -

$0039

tex

t |

Pro

gra

m a

nd l

ibra

ry c

ode

|

$07F

B -

$07F

B

tex

t0 | P

rogra

m a

nd l

ibra

ry c

ode

|

$07F

C -

$07F

E

idat

a_0

| B

ank

0 d

ata

(RO

M i

mag

e) | $

07

FF

-$

07

FF

rdat

a_0

| B

ank 0

RA

M i

nit

iali

sed d

ata

| $

0020 -

$0020

cod

e_p

tr | I

ndir

ect

const

poin

ter

| $

0021 -

$0022

tem

p | T

emp

ora

ry R

AM

dat

a

| $

00

70

-$

00

70

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

83

83

Program Sections -

(cont.)

Progra

m Se

ctio

ns

Program Sections --

(cont.)

(cont.)

Mem

ory

Usa

ge

Map

:

Use

r se

gm

ent

$

00

B0

-$

00

C6

$

00

17

( 2

3)

by

tes

tota

l U

ser

seg

men

t

Pro

gra

m R

OM

$

00

00

-$0

03

9 $

00

3A

( 5

8)

wo

rds

Pro

gra

m R

OM

$

07

FB

-$

07

FF

$

00

05

( 5

) w

ord

s

$0

03

F (

6

3)

wo

rds

tota

l P

rog

ram

RO

M

Ban

k 0

RA

M $

00

20

-$

00

22

$

00

03

( 3

) b

yte

s

Ban

k 0

RA

M $

00

70

-$

00

70

$

00

01

( 1

) b

yte

s

$0

00

4 (

4

) b

yte

s to

tal

Ban

k 0

RA

M

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

84

84

中斷管理

中斷管理

中斷管理

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

85

85

中斷處理

–PIC16Fxxx

中斷處理

中斷處理

––PIC16Fxxx

PIC16Fxxx

C“interrupt”

C

C“interrupt”

C void

inte

rrupt

isr_

rec

( void

)

{

if (

RC

IF )

byte

= R

CR

EG

;

}

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

86

86

多個中斷處理

多個中斷處理

多個中斷處理

Sta

tic v

oid

inte

rrupt is

r (v

oid

)

{

if (

T0IF

) {

// T

om

er0

inte

rrupt

TM

R0 =

250 ;

// R

elo

ad the T

imer

valu

e

T0IF

= 0

;// C

lear

Tim

er0

IN

T fla

g

}

if (

INT

F)

{

Rela

y =

1 ;

// T

urn

the r

ela

y o

n

INT

F =

0 ;

// C

lear

the inte

rrupt

}

}Sta

tic v

oid

inte

rrupt is

r (v

oid

)

{

if (

T0IF

) {

// T

om

er0

inte

rrupt

TM

R0 =

250 ;

// R

elo

ad the T

imer

valu

e

T0IF

= 0

;// C

lear

Tim

er0

IN

T fla

g

}

if (

INT

F)

{

Rela

y =

1 ;

// T

urn

the r

ela

y o

n

INT

F =

0 ;

// C

lear

the inte

rrupt

}

}

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

87

87

中斷參數的存取

中斷參數的存取

中斷參數的存取

/

W , S

TA

TU

S , F

SR

, P

CLA

TH

RA

M

/

W , S

TA

TU

S , F

SR

, P

CLA

TH

RA

M

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

88

88

準備練習六

處理

Timer2 中斷

準備練習六

準備練習六

處理

處理

Timer2

Timer2 中斷

中斷

Tim

er2

8-b

it

PW

M(D

uty

& P

erio

d)

TM

R2

TM

R2

PR

2

TM

R2

PR

2p

osts

ca

ler

SS

P (

SP

I)

Tim

er2

8-b

it

PW

M(D

uty

& P

erio

d)

TM

R2

TM

R2

PR

2

TM

R2

PR

2p

osts

ca

ler

SS

P (

SP

I)

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

89

89

準備練習六

Timer2 方塊說名

準備練習六

準備練習六

Timer2

Timer2 方塊說名

方塊說名

TM

R2

ou

tpu

t8

-bit

Data

Bu

s

Fo

sc/4

Pre

scale

r

1:1,

1:4

, 1:1

6 2

Co

mp

arat

or

EQ

Se

t T

2IF

fla

g b

itR

eset

TM

R2

Po

sts

cale

r

1:1

to 1

:16 4

PR

2

8-b

it D

ata

Bu

s

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

90

90

準備練習六

Timer2 暫存器說明

準備練習六

準備練習六

Timer2

Timer2 暫存器說明

暫存器說明

: G

IE , P

EIE

, T

MR

2IE

: T

MR

2IF

: 0.2

5us *

16 *

16 *

(155+

1)

= 9

.984m

S

: G

IE , P

EIE

, T

MR

2IE

: T

MR

2IF

: 0.2

5us *

16 *

16 *

(155+

1)

= 9

.984m

S

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

91

91

練習六

練習六

練習六

lab6.c

Tim

er2

10m

S

100m

S

lab6.c

Tim

er2

10m

S

100m

S

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

92

92

合併組合語言與

C 語言

合併

合併組合語言與

組合語言與

C C 語言

語言

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

93

93

合併組合語言與

C 語言

合併組合語言與

合併組合語言與

C C 語言

語言

?

RA

M

?

RA

M

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

94

94

合併組合語言與

C 語言

合併組合語言與

合併組合語言與

C C 語言

語言

ljmp

–(P

ag

e)

(Go

to)

fca

ll–

(Ca

ll)

“,w

”“,

f ”

()

ljmp

–(P

ag

e)

(Go

to)

fca

ll–

(Ca

ll)

“,w

”“,

f ”

()

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

95

95

嵌嵌入式的組合語言

嵌入式的組合語言

嵌入式的組合語言 C

(sta

tem

ents

)

#asm

, #

endasm

C(s

tate

ments

)

#asm

, #

endasm

Exam

ple

s:

asm

(“m

ovlw

0x20”)

; /

/ C

sta

tem

ent

style

asm

(“m

ovw

f 0

x30”)

;

#as

m

movlw

0x10 //

Ass

emble

r dir

ecti

ve

style

movw

f 0

x20

#en

das

m

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

96

96

獨立式組合語言

獨立式組合語言

獨立式組合語言

“.as”

“.as”

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

97

97

獨立式組合語言

獨立式組合語言

獨立式組合語言

C(O

bje

cts

)

“_”

“?

_fu

nctio

n n

am

e”

(au

to v

ari

ab

les)

“?a

_fu

nctio

n_

na

me

C(O

bje

cts

)

“_”

“?

_fu

nctio

n n

am

e”

(au

to v

ari

ab

les)

“?a

_fu

nctio

n_

na

me

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

98

98

獨立式組合語言

獨立式組合語言

獨立式組合語言

CC 在組合語言使用

C 的物件

int

resp

on

se;

//

a g

lobal

C o

bje

ct

glo

bal

_re

sponse

;

dir

ecti

ve

use

d t

o g

ive

refe

rence

to e

xte

rnal

obje

ct

mo

vw

f (

_re

spo

nse

& 0

7F

h)

; a

cces

sing C

obje

ct i

n a

ssem

bly

code

在C程式下使用組合語言的物件

glo

bal

_an

swer

; dir

ecti

ve

to g

ive

obje

ct g

lobal

sco

pe

_an

swer

d

s2

; d

efin

ing

2 b

yte

var

iab

le

exte

rnu

nsi

gn

ed i

nt

answ

er;

// r

efer

ence

lin

kag

e

answ

er =

10;

// a

cces

sing a

ssem

bly

obje

ct f

rom

C

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

99

99

獨立式組合語言

獨立式組合語言

獨立式組合語言

DS

PS

EC

T

GLO

BA

L

DS

PS

EC

T

GLO

BA

L

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

100

100

獨立式組合語言

獨立式組合語言

獨立式組合語言

C

C

sta

tic

C(

_ )

Mu

ltip

le B

yte

(LS

B)

(MS

B)

byte

C

C

sta

tic

C(

_ )

Mu

ltip

le B

yte

(LS

B)

(MS

B)

byte

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

101

101

獨立式組合語言

獨立式組合語言

獨立式組合語言

Asse

mb

ly:

C:

Asse

mb

ly:

C:

processor 16C74A

#include “

p16c74a.hti”

psecttext,globa

l,class=CODE,delta=2

global _Foo

signat _Foo,88

global _Var1,_Var2

_Foo

movf

_Var1,

Waddwf_Var2,

Wmovwf(_Var2+1)

return

end

#include <pic1674.h>

extern void Foo( void );

char

Var1;

int

Var2

;

void main( void )

{Var1 = 0xAA;

Var2 = 0x55;

Foo();

}

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

102

102

Hi-Tech PICC

Hi 所提供的資料庫

Hi-- Tech PICC

Tech PICC

所提供的資料庫

所提供的資料庫

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

103

103

函數庫

函數庫

函數庫

AN

SI

C

,e.g

. str

cp

y(

), islo

we

r( )

, e.g

. sin

( ),

sqrt

( )

Hi-

Te

ch

, e

.g.

pe

rsis

t_ch

eck()

,

ge

t_ca

l_d

ata

( )

AN

SI

C

,e.g

. str

cp

y(

), islo

we

r( )

, e.g

. sin

( ),

sqrt

( )

Hi-

Te

ch

, e

.g.

pe

rsis

t_ch

eck()

,

ge

t_ca

l_d

ata

( )

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

104

104

函數庫

函數庫

函數庫

60

C

str

cp

y

str

cm

p

str

len

str

ca

t

me

mse

t

me

mcp

y

me

mcm

p

and m

ore

. . .

60

C

str

cp

y

str

cm

p

str

len

str

ca

t

me

mse

t

me

mcp

y

me

mcm

p

and m

ore

. . .

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

105

105

函數庫

函數庫

函數庫

sin

co

s

tan

log

po

w

exp

flo

or

sq

rt

and m

ore

. . .

sin

co

s

tan

log

po

w

exp

flo

or

sq

rt

and m

ore

. . .

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

106

106

函數庫

函數庫

函數庫

isa

lnu

m

isa

lph

a

isa

scii

isu

pp

er

islo

we

r

issp

ace

isd

igit

an

d m

ore

. . .

isa

lnu

m

isa

lph

a

isa

scii

isu

pp

er

islo

we

r

issp

ace

isd

igit

an

d m

ore

. . .

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

107

107

函數庫

函數庫

函數庫

ato

f

ato

i

ato

l

ldiv

ran

d

an

d m

ore

. . .

ato

f

ato

i

ato

l

ldiv

ran

d

an

d m

ore

. . .

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

108

108

Hi-Tech PICC

Hi其它功能

Hi-- Tech PICC

Tech PICC

其它功能

其它功能

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

109

109

有用的巨集定義

PIC168xA.h

有用的巨集定義

有用的巨集定義

PIC168xA.h

PIC168xA.h

#d

efin

e sle

ep

( )

a

sm

(“sle

ep

”)

#d

efin

e n

op

( )

a

sm

(“n

op

”)

#d

efin

e

clr

wd

t( )

a

sm

(“clr

wd

t”)

Sle

ep

( );

C

ei(

);

di(

);

#d

efin

e sle

ep

( )

a

sm

(“sle

ep

”)

#d

efin

e n

op

( )

a

sm

(“n

op

”)

#d

efin

e

clr

wd

t( )

a

sm

(“clr

wd

t”)

Sle

ep

( );

C

ei(

);

di(

);

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

110

110

C 程式下設定

Conf

ig.

C C 程式下設定

程式下設定

Config

Config

..C

onfigura

tion F

uses S

et in

C C

ode

?

.he

x

Hi-

Te

ch

“_ _

CO

NF

IG”

#d

efin

e.h

Hi-T

ech

PIC

C

Co

nfig

. F

use

Configura

tion F

uses S

et in

C C

ode

?

.he

x

Hi-

Te

ch

“_ _

CO

NF

IG”

#d

efin

e.h

Hi-T

ech

PIC

C

Co

nfig

. F

use

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

111

111

PIC16F877A.INC (組合語言)

PIC16F877A.INC (

PIC16F877A.INC (組合語言

組合語言

))_CP_ALL

EQU H'1FFF'

_CP_OFF

EQU H'3FFF'

_DEBUG_OFF

EQU H'3FFF'

_DEBUG_ON

EQU H'37FF'

_WRT_OFF

EQU H'3FFF' ; No prog memmorywrite protection

_WRT_256 EQU

H'3DFF' ; First 256 prog memmorywrite

_WRT_1FOURTH EQU

H'3BFF' ; First quarter prog memmorywrite

_WRT_HALF EQU H'39FF'

; First half memmorywrite protected

_CPD_OFF

EQU H'3FFF'

_CPD_ON

EQU H'3EFF'

_LVP_ON

EQU H'3FFF'

_LVP_OFF

EQU H'3F7F'

_BODEN_ON

EQU H'3FFF'

_BODEN_OFF

EQU H'3FBF'

_PWRTE_OFF

EQU H'3FFF'

_PWRTE_ON

EQU H'3FF7'

_WDT_ON

EQU H'3FFF'

_WDT_OFF

EQU H'3FFB'

_RC_OSC

EQU H'3FFF'

_HS_OSC

EQU H'3FFE'

_XT_OSC

EQU H'3FFD'

_LP_OSC

EQU H'3FFC'

_CP_ALL

EQU H'1FFF'

_CP_OFF

EQU H'3FFF'

_DEBUG_OFF

EQU H'3FFF'

_DEBUG_ON

EQU H'37FF'

_WRT_OFF

EQU H'3FFF' ; No prog memmorywrite protection

_WRT_256 EQU

H'3DFF' ; First 256 prog memmorywrite

_WRT_1FOURTH EQU

H'3BFF' ; First quarter prog memmorywrite

_WRT_HALF EQU H'39FF'

; First half memmorywrite protected

_CPD_OFF

EQU H'3FFF'

_CPD_ON

EQU H'3EFF'

_LVP_ON

EQU H'3FFF'

_LVP_OFF

EQU H'3F7F'

_BODEN_ON

EQU H'3FFF'

_BODEN_OFF

EQU H'3FBF'

_PWRTE_OFF

EQU H'3FFF'

_PWRTE_ON

EQU H'3FF7'

_WDT_ON

EQU H'3FFF'

_WDT_OFF

EQU H'3FFB'

_RC_OSC

EQU H'3FFF'

_HS_OSC

EQU H'3FFE'

_XT_OSC

EQU H'3FFD'

_LP_OSC

EQU H'3FFC'

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

112

112

Config 的設定

(pic168xa.h)

Config

Config 的設定

的設定

(( pic16

8xa.h)

pic168xa.h)

#define

CONFIG_ADDR

0x2007

#define

RC0x3FFF

// resistor/capacitor

#define

HS0x3FFE

// high speed crystal/resonator

#define

XT0x3FFD

// crystal/resonator

#define

LP0x3FFC

// low power crystal/resonator

#define

WDTEN

0x3FFF

// enable watchdog timer

#define

WDTDIS

0x3FFB

// disable watchdog timer

#define

PWRTEN

0x3FF7

// e

nable power up timer

#define

PWRTDIS

0x3FFF

// disable power up timer

#define

BOREN

0x3FFF

// enable brown out reset

#define

BORDIS

0x3FBF

// disable brown out reset

#define

LVPEN

0x3FFF

// LVP enabled

#define

LVPDIS

0x3F

7F// LVP disabled

#define

WRTEN

0x3FFF

// flash write enabled

#define

WP1

0x3DFF

// protect 0000 -

00FF

#define

WP2

0x3BFF

// protect 0000 -

07FF

#define

WP3

0x39FF

// protect 0000 -

1FFF

#define

PROTECT

0x1FFF

// protect program code

#define

UNPROTECT

0x3FFF

// do not protect the code

#define

CONFIG_ADDR

0x2007

#define

RC0x3FFF

// resistor/capacitor

#define

HS0x3FFE

// high speed crystal/resonator

#define

XT0x3FFD

// crystal/resonator

#define

LP0x3FFC

// low power crystal/resonator

#define

WDTEN

0x3FFF

// enable watchdog timer

#define

WDTDIS

0x3FFB

// disable watchdog timer

#define

PWRTEN

0x3FF7

// e

nable power up timer

#define

PWRTDIS

0x3FFF

// disable power up timer

#define

BOREN

0x3FFF

// enable brown out reset

#define

BORDIS

0x3FBF

// disable brown out reset

#define

LVPEN

0x3FFF

// LVP enabled

#define

LVPDIS

0x3F

7F// LVP disabled

#define

WRTEN

0x3FFF

// flash write enabled

#define

WP1

0x3DFF

// protect 0000 -

00FF

#define

WP2

0x3BFF

// protect 0000 -

07FF

#define

WP3

0x39FF

// protect 0000 -

1FFF

#define

PROTECT

0x1FFF

// protect program code

#define

UNPROTECT

0x3FFF

// do not protect the code

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

113

113

設定

Config. Word

設定

設定

Config

Config

. Wo

rd. Wo

rdC

ON

FIG

(pic

.h)

#define __C

ON

FIG

(x)

asm

("\tpsect config,c

lass=

CO

NF

IG,d

elta=

2")

;\

asm

("\tdw

"___m

kstr

(x))

CO

NF

IG(p

ic.h

)

#define __C

ON

FIG

(x)

asm

("\tpsect config,c

lass=

CO

NF

IG,d

elta=

2")

;\

asm

("\tdw

"___m

kstr

(x))

PIC

16

F8

77

A

#in

clu

de <

pic

.h>

__C

ON

FIG

(

HS

& W

DT

DIS

& P

WR

TE

N &

BO

RD

IS &

LV

PD

IS &

UN

PR

OT

EC

T )

;

vo

id m

ain

(vo

id)

{

//

.

.. y

ou

r co

de

}

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

114

114

設定

ID Locations

設定

設定

ID Locations

ID Locations

ID L

ocations

(pic

.h)

_ _

ID

LO

C

16

4(

0 ~

F )

ID L

ocations

(pic

.h)

_ _

ID

LO

C

16

4(

0 ~

F )

PIC

16F

877A

PIC

16F

877A

#inclu

de <pic.h>

__IDLOC ( 12AB );

void main(void)

{

// ... your code

}

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

115

115

設定

EEPROM

設定

設定

EEPROM

EEPROM

EE

PR

OM

(pic

.h)

_ _

EE

PR

OM

_D

AT

A Byte

s

EE

PR

OM

(pic

.h)

_ _

EE

PR

OM

_D

AT

A Byte

s

PIC

16

F8

77

A

__

EE

PR

OM

_D

AT

A

(0x0

0,0

x0

1,0

x0

2,0

x0

3,0

x0

4,0

x0

5,0

x0

6,0

x0

7);

__

EE

PR

OM

_D

AT

A(0

x0

8,0

x0

9,0

x0

a,0

x0

b,0

x0

c,0

x0

d,0

x0

e,0

x0

f);

__

EE

PR

OM

_D

AT

A('H

','i'

,'-','T

','e

','c

','h

',0

x0

0,)

;

PIC

16

F8

77

A

__

EE

PR

OM

_D

AT

A

(0x0

0,0

x0

1,0

x0

2,0

x0

3,0

x0

4,0

x0

5,0

x0

6,0

x0

7);

__

EE

PR

OM

_D

AT

A(0

x0

8,0

x0

9,0

x0

a,0

x0

b,0

x0

c,0

x0

d,0

x0

e,0

x0

f);

__

EE

PR

OM

_D

AT

A('H

','i'

,'-','T

','e

','c

','h

',0

x0

0,)

;

©2

003

M

icro

chip

Taiw

an

. A

ll R

igh

ts R

ese

rve

d.

HI-

TE

CH

PIC

C-1

6 C

Com

pile

r W

ork

shop

116

116

課程結束!

課程結束

課程結束

!!

感謝您對

Mic

roc

hip的支持

如有任何問題請電

02

-27

17

-71

75

或e-M

ail:

Taiw

an

.Tech

help

@M

icro

ch

ip.c

om

感謝您對

感謝您對

Mic

roc

hip

Mic

roc

hip的支持

的支持

如有任何問題請電

如有任何問題請電

02

02

-- 27

17

27

17

-- 71

75

71

75

或或ee

-- Ma

il:

Ta

iwa

n.

Ma

il:

Ta

iwa

n. T

ech

help

Tech

help

@M

icro

ch

ip.c

om

@M

icro

ch

ip.c

om