[ZigBee] 13, ZigBee Foundation Stage Review and deepen understanding-with timer 1 to generate PWM to control the LED brightness (seven-color lamp)

Source: Internet
Author: User
Tags 0xc0

Introduction: PWM for a lot of software engineers may be familiar and unfamiliar with PWM to adjust the LED brightness For example, its essence is in every period of cutting corners, the overall performance of the LED under-voltage brightness of different effects. As you can see the seven color lantern its principle is similar, just with 3 PWM control red, green, blue three colors of the lamp output brightness, and then combined with the principle of mixing color to show the colorful effect ~

Written in front: the first more than 10 introduced the basic usage of some of CC2530 's peripherals, the next few take a few examples to review and deepen the previous knowledge points, the above introduction is universal, the following high-energy warning!

First example: Using Timer 1 to generate PWM to control led brightness

We in the [ZigBee] 5, ZigBee basic Experiment-graphics and code detailed timer 1 (16-bit timer) (long text), said timer 1 is a typical timing/counting function of the independent 16-bit timer, support input capture, output comparison and PWM functions. The project is to use the timing counter 1 to generate 1 milliseconds pwm,20% duty ratio, with PWM to adjust the brightness of the LED, if the brightness of the LED can be adjusted PWM frequency and duty ratio to control the brightness of the LED light.

1 /****************************************************************************2 * File name: main.c3 * Description: cc2530 Timer counter 1 produces 1 milliseconds pwm,20% duty-free, led brightness is darker4 * Adjustable PWM frequency and duty ratio to control the brightness of LED lights5 ****************************************************************************/6#include <ioCC2530.h>7 8typedef unsignedCharUchar;9typedef unsignedint  UINT;Ten  One #defineLED1 P1_0//P1.0 Port Control LED1 A  -  - /**************************************************************************** the * Name: initled () - * Function: Set the corresponding IO port of the LED lamp - * Entry parameters: None - * Export parameters: None + ****************************************************************************/ - voidInitled (void) + { AP1dir |=0x01;//P1.0 defined as output atLED1 =1;//make the LED1 lamp power off by default - } -  - /**************************************************************************** - * Name: InitT1 () - * Function: Timer initialization, TICKSPD is 2 MHz when the system is not configured by default, that is, 16MHz in * Entry parameters: None - * Export parameters: None to ****************************************************************************/ + voidInitT1 () - { theClkconcmd &= ~0x40;//set the system clock source to 32MHZ Crystal oscillator *      while(Clkconsta &0x40);//wait for crystal stability to 32M $Clkconcmd &= ~0x07;//set the system master clock frequency to 32MHZPanax NotoginsengClkconcmd |=0x38;//clock speed + MHz timer marker output setting [5:3]250khz -  thePercfg |=0x40;//Timer 1 IO position 1: Alternate position 2 +P2sel &= ~0x10;//Timer 1 priority AP2dir |=0xC0;//1th Priority: Timer 1-channel 2-3 the  +P1dir |=0xFF;//Port 1 is output -P1sel |=0x01;//timer1 Channel 2 mapping port P1_0 $      $T1CC2H =0x00;//20% Duty-free ratio is 200US -T1CC2L =0x32;//Modify the t1cc2l to adjust the brightness of the LEDs -t1cc0h =0x00;//1ms cycle clock with a frequency of 976.516HZ thet1cc0l =0xFF;  -T1CCTL2 =0x1c;//Mode selection Channel 2 comparison modeWuyiT1ctl =0x02;//250KHz 1 Divide the } -  Wu /**************************************************************************** - * Program Entry function About ****************************************************************************/ $ voidMainvoid) - { -Initled ();//Call initialization function -InitT1 ();//Timer initialization and PWM configuration A      while(1) +     { the     } -}

