CC2530 timer configuration note 2 (comparison output function configuration)

foreword

Recently, CC2530 is used for PWM dimming, and the output comparison function of the timer is used. The timer configuration of CC2530 is relatively simple, and it only needs to configure a few registers to achieve it.

Timer Compare Output Function Register Configuration

I use the timer comparison output function as a PWM generator. The general idea is that when the count value of the timer is less than the set value of the comparison register, the level of the timer comparison output port remains unchanged, and when the count value of the timer is When it is equal to or greater than the set value of the compare register, the level of the timer compare output port is inverted, so it is necessary to adjust the set value of the compare register within one timer cycle to complete the modification of the duty cycle of the output waveform. So as to realize PWM stepless dimming.

Taking timer 4 and the output port as P2.0 port as an example, if you want to complete the comparison output function through timer 3, you need to configure the T4CCTL0 register, the T4CC0 register, and the T4CTL register. It can be known from the data sheet that the P2.0 port is the 0th channel (second position) of the timer, so the PERCFG register and the P2SEL register need to be configured.

IO port configuration

Introduction to the PERCFG register

The PERCFG register is an 8-bit register, which is mainly used to configure the multiplexed pins of the timer and serial port.

Rank name reset value Read and write permissions configure
4 T4CFG 0 read/write Configure the multiplexed pin of timer 4
- - - - 0: Configure the multiplexed pin address of timer 4 on the P1 port (P1.0 is the channel 0 pin, P1.1 is the channel 1 pin)
- - - - 1: Configure the multiplexed pin address of timer 4 on the P2 port (P2.0 is the channel 0 pin, P2.1 is the channel 1 pin)

Introduction to the P2SEL register

The P2SEL register is an 8-bit register, which is mainly used to configure the function of the P2 port and the peripheral control priority of port 1.

Rank name reset value Read and write permissions configure
0 SELP2_0 0 read/write Configure the function of the P2.0 pin
- - - - 0: Configure P2.0 as a general IO port
- - - - 1: Configure P2.0 as a peripheral pin

Timer configuration

T4CTL register introduction

T4CTL is an 8-bit register, which is mainly used for the configuration of timer 4 clock frequency division, operation mode, counter reset, timer start, overflow interrupt enable and other functions.

Rank name reset value Read and write permissions configure
7:5 DIV[2:0] 000 read/write Timer division factor
- - - - 000: system clock divided by 1
- - - - 001: System clock divided by 2
- - - - 010: System clock divided by 4
- - - - 011: System clock divided by 8
- - - - 100: system clock divided by 16
- - - - 101: system clock divided by 32
- - - - 110: System clock divided by 64
- - - - 111: system clock divided by 128
4 START 0 read/write timer start flag
- - - - 0: Turn off the timer
- - - - 1 : Start the timer
3 OVFIM 1 read/write Timer overflow interrupt enable flag
- - - - 0: Disable timer overflow interrupt
- - - - 1: Enable timer overflow interrupt
2 CLR 0 read/write Timer count value clear bit, only 0 can be read when reading this bit
- - - - 0 : invalid
- - - - 1: Clear the timer count value
1:0 MODE[1:0] 00 read/write Timer working mode configuration
- - - - 00 : Free run, counting repeatedly from 0x00 to 0xFF
- - - - 01: Down counting mode, counting from T3CC0 to 0x00
- - - - 10: Up counting mode, counting repeatedly from 0x00 to T3CC0
- - - - 11: Up and down counting mode, counting repeatedly from 0x00 to T3CC0 to 0x00

T4CCTL0 register introduction

T4CCTL0 is an 8-bit register, which is mainly used to control the input capture and output comparison functions of timer 4 channel 0.

Rank name reset value Read and write permissions configure
7 - 0 read Reserve
6 IM 1 read/write Timer channel interrupt enable flag
- - - - 0: Disable the channel interrupt
- - - - 1: Enable this channel interrupt
5:3 CMP[2:0] 000 read/write Configure the working mode of the comparison output function of this channel
- - - - 000: Output high level during comparison (when the timer count value is less than the set value of the compare register)
- - - - 001: Output low level during comparison (when the timer count value is less than the set value of the compare register)
- - - - 010: Invert the output level during comparison (when the timer count value is less than the set value of the comparison register)
- - - - 011: Output high level when comparing positive counting, output low level at 0
- - - - 100: output low level when comparing positive count, output high level when 0
- - - - 101: Output high level in comparison (when the timer count value is less than the set value of the comparison register), and output low level at 0xFF
- - - - 110: output high level at 0x00, output low level in comparison (when the timer count value is less than the set value of the comparison register)
- - - - 111: Initialize the output pin, CMP[2:0] remains unchanged
2 MODE 0 read/write Timer channel mode selection
- - - - 0 :该通道工作在输入捕获模式
- - - - 1 :该通道工作在输出比较模式
1:0 CAP[1:0] 00 读/写 配置该通道输入捕获功能的工作模式
- - - - 00 :关闭捕获功能
- - - - 01 :开启上升沿捕获
- - - - 10 :开启下降沿捕获
- - - - 11 :开启双边沿捕获

T4CC0寄存器介绍

T4CC0寄存器是一个8位寄存器,在比较输出模式中主要作为比较设定值的来使用。

名称 复位值 读写权限 配置
7:0 VAL[7:0] 0x00 读/写 在比较输出功能中主要作为比较设定值来用

演示程序

    #include <ioCC2530.h>

    void Timer4_PWM_Init( void )
    {
        // 配置定时器3的IO位置为备用位置2
        PERCFG |= 0x10;
        // 配置P2_0为外设IO
        P2SEL |= 0x01;

        // 定时器输出比较配置
        T4CCTL0 = 0xAC;
        // 定时器比较寄存器配置
        T4CC0 = 0x00;
        // 启动定时器3 128时钟分频
        T4CTL = 0xF4;
    }

    int main( void )
    {
        Timer4_PWM_Init();
        while(1);
    }

参考资料

[1]. CC2530数据手册

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325641530&siteId=291194637
Recommended