| I. Taking up where we left off In the previous article , "Zigbee Journey (I): The Beginning", we briefly introduced Zigbee and its development environment, OK, now that the tools are complete, a question arises: how to use the software and hardware to write a program that can run? This article is basically to answer the above question: to "LED light blinking" as an example of a small experiment, how to configure the IAR for the development of CC2430-based programs, how to use the IAR to write and debug the program online. Configuration of IAR IAR is a powerful embedded development platform that supports many kinds of chips, and each Project in IAR can have its own configuration, including Device type, Heap/Stack, Linker, Debugger, etc. (1) Create a new Workspace and Workstation. (1) New Workspace and Project First of all, create a new folder ledtest, open IAR, select the main menu File -> New -> Workspace to create a new workspace. Select Project -> Create New Project -> Empty Project, click OK, and save the project file to the folder ledtest, named as: ledtest.ewp (as below). (2) Configure General Options Target setting: Device: CC2430; Code Mode: Near; Data model: Large; Calling convention: XDATA stack reetrant
Data pointer setting: Number of DPTRs: 1
Stack/Heap setting: XDATA stack size: 0x1FF (3) Linker settings Linker command file: select lnk51ew_cc2430.xcl
(4) Debugger settings: Driver: Texas Instruments (this experiment for the real machine debugging, so choose TI; if other programs to use the IAR emulator, optional Simulator) Device Description file: CC2430.ddf
At this point, the IAR configuration for this experiment is basically over, the following to introduce its coding implementation. Third, the preparation of program code(1) New program file Choose File->New->File, create a new file main.c. (2) Introduce header files CC2430-based program must include a reference to ioCC2430.h, this file defines the CC2430's various types of special function registers (SFR) address mapping. #include //Introduce the header file corresponding to the CC2430 (contains the definition of each SFR) This file is built into the IAR (similar to stdio.h), the mouse to this line of code, right-click, select OPen "ioCC2430.h", you can see the full contents of this header file. (3) Define the LED pins Check the circuit diagram of the development board, as shown below:
It is known that led1~4 are controlled by pins P1_0~P4_0 respectively, so we can define led1, led2, led3, led4 as pins P1_0, P2_0, P3_0, P4_0 respectively. #define led1 P1_0 //define LED1 as P1_0 port control #define led2 P1_1 //define led2 as P1_1 port control #define led3 P1_2 //define led3 as P1_2 port control #define led4 P1_3 //define LED4 for P1_3 port control
(4) Main function Next, start writing the main function. First of all, before using the P1.0~P1.4 ports, you need to set up its working mode and input/output direction, which involves two SFRs: P1SEL, P1DIR. P1SEL = 0x00; //Set P1 as an ordinary I/O port P1DIR |= 0x0F; & nbsp; //Set P1.0 P1.1 P1.2 P1.3 as outputs Zigbee Tips CC2430 has P0_0 ~ P0_7, P1_0~P1_7, P2_0~P2_7 total 21 I/O ports. They can be used as general-purpose digital I/Os or for connecting peripheral IOs such as ADC, timer/counter or USART. There are three types of registers in the SFR of CC2430 that can be used to configure these IO ports: ①PxSEL (x is 0/1/2): P0/P1/P2 port function selection PxSEL (x is 0/1/2): P0/P1/P2 port function selection nbsp;0: general-purpose digital IO, 1: peripheral IO, default default is 0 ② PxDIR (x is 0/1/2): P0/P1/P2 port direction & nbsp;0: input, 1: output, default default is 0 ③PxINP (x is 0/1): P0/P1 port input mode 0: input, 1: output, default default 0 nbsp;0: pull-up/down, 1: tri-state, default default is 0 Need to be configured when using the IO port, if default, then take the system default. Then initialize the 4 LEDs and set them to all out: led1 = 1; led2 = 1; led3 = 1; led4 = 1. Finally, write the LED blinking effect code: led1 = 0; //led1 blinking Delay(10); led1 = 1; Delay(10); led2 = 0; //led2 blinks Delay(10); led2 = 1; Delay(10); led3 = 0; //led3 blinks Delay(10); / //led3 blinking Delay(10); led3 = 1; Delay(10); led4 = 0; //led4 blinking & nbsp; Delay(10); led4 = 1; Delay(10);
which involves a delay sub-function Delay(unsigned char n): void Delay(unsigned char n) { unsigned char i; unsigned int j; for(i = 0; i < n; i++)   ; for(j = 1; j; j++) ; }
(5) Code Overview Combine the above code as a whole as shown below: //Introduce header files #include //introduce the header file corresponding to CC2430 (including the definition of each SFR)
// Define the LED pins #define led1 P1_0 //define LED1 to be controlled by P1_0 port #define led2 P1_1 //define LED2 as P1_1 port control #define led3 P1_2 //define LED3 as P1_2 port control #define led4 P1_3 //define LED4 for P1_3 port control //delay subroutine void Delay (unsigned char n) { unsigned char i. unsigned int j. for (i = 0; i < n; i++) for (j = 1; j; j++) ; }
void main (void ) { P1SEL = 0x00; //Set P1 as normal I/O port P1DIR |= 0x0F; //Set P1.0 P1.1 P1.2 P1.3 as outputs led1 = 1; //initialize, all 4 led lights off led2 = 1; //initialize, all 4 led lights off led3 = 1; led4 = 1; //initialize, all 4 led are off led2 = 1; led3 = 1; led4 = 1; //initialize, all 4 led lights off while(1 ) //start cycle { led1 = 0; //led1 blinking Delay (10 ); //Delay (10 ); //Delay (10 ); //Delay (10 ) delay(10); led1 = 1; //led1 blinks Delay (10 ). led2 = 0; //led2 is blinking Delay (10 ). led2 = 1; //led2 blinking Delay (10 ). led3 = 0; //led3 is blinking Delay (10 ); led3 = 1; //led3 blinks led3 = 1; //led3 is blinking Delay (10 ). led4 = 0; //led4 is blinking Delay (10 ). led4 = 1; //led4 blinking Delay (10 ); //led4 blinks. } }
OK, the code for this small experiment has been written, is not very simple ah, hehe ~ Fourth, compile and debugging Select Project -> Make, compile the code, if successful, the following output will appear:
Connect the Zigbee device in the order of Zigbee development board → Debugger → PC's USB port , and then select Project -> Debug, the program will be downloaded to the development board automatically. Then select Debug -> Go to start the program, then you will see the 4 LEDs blinking in sequence! Although this is a simple can not be more simple small experiments, but when the author successfully realized it, or a small excited ~ huh! V. Conclusion This paper is based on the "LED light flashing experiment" as the basis, respectively, from the IAR configuration, program code writing, and compilation and debugging process introduces the entire implementation process. Next, it will be in the understanding of the development of the basic process of the program on the basis of the introduction of several CC2430 development of basic experiments involving timers, serial communications, AD conversion, system sleep and watchdog , etc., so stay tuned! |