|
1 | | -PIC18 Software PWM Driver |
2 | | -************************* |
| 1 | +PIC18 PWM Driver |
| 2 | +**************** |
3 | 3 |
|
4 | | -The driver provides PWM output functionality for PIC18F26K83 on one pin, with timer only. Pulse-width/duty-cycle can be adjusted when output is enabled. Timer 2 is used by this driver. |
| 4 | +The driver provides PWM output functionality for the PIC18F26K83 using the CCP (Capture/Compare/PWM) modules. It supports up to four PWM channels, each with configurable pin mapping. Timer 2 is utilized by this driver to manage PWM periods. |
5 | 5 |
|
6 | 6 | Integration |
7 | 7 | =========== |
8 | 8 |
|
9 | | -ROCKETLIB_PWM_PIN Macro |
10 | | ------------------------ |
11 | | -Set ``ROCKETLIB_PWM_PIN`` macro to the pin register name for output, e.g. ``LATC6`` |
| 9 | +Pin Configuration |
| 10 | +----------------- |
| 11 | +.. c:macro:: CONCAT(a, b, c) |
12 | 12 |
|
13 | | -ROCKETLIB_PWM_INVERT_POLARITY Macro |
14 | | ------------------------------------ |
15 | | -Set ``ROCKETLIB_PWM_INVERT_POLARITY`` macro to inverse output polarity |
| 13 | + Concatenates three tokens to dynamically form register names. |
16 | 14 |
|
17 | | -Interrupt |
18 | | ---------- |
19 | | -``void timer2_handle_interrupt(void)`` have to be called when having a timer 2 interrupt |
| 15 | + :param a: First part of the token. |
| 16 | + :param b: Second part of the token. |
| 17 | + :param c: Third part of the token. |
20 | 18 |
|
21 | | -SoftPWM Controller Functions |
22 | | -============================ |
| 19 | +.. c:macro:: CCPR_L(module) |
| 20 | +
|
| 21 | + Accesses the CCPR Low register for the specified module. |
| 22 | + |
| 23 | + :param module: CCP module number (1-4). |
| 24 | + |
| 25 | +.. c:macro:: CCPR_H(module) |
| 26 | +
|
| 27 | + Accesses the CCPR High register for the specified module. |
| 28 | + |
| 29 | + :param module: CCP module number (1-4). |
| 30 | + |
| 31 | +.. c:macro:: CCP_CON(module) |
| 32 | +
|
| 33 | + Accesses the CCPxCON control register for the specified module. |
| 34 | + |
| 35 | + :param module: CCP module number (1-4). |
| 36 | + |
| 37 | +.. c:macro:: GET_TRIS_REG(port) |
| 38 | +
|
| 39 | + Retrieves the TRIS register for the specified port. |
| 40 | + |
| 41 | + :param port: Port letter (A, B, C). |
| 42 | + |
| 43 | +.. c:macro:: GET_PPS_REG(port, pin) |
| 44 | +
|
| 45 | + Retrieves the PPS register address for the specified port and pin. |
| 46 | + |
| 47 | + :param port: Port letter (A, B, C). |
| 48 | + :param pin: Pin number (0-7). |
| 49 | + |
| 50 | +.. c:macro:: SET_TRIS_OUTPUT(port, pin) |
| 51 | +
|
| 52 | + Sets the specified pin as output by modifying the TRIS register. |
| 53 | + |
| 54 | + :param port: Port letter (A, B, C). |
| 55 | + :param pin: Pin number (0-7). |
| 56 | + |
| 57 | +.. c:macro:: ASSIGN_PPS(port, pin, ccp_module) |
| 58 | +
|
| 59 | + Assigns the CCP module to the specified PPS register to map the peripheral to the desired pin. |
| 60 | + |
| 61 | + :param port: Port letter (A, B, C). |
| 62 | + :param pin: Pin number (0-7). |
| 63 | + :param ccp_module: CCP module number (1-4). |
| 64 | + |
| 65 | +CCP Mode Configuration |
| 66 | +---------------------- |
| 67 | +.. c:macro:: CONFIGURE_CCP_MODE(ccp_module, ccp_con) |
| 68 | +
|
| 69 | + Configure the CCP module for PWM mode. |
| 70 | + |
| 71 | + :param ccp_module: CCP module number (1-4) |
| 72 | + :param ccp_con: CCPxCON register to configure |
| 73 | + |
| 74 | +Output Pin Configuration |
| 75 | +------------------------ |
| 76 | +.. c:macro:: SET_PWM_OUTPUT_PIN(ccp_module, output_pin) |
| 77 | +
|
| 78 | + Set the TRIS register for the output pin. |
| 79 | + |
| 80 | + :param ccp_module: CCP module number (1-4) |
| 81 | + :param output_pin: Output pin number |
| 82 | + |
| 83 | +Duty Cycle Configuration |
| 84 | +------------------------ |
| 85 | +.. c:macro:: WRITE_DUTY_CYCLE(ccp_module, duty_cycle) |
| 86 | +
|
| 87 | + Write the 10-bit duty cycle value to the appropriate CCPRxH:CCPRxL register pair. |
| 88 | + |
| 89 | + :param ccp_module: CCP module number (1-4) |
| 90 | + :param duty_cycle: 10-bit duty cycle value (0-1023) |
| 91 | + |
| 92 | +PWM Controller Functions |
| 93 | +======================== |
| 94 | + |
| 95 | +PWM Pin Configuration Structure |
| 96 | +------------------------------- |
| 97 | +.. c:type:: pwm_pin_config_t |
| 98 | +
|
| 99 | + Structure that holds the configuration details for a PWM pin. |
| 100 | + |
| 101 | + :param port: Port letter (A, B, C). |
| 102 | + :param pin: Pin number (0-7). |
| 103 | + :param pps_reg: PPS register value for this pin. |
23 | 104 |
|
24 | 105 | Initialization |
25 | 106 | -------------- |
26 | | -.. c:function:: void pwm_init(uint16_t period) |
| 107 | +.. c:function:: w_status_t pwm_init(uint8_t ccp_module, pwm_pin_config_t pin_config, uint16_t pwm_period) |
27 | 108 |
|
28 | | - Initialize Software PWM |
| 109 | + Initializes PWM for the specified CCP module with the given pin configuration and PWM period. |
29 | 110 |
|
30 | | - :param uint16_t period: period in increment of 10 us |
| 111 | + :param ccp_module: CCP module number (1-4). |
| 112 | + :param pin_config: PWM pin configuration structure containing port, pin, and PPS register values. |
| 113 | + :param pwm_period: PWM period value. |
| 114 | + :return: W_SUCCESS on successful initialization, otherwise an error code. |
31 | 115 |
|
32 | | -Enable |
33 | | ------- |
34 | | -.. c:function:: void pwm_enable(void) |
| 116 | + This function configures Timer 2, sets the PWM period, and enables the PWM output for the specified CCP module. |
35 | 117 |
|
36 | | - Enable PWM Output |
| 118 | +PWM Operation |
| 119 | +============= |
37 | 120 |
|
38 | | -Disable |
39 | | -------- |
40 | | -.. c:function:: void pwm_disable(void) |
| 121 | +.. c:function:: w_status_t pwm_update_duty_cycle(uint8_t ccp_module, uint16_t duty_cycle) |
41 | 122 |
|
42 | | - Disable PWM Output |
| 123 | + Updates the duty cycle of the specified CCP module to the new value. |
43 | 124 |
|
44 | | -Set Duty Cycle |
45 | | --------------- |
46 | | -.. c:function:: bool pwm_set_duty_cycle(uint16_t duty_cycle) |
| 125 | + :param ccp_module: CCP module number (1-4). |
| 126 | + :param duty_cycle: New duty cycle value (0-1023). |
| 127 | + :return: W_SUCCESS if successful, W_INVALID_PARAM if parameters are out of range. |
| 128 | +
|
| 129 | + The duty cycle is a 10-bit value that determines the percentage of time the signal stays high. The lower 8 bits are written to CCPRxL, and the upper 2 bits are written to CCPRxH. |
| 130 | +
|
| 131 | +Timer Configuration |
| 132 | +------------------- |
| 133 | +.. c:macro:: CONFIGURE_TIMER2(pwm_period) |
| 134 | +
|
| 135 | + Configures Timer 2 to manage PWM periods. The prescaler and postscaler are set to 1:1. |
| 136 | +
|
| 137 | + :param pwm_period: PWM period value to load into the PR2 register. |
| 138 | +
|
| 139 | +Helper Functions |
| 140 | +================ |
| 141 | +
|
| 142 | +PPS Configuration |
| 143 | +----------------- |
| 144 | +.. c:function:: static w_status_t configure_pps(uint8_t ccp_module, pwm_pin_config_t pin_config) |
47 | 145 |
|
48 | | - Set duty cycle |
| 146 | + Configures Peripheral Pin Select (PPS) for the specified pin and CCP module. This function is essential for routing the PWM signal to the correct output pin. |
49 | 147 |
|
50 | | - :param uint16_t duty_cycle: Duty cycle in per-mille(1/1000) |
51 | | - :return: success or not |
52 | | - :retval true: success |
53 | | - :retval false: failed |
| 148 | + :param ccp_module: CCP module number (1-4). |
| 149 | + :param pin_config: Structure containing port, pin, and PPS register values. |
| 150 | + :return: W_SUCCESS if successful, W_INVALID_PARAM if the module number is out of range. |
54 | 151 |
|
55 | | -Set Pulse Width |
56 | | ---------------- |
57 | | -.. c:function:: bool pwm_set_pulse_width(uint16_t pulse_width) |
| 152 | +Error Handling |
| 153 | +============== |
58 | 154 |
|
59 | | - Set pulse width |
| 155 | +Error Codes |
| 156 | +----------- |
| 157 | +- **W_SUCCESS**: Operation completed successfully. |
| 158 | +- **W_INVALID_PARAM**: Parameter value is out of range. Typically returned if an invalid CCP module number or duty cycle value is provided. |
60 | 159 |
|
61 | | - :param uint16_t pulse_width: pulse width in increment of 10 us |
62 | | - :return: success or not |
63 | | - :retval true: success |
64 | | - :retval false: failed |
| 160 | +Practical Considerations |
| 161 | +======================== |
| 162 | +- **Timer Usage**: Ensure Timer 2 is not shared with other peripherals to avoid conflicts in PWM generation. |
| 163 | +- **Prescaler and Postscaler**: The prescaler and postscaler are currently set to 1:1 for simplicity. These can be adjusted to modify the frequency of the PWM signal. |
| 164 | +- **Pin Mapping**: Correct pin mapping is critical for proper operation. Incorrect configuration can result in no output or conflicts with other peripherals. |
0 commit comments