Microprocessor and Interfacing
PIC Flash Memory Segments 2K 01012
PIC’s RAM Banking
Instruction Format
f = 7 bit Maximum memory = 2^7 = 128 Bytes
PIC 16F877 has 512 bytes of RAM: How do we access all of it? STATUS (F# 0x03) Bit 5-6 (RP0,RP1) in STATUS are used for RAM Page selection 07 Total Memory becomes 2^9 = 512 Bytes
Creating a 9 bit RAM address STATUS f (File#) Bit RAM Address OPCODE
void main() { int i; i = 1; i++; } int i; i=1; 000D: MOVLW E: BCF F: MOVWF i++; 0010: INCF 21,F This code should now make sense
RAM location First 32 bytes of every page are reserved. Except on pages 3 and 4 User RAM space. Last 16 Bytes are mirrored
The big picture: where the bits are Bit 5-6 (RP0,RP1) in STATUS are used for RAM Page selection 07 Bit 3-4 in PCLATH are used for memory access
2. เร่ง Memory Operations ด้วย Indirect Addressing
ทำไมต้องมี Indirect Addressing? สมมุติว่าเรามี Array ในภาษา C ดังนี้ int value[8]; หลังจากที่ใช้งานมันไปสักพักเราอยาก Clear ค่าใน array ทั้งหมดมีค่าเป็น 0 Code จะเป็นอย่างไรถ้าเราไม่สามารถใช้ตัวแปรอ้างตำแหน่ง ใน Array เช่นห้ามใช้ value[i] = 0;
With indirect addressing For (i=0 ; i<8 ; i++) { value[i] = 0; } Without indirect addressing Value[0] = 0; Value[1] = 0; Value[2] = 0; Value[3] = 0; Value[4] = 0; Value[5] = 0; Value[6] = 0; Value[7] = 0;
PIC’s Indirect Addressing OpcodeFile
Creating a 9 bit indirect RAM address STATUS bit address 07 9 Bit RAM Address FSR (File #4)
Example: clear RAM locations 0x20 – 0x27 (8 Bytes) InstructionComment BCF0x03, 7 ;select page MOVLW0x20 MOVWF0x04 ;Set FSR NextCLRF0x00 ;clear RAM INCFFSR,F ;increase counter BTFSSFSR,3 ;all done? GOTONext ; no, clear next 0x20 = 0b x27 = 0b
The big picture: where the bits are Bit 5-6 (RP0,RP1) in STATUS are used for RAM Page selection 07 Bit 3-4 in PCLATH are used for memory access Bit 7 (IRP) in STATUS is used for indirect Addressing
Indirect Addressing Ex Find the sum of values in RAM address 0x22-0x25 and store at address 0x21 AddressFunction 0x21Sum 0x22Value1 0x23Value2 0x24Value3 0x25Value4 End Result: Sum = Value1+Value2+Value3+Value4 ; 0x21 = sum result variable clrf0x21 ; 0x22 - 0x25 = input numbers to be summed movlw4 movwf0x22 movlw3 movwf0x23 movlw2 movwf 0x24 movlw1 movwf0x25