CS224 – Programming PIC32 Microcontroller (Solution)

$ 24.99
Category:

Description

Purpose:
The new version of the lab assignment only involves C to MIPS assembly language conversion. Purpose of the practice is understanding the fundamental principles of PIC32 programming that involves I/O and bit operations on this microcontroller.

Summary
Introduction to PIC32 programming.

YES paper submission this time too.

You are required to read the instructions provided on this document. No excuses will be accepted.
YES paper submission this time too. YES paper submission this time too. YES paper submission this time too. YES paper submission this time too. YES paper submission this time too.

No late submission will be accepted. No late submission will be accepted. No late submission will be accepted. No late submission will be accepted. No late submission will be accepted.

Upload txt. Upload txt. Upload txt. Upload txt. Upload txt. Upload txt. Upload txt. Upload txt.
Upload txt. Upload txt. Upload txt. Upload txt. Upload txt. Upload txt.

C to MIPS Assembly Language Conversion (100 points)

At the top of your submission provide the following information.

CS224
Your Full Name/Bilkent ID:

Convert the C programs 1 & 2 into MIPS assembly language.

For register addresses of PIC32 please refer to the class handout or the PIC32 datasheets (see the Lab7 Unilica folder).

In the body make sure that you clearly indicate what is provided for what:

1. Clearly indicate the programs. At the beginning of each program provide your name and section in a comment line.
2. Use consistent and meaningful format in your MIPS code. Neat and understandable presentation is important during grading.
4. If a C code line does not require any code in MIPS, like declarations etc., just provide the C code.
5. In your presentation delete the comments of the C code.

Program No. 1 (40 points) void initspi(void) {
char junk;
SPI2CONbits.ON = 0; // disable SPI to reset any previous state
junk = SPI2BUF; // read SPI buffer to clear the receive buffer
SPI2BRG = 7;
SPI2CONbits.MSTEN = 1;// enable master mode
SPI2CONbits.CKE = 1; // set clock-to-data timing
while(!SPI2CONbits.DONESSEN); // Check the SSEN bit
SPI2CONbits.ON = 1; // turn SPI on
}

Program No. 2 (60 points)
// This code shows and rotates the pattern (10001000) right or stops based on the input coming from the user. The pattern is to be shown on the LEDs.

int stop = 0; int initial = 0b01110111; //Initial pattern. Note that 0 means on, while 1 means off. int right = 1;

void main(){
TRISD = 0x0; // All bits of PORTD are output. ~0 means output~

// Three bits of PORTA are inputs but only one of them is used in this example as a
stop button, others are redundant. ~1 means input~

TRISA = 0b111;

// From PORTD, outputs will be sent to LEDs. Make sure that you physically connected them by looking at the Figure 1, in the directives document.
// Initial pattern is sent to the LEDs through PORTD.

PORTD = initial; while(1){ int lsb; int mask;
// Stop button is the push-button which is labeled as 1 on the board.
if(PORTABits.RA1 == 0){ // If stop button clicked
stop = !stop; if(!stop){ // If process restarted, copy initial pattern into PORTD.
PORTD = initial;
}
} if(!stop){ //Rotate right
lsb = PORTD & 0x1; // Extract least significant bit mask = lsb << 7; // Least significant bit will be the msb of the
// shifted pattern
PORTD = (PORTD >> 1) | mask; // Paste lsb to the leftmost bit the
// right shifted portd
} else {
//Do not shift anything, that is, stop.
PORTD = 0b11111111;
} delay_ms(1000); // Wait 1 second.
} }

APPENDIX

HOW TO FIND MEMORY ADDRESSES OF REGISTERS AND
HOW TO ACCESS THEIR BITS: AN EXAMPLE

Translate the following C code into MIPS assembly code, as the PIC32 compiler would based on the p32xxxx header file. You can refer to the Table 4-16 from the PIC32 datasheet. Use only real MIPS instructions.

int readadc (void) {
AD1CON1bits.SAMP = 0 ; while (!AD1CON1bits.DONE) ; AD1CON1bits.SAMP = 1 ; AD1CON1bits.DONE = 0 ; return (ADC1BUF0) ;
}

readadc:
lui $t0, 0xBF80 // load AD1CON1 address into $t0
ori $t0, $t0, 0x9000 // (0xBF809000 from Table 4-16)
lw $t1, 0 ($t0) // get AD1CON1 value into register $t1
andi $t1, $t1, 0xFFFD
// AND mask to clear bit 1 (SAMP bit)
sw $t1, 0 ($t0) // store value back to AD1CON1
while: lw $t1, 0 ($t0)
// get AD1CON1 value into register $t1
andi $t2, $t1, 0x0001
// AND mask to clear all bits except bit 0
// (DONE bit)
beq $t2, $0, while
// spin in while loop until DONE = 1
ori $t1, $t1, 0x0002
// OR mask to set bit 1 (SAMP bit)
andi $t1, $t1, 0xFFFE // AND mask to clear bit 0 (DONE bit)
sw $t1, 0 ($t0) // store value back to AD1CON1
lw $v0, 0x70 ($t0)
// load ADC1BUF0 into $v0
// (address is 0xBF809070 in Table 4-16)
jr $ra // return to callee

What is the address of ADCON1: Find AD1CON1 in the data sheets of PIC32 (see the documents folder in Unilica for the data sheets, or web). On that table take the number on the left upper corner (BF80) from the table contains AD1CON1 and take the number horizontally aligned with AD1CON1 (9000) and concatenate them, it becomes 0XBF809000. See the firts 3 lines of the MIPS code above.

How to access AD1CON1bits.SAMP: Bit names of AD1CON1 are horizontally available next to AD1CON1 on the right hand side in two narrow lines/rows. Lower line shows the bit positions 0 to 15 and the upper line shows the bit position 16 to 31/ Notice the notation 31/15 to 16/0. Bit position of SAMP is 2. For clearing the SAMP bit see the 4th line of the MIPS code.

2

Reviews

There are no reviews yet.

Be the first to review “CS224 – Programming PIC32 Microcontroller (Solution)”

Your email address will not be published. Required fields are marked *