CooCox NVIC Example - Author: CooCox
http://www.coocox.org/show_exam/NVIC/82.html
Description:
This example describes how to configure the Nested Vectored Interrupt Controller(NVIC)IRQ Channels:
* Enable the following external interrupts in NVIC: LVD, BOD, WDT.
* Configure the priority of each external interrupt.
* Set each external interrupt pending bit during a lower priority ISR(Interrupt Service Routine).
* Show the order of interrupts by LEDs.
This example runs on HT32F175x/275x Development board.
Example Code
#include "ht32f175x_275x.h"
#include "ht32f175x_275x_gpio.h"
#include "ht32f175x_275x_ckcu.h"
#include "ht32f175x_275x_misc.h"
#define DELAY_TIME 0x7FFFFF
void LEDInit(void);
void LVD_IRQHandler(void);
void BOD_IRQHandler(void);
void WDT_IRQHandler(void);
void Delay(vu32 count);
void External_Interrupt(void)
{
//
// Config led pins
//
LEDInit();
//
// Enable the Interrupts
//
NVIC_EnableIRQ(LVD_IRQn);
NVIC_EnableIRQ(BOD_IRQn);
NVIC_EnableIRQ(WDT_IRQn);
//
// Configure 2 bits for preemption priority 2 bits for subpriority
//
NVIC_SetPriorityGrouping(5);
//
// Configure the External Interrupts Priority
//
NVIC_SetPriority(LVD_IRQn, NVIC_EncodePriority(5, 2, 0));
NVIC_SetPriority(BOD_IRQn, NVIC_EncodePriority(5, 1, 0));
NVIC_SetPriority(WDT_IRQn, NVIC_EncodePriority(5, 0, 0));
//
// Generate LVD Interrupt
//
NVIC_SetPendingIRQ(LVD_IRQn);
//
// Check on the LEDs flash sequence
//
while(1);
}
void LEDInit(void)
{
//
// Enable PA,PB clock
//
CKCU_APBPerip0ClockConfig(CKCU_APBEN0_PA, ENABLE);
CKCU_APBPerip0ClockConfig(CKCU_APBEN0_PB, ENABLE);
GPIO_SetOutBits(GPIOA, GPIO_PIN_15);
GPIO_SetOutBits(GPIOB, GPIO_PIN_0);
GPIO_SetOutBits(GPIOB, GPIO_PIN_1);
//
// Set PA15,PB0,PB1 as output mode
//
GPIO_DirectionConfig(GPIOA, GPIO_PIN_15, GPIO_DIR_OUT);
GPIO_DirectionConfig(GPIOB, GPIO_PIN_0, GPIO_DIR_OUT);
GPIO_DirectionConfig(GPIOB, GPIO_PIN_1, GPIO_DIR_OUT);
}
void LVD_IRQHandler(void)
{
//
// Generate BOD Interrupt
//
NVIC_SetPendingIRQ(BOD_IRQn);
//
// Turn on LED3
//
GPIO_ClearOutBits(GPIOB, GPIO_PIN_1);
Delay(DELAY_TIME);
}
void BOD_IRQHandler(void)
{
//
// Generate WDT Interrupt
//
NVIC_SetPendingIRQ(WDT_IRQn);
//
// Turn on LED2
//
GPIO_ClearOutBits(GPIOB, GPIO_PIN_0);
Delay(DELAY_TIME);
}
void WDT_IRQHandler(void)
{
//
// Turn on LED1
//
GPIO_ClearOutBits(GPIOA, GPIO_PIN_15);
Delay(DELAY_TIME);
}
void Delay(vu32 count)
{
while(count--);
}
Somy IRQ Example
#include "lpc1114.h"
#include "typedef.h"
#define LED2 (1 << 7) //PIO2_7
#define LED3 (1 << 8) //PIO2_8
#define LED4 (1 << 5) //PIO2_5
#define BEEP (1 << 7) //GPIO0-7
#define TMR16B0_CLK (1<<7)
#define KEY1 (1 << 2) //PIO0_2
uint8 KEY1_FLAG = 0;
void delay(uint32 time)
{
uint32 i,j;
for(i=0; i<time; i++)
for(j=0; j<50000; j++);
}
sys_AHB_clk_ctrl(uint32 xxx_ckl)
{
AHBCLKCTRL |= xxx_ckl;
}
sys_ahb_clk_enable(uint32 xxx_ckl)
{
AHBCLKCTRL |= xxx_ckl;
}
sys_ahb_clk_disable(uint32 xxx_ckl)
{
AHBCLKCTRL &= ~xxx_ckl;
}
//
void pio2_data_clr(uint32 num)
{
GPIO2DATA_ALL &= ~num; // 亮
}
void pio0_data_clr(uint32 num)
{
GPIO0DATA_ALL &= ~num; // 亮
}
void pio0_dir_set(uint32 num)
{
GPIO0DIR |= num;
}
void pio2_dir_set(uint32 num)
{
GPIO2DIR |= num;
}
void PIO02_irq_init(uint32 num)
{
GPIO0IS &= ~(num); //选择PIO0.2为边沿触发
GPIO0IEV &=~(num);//选择PIO0.2为下降沿触发
GPIO0IM |= (num); //设置PIO0.2中断不被屏蔽
}
void pio2_data_set(uint32 num)
{
GPIO2DATA_ALL |= num; // 灭
}
void time0_init(void)
{
TMR16B0PR = 999; // 设置定时器分频为分频,得Hz
TMR16B0MCR = 0x03; // 匹配通道匹配中断并复位T0TC
TMR16B0MR0 = 12000; // 比较值(1S定时值)
TMR16B0TCR = 0x3; // 启动并复位T0TC 0011
TMR16B0TCR = 0x01; // 0001
}
static __inline void NVIC_EnableIRQ(IRQn_Type IRQn)
{
NVIC_ISER = (1 << ((uint32)(IRQn) & 0x1F));
}
void PIOINT0_IRQHandler(void)
{
uint8 temp =0;
if((GPIO0MIS&0x004)==0x004) // 检测是不是PIO0.2引脚产生的中断
{
delay(4);
if( (GPIO0DATA_ALL & KEY1) != KEY1 ) //如果KEY1按下
{
while((GPIO0DATA_ALL & KEY1) == 0);
if( (GPIO2DATA_ALL & LED2) == 0 ) //如果KEY1按下
{
pio2_data_set( LED2 | LED3 | LED4 );
}else{
pio2_data_clr( LED2 | LED3 | LED4 );
}
}
}
GPIO0IC = 0x04; // 清除PIO0.2上的中断
temp = temp;
}
int main(void)
{
pio2_dir_set( LED2 | LED3 | LED4 );
pio2_data_set( LED2 | LED3 | LED4 );
PIO02_irq_init(KEY1);
NVIC_EnableIRQ(EINT0_IRQn);
while(1);
}
No comments:
Post a Comment