Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
learn:courses:unit-2:start [2017/04/14 19:02] – [Listing A.6. Example Timer 1 Delay Using Interrupts] Marthalearn:courses:unit-2:start [2022/10/04 11:38] (current) – external edit 127.0.0.1
Line 2: Line 2:
 [[{}/learn/courses/microprocessor-io-unit-1/start|Back to Unit 1]] [[{}/learn/courses/microprocessor-io-unit-1/start|Back to Unit 1]]
 ==Unit 2 Labs== ==Unit 2 Labs==
-  * [[https://reference.digilentinc.com/learn/courses/unit-2-lab2a/start|Lab 2a]] +  * [[/learn/courses/unit-2-lab2a/start|Lab 2a]] 
-  * [[https://reference.digilentinc.com/learn/courses/unit-2-lab2b/start|Lab 2b]]+  * [[/learn/courses/unit-2-lab2b/start|Lab 2b]]
  
  
Line 20: Line 20:
  
 ===== 2. Objectives: ===== ===== 2. Objectives: =====
-  * Implement task management based on [[https://en.wikipedia.org/wiki/Polling_(computer_science)|polling]] PIC32 timers. +  - Implement task management based on [[https://en.wikipedia.org/wiki/Polling_(computer_science)|polling]] PIC32 timers. 
-  * Implement task management based on PIC32 timer [[https://en.wikipedia.org/wiki/Interrupt_latency|interrupts]]. +  - Implement task management based on PIC32 timer [[https://en.wikipedia.org/wiki/Interrupt_latency|interrupts]]. 
-  * Understand the principles of a software implemented [[https://en.wikipedia.org/wiki/Finite-state_machine|finite state machine]] (FSM). +  - Understand the principles of a software implemented [[https://en.wikipedia.org/wiki/Finite-state_machine|finite state machine]] (FSM). 
-  * Use time management and FSM code to control the speed of rotation of the [[https://en.wikipedia.org/wiki/Stepper_motor|stepper motor rotor]]. +  - Use time management and FSM code to control the speed of rotation of the [[https://en.wikipedia.org/wiki/Stepper_motor|stepper motor rotor]]. 
-  * Implement a multitasking real-time application using [[https://en.wikipedia.org/wiki/Foreground-background|foreground-background]] scheduling.+  - Implement a multitasking real-time application using [[https://en.wikipedia.org/wiki/Foreground-background|foreground-background]] scheduling.
  
  
 ===== 3. Basic Knowledge: ===== ===== 3. Basic Knowledge: =====
-  * [[https://www.cs.umd.edu/class/sum2003/cmsc311/Notes/Seq/diff.html|Understanding of combinational logic and sequential logic]]. +  - [[https://www.cs.umd.edu/class/sum2003/cmsc311/Notes/Seq/diff.html|Understanding of combinational logic and sequential logic]]. 
-  * [[http://www.tutorialspoint.com/cprogramming/switch_statement_in_c.htm|Using the SWITCH-CASE construct in C]]. +  - [[http://www.tutorialspoint.com/cprogramming/switch_statement_in_c.htm|Using the SWITCH-CASE construct in C]]. 
-  * [[http://www.learn-c.com/schemat.htm|How to interpret a schematic diagram and electric circuits]]. +  - [[http://www.learn-c.com/schemat.htm|How to interpret a schematic diagram and electric circuits]]. 
-  * [[http://homepage.cs.uiowa.edu/~jones/step/an907a.pdf|Fundamentals of stepper motors]].+  - [[http://homepage.cs.uiowa.edu/~jones/step/an907a.pdf|Fundamentals of stepper motors]].
  
  
 ===== 4. Unit Equipment List ===== ===== 4. Unit Equipment List =====
 ==== 4.1. Hardware ==== ==== 4.1. Hardware ====
-  * [[http://store.digilentinc.com/basys-mx3-pic32mx-trainer-board-recommended-for-embedded-systems-courses/|Basys MX3 trainer board]] +  - [[https://digilent.com/shop/basys-mx3-pic32mx-trainer-board-for-embedded-systems-courses/|Basys MX3 trainer board]] 
-  * [[http://store.digilentinc.com/usb-a-to-micro-b-cable/|Micro USB cable]] +  - [[https://digilent.com/shop/usb-a-to-micro-b-cable/|Micro USB cable]] 
-  * Workstation computer running Windows 10 or higher, MAC OS, or Linux +  - Workstation computer running Windows 10 or higher, MAC OS, or Linux 
-  * [[http://store.digilentinc.com/stepper-motor/|4-wire stepper motor]] +  - [[https://digilent.com/shop/stepper-motor/|4-wire stepper motor]] 
-  * [[http://store.digilentinc.com/5v-4000ma-switching-power-supply/|5V, 4A DC power supply]]+  - [[https://digilent.com/shop/5v-4a-switching-power-supply/|5V, 4A DC power supply]]
  
 ==== 4.2. Software ==== ==== 4.2. Software ====
 The following programs must be installed on your development workstation: The following programs must be installed on your development workstation:
  
-  * [[http://www.microchip.com/mplab/mplab-x-ide|Microchip MPLAB X® v3.35 or higher]] +  - [[http://www.microchip.com/mplab/mplab-x-ide|Microchip MPLAB X® v3.35 or higher]] 
-  * [[http://www.microchip.com/xcdemo/xcpluspromo.aspx|XC32 Cross Compiler]] +  - [[http://www.microchip.com/xcdemo/xcpluspromo.aspx|XC32 Cross Compiler]] 
-  * [[http://www.microchip.com/SWLibraryWeb/product.aspx?product=PIC32%20Peripheral%20Library|PLIB Peripheral Library]]+  - [[http://www.microchip.com/SWLibraryWeb/product.aspx?product=PIC32%20Peripheral%20Library|PLIB Peripheral Library]]
  
 ===== 5. Project Takeaways ===== ===== 5. Project Takeaways =====
-  * How to set the period of a PIC32 Timer. +  - How to set the period of a PIC32 Timer. 
-  * How to write a software state machine. +  - How to write a software state machine. 
-  * How to allocate tasks to either foreground or background scheduling. +  - How to allocate tasks to either foreground or background scheduling. 
-  * Understanding of stepper motor operations and applications.+  - Understanding of stepper motor operations and applications.
    
  
Line 63: Line 63:
 The correct operation of a dynamic real-time scheduling scheme is predicated on two requirements. The first is that the worst case (longest) execution time of the timer interrupt service routine (ISR) must be less than the interrupt interval. The second is that the time remaining in the interrupt period after execution of the ISR must be sufficient so that it appears to the user that the processor is acknowledging his or her inputs in a timely manner. The correct operation of a dynamic real-time scheduling scheme is predicated on two requirements. The first is that the worst case (longest) execution time of the timer interrupt service routine (ISR) must be less than the interrupt interval. The second is that the time remaining in the interrupt period after execution of the ISR must be sufficient so that it appears to the user that the processor is acknowledging his or her inputs in a timely manner.
  
-Real-time operation has two basic requirements: correct value at the correct time. A system that cannot always meet both of these requirements is a failed system. Hence, it is not sufficient to just have the correct output values; these outputs have to be generated at the correct time. You have already developed a real-time operating system for [[https://reference.digilentinc.com/learn/courses/microprocessor-io-lab-1b/start|Lab 1b]].+Real-time operation has two basic requirements: correct value at the correct time. A system that cannot always meet both of these requirements is a failed system. Hence, it is not sufficient to just have the correct output values; these outputs have to be generated at the correct time. You have already developed a real-time operating system for [[/learn/courses/microprocessor-io-lab-1b/start|Lab 1b]].
  
 The time required to service the ISR includes the execution time of the user ISR code in addition to the time required to save and restore the system context. Using the MPLAB X Stopwatch debugging feature, we find that it requires 33 machine cycles to save and restore the system context. Using a core frequency of 80 MHz, the context saving and restoring time is 1.03 μs. The time to execute the user written code varies. In the case of Labs 2a and 2b, execution of the ISR code will vary based upon the conditional execution of the LED and stepper motor functions. The time required to service the ISR includes the execution time of the user ISR code in addition to the time required to save and restore the system context. Using the MPLAB X Stopwatch debugging feature, we find that it requires 33 machine cycles to save and restore the system context. Using a core frequency of 80 MHz, the context saving and restoring time is 1.03 μs. The time to execute the user written code varies. In the case of Labs 2a and 2b, execution of the ISR code will vary based upon the conditional execution of the LED and stepper motor functions.
Line 251: Line 251:
 ==== Listing 7.6. Timer 1 ISR at Interrupt Level 2 ==== ==== Listing 7.6. Timer 1 ISR at Interrupt Level 2 ====
 <code> <code>
-void __ISR(_TIMER_1_VECTOR, IPL2) Timer1Handler(void) +void __ISR(_TIMER_1_VECTOR, IPL2SOFT) Timer1Handler(void) 
 {  {
  
Line 322: Line 322:
 //Figure 7.2. State diagram for a push-on / push-off button operation.// //Figure 7.2. State diagram for a push-on / push-off button operation.//
  
-are needed for a button press and release when the switch is on, as well as when the switch is off. There is only one possible event that initiates a transition: the clock event, which for this example is a millisecond timer. The number of clock pulses is counted to hold the system in a fixed state until the predefined delay period has expired. This is used to eliminate multiple transitions due to [[http://www.eng.utah.edu/~cs5780/debouncing.pdf|switch contact bounce]] when a button is pressed.  +The button action described by Fig. 7.2 shows that four states are required to represent its operation: two states are needed for a button press and release when the switch is on, as well as when the switch is off. There is only one possible event that initiates a transition: the clock event, which for this example is a millisecond timer. The number of clock pulses is counted to hold the system in a fixed state until the predefined delay period has expired. This is used to eliminate multiple transitions due to [[http://www.eng.utah.edu/~cs5780/debouncing.pdf|switch contact bounce]] when a button is pressed.  
    
 The guard condition on the clock event inhibits the transition between states unless the button is in the proper pressed or released condition. The action associated with the transaction controls an LED to indicate the condition of the push-on/push-off switch output, sw. It is apparent from Fig. 7.2 that even relatively simple dynamic systems can result in complex logic. The guard condition on the clock event inhibits the transition between states unless the button is in the proper pressed or released condition. The action associated with the transaction controls an LED to indicate the condition of the push-on/push-off switch output, sw. It is apparent from Fig. 7.2 that even relatively simple dynamic systems can result in complex logic.
Line 343: Line 343:
    {    {
        case 0:        case 0:
-if(button) /* Button pressed - switch on */+           if(button) /* Button pressed - switch on */
            {            {
                setLED0(1);                setLED0(1);
Line 410: Line 410:
 ==== Listing A.1. Millisecond Delay Based on Polling the Core Timer ==== ==== Listing A.1. Millisecond Delay Based on Polling the Core Timer ====
 <code> <code>
-#define CORE_MS_TICK_RATE CORE_OSCILLATOR_FREQUERNCY/2/1000+#define CORE_MS_TICK_RATE CORE_OSCILLATOR_FREQUENCY/2/1000
 void msDelay(unsigned int mS) void msDelay(unsigned int mS)
 { {
Line 440: Line 440:
 The maximum counter value is determined by the size of the register that holds the counts. Common sizes for microprocessors are 8-, 16-, and 32-bit. Including the core timer, the PIC32 processor has six timers that can be used for delay timing. The core timer is 32 bits wide. Timers 2 and 3 along with Timers 4 and 5 can be used together as 32-bit counters, respectively, extending the count value range from 65,536 to 4,294,967,296. As shown in Fig. A.3, when the counter register equals the value of the period register (PR), the counter is reset to zero and the timer interrupt flag is set. Although the core timer can be reset to any value, PIC32 documentation recommends that such practices be avoided.  The maximum counter value is determined by the size of the register that holds the counts. Common sizes for microprocessors are 8-, 16-, and 32-bit. Including the core timer, the PIC32 processor has six timers that can be used for delay timing. The core timer is 32 bits wide. Timers 2 and 3 along with Timers 4 and 5 can be used together as 32-bit counters, respectively, extending the count value range from 65,536 to 4,294,967,296. As shown in Fig. A.3, when the counter register equals the value of the period register (PR), the counter is reset to zero and the timer interrupt flag is set. Although the core timer can be reset to any value, PIC32 documentation recommends that such practices be avoided. 
  
-The period register is used to set the timer’s maximum count. If a period equal to a value of N is set, then N-1 is written to the PR register. The period registers are declared as PR1 for Timer 1, PR2 for Timer 2, and so on. Each Timer 1 count is compared to the value of the PR1 register. When the PR1 register equals the Timer 1 register, the timer has reached its terminal count and initiates two actions: first, the Timer 1 register is reset to zero, and second, the Timer 1 Interrupt Flag (T1IF) bit is set in the Interrupt Flag Status register (IFS0). (See the [[http://ww1.microchip.com/downloads/en/DeviceDoc/61156G.pdf|PIC32MX3xx/4xx/5xx User’s Manual]] Sheet Section 7, Table 7-1 for additional information.) +The period register is used to set the timer’s maximum count. If a period equal to a value of N is set, then N-1 is written to the PR register. The period registers are declared as PR1 for Timer 1, PR2 for Timer 2, and so on. Each Timer 1 count is compared to the value of the PR1 register. When the PR1 register equals the Timer 1 register, the timer has reached its terminal count and initiates two actions: first, the Timer 1 register is reset to zero, and second, the Timer 1 Interrupt Flag (T1IF) bit is set in the Interrupt Flag Status register (IFS0). (See the [[http://ww1.microchip.com/downloads/en/DeviceDoc/60001185F.pdf|PIC32MX3xx/4xx/5xx User’s Manual]] Sheet Section 7, Table 7-1 for additional information.) 
  
 As Fig. A.3 illustrates, the Timer 1 clock frequency (T1CLK) is determined from the crystal frequency on the Basys MX3 trainer board (XTAL), the PLL input divider (FPLLIDIV), the PLL multiplier (FPLLMUL), the PLL output divider (FPLLODIV), the peripheral bus clock divider (FPBDIV), and the Timer pre-scale (TCKPS). This frequency is computed using Eq.1. The values of XTAL, FPLLMUL, FPLLDIV, FPLLODIV, and FPBDIV are set in the common “config_bits.h” header file.   As Fig. A.3 illustrates, the Timer 1 clock frequency (T1CLK) is determined from the crystal frequency on the Basys MX3 trainer board (XTAL), the PLL input divider (FPLLIDIV), the PLL multiplier (FPLLMUL), the PLL output divider (FPLLODIV), the peripheral bus clock divider (FPBDIV), and the Timer pre-scale (TCKPS). This frequency is computed using Eq.1. The values of XTAL, FPLLMUL, FPLLDIV, FPLLODIV, and FPBDIV are set in the common “config_bits.h” header file.  
Line 449: Line 449:
 //Figure A.3. Divider chain from the CPU crystal (oscillator) to the Timer 1 Interrupt flag.// //Figure A.3. Divider chain from the CPU crystal (oscillator) to the Timer 1 Interrupt flag.//
  
-Since the Timer 1 register is reset to zero, the value set into the PR register is one less than the desired number of Timer 1 clock counts. Hence, the rate that the timer interrupt flag is set equals the timer clock frequency divided by the value written to the PR register, as expressed by Eq. 2. For example, using the values shown in Fig. A.4, the T1PS is set for 8 and the PR1 register is set to 24999. The result of these settings is that the T1 interrupt flag is set at the rate of 50 Hz, provided that the subsequent interrupt flag is cleared in software. The T1IF can be polled or used to generate interrupts. Timers 2 through 5 are similar to Timer 1, with the differences being the timer pre-scale options and the period register.+Since the Timer 1 register is reset to zero, the value set into the PR register is one less than the desired number of Timer 1 clock counts. Hence, the rate that the timer interrupt flag is set equals the timer clock frequency divided by the value written to the PR register, as expressed by Eq. 2. For example, using the values shown in Fig. A.3, the T1PS is set for 8 and the PR1 register is set to 24999. The result of these settings is that the T1 interrupt flag is set at the rate of 50 Hz, provided that the subsequent interrupt flag is cleared in software. The T1IF can be polled or used to generate interrupts. Timers 2 through 5 are similar to Timer 1, with the differences being the timer pre-scale options and the period register.
  
 ==== A.5. Timer Interrupts ==== ==== A.5. Timer Interrupts ====
Line 485: Line 485:
 * *
 * Oscillator Configuration Bit Settings: config_bits.h * Oscillator Configuration Bit Settings: config_bits.h
-* Basys MX3 hardware initialization is provided by Hardware_Setup.c +* Basys MX3 hardware initialization is provided by hardware.c and should be included as a project source file.
 * Notes: * Notes:
 *   - Peripheral clock: BPCLK = FOSC/PB_DIV = 80E6/8 = 10MHz *   - Peripheral clock: BPCLK = FOSC/PB_DIV = 80E6/8 = 10MHz
 *   - Timer 1 clock = T1_CLK = PBCLK / T1PS = 10E6 / 8 = 1.25E5 *   - Timer 1 clock = T1_CLK = PBCLK / T1PS = 10E6 / 8 = 1.25E5
-*   - To generate a 1 ms delay, PR1 is loaded with T1_TICK = (10000-1) = 9999+*   - To generate a 1 ms delay, PR1 is loaded with T1_TICK - 1 = (1250 - 1) = 1249
 * *
 ********************************************************************/ ********************************************************************/
-#include "config_bits.h" 
 #include <plib.h> #include <plib.h>
 +#include "config_bits.h"
 +#include "hardware.h"
  
 /* Application constant */ /* Application constant */
Line 506: Line 507:
  
 /* Configure Timer1 to roll over once each second */ /* Configure Timer1 to roll over once each second */
-   OpenTimer1(T1_ON | T1_SOURCE_INT | T1_PS_1_256, T1_TICK - 1);+   OpenTimer1(T1_ON | T1_SOURCE_INT | T1_PS_1_8, T1_TICK - 1);
  
    while (1)     while (1) 
Line 519: Line 520:
    }     } 
        
-   return (EXIT_FAILURE);  /* System has failed is this line is reached */+   return (EXIT_FAILURE);  /* System has failed if this line is reached */
 } /* End of File main.c */  } /* End of File main.c */ 
 </code> </code>
Line 563: Line 564:
 /********************************************************************* /*********************************************************************
 * The purpose of this example code is to demonstrate the use of the Type A  * The purpose of this example code is to demonstrate the use of the Type A 
-* Timer 1 interrupts to generate a 1 ms delay.+* Timer 1 interrupts to generate a 1 second delay.
 * *
 * Platform:     Basys MX3  * Platform:     Basys MX3 
Line 576: Line 577:
 * *
 * Oscillator Configuration Bit Settings: config_bits.h * Oscillator Configuration Bit Settings: config_bits.h
-* Basys MX3 hardware initialization is provided by Hardware_Setup.c +* Basys MX3 hardware initialization is provided by hardware.c 
 * Notes: * Notes:
 *        - Peripheral Clock: PBCLK = 80E6 / 8 = 10E6  *        - Peripheral Clock: PBCLK = 80E6 / 8 = 10E6 
Line 583: Line 584:
 * *
 ********************************************************************/ ********************************************************************/
 +
 +#include <plib.h>       /* Include peripheral library */
 #include "config_bits.h"   /* System configuration */ #include "config_bits.h"   /* System configuration */
-#include <plib.h>    /* Include peripheral library */+#include "hardware.h"      /* Includes GetPeripheralClock() */
                                                
 /* Application constant */ /* Application constant */
-#define T1_PRESCALE      8 +#define T1_PRESCALE      256 
-#define TOGGLES_PER_SEC  1000+#define TOGGLES_PER_SEC  1
 #define T1_TICK          (GetPeripheralClock() / T1_PRESCALE / TOGGLES_PER_SEC) #define T1_TICK          (GetPeripheralClock() / T1_PRESCALE / TOGGLES_PER_SEC)
  
Line 617: Line 620:
        /* Code background tasks */        /* Code background tasks */
    }     } 
-   return (EXIT_FAILURE);  /* System has failed is this line is reached */+   return (EXIT_FAILURE);  /* System has failed if this line is reached */
 } /* End of main */  } /* End of main */ 
  
Line 624: Line 627:
 * SYNTAX:         void __ISR(_TIMER_1_VECTOR, IPL2SOFT) Timer1Handler(void) * SYNTAX:         void __ISR(_TIMER_1_VECTOR, IPL2SOFT) Timer1Handler(void)
 * KEYWORDS:       millisecond, hardware delay, Timer A, timer * KEYWORDS:       millisecond, hardware delay, Timer A, timer
-* DESCRIPTION:    Services the Timer 1 interrupt and toggles LED1+* DESCRIPTION:    Services the Timer 1 interrupt and toggles LED0
 * Parameters:     None * Parameters:     None
 * RETURN:         None * RETURN:         None
Line 643: Line 646:
 ---- ----
  
-[[{}/learn/courses/unit-1/start|Back to Unit 1]]+[[{}learn/courses/microprocessor-io-unit-1/start|Back to Unit 1]]
 [[{}/learn/courses/unit-2-lab2a/start|Go to Lab 2a]] [[{}/learn/courses/unit-2-lab2a/start|Go to Lab 2a]]
 [[{}/learn/courses/unit-2-lab2b/start|Go to Lab 2b]] [[{}/learn/courses/unit-2-lab2b/start|Go to Lab 2b]]