This article is a mirror article of machine translation, please click here to jump to the original article.

View: 7368|Reply: 0

Zigbee Journey (3): Several important CC2430 basic experiments - external interruption

[Copy link]
Posted on 10/30/2014 11:16:46 PM | | | |
1. Connecting the upper and lower levels

In the previous lecture, we familiarized ourselves with the basic process of IAR developing the CC2430 program through a simple LED flashing experiment. The knife has been sharpened anyway (although my whetstone is not good), and now I will start slaughtering insects :). Next, let's learn a few basic experiments of CC2430. Each small experiment is divided into three parts: "Experiment Introduction", "Program Flow Chart", and "Experimental Source Code and Analysis".

This article explains external interruptions.

2. External interruption(1) Introduction to the experiment

Interrupts are an internal mechanism for microcontrollers to process internal or external events in real time. When an internal or external event occurs, the interrupt system of the microcontroller will force the CPU to pause the executing program and instead process the interrupt event.

Interrupts are divided into external and internal interrupts, and CC2430 contains a total of 18 interrupt sources (for specific interrupt descriptions and definitions of interrupt vectors, please refer to "CC2430 Chinese Manual》)。

Now let's take a look at the circuit diagram of this development board:

The S1 button has been connected to P0.1 on the development board, and the effect of this experiment is to trigger the interrupt of P0.1 through the button S1, and then control the on/off of LED1 in the interrupt service subprogram.

(2) Experimental principle and flow chart

The experimental flow chart is as follows:


(3) Experimental source code//头文件
#include

//延时子函数
#defineled1 P1_0
#defineled2 P1_1
#defineled3 P1_2
#defineled4 P1_3

voidDelay(unsignedn)   
{
  unsignedtt;
  for(tt =0; tt<n; tt++);
  for(tt =0; tt<n; tt++);
  for(tt =0; tt<n; tt++);
  for(tt =0; tt<n; tt++);
  for(tt =0; tt<n; tt++);
}

//32M晶振初始化
voidxtal_init(void)
{
  SLEEP &= ~0x04;            //都上电
  while(! (SLEEP &0x40));     //晶体振荡器开启且稳定
  CLKCON &= ~0x47;           //Choose a 32MHz crystal oscillator
  SLEEP |=0x04;
}

//LED灯初始化
voidled_init(void)
{
  P1SEL  =0x00;         P1 is the normal I/O port
  P1DIR |=0x0F;         P1.0 P1.1 P1.2 P1.3 output
  
  led1 = 0;
  led2 = 0;
  led3 = 0;
  led4 = 0;
}

//io及外部中断初始化
voidio_init(void)
{
    P0INP &= ~0X02;   //P0.1有上拉、下拉

    EA =1;           //总中断使能
   
    P0IE =1;   //P0中断使能
   
    PICTL |=  0X09;   //P0.1口中断使能,下降沿触发
   
    P0IFG &= ~0x02;   //P0.1中断标志清0
};

//主函数
voidmain(void)   
{
  xtal_init();
  led_init();  
  io_init();

  while(1);   //等待中断
}

//中断服务子程序
#pragma vector = P0INT_VECTOR
__interrupt voidP0_ISR(void)
{
  EA =0;                        The gate is interrupted  

  Delay(10000);  
  Delay(10000);
  Delay(10000);
  Delay(10000);
  Delay(10000);

  if((P0IFG &0x02) >0)         //按键中断
  {
    P0IFG &= ~0x02;               //P0.1中断标志清0
    led1 = !led1;
  }
  P0IF =0;                       //P0中断标志清0

  EA =1;                        //开中断
}

First, initialize the system clock: select a 32MHz crystal oscillator.

Then initialize the LEDs: set P1 as the general I/O port, set the P1.0 ~ P1.3 direction as the output, and then turn off the 4 LED lights.

Next, configure the relevant SFR registers for external interrupts to enable interrupts at all levels, involving 3 SFRs:EAIEN1PICTL(For details of each SFR, please refer to theCC2430 Chinese Manual》):

EA- Total interrupt enable;

    IEN1.5- P0 interrupt enable;

    PICTL.3—— P0.1 port interrupt enable;

    PICTL.0—— Set the P0.1 port input drop edge to cause interrupt triggering.

Then use while(1) in the main function and wait for the interrupt.

CC2430 Tips
(1) Summary of bit assignment syntax
Many times, we need to assign a bit (0 or 1) to a single byte of SFR to precisely control the hardware device.
There is SFRSupport bit addressingFor example, TCON, P0, etc., at this time, the assignment of counterpoints is very simple, just query the bit definition in the SFR Bit Access section of the ioCC2430.h header file:
P0_0 = 0; // assign 0 to the first digit of P0
P0_0 = 1; // assign a value of 1 to the first digit of P0  

But some SFRs are not thereBit addressing is not supported, as in this experimentPICTL, at this time, you want to assign a value to one of them, the syntax is as follows:
PICTL &= ~0x01;   Assign a value of 0 to the first digit
PICTL |= 0x01;     Assign a value of 1 to the first digit

You can remember&= ~|=These two commonly used bit assignment syntax.
(2) Summary of interruption enablement
When an interrupt is involved in the program, it must be enabled before the interrupt is triggered.
The hierarchy of the C51 interrupt enable system is very obvious:
Interrupt Boss: EAis the boss, responsibleGeneralInterrupt Enable:
EA = 1;

  Each interrupted squad leader: Next is for each oneFunctional parts(such as P0, timer 1, etc.), such SFRs are generally bit-addressable, and the name generally contains IE (Interrupt Enable):
P0IE = 1;

  Each team member was interrupted: Squad But since each feature also contains multiple interrupts within it, the last level is for thisEvery interruptionThis type of SFR is generally bitless and unaddressable, and usually contains IE (Interrupt Enable) or IM (Interrupt Mask) in the name:
PICTL |=0x01;

No need to rote interrupt SFR, just understand its hierarchy and then take the time to look up the manual or header file.
(3) Interrupt the writing of programs
The use of interrupts in a program generally includes two parts: the writing of the interrupt service subprogram and the opening of the interrupt enablement. The interrupt function has been introduced above, and the following is a brief introduction to the writing of the interrupt service subprogram:
Specify firstBreak vector, which can be queried in the Interrupt Vectors section of the ioCC2430.h header file, with the following syntax:
#pragma vector = interrupt vector

Then write the interrupt handler immediately afterwards, which is structured as follows:
__interrupt void function name (void)
  {
    //开中断
    //中断处理
    //中断标志清0
    //关中断
  }



3. Conclusion

This article introduces the implementation method of simple external interrupts based on CC2430, and after the basis of interrupts, we will introduce another very important module - timers. CC2430 has a total of 4 timers, which can be divided into three categories: timer 1, timer 2, timer 3/4 (3 and 4 are basically the same usage).


</n; tt++);
</n; tt++);
</n; tt++);
</n; tt++);
</n; tt++);




Previous:Zigbee Journey (2): The first CC2430 program - LED light flashing experiment
Next:Zigbee Journey (4): Several important CC2430 basic experiments - timer interruption
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com