STC15W408AS library 0.18.0
Loading...
Searching...
No Matches
Enumerations
PCA/CCP

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.
 

Detailed Description

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.

Author
Michael Golovanov

Macro Definition Documentation

◆ is_pca_counter_gated_off_in_idle

#define is_pca_counter_gated_off_in_idle ( )    (test_if_bit_set(CMOD, SBIT7))

Get PCA counter idle mode status.

Returns
bool true if counter is gated off in idle mode, otherwise false

◆ is_pca_module_flag_set

#define is_pca_module_flag_set (   module)    (CCF ## module)

Get PCA module compare/capture flag status.

Get CCON register CCFn bit for given module.

Parameters
moduleuint8_t PCA module number 0, 1 or 2
Returns
bool true if match or capture occurred, otherwise false

◆ is_pca_module_interrupt_enabled

#define is_pca_module_interrupt_enabled (   module)    (test_if_bit_set(CCAPM ## module, SBIT0))

Get PCA module compare/capture interrupt enable status.

Parameters
moduleuint8_t PCA module number 0, 1 or 2
Returns
bool true if module interrupt is enabled, otherwise false

◆ is_pca_module_pwm_enabled

#define is_pca_module_pwm_enabled (   module)    (test_if_bit_set(CCAPM ## module, SBIT1))

Get PCA module PWM output enable status.

Parameters
moduleuint8_t PCA module number 0, 1 or 2
Returns
bool true if PWM output is enabled, otherwise false

◆ is_pca_overflow

#define is_pca_overflow ( )    (CF)

Get PCA counter overflow flag status.

Returns
bool true if counter overflow occurred, otherwise false

◆ is_pca_overflow_interrupt_enabled

#define is_pca_overflow_interrupt_enabled ( )    (test_if_bit_set(CMOD, SBIT0))

Get PCA counter overflow interrupt enable status.

Returns
bool true if overflow interrupt is enabled, otherwise false

◆ is_pca_running

#define is_pca_running ( )    (CR)

Get PCA counter run status.

Returns
bool true if counter is running, otherwise false

◆ pca_clear_module_flag

#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.

Parameters
moduleuint8_t PCA module number 0, 1 or 2

◆ pca_clear_overflow_flag

#define pca_clear_overflow_flag ( )    (CF = 0)

Clear PCA counter overflow flag.

Clear CCON register CF bit. CF flag should be cleared by software.

◆ pca_counter_gate_off_in_idle

#define pca_counter_gate_off_in_idle ( )    (bit_set(CMOD, SBIT7))

Gate off PCA counter in MCU idle mode.

Set CMOD register CIDL bit.

◆ pca_counter_run_in_idle

#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.

◆ pca_disable_overflow_interrupt

#define pca_disable_overflow_interrupt ( )    (bit_clr(CMOD, SBIT0))

Disable PCA counter overflow interrupt.

Clear CMOD register ECF bit.

◆ pca_enable_overflow_interrupt

#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()

◆ pca_get_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.

Returns
pca_clock_source_t current clock source

◆ pca_get_counter

#define pca_get_counter ( )    ((uint16_t)(bit_shift_left((uint16_t)CH, 8) | (uint16_t)CL))

Get PCA counter value.

Returns
uint16_t PCA counter value

◆ pca_get_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.

Returns
pca_pin_group_t current pin group

◆ pca_module_disable_capture_negative

#define pca_module_disable_capture_negative (   module)    (bit_clr(CCAPM ## module, SBIT4))

Disable capture on negative edge.

Clear CCAPMn register CAPNn bit.

Parameters
moduleuint8_t PCA module number 0, 1 or 2

◆ pca_module_disable_capture_positive

#define pca_module_disable_capture_positive (   module)    (bit_clr(CCAPM ## module, SBIT5))

Disable capture on positive edge.

Clear CCAPMn register CAPPn bit.

Parameters
moduleuint8_t PCA module number 0, 1 or 2

◆ pca_module_disable_comparator

#define pca_module_disable_comparator (   module)    (bit_clr(CCAPM ## module, SBIT6))

Disable PCA module comparator.

Clear CCAPMn register ECOMn bit.

Parameters
moduleuint8_t PCA module number 0, 1 or 2

◆ pca_module_disable_interrupt

#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.

Parameters
moduleuint8_t PCA module number 0, 1 or 2

◆ pca_module_disable_match

#define pca_module_disable_match (   module)    (bit_clr(CCAPM ## module, SBIT3))

Disable PCA module match.

Clear CCAPMn register MATn bit.

Parameters
moduleuint8_t PCA module number 0, 1 or 2

◆ pca_module_disable_toggle

#define pca_module_disable_toggle (   module)    (bit_clr(CCAPM ## module, SBIT2))

Disable PCA module toggle output.

Clear CCAPMn register TOGn bit.

Parameters
moduleuint8_t PCA module number 0, 1 or 2

◆ pca_module_enable_capture_negative

#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.

Parameters
moduleuint8_t PCA module number 0, 1 or 2

◆ pca_module_enable_capture_positive

#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.

Parameters
moduleuint8_t PCA module number 0, 1 or 2

◆ pca_module_enable_comparator

#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.

Parameters
moduleuint8_t PCA module number 0, 1 or 2

◆ pca_module_enable_interrupt

#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()

Parameters
moduleuint8_t PCA module number 0, 1 or 2

◆ pca_module_enable_match

#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.

Parameters
moduleuint8_t PCA module number 0, 1 or 2

◆ pca_module_enable_toggle

#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.

Parameters
moduleuint8_t PCA module number 0, 1 or 2