The core of the visible code is the 39~51 line:

     percfg |= 0x40;          Timer 1 io position   1: Alternate position 2     p2sel &= ~0x10;          Timer 1 priority     P2dir |= 0xC0;           1th Priority: Timer 1-channel 2-342     p1dir |= 0xFF;           Port 1 for Output     p1sel |= 0x01;           Timer1 Channel 2 mapping port p1_045     t1cc2h = 0x00;           20% duty-free ratio for 200us47     t1cc2l = 0x32;           Modify t1cc2l to adjust the brightness of the LED     t1cc0h = 0x00;           1ms cycle clock with a frequency of 976.516hz49     t1cc0l = 0xFF;     t1cctl2 = 0x1c;          Mode Selection Channel 2 comparison mode     t1ctl = 0x02;            250KHz 1 Divide

1.1, the first step: Call the Peripheral control register to set the peripheral mapped IO pin scheme

The 39th merriness percfg |= 0x40,percfg is the peripheral control register, such as the register used to set the location of some peripheral I/O. Here is the sixth bit t1cfg, select timer 1 io for the alternate location 2.

The IO mapping location scheme 1 and Scenario 2 described here need to be viewed in the table "peripheral I/O Pin Mapping" in section Seventh, "[ZigBee] 7, ZigBee UART analysis (only serial port)" I have used the UART example to detail how to look at this table:

Take USART0 as an example, the first mapping relationship is RT-P05\CT-P04\TX-P03\RX-P02; the other mapping relationship is TX-P15\RX-P14\RT-P13\CT-P12.

Then in the use of the process, a pattern corresponding to the two mappings will certainly appear contradictions!

Then the peripheral control register is used to set the choice of which scheme! This is set to 0x40 that is the selection of the PIN mapping scheme of timer 1 2:p07 corresponding Channel 3, p06-Channel 4, p12-Channel 0, p11-Channel 1, p10-channel 2~

1.2, the second step: Call the Port2 feature selection and PORT1 Device Priority control Register, set the timer1-PORT1 pin peripheral priority is high

In the seventh section of the [ZigBee] 7, ZigBee of the UART analysis (only serial transmission) "also introduced, the above peripheral control register only solves the same peripheral multiple IO mapping scheme conflict problem, but did not solve the different peripheral IO mapping conflict problem. P2sel Register is to deal with this problem!

3, 4, 5, 6 bits are used to set the priority of two devices on the same IO when PERCFG is assigned, so set P2sel &= ~0x10 here and set the timer to the top priority.

1.3, the third step: Call Port2 Direction selection and Port0 Device Priority control Register, set the timer1 accounted for Port0 pin peripheral priority is high

In the seventh section of the [ZigBee] 7, ZigBee of the UART analysis (only serial transmission) "also introduced, because the timer1 pin mapping accounted for the Port0 and Port1, so the above P2sel register to set the PORT1 part of the timer is the priority of the PIN, Here call P2dir to set its Port0 pin priority, the code P2dir |= 0xC0, that is, set the 7~6 bit to 11 so that the timer 1 channel 2-3 is the highest priority.

rule: not difficult to find here the usage of the timer and the seventh article described in the UART initialization is very similar, the first three lines to do port mapping, priority selection, the general peripherals will involve two ports, so need to use P0sel and P2dir set separately. The next step is to set the parameters for the specific peripherals!

1.4, Fourth step: Select the PWM channel, and enable

In 1.1, the introduction of the PIN mapping scheme of timer 1 is alternative 2 scheme: P07 corresponding channel 3, p06-Channel 4, p12-Channel 0, p11-Channel 1,p10-channel 2~

Set Channel 2, 3 priority highest in 1.3

The initialization of the PWM from 1.1~1.3 to timer1 also sets the priority of the channel, if you want to enable the channel to further set:

     p1dir |= 0xFF;           Port 1 for Output     p1sel |= 0x01;           Timer1 Channel 2 mapping Port P1_0

Where line 43rd sets port 1 to full output, the 44th line is critical to set the IO pin to the normal IO pin or to the IO peripheral pin, where the P1_0 is set to the peripheral pin. According to the previous setting, we know that the P10 is set to the Channel 2 output of timer 1.

