• Tidak ada hasil yang ditemukan

PIC Programming in C and Assembly

N/A
N/A
Protected

Academic year: 2024

Membagikan "PIC Programming in C and Assembly"

Copied!
7
0
0

Teks penuh

(1)

PIC Programming in C and Assembly

Krerk Piromsopa, Ph.D.

Department of Computer Engineering Chulalongkorn University

Outlines

• Microprocessor vs. MicroController

• PIC in depth

• PIC Programming

• Assembly Programming

• Embedded C

• In-line assembly

• PIC Interface

Microcontroller vs. Microprocessor Microcontroller vs. Microprocessor

(2)

Microcontroller vs. Microprocessor Microcontroller vs. Microprocessor

Figures from http://www.mhennessy.f9.co.uk/pic/introduction.htm

PIC in depth

PIC16F87X

DS30292C-page 6  2001 Microchip Technology Inc.

FIGURE 1-2: PIC16F874 AND PIC16F877 BLOCK DIAGRAM

FLASH Program Memory

13 Data Bus 8

Program14 Bus

Instruction reg Program Counter

8 Level Stack (13-bit)

RAM File Registers

Direct Addr 7 RAM Addr(1) 9

Addr MUX Indirect

Addr FSR reg

STATUS reg

MUX

ALU

W reg Power-up

Timer Oscillator Start-up Timer

Power-on Reset Watchdog

Timer Instruction Decode &

Control

Timing Generation OSC1/CLKIN OSC2/CLKOUT

MCLRVDD, VSS

PORTA

PORTB

PORTC

PORTD

PORTE RA4/T0CKI RA5/AN4/SS

RC0/T1OSO/T1CKI RC1/T1OSI/CCP2 RC2/CCP1 RC3/SCK/SCL RC4/SDI/SDA RC5/SDO RC6/TX/CK RC7/RX/DT

RE0/AN5/RD RE1/AN6/WR RE2/AN7/CS 8

8

Brown-out Reset

Note 1:Higher order bits are from the STATUS register.

USART

CCP1,2 Synchronous

10-bit A/D

Timer0 Timer1 Timer2

Serial Port

RA3/AN3/VREF+

RA2/AN2/VREF- RA1/AN1 RA0/AN0

Parallel Slave Port 8

3

Data EEPROM

RB0/INT RB1 RB2 RB3/PGM RB4 RB5 RB6/PGC RB7/PGD Device Program

FLASH Data Memory Data EEPROM

PIC16F874 4K 192 Bytes 128 Bytes

PIC16F877 8K 368 Bytes 256 Bytes

In-Circuit Debugger Low-Voltage Programming

RD0/PSP0 RD1/PSP1 RD2/PSP2 RD3/PSP3 RD4/PSP4 RD5/PSP5 RD6/PSP6 RD7/PSP7

Memory Model

 2001 Microchip Technology Inc. DS30292C-page 11

PIC16F87X

2.0 MEMORY ORGANIZATION There are three memory blocks in each of the PIC16F87X MCUs. The Program Memory and Data Memory have separate buses so that concurrent access can occur and is detailed in this section. The EEPROM data memory block is detailed in Section 4.0.

Additional information on device memory may be found in the PICmicro Mid-Range Reference Manual, (DS33023).

FIGURE 2-1: PIC16F877/876 PROGRAM MEMORY MAP AND STACK

2.1 Program Memory Organization The PIC16F87X devices have a 13-bit program counter capable of addressing an 8K x 14 program memory space. The PIC16F877/876 devices have 8K x 14 words of FLASH program memory, and the PIC16F873/874 devices have 4K x 14. Accessing a location above the physically implemented address will cause a wraparound.

The RESET vector is at 0000h and the interrupt vector is at 0004h.

FIGURE 2-2: PIC16F874/873 PROGRAM MEMORY MAP AND STACK

PC<12:0>

13

0000h

0004h 0005h Stack Level 1

Stack Level 8

RESET Vector

Interrupt Vector

On-Chip CALL, RETURN RETFIE, RETLW

1FFFh Stack Level 2

Program Memory

Page 0

Page 1

Page 2

Page 3

07FFh 0800h

0FFFh 1000h

17FFh 1800h

PC<12:0>

13

0000h

0004h 0005h Stack Level 1

Stack Level 8

RESET Vector

Interrupt Vector

On-Chip CALL, RETURN RETFIE, RETLW

