Файл: PICmicro MCU C - An itroduction to programming The Microchip PIC in CCS C (N.Gardner, 2002).pdf

ВУЗ: Не указан

Категория: Не указан

Дисциплина: Не указана

Добавлен: 14.06.2025

Просмотров: 1085

Скачиваний: 0

ВНИМАНИЕ! Если данный файл нарушает Ваши авторские права, то обязательно сообщите нам.

shitf_right(&z,3,0);

rotate_left and rotate_right work like the shift functions above except the bit shifted out of one side gets shifted in the other side. For example:

int x[3] = {0b10010001, 0b00011100, 0b10000001};

//x msb first is: 10000001, 00011100, 10010001 rotate_left(x,sizeof(x));

//x msb first is: 00000010, 00111001, 00100011

The swap function swaps the upper 4 bits and lower 4 bits of a byte. For example:

int x;

x = 0b10010110

swap(x); //x is now 01101001

9.4Timers

All PICmicro®’s have an 8-bit timer and some PIC’s have two more advanced timers. The capabilities are as follows:

rtcc (timer0) = 8Bit.

May increment on the instruction clock or by an external source.

Applying a pre-scaler may slow increment.

When timer0 overflows from 255 to 0, an interrupt can be generated (not 16C5X series)

timer1 = 16Bit.

May increment on the instruction clock or by an external source.

Applying a pre-scalar may slow increment.

When timer1 overflows from 65535 to 0, an interrupt can be generated.

In capture mode, the timer1 count may be saved in another register when a pin changes. An interrupt may also be generated.

In compare mode, a pin can be changed when the count reaches a preset value, and an interrupt may also be generated.

This timer is used as part of the PWM.

114

timer2 = 8Bit.

May increment on the instruction clock or by an external source.

Applying a pre-scalar may slow increment.

When timer2 overflows from 255 to 0, an interrupt can be generated.

The interrupt can be slowed by applying a post-scaler, so it requires a certain number of overflows before the interrupt occurs.

This timer is used as part of the PWM.

The following is a simple example using the rtcc to time how long a pulse is high:

#include <16c74.h> #fuses HS,NOWDT

#use delay(clock=1024000)

#use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7)

main() {

int time;

setup_counters(rtcc_internal, rtcc_div_256); //increments 1024000/4*256 times per second //or every millisecond

while(!input(PIN_B0)); //wait for high set_rtcc(0);

while(!input(PIN_B0)); //wait for low time = get_rtcc();

printf(“High time = %u ms.”,time);

}

The following is an example using the timer1 capture feature to time how long it takes for pin C2 to go high after pin B0 is driven high:

#include <16c74.h> #fuses HS,NOWDT

#use delay(clock=8000000)

#use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7) #bit capture_1 = 0x0c.2 //pir1 register

//bit 2 = capture has taken place

main()

{

long time;

setup_timer1(t1_internal | t1_div_by_2); //Increments every 1 us

setup_ccp1(ccp_capture_re);

//configure CCP1 to capture rise

capture_1=0;

115

set_timer1(0); output_high(PIN_B0); while(!capture_1);

time = ccp_1;

printf(“Reaction time = %1u us.”,time);

}

ADCON1

ANALOG/

DIGITAL

CONTROL

PORTA

MUX

A/D

ADRES

(PORTE)

CONVERTOR

A/D RESULT

TRISA

ADCON0

CONTROL AND

(TRISE)

STATUS

REGISTER

9.5A/D Conversion

The A/D in the 16C7x and 12C67x devices has a resolution of 8 bits. This means that the voltage being measured can be resolved to one of 255 values. If a 5 volt supply is used, then the measured accuracy is 5/255 = 19.6mV over a 0 to 5 volt range. However, if the reference voltage is reduced to 2.55 volts, the resolution becomes 10mV but the working range falls to 0 to 2.55 volts.

Other Microchip parts have 10, 11, 12 and 16 bits resolution.