1.5, timer 1 Channel 2 output comparison mode Select & Set period and duty cycle

About the output comparison mode in the [ZigBee] 5, ZigBee basic Experiment-graphics and code detailed timer 1 (16-bit timer) (long text) in the 9th paragraph is described in more detail: The PWM output can be generated by selecting the timer positive count/reverse count mode. Select the channel output comparison mode 4 or 5 (defined by the T1CCTLN.CMP bit, where n is 1 or 2) according to the desired polarity of the PWM signal. The period of the PWM signal is determined by the T1CC0, and the duty cycle of the channel output is determined by the T1CCN, where n is the PWM Channel 1 or 2. Some types of motor-driven applications require a center-aligned PWM mode, which typically produces less noise than an edge-aligned PWM mode because the I/O pin transmission is not centered on the same clock edge.

     t1cc2h = 0x00;           20% duty-free ratio for 200us47     t1cc2l = 0x32;           Modify t1cc2l to adjust the brightness of the LED     t1cc0h = 0x00;           1ms cycle clock with a frequency of 976.516hz49          t1cctl2 = 0x1c;          Mode Selection Channel 2 comparison mode     t1ctl = 0x02;            250KHz 1 divide

Where line 50th is used to set the Timer 1 Channel 2 capture/Compare control register for 0x1c,[5:3]=011 the output comparison mode 4 is selected, and the PWM signal period is determined by T1CC0, The duty ratio is determined by the T1CCN (the positive count to the Count mode generates PWM than the edge mode generated PWM is more suitable here, if you do not understand this sentence please see the 5th article)! Therefore, the 46~49 line is the period and duty cycle of setting up the PWM ~

Note: The 0x0032/0x00ff here is not equal to 20%, why does the note say that the duty ratio is 20%? We use a simple example to explain the particularity of this problem (draw the value of the timer counter line below): A t1cc0=4,t1cc2=2 on the left, obviously the duty cycle is not 2/4, nor is it 3/9, because the time in the last green box belongs to the next cycle, A complete period should be 0~4~1, so the duty cycle is 3/8, and the duty ratio on the right is 3/6. Here T1CC2 is set to 0x0032, then the lower segment has: 0~0x0032,0x0032~1, Total (0x0032-0+1) + (0x0032-1+1) =0x0032*2+1=0x0065=101; Total period is 0x00ff*2=0x01fe= 510, duty ratio of 0X65/0X01FE is approximately equal to 5

The last line T1ctl = 0x02, used to set the free-running mode (about the pattern is also introduced in the seventh chapter), using 1-way, 250khz~

Summary: This will be able to use PWM output channel 2 directly associated to the P10 to control the brightness of the LED, simple and effective ~

ZigBee Series Articles:

[ZigBee] 1, ZigBee introduction

[ZigBee] 2, ZigBee development environment construction

[ZigBee] 3, ZigBee basic experiment--gpio Output control experiment-control LED light off

[ZigBee] 4, ZigBee basic experiment--interrupt

[ZigBee] 5, ZigBee basic Experiment--graphics and code detailed timer 1 (16-bit timer) (long text)

[ZigBee] 6, ZigBee basic Experiment--timer 3 and Timer 4 (8-bit timer)

[ZigBee] 7, ZigBee analysis of the UART (only serial transmission)

[ZigBee] 8, ZigBee of the UART analysis • Two (serial transceiver)

[ZigBee] 9, ZigBee AD Analysis--ad collection CC2530 temperature serial display

[ZigBee] 10, ZigBee sleep Timer

[ZigBee] 11, ZigBee Sleep Timer two

[ZigBee] 12, ZigBee watchdog timer--Bite the good dog when hungry

PS: If you feel good, click a praise, let more people benefit ~

@beautifulzzzz 2016-07-22 continue~
e-mail:[email protected]
Sina:http://weibo.com/beautifulzzzz?is_all=1

[ZigBee] 13, ZigBee Foundation Stage Review and deepen understanding-with timer 1 to generate PWM to control the LED brightness (seven-color lamp)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.