1FFFh Stack Level 2

Program Memory

Page 0

Page 1

07FFh 0800h

0FFFh 1000h

(3)

Register File Map

 2001 Microchip Technology Inc. DS30292C-page 13

PIC16F87X

FIGURE 2-3: PIC16F877/876 REGISTER FILE MAP

Indirect addr.(*) TMR0

PCL STATUS

FSR PORTA PORTB PORTC

PCLATH INTCON PIR1

TMR1L TMR1H T1CON TMR2 T2CON SSPBUF SSPCON CCPR1L CCPR1H CCP1CON

OPTION_REG PCL STATUS

FSR TRISA TRISB TRISC

PCLATH INTCON PIE1

PCON

PR2 SSPADD SSPSTAT 00h 01h 02h 03h 04h 05h 06h 07h 08h 09h 0Ah 0Bh 0Ch 0Dh 0Eh 0Fh 10h 11h 12h 13h 14h 15h 16h 17h 18h 19h 1Ah 1Bh 1Ch 1Dh 1Eh 1Fh

80h 81h 82h 83h 84h 85h 86h 87h 88h 89h 8Ah 8Bh 8Ch 8Dh 8Eh 8Fh 90h 91h 92h 93h 94h 95h 96h 97h 98h 99h 9Ah 9Bh 9Ch 9Dh 9Eh 9Fh

20h A0h

7Fh FFh

Bank 0 Bank 1

Unimplemented data memory locations, read as ’0’.

* Not a physical register.

Note 1:These registers are not implemented on the PIC16F876.

2:These registers are reserved, maintain these registers clear.

File Address Indirect addr.(*) Indirect addr.(*)

PCL STATUS

FSR

PCLATH INTCON

PCL STATUS

FSR

PCLATH INTCON 100h 101h 102h 103h 104h 105h 106h 107h 108h 109h 10Ah 10Bh 10Ch 10Dh 10Eh 10Fh 110h 111h 112h 113h 114h 115h 116h 117h 118h 119h 11Ah 11Bh 11Ch 11Dh 11Eh 11Fh

180h 181h 182h 183h 184h 185h 186h 187h 188h 189h 18Ah 18Bh 18Ch 18Dh 18Eh 18Fh 190h 191h 192h 193h 194h 195h 196h 197h 198h 199h 19Ah 19Bh 19Ch 19Dh 19Eh 19Fh

120h 1A0h

17Fh 1FFh

Bank 2 Bank 3

Indirect addr.(*)

PORTD(1) PORTE(1)

TRISD(1)

ADRESL TRISE(1)

TMR0 OPTION_REG

PIR2 PIE2

RCSTA TXREG RCREG CCPR2L CCPR2H CCP2CON ADRESH ADCON0

TXSTA SPBRG

ADCON1

General Purpose Register

General Purpose Register

General Purpose Register

General Purpose Register

1EFh accesses 1F0h 70h - 7Fh EFh

accesses F0h 70h-7Fh

16Fh accesses 170h 70h-7Fh General Purpose Register

General Purpose Register TRISB PORTB

96 Bytes 80 Bytes 80 Bytes 80 Bytes

16 Bytes 16 Bytes

SSPCON2

EEDATA EEADR

EECON1 EECON2 EEDATH

EEADRH

Reserved(2) Reserved(2) AddressFile File

Address File

Address AddressFile

Device?

• I/O Port

• EEPROM

• Timer & PWM

• Serial Port

• A2D

• Interrupt

• Watchdog Timer

Can you use any of them?

Assembly 101

• Assembly is the language closest to hardware level (machine language level).

• Why assembly?

• Low level

• speed

• time critical

• small program size

When to program in assembly?

Development Cycle

(4)

Component of Assembly

• Labels

• start in column 1. may follow by a colon (:), space, tab or the end of line.

• begin with an alpha character or an under bar (_) and may contain alphanumeric characters, the under bar and the question mark.

• Mnemonics, Directives and Macros

• Operands

• Comments

Example

list p=18f452 #include p18f452.inc

Dest equ 0x0B ;Define constant org 0x0000 ;Reset vector goto Start

org 0x0020 ;Begin program Start

movlw 0x0A movwf Dest

bcf Dest, 3 ;This line uses 2 operands

goto Start end

Notice the directive

List File

--- C:\PIC\SAMPLE.ASM --- 1: list p=16f877 , b=2, c=80 2: #include p16f877.inc