◆ pca_module_get_capture

#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.

Parameters
moduleuint8_t PCA module number 0, 1 or 2
Returns
uint16_t capture value
Note
The capture registers have no read latch, so a capture that happens between the two byte reads can produce a value made of one old byte and one new byte. Clear the CCFn flag before reading to make this window as small as possible. If the capture rate is high enough for this to matter, read the value in the PCA interrupt handler.

◆ pca_module_pwm_disable

#define pca_module_pwm_disable (   module)    (bit_clr(CCAPM ## module, SBIT1))

Disable PCA module PWM output.

Clear CCAPMn register PWMn bit.

Parameters
moduleuint8_t PCA module number 0, 1 or 2

◆ pca_module_pwm_init

#define pca_module_pwm_init (   module,
  resolution 
)
Value:
do { \
bit_clr(PCA_PWM ## module, PCA_PWM_EBS_MASK); \
bit_set(PCA_PWM ## module, (uint8_t)((resolution) << PCA_PWM_EBS0_BIT)); \
bit_set(CCAPM ## module, SBIT1); \
} while(0)
#define PCA_PWM_EBS0_BIT
PCA_PWMn register EBS0 (PWM resolution select) bit position.
Definition pca.h:56
#define PCA_PWM_EBS_MASK
PCA_PWMn register EBS (PWM resolution select) bits mask.
Definition pca.h:58

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().

Parameters
moduleuint8_t PCA module number 0, 1 or 2
resolutionpca_pwm_resolution_t PWM resolution

◆ pca_module_pwm_set_duty

#define pca_module_pwm_set_duty (   module,
  duty 
)
Value:
do { \
CCAP ## module ## H = (uint8_t)(duty); \
CCAP ## module ## L = (uint8_t)(duty); \
} while(0)

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.

Parameters
moduleuint8_t PCA module number 0, 1 or 2
dutyuint8_t PWM duty value

◆ pca_module_pwm_set_duty_on_overflow

#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.

Parameters
moduleuint8_t PCA module number 0, 1 or 2
dutyuint8_t PWM duty value

◆ pca_module_set_compare

#define pca_module_set_compare (   module,
  value 
)
Value:
do { \
CCAP ## module ## H = (uint8_t)bit_shift_right((uint16_t)(value), 8); \
CCAP ## module ## L = (uint8_t)(value); \
} while(0)

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.

Parameters
moduleuint8_t PCA module number 0, 1 or 2
valueuint16_t compare value

◆ pca_set_clock_source

#define pca_set_clock_source (   src)
Value:
do { \
bit_clr(CMOD, PCA_CPS_MASK); \
bit_set(CMOD, (uint8_t)((src) << PCA_CPS0_BIT)); \
} while(0)
#define PCA_CPS0_BIT
CMOD register CPS0 (counter pulse select) bit position.
Definition pca.h:52
#define PCA_CPS_MASK
CMOD register CPS (counter pulse select) bits mask.
Definition pca.h:54

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.

Parameters
srcpca_clock_source_t clock source

◆ pca_set_counter

#define pca_set_counter (   value)
Value:
do { \
CH = (uint8_t)bit_shift_right((uint16_t)(value), 8); \
CL = (uint8_t)(value); \
} while(0)

Set PCA counter value.

Load 16 bit value into PCA counter CH/CL registers.

Parameters
valueuint16_t counter value

◆ pca_set_pin_group

#define pca_set_pin_group (   group)
Value:
do { \
bit_clr(P_SW1, PCA_CCP_SW_MASK); \
bit_set(P_SW1, (uint8_t)((group) << PCA_CCP_S0_BIT)); \
} while(0)
#define PCA_CCP_SW_MASK
P_SW1 register CCP pin group select bits mask.
Definition pca.h:67
#define PCA_CCP_S0_BIT
P_SW1 register CCP_S0 (CCP pin group select) bit position.
Definition pca.h:61

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.

Parameters
grouppca_pin_group_t pin group to use

◆ pca_start

#define pca_start ( )    (CR = 1)

Start PCA counter.

Set CCON register CR bit. Counter starts to count from the current CH/CL value.

◆ pca_stop

#define pca_stop ( )    (CR = 0)

Stop PCA counter.

Clear CCON register CR bit.

Enumeration Type Documentation

◆ 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().

Enumerator
PCA_CLOCK_SYS_12 

System clock, SYSclk/12

PCA_CLOCK_SYS_2 

System clock, SYSclk/2

PCA_CLOCK_TIMER0 

Timer0 overflow pulse

PCA_CLOCK_ECI 

External clock at ECI pin, max frequency SYSclk/2

PCA_CLOCK_SYS 

System clock, SYSclk

PCA_CLOCK_SYS_4 

System clock, SYSclk/4

PCA_CLOCK_SYS_6 

System clock, SYSclk/6

PCA_CLOCK_SYS_8 

System clock, SYSclk/8

◆ pca_pin_group_t

PCA pin group.

describes possible CCP/ECI pin mapping variants. Group selection is done with pca_set_pin_group().

Enumerator
PCA_PIN_GROUP_1 

CCP on [P1.2/ECI, P1.1/CCP0, P1.0/CCP1, P3.7/CCP2]

PCA_PIN_GROUP_2 

CCP on [P3.4/ECI_2, P3.5/CCP0_2, P3.6/CCP1_2, P3.7/CCP2_2]

PCA_PIN_GROUP_3 

CCP on [P2.4/ECI_3, P2.5/CCP0_3, P2.6/CCP1_3, P2.7/CCP2_3]

◆ 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