#**************************************************************************************** # PIC32MX120F032B Sample Program # Project : # Program Name : # Programer : # Create date : Version 1.0 H24.06.14 # History : # Laungage : Gcc (MPLAB C32) Asm / Internal OSC : 8MHz #***************************************************************************************** # Description # (1)Memory load map # Config words : Physical(0x1FC00BF0-0x1FC00BFF)/Virtual(0xBFC00BF0-0xBFC00BFF) # RAM : Physical(0x00000000-0x00001FFF)/Virtual(0x80000000-0x80001FFF) # Program memory:Physical(0x1D000000-0x1D007FFF)/Virtual(0x9D000000-0x9D007FFF) # SFRs : Physical(0x1F800000-0x1F8FFFFF)/Virtual(0xBF800000-0xBF8FFFFF) # (2)Assembler File extension # ****.S (S:Preprocessed Source file / s:Source file) # (3)Compile Gcc(C32) # (4)general purpose register ( 32bit * 32 ) memo # $0(Fixed 0), $1(not use), $2-$3(v0-v1:return-value register), $4-$7(a0-a3:argument register) # $8-$15,$24-$25(t0-t9:free), $16-$23,$30(s0-s8:free), $26-$27(k0-k1:OS/not use), $28(gp:groval pointer) # $29(sp:stack pointer), $31(ra:return adress) # $f0, $12/13,$14/15(float type pare) #***************************************************************************************** #include # # Define config words # .section .config_1FC00BF0,code // USERID,PMDL1WAY,IOL1WAY,FUSBIDIO,FVBUSONIO .word 0x3FFFFFFF .section .config_1FC00BF4,code // FPLLIDIV,FPLLMUL .word 0xFFF8FF88 // Internal OSC 8MHz .section .config_1FC00BF8,code // FNOSC,FSOSCEN,IESO,POSCMOD,OSCIOFNC,FPBDIV,FCKSM,WDTPS,FWDTEN,FWDTWINSZ .word 0xFD60CF58 // Internal OSC 8MHz .section .config_1FC00BFC,code // JTAGEN,ICESEL,PWP,BWP,CP .word 0x7FFFFFE7 # # Program main # .global main .global Timer2 .text // Start text area .set noreorder // Don't optimize the order of the instructions as well as don't insert 'nop' instructions after jumps and branches. .ent main main: # ******************* # 1.Initialize # ******************* # (1)Define PortB li t0, 0x0000FF00 // 0x0000FF00 => t0-Register sw t0, TRISB($0) // t0-Register => TRISB+0 li s0, 0x00000001 // 0x00000001 => s0-Register sw s0, PORTB($0) // s0-Register => PORTB+0 # (2)Initialized Timer2 jal Timer2 // Wait 1[S] nop # ******************* # 2.Program main # ******************* main1: j main1 nop .end main #****************************************************************************************** # Function Name : mvLED # Title : Move LED function # Input : # Output : #****************************************************************************************** .ent mvLED mvLED: # ******************* # 1.Initialize # ******************* addiu sp, sp, -4 sw t0, 0(sp) addiu sp, sp, -4 sw t1, 0(sp) addiu sp, sp, -4 sw t2, 0(sp) addiu sp, sp, -4 sw t3, 0(sp) addiu sp, sp, -4 sw t4, 0(sp) addiu sp, sp, -4 sw t5, 0(sp) li t4, 0x00000100 /* 0x000FFFFF => t2-Register */ li t1, 0x00000002 /* 0x00000001 => t1-Register */ li t0, 0x00000001 /* 0x00000000 => t0-Register */ # ******************* # 2.Function main # ******************* move t2, s0 /* t2 <= s0 */ mult t2, t1 /* t2 * t1 */ nop nop mflo t3 /* t3 <= LO */ slt t5, t3, t4 /* t3 t4 => t5 */ bne t5, $0 , mvLED1 /* t5 != 0 */ nop move s0, t0 /* s0 <= t0 */ sw s0, PORTB($0) /* s0-Register => PORTB+0 */ j mvLEDE /* GOTO mvLEDE */ nop mvLED1: move s0, t3 /* s0 <= t4 */ sw s0, PORTB($0) /* s0-Register => PORTB+0 */ # ******************* # 3.Function end # ******************* mvLEDE: lw t5, 0(sp) addiu sp, sp, 4 lw t4, 0(sp) addiu sp, sp, 4 lw t3, 0(sp) addiu sp, sp, 4 lw t2, 0(sp) addiu sp, sp, 4 lw t1, 0(sp) addiu sp, sp, 4 lw t0, 0(sp) addiu sp, sp, 4 jr ra /* Return */ nop .end mvLED #****************************************************************************************** # Function Name : Timer2 # Title : Wait 1[S] # Input : # Output : #****************************************************************************************** .ent Timer2 Timer2: # ******************* # 1.Initialize # ******************* addiu sp, sp, -4 sw t0, 0(sp) addiu sp, sp, -4 sw t1, 0(sp) addiu sp, sp, -4 sw t2, 0(sp) # ******************* # 2.Define Timer2 # ******************* # *********************************************** # (1)Define T2CON Register (32Bit) # T2CON(31-16) Unimplemented: Read as e0f # T2CON(15) ON: Timerx On bit # 1 = Starts / 0 = Stop # T2CON(14) Unimplemented: Read as e0f # T2CON(13) SIDL: Stop in Idle Mode bit # 1 = Discontinue module operation when device enters Idle mode # 0 = Continue module operation in Idle mode # T2CON(12-8) Unimplemented: Read as e0f # T2CON(7) TGATE: Timer Gated Time Accumulation Enable bit(3) # When TCS = 1: # This bit is ignored and is read as e0f. # When TCS = 0: # 1 = Gated time accumulation is enabled # 0 = Gated time accumulation is disabled # T2CON(6-4) TCKPS1:TCKPS0: Timerx Input Clock Prescale Select bits # 111 = 1:256 prescale value # 110 = 1:64 prescale value # 101 = 1:32 prescale value # 100 = 1:16 prescale value # 011 = 1:8 prescale value # 010 = 1:4 prescale value # 001 = 1:2 prescale value # 000 = 1:1 prescale value # T2CON(3) T32: 32-Bit Timer Mode Select bit(1) # 1 = Odd numbered and even numbered timers form a 32-bit timer # 0 = Odd numbered and even numbered timers form a separate 16-bit timer # T2CON(2) Unimplemented: Read as e0f # T2CON(1) TCS: Timerx Clock Source Select bit(2) # 1 = External clock from TxCK pin # 0 = Internal peripheral clock # T2CON(0) Unimplemented: Read as e0f # *********************************************** # T2CON:0b00000000000000001000000001100000 <== 1/64,16Bit # T2CON:0b00000000000000001000000001110000 <== 1/256,16Bit li t0, 0b00000000000000001000000001110000 sw t0, T2CON($0) # *********************************************** # (2)Set PR2-Register (PR2[31-16]:Unimplemented, PR2[15-0]timer ) # *********************************************** # PR2 = 0x00007A12: 1Hz (0x7A12;0d31250),FRC 8MHz <== 8MHz/1(pic32)/256(prescale)/1(Hz) li t0, 0x00007A12 sw t0, TMR2($0) # ******************* # 3.Define Interrupt # ******************* # 1)INTCON: INTERRUPT CONTROL REGISTER # INTCONbits.SS0 = 0; # INTCONbits.MVEC = 1; //Multi Vector Configuration bit : 1 = Interrupt controller configured for multi vectored mode # INTCONbits.TPC = 0b010; //Temporal Proximity Control bits : Interrupts of group priority 2 or lower start the TP timer # INTCONbits.INT4EP = 0; //External Interrupt 4 Edge Polarity Control bit : 1 = Rising edge / 0 = Falling edge # INTCONbits.INT3EP = 0; //External Interrupt 3 Edge Polarity Control bit : 1 = Rising edge / 0 = Falling edge # INTCONbits.INT2EP = 0; //External Interrupt 2 Edge Polarity Control bit : 1 = Rising edge / 0 = Falling edge # INTCONbits.INT1EP = 0; //External Interrupt 1 Edge Polarity Control bit : 1 = Rising edge / 0 = Falling edge # INTCONbits.INT0EP = 0; //External Interrupt 0 Edge Polarity Control bit : 1 = Rising edge / 0 = Falling edge li t0, 0b00000000000000000001001000000000 sw t0, INTCON($0) # 2)INTSTAT: INTERRUPT STATUS REGISTER # INTSTATbits.SRIPL = 0b000; //The priority level of the latest interrupt presented to the CPU (This value should only be used when the interrupt controller is configured for Single Vector mode) # INTSTATbits.VEC = 0b000000;//The interrupt vector that is presented to the CPU (This value should only be used when the interrupt controller is configured for Single Vector mode) li t0, 0b00000000000000000000000000000000 sw t0, INTSTAT($0) # 3)TPTMR: TEMPORAL PROXIMITY TIMER REGISTER # TPTMR = 0; Temporal Proximity Timer Reload bits # 4)IFSx: INTERRUPT FLAG STATUS REGISTER : Interrupt Flag Status bits (OK!!) # IFS0bits.T2IF = 0; // 1 = Interrupt request has occurred, 0 = No interrupt request has occurred li t0, 0b00000000000000000000000000000000 sw t0, IFS0($0) # 5)IECx: INTERRUPT ENABLE CONTROL REGISTER : Interrupt Enable bits # IEC0bits.T2IE = 1; // 1 = Interrupt is enabled, 0 = Interrupt is disabled li t0, 0b00000000000000000000001000000000 sw t0, IEC0($0) # 6)IPCx: INTERRUPT PRIORITY CONTROL REGISTER # IPC2bits.T2IP = 0b010; //T2:Interrupt Priority bits(111 = Interrupt priority is 7,,, 000 = Interrupt is disabled) # IPC2bits.T2IS = 0b00; //T2:Interrupt Subpriority bits(11 = Interrupt subpriority is 3,,, 00 = Interrupt subpriority is 0) li t0, 0b00000000000000000000000000001000 sw t0, IPC2($0) # 7)enable interrupts ei # ******************* # 4.Timer # ******************* lw t0, IFS0($0) andi t1, t0, 0b1111110111111111 // T2IF(9) <= 0 sw t1, IFS0($0) # ******************* # 5.Function end # ******************* lw t2, 0(sp) addiu sp, sp, 4 lw t1, 0(sp) addiu sp, sp, 4 lw t0, 0(sp) addiu sp, sp, 4 jr ra // Return nop .end Timer2 #****************************************************************************************** # Function Name : # Title : Interrupt handler function # Input : # Output : #****************************************************************************************** .section .vector_8,code j IntTMR2 nop .text .ent IntTMR2 IntTMR2: # ******************************** # 1.Initialize # ******************************** addiu sp, sp, -4 sw t0, 0(sp) addiu sp, sp, -4 sw t1, 0(sp) addiu sp, sp, -4 sw t2, 0(sp) # ******************************** # 2.Clear the Timer2 interrupt Flag # ******************************** lw t0, IFS0($0) andi t1, t0, 0b1111110111111111 // T2IF(9) <= 0 sw t1, IFS0($0) # ******************************** # 3.Move LED # ******************************** jal mvLED // CALL mvLED nop # ******************************** # 4.Function end # ******************************** lw t2, 0(sp) addiu sp, sp, 4 lw t1, 0(sp) addiu sp, sp, 4 lw t0, 0(sp) addiu sp, sp, 4 eret .end IntTMR2