PIC ming in C and Assembly Outlines Microprocessor vs. MicroController PIC in depth PIC ming Assembly ming Krerk Piromsopa, Ph.D. Department of Computer Engineering Chulalongkorn University Embedded C In-line assembly PIC Interface Microcontroller vs. Microprocessor Microcontroller vs. Microprocessor
Microcontroller vs. Microprocessor Microcontroller vs. Microprocessor Figures from http://www.mhennessy.f9.co.uk/pic/introduction.htm PIC16F7X 2.0 MEMORY ORGANIZATION 2.1 Memory Organization PIC in depth FIGURE 1-2: Device PIC16F74 AND PIC16F77 BLOCK DIAGRAM FLASH Data Memory Data EEPROM PIC16F74 4K 192 Bytes 12 Bytes Memory Model There are three memory blocks in each of the PIC16F7X MCUs. The 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). The PIC16F7X devices have a 13-bit program counter capable of addressing an K x 14 program memory space. The PIC16F77/76 devices have K x 14 words of FLASH program memory, and the PIC16F73/74 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. PIC16F77 K 36 Bytes 256 Bytes Bus OSC1/CLKIN OSC2/CLKOUT FLASH Memory 14 Instruction reg Instruction Decode & Control Timing Generation 13 Counter Level Stack (13-bit) Direct Addr 7 Power-up Timer Oscillator Start-up Timer MCLR Power-on Reset Watchdog Timer Brown-out Reset In-Circuit Debugger Low-Voltage ming VDD, VSS Timer0 Timer1 Timer2 Data Bus RAM s RAM Addr (1) 9 3 Addr MUX ALU W reg FSR reg Indirect Addr STATUS reg MUX Parallel Slave Port 10-bit A/D PORTA PORTB PORTC PORTD PORTE RA0/AN0 RA1/AN1 RA2/AN2/VREF- RA3/AN3/VREF+ RA4/T0CKI RA5/AN4/SS RB0/INT RB1 RB2 RB3/PGM RB4 RB5 RB6/PGC RB7/PGD RC0/T1OSO/T1CKI RC1/T1OSI/CCP2 RC2/CCP1 RC3/SCK/SCL RC4/SDI/SDA RC5/SDO RC6/TX/CK RC7/RX/DT RD0/PSP0 RD1/PSP1 RD2/PSP2 RD3/PSP3 RD4/PSP4 RD5/PSP5 RD6/PSP6 RD7/PSP7 RE0/AN5/RD RE1/AN6/WR RE2/AN7/CS FIGURE 2-1: On-Chip Memory CALL, RETURN RETFIE, RETLW PIC16F77/76 PROGRAM MEMORY MAP AND STACK PC<12:0> Stack Level 1 Stack Level 2 Stack Level RESET Vector 13 Interrupt Vector Page 0 Page 1 Page 2 0000h 0004h 0005h 07FFh 000h 0FFFh 1000h FIGURE 2-2: On-Chip Memory CALL, RETURN RETFIE, RETLW PIC16F74/73 PROGRAM MEMORY MAP AND STACK PC<12:0> Stack Level 1 Stack Level 2 Stack Level RESET Vector Interrupt Vector Page 0 Page 1 13 0000h 0004h 0005h 07FFh 000h 0FFFh 1000h 17FFh Data EEPROM CCP1,2 Synchronous Serial Port USART Page 3 100h 1FFFh 1FFFh Note 1: Higher order bits are from the STATUS register.
Map FIGURE 2-3: PIC16F77/76 REGISTER FILE MAP Indirect addr. (*) 00h Indirect addr. (*) 0h Indirect addr. (*) 100h Indirect addr. (*) TMR0 01h OPTION_REG 1h TMR0 101h OPTION_REG PCL 02h PCL 2h PCL 102h PCL STATUS 03h STATUS 3h STATUS 103h STATUS FSR 04h FSR 4h FSR 104h FSR PORTA 05h TRISA 5h 105h PORTB 06h TRISB 6h PORTB 106h TRISB PORTC 07h TRISC 7h 107h PORTD (1) 0h TRISD (1) h 10h PORTE (1) 09h TRISE (1) 9h 109h PCLATH 0Ah PCLATH Ah PCLATH 10Ah PCLATH INTCON 0Bh INTCON Bh INTCON 10Bh INTCON PIR1 0Ch PIE1 Ch EEDATA 10Ch EECON1 PIR2 0Dh PIE2 Dh EEADR 10Dh EECON2 TMR1L 0Eh PCON Eh EEDATH 10Eh Reserved (2) TMR1H 0Fh Fh EEADRH 10Fh Reserved (2) T1CON 10h 90h 110h TMR2 11h SSPCON2 91h 111h T2CON 12h PR2 92h 112h SSPBUF 13h SSPADD 93h 113h SSPCON 14h SSPSTAT 94h 114h CCPR1L 15h 95h 115h CCPR1H 16h 96h 116h CCP1CON 17h 97h 117h RCSTA 1h TXSTA 9h 11h TXREG 19h SPBRG 99h 16 Bytes 119h 16 Bytes RCREG 1Ah 9Ah 11Ah CCPR2L 1Bh 9Bh 11Bh CCPR2H 1Ch 9Ch 11Ch CCP2CON 1Dh 9Dh 11Dh ADRESH 1Eh ADRESL 9Eh 11Eh ADCON0 1Fh ADCON1 9Fh 11Fh 20h A0h 120h 0 Bytes 0 Bytes 0 Bytes 96 Bytes EFh 16Fh accesses F0h accesses 170h accesses 70h-7Fh 70h-7Fh 70h - 7Fh 7Fh FFh 17Fh Bank 0 Bank 1 Bank 2 Bank 3 10h 11h 12h 13h 14h 15h 16h 17h 1h 19h 1Ah 1Bh 1Ch 1Dh 1Eh 1Fh 190h 191h 192h 193h 194h 195h 196h 197h 19h 199h 19Ah 19Bh 19Ch 19Dh 19Eh 19Fh 1A0h 1EFh 1F0h 1FFh Device? I/O Port Watchdog Timer EEPROM Timer & PWM Serial Port Can you use any of them? A2D Interrupt Unimplemented data memory locations, read as 0. * Not a physical register. Note 1: These registers are not implemented on the PIC16F76. 2: These registers are reserved, maintain these registers clear. Assembly 101 Development Cycle Assembly is the language closest to hardware level (machine language level). Why assembly? Low level speed time critical When to program in assembly? small program size
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 Notice the directive list p=1f452 #include p1f452.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 List Notice the directive --- C:\PIC\SAMPLE.ASM ---------------------------------------------------- 1: list p=16f77, b=2, c=0 2: #include p16f77.inc 3: Dest equ 0x0B ;Define constant 4: org 0x0000 ;Reset vector 0000 220 GOTO 0x20 5: goto Start 6: org 0x0020 ;Begin program 7: Start 0020 300A MOVLW 0xa : movlw 0x0A 0021 00B MOVWF 0xb 9: movwf Dest 0022 11B BCF 0xb, 0x3 10: bcf Dest, 3 ;This line uses 2 opera 0023 220 GOTO 0x20 11: goto Start Quick tip Use banksel directive to avoid bank switching issue. BSF STATUS,RS0 movlw! 0x00 movwf! TRISB 0020 163 BSF 0x3, 0x5 : banksel TRISB 0022 3000 MOVLW 0 9:!movlw! 0x00 0023 006 MOVWF 0x6 10:!movwf! TRISB
Macro BCF 0x3, 0x5 MOVLW 0x55 MOVWF PORTB BCF 0x3, 0x5 MOVLW 0xaa MOVWF PORTA OutPort!macro!myport, myvalue!! banksel!myport!! movlw!myvalue!! movwf!myport!! endm 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 <pic16f7.h> void main (void) {! TRISB=0x01;! while ( 1 ) {! if (RB0) {!! PORTB =0x02;!! PORTB &=0xFB;!! //PORTB=0x02; else {!! PORTB =0x04;!! PORTB &=0xFD;!! //PORTB=0x04; }
Mix C and Assembly #include <pic16f7.h> void main (void) {! TRISB=0x01;! while ( 1 ) { Inline assembly! if (RB0) { Use #asm and #endasm for!! #asm multiple line.!! BSF!_PORTB, 1 Use asm( ) for single line!! BCF!_PORTB, 2 Add _ to variable in C!! #endasm else {!! asm("bcf! _PORTB, 1");!! asm("bsf! _PORTB, 2"); } Good library can help inexperience programmer. See 16 adders in C int sum(int i, int j) {! int sum;! sum=i+j;! return sum; } How is your LAB 2? 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 What if there is no library to do your job?
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?