基于LPC1114 DevKit口袋开发板,在Keil MDK 4.74上编写一段程序,系统上电,系统默认工作频率为48MHz,利用系统节拍定时器SysTick实现定时1s,控制LPC1114微控制器的GPIO引脚PIO1_9状态反转(LED灯BLINKY闪烁),设置按键BUTTON(PIO3_5)、WEAKUP(PIO1_4)为GPIO输入,使能滞后模式,设置中断方式判断按键状态。
按键功能:
- BUTTON(PIO3_5)按键按下,闪烁频率为1Hz,再次按下,恢复闪烁频率为0.5Hz;
- WEAKUP(PIO1_4)按键按下,闪烁频率为2Hz,再次按下,恢复闪烁频率为0.5Hz;
- 适当考虑按键防抖功能。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
| #include "LPC11xx.h" #define INT_TIME_1MS SystemCoreClock / 1000 #define BASIC_FREQUENCY 2000
void Program_Init(void); void GPIO_Clock_Init(void); void LED_Init(void); void Button_Init(void); void WEAKUP_Init(void); void SysTick_Init(uint32_t ticks); void SysTick_Handler(void); void PIOINT3_IRQHandler(void); void PIOINT1_IRQHandler(void);
void LED_Toggle(void);
static uint32_t msTicks = 0; static uint32_t COUNT_MAX = 2000; static uint32_t GPIO3_5_flag = 0; static uint32_t GPIO1_4_flag = 0;
int main(void){ Program_Init();
while (1) {
if (msTicks >= COUNT_MAX) { LED_Toggle(); msTicks = 0; } } }
void Program_Init(void){ GPIO_Clock_Init(); LED_Init(); Button_Init(); WEAKUP_Init(); SysTick_Init(INT_TIME_1MS); }
void GPIO_Clock_Init(void){ LPC_SYSCON->SYSAHBCLKCTRL |= (1UL << 6)|(1UL << 16); }
void LED_Init(void) { LPC_GPIO1->DIR |= (1UL << 9); }
void Button_Init(void){ LPC_IOCON->PIO3_5 &= ~(0x1F); LPC_IOCON->PIO3_5 |= 0x00; LPC_GPIO3->DIR &= ~(1UL << 5); LPC_GPIO3->IE |= (1UL << 5); LPC_GPIO3->IS &= ~(1UL << 5); LPC_GPIO3->IBE &= ~(1UL << 5); LPC_GPIO3->IEV &= ~(1UL << 5); LPC_IOCON->PIO3_5 |= (1UL << 5); LPC_GPIO3->IC |= (1UL << 5);
NVIC_EnableIRQ(EINT3_IRQn); }
void WEAKUP_Init(void){ LPC_IOCON->PIO1_4 &= ~(0x1F); LPC_IOCON->PIO1_4 |= 0x00; LPC_GPIO1->DIR &= ~(1UL << 4); LPC_GPIO1->IE |= (1UL << 4); LPC_GPIO1->IS &= ~(1UL << 4); LPC_GPIO1->IBE &= ~(1UL << 4); LPC_GPIO1->IEV &= ~(1UL << 4); LPC_IOCON->PIO1_4 |= (1UL << 5); LPC_GPIO1->IC |= (1UL << 4);
NVIC_EnableIRQ(EINT1_IRQn); }
void SysTick_Init(uint32_t ticks){ SysTick_Config(ticks); }
void SysTick_Handler(void) { msTicks++; }
void PIOINT3_IRQHandler(void){ if((LPC_GPIO3->MIS & (1UL << 5)) == (1UL << 5)){
if(GPIO3_5_flag){ COUNT_MAX = BASIC_FREQUENCY; GPIO3_5_flag = 0; }else{ COUNT_MAX = 1000; GPIO3_5_flag = 1; }
LPC_GPIO3->IC |= (1UL << 5); } }
void PIOINT1_IRQHandler(void){ if((LPC_GPIO1->MIS & (1UL << 4)) == (1UL << 4)){
if(GPIO1_4_flag){ COUNT_MAX = BASIC_FREQUENCY; GPIO1_4_flag = 0; }else{ COUNT_MAX = 500; GPIO1_4_flag = 1; }
LPC_GPIO1->IC |= (1UL << 4); } }
void LED_Toggle(void){ LPC_GPIO1->DATA ^= (1UL << 9); }
|