3: Dest equ 0x0B ;Define constant 4:

org 0x0000 ;Reset vector 0000 2820 GOTO 0x20 5: goto Start

6:

org 0x0020 ;Begin program 7: Start

0020 300A MOVLW 0xa 8: movlw 0x0A 0021 008B MOVWF 0xb 9: movwf Dest

0022 118B BCF 0xb, 0x3 10: bcf Dest, 3 ;This line uses 2 operands 0023 2820 GOTO 0x20 11: goto Start

Notice the directive

Quick tip

Use “banksel” directive to avoid bank switching issue.

0020 1683 BSF 0x3, 0x5 8: banksel TRISB 0022 3000 MOVLW 0 9: ! movlw! 0x00 0023 0086 MOVWF 0x6 10: ! movwf! TRISB

BSF STATUS,RS0

movlw! 0x00

movwf! TRISB

(5)

Macro

OutPort!macro!myport, myvalue

! ! banksel!myport

! ! movlw!myvalue

! ! movwf!myport

! ! endm BCF 0x3, 0x5

MOVLW 0x55 MOVWF PORTB BCF 0x3, 0x5 MOVLW 0xaa MOVWF PORTA

OutPort PORTB, 0x55 OutPort PORTA, 0xAA

Advanced Macro

Add1_x !macro!x

! ! local!i=0

! ! clrw

! ! while i<x

! ! ! addlw ! i!

! ! ! i += 1

! ! endw

! ! endm

Add1_x! 10

; This will give CLRW

ADDLW 0 ADDLW 0x1 ADDLW 0x2 ....

....

ADDLW 0xe ADDLW 0xf

See MPASM HELP for more details.

Embedded C

Lab1 in C

#include <pic16f87.h>

void main (void) {

! TRISB=0x01;

! while ( 1 ) {

! if (RB0) {

! ! PORTB |=0x02;

! ! PORTB &=0xFB;

! ! //PORTB=0x02;

! } else {

! ! PORTB |=0x04;

! ! PORTB &=0xFD;

! ! //PORTB=0x04;

! }

! }

}

(6)

Mix C and Assembly

#include <pic16f87.h>

void main (void) {

! TRISB=0x01;

! while ( 1 ) {

! if (RB0) {

! ! #asm

! ! BSF!_PORTB, 1

! ! BCF!_PORTB, 2

! ! #endasm

! } else {

! ! asm("BCF! _PORTB, 1");

! ! asm("BSF! _PORTB, 2");

! }

! } }

Inline assembly

Use #asm and #endasm for multiple line.

Use asm(“ “) for single line Add _ to variable in C

Good library can help inexperience

programmer.

See 16 adders in C

int sum(int i, int j) {

! int sum;

! sum=i+j;

! return sum;

}

CLRF 0x3 MOVF 0x22, W ADDWF 0x20, W MOVWF 0x20 MOVF 0x23, W BTFSC 0x3, 0 INCF 0x23, W ADDWF 0x21, W MOVWF 0x21 MOVWF 0x7f MOVF 0x20, W MOVWF 0x7e RETURN

How is your LAB 2?

What if there is no library to do your job?

(7)

How to use C in my LAB?

When to write in C?

When to write in Assembly?

POP Quiz:

Will recursive program work in PIC?

Referensi

Dokumen terkait

This service enables user to search instantly access over 200,000,000 criminal, sex offender, and terrorist watch list records, some going back ten years.. Criminal data base search

The analysis described in this section is based on the results of data processing done in two steps: first stage analysis model that consists of eight variables,

(An essential element of the index, which has been omitted from the diagram for simplicity, is the physical address of the block of data records). Should we wish to access record

Territorial conflicts that occur related to the access Source: Author, 2016 Conclusion By knowing the mechanism that is carried out by the users to maintain the privacy and

Hence, it is suggested that the available data warrant i more detailed scrutiny of the potential for creating unusual man-made habitats for mosquito vector develop- ment, during

H Volunteers Volunteers enjoy protection from liability under section 392 of the CLA, which states that a person is not liable for any act or omission when donating food in any of

We evaluate the performance of a disk cache divided into data and metadata regions, where metadata represents only a small portion of the memory, but is accessed very frequently [2, 5,

[84] GA based mapping function is used to embed data in 4×4 block of DWT coefficients and OPAP is applied after embedding the message Outperforms adaptive steganography technique based