NOTE:

The default for ports having both analog and digital capability is ANALOG.

It is important to note which combination of I/O lines can be used for analog and digital. The following tables are extracted from the data sheets.

16C72/3

16C74 only

A0, A1

A2

A3

A5

E0

E1

E2

Vref

A

A

A

A

A

A

A

Vdd

A

A

Vref

A

A

A

A

A3

A

D

A

A

D

D

D

Vref

D

D

Vref

A

D

D

D

A3

116


A

A

A

D

D

D

D

Vref

A

A

Vref

D

D

D

D

A3

D

D

D

D

D

D

D

---

16C71, 16C710, 16C711

A0, A1

A2

A3

Vref

A

A

A

Vdd

A

A

Vref

A3

A

D

D

Vdd

D

D

D

---

In C, the setup and operation of the A/D is simplified by ready made library routines.

set_adc_channel(0-7)

select the channel for a/d conversion

setup_adc(mode)

sets up the analog to digital converter The modes are as follows:

adc_off, adc_clock_div_2, adc_clock_div_8, adc_clock_div_32,adc_clock_internal

setup_adc_ports(mix)

will setup the ADC pins to be analog, digital or combination. The allowed combinations for mix vary depending on the chip.

The constants all_analog and no_analog are valid for all chips. Some other example constants:

ra0_ra1_ra2_ra3_analog/a0_ra1_analog_ra3_ref

read_adc()

will read the digital value fro the analog to digital converter. Calls to setup_adc and set_adc_channel should be made sometime before this function is called. This function returns an 8-bit value 00h – FFh on parts with an 8 bits A/D converter. On parts with greater than 8 bits A/D the value returned is always a long with the range 000h – FFFFh.

The range may be fixed regardless of the part to aid in compatibility across parts by adding on of the following directives:

#device ADC=8

#device ADC=16

Example

setup_adc(ALL_ANALOG);

//sets porta

to all analog inputs

set_adc_channel(1);

//points a/d

at channel 1

delay_ms(5000);

//waits

5 seconds

value = read_adc();

//reads

value

117


printf(“A/D value = %2x\n\r”, value);//prints value

9.6Data Communications/RS232

RS232 communications between PCs, modems etc. form part of an engineer’s life. The problem seems to arise when self built products need to be interfaced to the outside world. The permutations of 9 or 25 pins on a D connector and the software controlling communications are endless. A minimum interface can be 3 wires – Ground, Transmit, and Receive – but what to do with the remaining pins? The voltage levels are between ±3 and ±15 volts allowing

plenty of leeway for both drivers and receivers. When connecting equipment with RS232 interfaces, it is important to know which is classified as the Data Controlling Equipment (DCE) and which is Data Terminal Equipment (DTE).

Cables/Connectors

9 ways D

Pin

Function

Data direction

1

Carrier Detect

I

2

Receive Data

I

3

Transmit Data

O

4

Data Terminal Ready

O

5

Ground

<>

6

Data Set Ready

I

7

Request To Send

O

8

Clear To Send

I

9

Ring Indicator

I

25 ways D

Pin

Function

Data direction

1

Protective Ground

<>

2

Transmit Data

O

3

Receive Data

I

4

Request To Send

O

5

Clear To Send

I

6

Data Set Ready

I

7

Signal Ground

<>

20

Data Terminal Ready

O

22

Ring Indicator

I

The remaining pins have other functions not normally used for basic interconnection, and are documented in the EIA-232-D or CCTT V24/28 specification.

Common Problems

Result

Possible reasons

Garbled characters

parity, speed, character length, stop bits

118


Lost data

flow control

Double space

translation of carriage returns or line feeds

Overwriting

translation of carriage returns or line feeds

No display of characters

duplex operation

Double characters

duplex operation

Data Format

Data sent via an RS232 interface follows a standard format.

Start bits

always 1 bit

Stop bits

1 or 2 bits

