|
STC15W408AS library 0.18.0
|
Enumerations | |
| enum | pca_pin_group_t { PCA_PIN_GROUP_1 = 0 , PCA_PIN_GROUP_2 = 1 , PCA_PIN_GROUP_3 = 2 } |
| PCA pin group. More... | |
| enum | pca_clock_source_t { PCA_CLOCK_SYS_12 = 0 , PCA_CLOCK_SYS_2 = 1 , PCA_CLOCK_TIMER0 = 2 , PCA_CLOCK_ECI = 3 , PCA_CLOCK_SYS = 4 , PCA_CLOCK_SYS_4 = 5 , PCA_CLOCK_SYS_6 = 6 , PCA_CLOCK_SYS_8 = 7 } |
| PCA counter clock source. More... | |
| enum | pca_pwm_resolution_t { PCA_PWM_8BIT = 0 , PCA_PWM_7BIT = 1 , PCA_PWM_6BIT = 2 } |
| PCA module PWM output resolution. More... | |
pin group | |
PCA pin mapping functions | |
| #define | pca_set_pin_group(group) |
| Set CCP/ECI pin group. | |
| #define | pca_get_pin_group() ((pca_pin_group_t)(bit_shift_right(get_reg(P_SW1, (PCA_CCP_S0_MSK | PCA_CCP_S1_MSK)), PCA_CCP_S0_BIT))) |
| Get CCP/ECI pin group. | |
counter | |
PCA counter functions | |
| #define | pca_set_clock_source(src) |
| Set PCA counter clock source. | |
| #define | pca_get_clock_source() ((pca_clock_source_t)(bit_shift_right(get_reg(CMOD, PCA_CPS_MASK), PCA_CPS0_BIT))) |
| Get PCA counter clock source. | |
| #define | pca_set_counter(value) |
| Set PCA counter value. | |
| #define | pca_get_counter() ((uint16_t)(bit_shift_left((uint16_t)CH, 8) | (uint16_t)CL)) |
| Get PCA counter value. | |
| #define | pca_start() (CR = 1) |
| Start PCA counter. | |
| #define | pca_stop() (CR = 0) |
| Stop PCA counter. | |
| #define | is_pca_running() (CR) |
| Get PCA counter run status. | |
| #define | pca_counter_run_in_idle() (bit_clr(CMOD,SBIT7)) |
| Let PCA counter run in MCU idle mode. | |
| #define | pca_counter_gate_off_in_idle() (bit_set(CMOD, SBIT7)) |
| Gate off PCA counter in MCU idle mode. | |
| #define | is_pca_counter_gated_off_in_idle() (test_if_bit_set(CMOD, SBIT7)) |
| Get PCA counter idle mode status. | |
overflow | |
PCA counter overflow flag and interrupt functions | |
| #define | pca_clear_overflow_flag() (CF = 0) |
| Clear PCA counter overflow flag. | |
| #define | is_pca_overflow() (CF) |
| Get PCA counter overflow flag status. | |
| #define | pca_enable_overflow_interrupt() (bit_set(CMOD, SBIT0)) |
| Enable PCA counter overflow interrupt. | |
| #define | pca_disable_overflow_interrupt() (bit_clr(CMOD, SBIT0)) |
| Disable PCA counter overflow interrupt. | |
| #define | is_pca_overflow_interrupt_enabled() (test_if_bit_set(CMOD, SBIT0)) |
| Get PCA counter overflow interrupt enable status. | |
module flags | |
PCA module compare/capture flag functions | |
| #define | pca_clear_module_flag(module) (CCF ## module = 0) |
| Clear PCA module compare/capture flag. | |
| #define | is_pca_module_flag_set(module) (CCF ## module) |
| Get PCA module compare/capture flag status. | |
| #define | pca_module_enable_interrupt(module) (bit_set(CCAPM ## module, SBIT0)) |
| Enable PCA module compare/capture interrupt. | |
| #define | pca_module_disable_interrupt(module) (bit_clr(CCAPM ## module, SBIT0)) |
| Disable PCA module compare/capture interrupt. | |
| #define | is_pca_module_interrupt_enabled(module) (test_if_bit_set(CCAPM ## module, SBIT0)) |
| Get PCA module compare/capture interrupt enable status. | |
compare/capture | |
PCA module compare and capture functions | |
| #define | pca_module_enable_comparator(module) (bit_set(CCAPM ## module, SBIT6)) |
| Enable PCA module comparator. | |
| #define | pca_module_disable_comparator(module) (bit_clr(CCAPM ## module, SBIT6)) |
| Disable PCA module comparator. | |
| #define | pca_module_enable_capture_positive(module) (bit_set(CCAPM ## module, SBIT5)) |
| Enable capture on positive edge. | |
| #define | pca_module_disable_capture_positive(module) (bit_clr(CCAPM ## module, SBIT5)) |
| Disable capture on positive edge. | |
| #define | pca_module_enable_capture_negative(module) (bit_set(CCAPM ## module, SBIT4)) |
| Enable capture on negative edge. | |
| #define | pca_module_disable_capture_negative(module) (bit_clr(CCAPM ## module, SBIT4)) |
| Disable capture on negative edge. | |
| #define | pca_module_enable_match(module) (bit_set(CCAPM ## module, SBIT3)) |
| Enable PCA module match. | |
| #define | pca_module_disable_match(module) (bit_clr(CCAPM ## module, SBIT3)) |
| Disable PCA module match. | |
| #define | pca_module_enable_toggle(module) (bit_set(CCAPM ## module, SBIT2)) |
| Enable PCA module toggle output. | |
| #define | pca_module_disable_toggle(module) (bit_clr(CCAPM ## module, SBIT2)) |
| Disable PCA module toggle output. | |
| #define | pca_module_set_compare(module, value) |
| Set PCA module compare/capture value. | |
| #define | pca_module_get_capture(module) ((uint16_t)(bit_shift_left((uint16_t)CCAP ## module ## H, 8) | (uint16_t)CCAP ## module ## L)) |
| Get PCA module capture value. | |
pwm | |
PCA module pulse width modulator functions | |
| #define | pca_module_pwm_init(module, resolution) |
| Init PCA module in PWM mode. | |
| #define | pca_module_pwm_disable(module) (bit_clr(CCAPM ## module, SBIT1)) |
| Disable PCA module PWM output. | |
| #define | is_pca_module_pwm_enabled(module) (test_if_bit_set(CCAPM ## module, SBIT1)) |
| Get PCA module PWM output enable status. | |
| #define | pca_module_pwm_set_duty(module, duty) |
| Set PCA module PWM duty. | |
| #define | pca_module_pwm_set_duty_on_overflow(module, duty) (CCAP ## module ## H = (uint8_t)(duty)) |
| Set PCA module PWM duty without immediate update. | |
Functions and data structures related to PCA module
PCA - Programmable Counter Array. It is a 16 bit counter (CH/CL) with three independent compare/capture modules (module 0, 1 and 2). Each module can be configured to work in one of the following modes:
PCA counter clock source is selected with pca_set_clock_source() and could be SYSclk with different dividers, Timer0 overflow or external ECI pin.
PCA counter overflow (CF flag) and module compare/capture events (CCFn flags) share the single PCA interrupt vector. Use INTERRUPT_PCA from interrupt.h for the interrupt handler.
Pin mapping is selected with pca_set_pin_group(). Three pin groups are available and the group selection is stored in P_SW1 register:
| Group | ECI | CCP0 | CCP1 | CCP2 |
|---|---|---|---|---|
| 1 | P1.2 | P1.1 | P1.0 | P3.7 |
| 2 | P3.4 | P3.5 | P3.6 | P3.7 |
| 3 | P2.4 | P2.5 | P2.6 | P2.7 |
PWM output resolution is 8, 7 or 6 bit depending on the module PCA_PWMn register EBSn bits. The datasheet also describes a software technique (section 11.8) which achieves 9..16 bit PWM by combining hardware compare with software updates, it is not implemented by this module.
Note: only the STC15W401AS series (which includes STC15W408AS) peripherals are implemented here. The enhanced PWM waveform generator registers (PWMCFG/PWMCR/PWMCKS) defined in STC15Fxx.h belong to the STC15W4K32S4 series and are not usable on STC15W401AS devices.
| #define is_pca_counter_gated_off_in_idle | ( | ) | (test_if_bit_set(CMOD, SBIT7)) |
Get PCA counter idle mode status.
| #define is_pca_module_flag_set | ( | module | ) | (CCF ## module) |
Get PCA module compare/capture flag status.
Get CCON register CCFn bit for given module.
| module | uint8_t PCA module number 0, 1 or 2 |
| #define is_pca_module_interrupt_enabled | ( | module | ) | (test_if_bit_set(CCAPM ## module, SBIT0)) |
Get PCA module compare/capture interrupt enable status.
| module | uint8_t PCA module number 0, 1 or 2 |
| #define is_pca_module_pwm_enabled | ( | module | ) | (test_if_bit_set(CCAPM ## module, SBIT1)) |
Get PCA module PWM output enable status.
| module | uint8_t PCA module number 0, 1 or 2 |
| #define is_pca_overflow | ( | ) | (CF) |
Get PCA counter overflow flag status.
| #define is_pca_overflow_interrupt_enabled | ( | ) | (test_if_bit_set(CMOD, SBIT0)) |
Get PCA counter overflow interrupt enable status.
| #define is_pca_running | ( | ) | (CR) |
Get PCA counter run status.
| #define pca_clear_module_flag | ( | module | ) | (CCF ## module = 0) |
Clear PCA module compare/capture flag.
Clear CCON register CCFn bit for given module. CCFn flags should be cleared by software.
| module | uint8_t PCA module number 0, 1 or 2 |
| #define pca_clear_overflow_flag | ( | ) | (CF = 0) |
Clear PCA counter overflow flag.
Clear CCON register CF bit. CF flag should be cleared by software.
| #define pca_counter_gate_off_in_idle | ( | ) | (bit_set(CMOD, SBIT7)) |
Gate off PCA counter in MCU idle mode.
Set CMOD register CIDL bit.
| #define pca_counter_run_in_idle | ( | ) | (bit_clr(CMOD,SBIT7)) |
Let PCA counter run in MCU idle mode.
Clear CMOD register CIDL bit. By default counter is gated off in idle mode.
| #define pca_disable_overflow_interrupt | ( | ) | (bit_clr(CMOD, SBIT0)) |
Disable PCA counter overflow interrupt.
Clear CMOD register ECF bit.
| #define pca_enable_overflow_interrupt | ( | ) | (bit_set(CMOD, SBIT0)) |
Enable PCA counter overflow interrupt.
Set CMOD register ECF bit. When enabled the CF flag generates PCA interrupt. Before call this method mcu interrupt support should be enabled by calling enable_mcu_interrupts()
| #define pca_get_clock_source | ( | ) | ((pca_clock_source_t)(bit_shift_right(get_reg(CMOD, PCA_CPS_MASK), PCA_CPS0_BIT))) |
Get PCA counter clock source.
| #define pca_get_counter | ( | ) | ((uint16_t)(bit_shift_left((uint16_t)CH, 8) | (uint16_t)CL)) |
Get PCA counter value.
| #define pca_get_pin_group | ( | ) | ((pca_pin_group_t)(bit_shift_right(get_reg(P_SW1, (PCA_CCP_S0_MSK | PCA_CCP_S1_MSK)), PCA_CCP_S0_BIT))) |
Get CCP/ECI pin group.
| #define pca_module_disable_capture_negative | ( | module | ) | (bit_clr(CCAPM ## module, SBIT4)) |
Disable capture on negative edge.
Clear CCAPMn register CAPNn bit.
| module | uint8_t PCA module number 0, 1 or 2 |
| #define pca_module_disable_capture_positive | ( | module | ) | (bit_clr(CCAPM ## module, SBIT5)) |
Disable capture on positive edge.
Clear CCAPMn register CAPPn bit.
| module | uint8_t PCA module number 0, 1 or 2 |
| #define pca_module_disable_comparator | ( | module | ) | (bit_clr(CCAPM ## module, SBIT6)) |
Disable PCA module comparator.
Clear CCAPMn register ECOMn bit.
| module | uint8_t PCA module number 0, 1 or 2 |
| #define pca_module_disable_interrupt | ( | module | ) | (bit_clr(CCAPM ## module, SBIT0)) |
Disable PCA module compare/capture interrupt.
Clear CCAPMn register ECCFn bit for given module.
| module | uint8_t PCA module number 0, 1 or 2 |
| #define pca_module_disable_match | ( | module | ) | (bit_clr(CCAPM ## module, SBIT3)) |
Disable PCA module match.
Clear CCAPMn register MATn bit.
| module | uint8_t PCA module number 0, 1 or 2 |
| #define pca_module_disable_toggle | ( | module | ) | (bit_clr(CCAPM ## module, SBIT2)) |
Disable PCA module toggle output.
Clear CCAPMn register TOGn bit.
| module | uint8_t PCA module number 0, 1 or 2 |
| #define pca_module_enable_capture_negative | ( | module | ) | (bit_set(CCAPM ## module, SBIT4)) |
Enable capture on negative edge.
Set CCAPMn register CAPNn bit. Capture occurs on negative edge on CEXn pin. Capture and PWM modes could not be used simultaneously.
| module | uint8_t PCA module number 0, 1 or 2 |
| #define pca_module_enable_capture_positive | ( | module | ) | (bit_set(CCAPM ## module, SBIT5)) |
Enable capture on positive edge.
Set CCAPMn register CAPPn bit. Capture occurs on positive edge on CEXn pin. Capture and PWM modes could not be used simultaneously.
| module | uint8_t PCA module number 0, 1 or 2 |
| #define pca_module_enable_comparator | ( | module | ) | (bit_set(CCAPM ## module, SBIT6)) |
Enable PCA module comparator.
Set CCAPMn register ECOMn bit. Comparator should be enabled for compare, toggle and PWM modes.
| module | uint8_t PCA module number 0, 1 or 2 |
| #define pca_module_enable_interrupt | ( | module | ) | (bit_set(CCAPM ## module, SBIT0)) |
Enable PCA module compare/capture interrupt.
Set CCAPMn register ECCFn bit for given module. When enabled the CCFn flag generates PCA interrupt. Before call this method mcu interrupt support should be enabled by calling enable_mcu_interrupts()
| module | uint8_t PCA module number 0, 1 or 2 |
| #define pca_module_enable_match | ( | module | ) | (bit_set(CCAPM ## module, SBIT3)) |
Enable PCA module match.
Set CCAPMn register MATn bit. When enabled a match of the PCA counter with module compare/capture register sets CCFn flag.
| module | uint8_t PCA module number 0, 1 or 2 |
| #define pca_module_enable_toggle | ( | module | ) | (bit_set(CCAPM ## module, SBIT2)) |
Enable PCA module toggle output.
Set CCAPMn register TOGn bit. When enabled a match of the PCA counter with module compare/capture register toggles CEXn pin.
| module | uint8_t PCA module number 0, 1 or 2 |
| #define pca_module_get_capture | ( | module | ) | ((uint16_t)(bit_shift_left((uint16_t)CCAP ## module ## H, 8) | (uint16_t)CCAP ## module ## L)) |
Get PCA module capture value.
Read 16 bit value from module CCAPnH/CCAPnL registers. In capture mode this value is the PCA counter value captured on CEXn pin edge.
| module | uint8_t PCA module number 0, 1 or 2 |
| #define pca_module_pwm_disable | ( | module | ) | (bit_clr(CCAPM ## module, SBIT1)) |
Disable PCA module PWM output.
Clear CCAPMn register PWMn bit.
| module | uint8_t PCA module number 0, 1 or 2 |
| #define pca_module_pwm_init | ( | module, | |
| resolution | |||
| ) |
Init PCA module in PWM mode.
Set PWM output resolution with module PCA_PWMn register EBSn bits and enable PWM output by setting CCAPMn register PWMn bit.
PWM resolution could be 8, 7 or 6 bit. Note that PWMn and CAPPn/CAPNn bits should not be set simultaneously.
Before PWM output is started the module pin should be configured as push-pull output and the CEXn pin function should be selected with pca_set_pin_group().
| module | uint8_t PCA module number 0, 1 or 2 |
| resolution | pca_pwm_resolution_t PWM resolution |
| #define pca_module_pwm_set_duty | ( | module, | |
| duty | |||
| ) |
Set PCA module PWM duty.
Load duty value into module CCAPnH/CCAPnL registers. CCAPnH is a reload register and it is loaded into CCAPnL on PCA counter overflow, so new duty value is applied without glitch on PWM output.
Duty value range depends on selected PWM resolution: 0..255 for 8 bit PWM, 0..127 for 7 bit PWM and 0..63 for 6 bit PWM.
| module | uint8_t PCA module number 0, 1 or 2 |
| duty | uint8_t PWM duty value |
| #define pca_module_pwm_set_duty_on_overflow | ( | module, | |
| duty | |||
| ) | (CCAP ## module ## H = (uint8_t)(duty)) |
Set PCA module PWM duty without immediate update.
Load duty value into module CCAPnH reload register only. New duty value is applied on next PCA counter overflow. Use this method to change duty synchronously with PWM period.
| module | uint8_t PCA module number 0, 1 or 2 |
| duty | uint8_t PWM duty value |
| #define pca_module_set_compare | ( | module, | |
| value | |||
| ) |
Set PCA module compare/capture value.
Load 16 bit value into module CCAPnH/CCAPnL registers. In compare mode this value is compared with the PCA counter.
| module | uint8_t PCA module number 0, 1 or 2 |
| value | uint16_t compare value |
| #define pca_set_clock_source | ( | src | ) |
Set PCA counter clock source.
Select PCA counter pulse source. Clock source is selected with CMOD register CPS2, CPS1 and CPS0 bits. Other CMOD bits are not changed.
| src | pca_clock_source_t clock source |
| #define pca_set_counter | ( | value | ) |
Set PCA counter value.
Load 16 bit value into PCA counter CH/CL registers.
| value | uint16_t counter value |
| #define pca_set_pin_group | ( | group | ) |
Set CCP/ECI pin group.
Select which pins are used as ECI, CCP0, CCP1 and CCP2. Pins are selected with P_SW1 register CCP_S1 and CCP_S0 bits.
By default (after MCU power on) pin group 1 is used.
| group | pca_pin_group_t pin group to use |
| #define pca_start | ( | ) | (CR = 1) |
Start PCA counter.
Set CCON register CR bit. Counter starts to count from the current CH/CL value.
| #define pca_stop | ( | ) | (CR = 0) |
Stop PCA counter.
Clear CCON register CR bit.
| enum pca_clock_source_t |
PCA counter clock source.
describes possible PCA counter pulse select variants. Clock source selection is done with pca_set_clock_source().
| enum pca_pin_group_t |
PCA pin group.
describes possible CCP/ECI pin mapping variants. Group selection is done with pca_set_pin_group().
| enum pca_pwm_resolution_t |
PCA module PWM output resolution.
describes possible PWM resolutions. Resolution is set with pca_module_pwm_init().
Note that the hardware supports only 8, 7 and 6 bit resolutions. The datasheet section 11.8 describes a software technique for 9..16 bit PWM which is not implemented by this module.
| Enumerator | |
|---|---|
| PCA_PWM_8BIT | Module works as 8 bit PWM |
| PCA_PWM_7BIT | Module works as 7 bit PWM |
| PCA_PWM_6BIT | Module works as 6 bit PWM |