Data bits

7 or 8 bits

Parity bits

none if no error detection is required

odd or even if error detection is required

DATA FORMAT: 8 DATA BITS, 1 STOP BIT

Asynchronous data transmission

+12V

-12V

IDLE

START BIT

BIT

BIT

BIT

BIT

BIT

BIT

BIT

STOP IDLE

BIT

1

2

3

4

5

6

7

8

BIT

Receiver samples in middle of each data element

Parity

Parity checking requires the addition of an extra bit to the data byte. The parity system may be either ‘odd’ or ‘even’ and both systems give the same level of error detection.

In an odd parity system, the overall count of ‘1’s in the combined data byte, plus parity bit, is odd. Thus, with an 8 bits data byte of ‘10101100’ the parity bit would be set to ‘1’.

In an even parity system, the overall count of ‘1’s in the combined data byte, plus parity bit, is even. Thus, with an 8 bits data byte of ‘10101100’ the parity bit would be set to ‘0’.

If corruption of either data bytes or of the parity bit itself takes place, when the receiver carries out the parity check, the corruption will be recognized. In the event of more than one bit being corrupted, it is possible that the receiver will not recognize the problem, provided that the parity appears correct. So, parity checking is not a cast iron method of checking for transmission errors, but in

119


practice, it provides a reasonable level of security in most systems. The parity system does not correct errors in itself; it only indicates that an error has occurred and it is up to the system software to react to the error state; in most systems this would result in a request for re-transmission of the data.

The PICmicro®MCU does not have on-chip parity testing or generation, so the function needs to be generated in software. This adds an overhead to the code generated which could have a knock on effect on execution times.

Bit Rate Time Calculation

As BAUD is bits per second, each data bit has a time of 1/(baud rate)

This works out as 1200 baud = 833uS, 2400 baud = 416uS, 9600 baud = 104uS

ASCII Conversion Table

Control

HEX

msb

0

1

2

3

4

5

6

7

lsb

bits

000

001

010

011

100

101

110

111

0

0000

NUL

DLE

SP

0

@

P

-

p

^A

1

0001

SOH

DC1

!

1

A

Q

a

q

^B

2

0010

STX

DC2

2

B

R

b

r

^C

3

0011

ETX

DC3

#

3

C

S

c

s

^D

4

0100

EOT

DC4

$

4

D

T

d

t

^E

5

0101

ENQ

NAK

%

5

E

U

e

u

^F

6

0110

ACK

SYN

&

6

F

V

f

v

^G

7

0111

BEL

ETB

7

G

W

g

w

^H

8

1000

BS

CAN

(

8

H

X

h

x

^I

9

1001

HT

EM

)

9

I

Y

i

y

^J

A

1010

LF

SUB

*

:

J

Z

j

z

^K

B

1011

VT

ESC

+

;

K

[

k

{

^L

C

1100FF

FS

,

<

L

\

l

^M

D

1101CR

GS

-

-

M

]

m

}

^N

E

1110SO

RS

.

>

N

^

n

~

^O

F

1111SI

US

|

?

O

_

o

DEL

Definitions for the ASCII symbols on the previous table are:

NUL

-

Null

DLE

-

Data Link Escape

SOH

-

Start of Heading

DC

-

Device Control

STX

-

Start of Text

EXT

-

End of Text

EOT

-

End of Transmission

ENQ

-

Enquiry

NAK

-

Negative Acknowledge

ACK

-

Acknowledge

SYN

-

Synchronous Idle

BEL

-

Bell

ETB

-

End Transmission Block

BS

-

Backspace

CAN

-

Cancel

HT

-

Horizontal Tab

EM

-

End of Medium

LF

-

Line Feed

SUB

-

Substitute

VT

-

Vertical Tab

ESC

-

Escape

FF

-

Form Feed

FS

-

File Separator

CR

-

Carriage Return

GS

-

Group Separator

SO

-

Shift Out

120