TKStudio-LPC1220_GPIO_LED工程后记
- 工程的main.c函數如下.
一.程序總體流程:
- 系統時鐘初始化:
- IOCON模塊使能
- GPIO1時鐘使能
- LED初始化
- 死循環:燈閃爍(一亮一滅)
二.硬件實物圖:
三.硬件原理圖
文件位置(E:\kpan\Desktop\LPC\硬件原理圖TinyM0_T12_ku\TinyM0 T12M原理圖與器件封裝庫\TinyM0 T12M原理圖)
(原理圖下載地址:【硬件資料】TinyM0 T12(M)原理圖與器件封裝庫 [出處 2-25 2418次])
LED引腳的定義:
在main.c中有,
#define LED_PORT??? LPC_GPIO1?????????????????????????????????? /* LED引腳端口P1??????????????? */
#define LED_PINS??? 2?????????????????????????????????????????????????? /* LED引腳P1.2????????????????? */
在lpc12xx.h中有,
#define LPC_GPIO1???????????? ((LPC_GPIO_TypeDef?? *) LPC_GPIO1_BASE )
#define LPC_GPIO2_BASE??????? (LPC_AHB_BASE? + 0x20000)
#define LPC_AHB_BASE????????? (0x50000000UL)
這樣就可以對引腳P1.2進行尋址了.
附:
在,(0x50000000UL) 中UL的意思是: unsigned long int.
四.部分程序解釋
(1)全局變量的定義.
static uint16_t GusGPIO1Port[7] = {???????????????????????????????????? /* GPIO1端口引腳類型??????????? */
??? IOCON_PIO_1_0,
??? IOCON_PIO_1_1,
??? IOCON_PIO_1_2,
??? IOCON_PIO_1_3,
??? IOCON_PIO_1_4,
??? IOCON_PIO_1_5,
??? IOCON_PIO_1_6
};
在lpc_12xxiocon.h中有,
#define IOCON_PIO_1_0???? ((uint16_t)0x0BC1 )?? /* PIO port1 type definition */
2013年3月30日 00:30:12
2013年3月30日 10:49:59
(2)
SystemInit();
系統初始化函數,在system_lpc12xx.c中
(3)
??? SYS_ConfigAHBCLK(SYS_AHBCLKCTRL_IOCON, ENABLE);???? /* IOCON模塊使能??????????????? */
??? SYS_ConfigAHBCLK(SYS_AHBCLKCTRL_GPIO1, ENABLE);????? /* GPIO1時鐘使能??????????????? */
系統_配置外圍模塊時鐘的使能,在Lpc12xx_syscrtl.c中
1: /** 2: * @brief 使能或者禁止系統和外圍時鐘. 3: Disable or enable system and peripheral clock. 4: * 5: * @param AHBClk_Type: 外圍模塊時鐘類型,AHB clock type. 6: * @param CmdState:使能/禁止 Enable or disable the clock for System or 7: * peripheral blocks. 8: * This parameter can be ENABLE or DISABLE. 9: * @retval None. 10: */ 11: void SYS_ConfigAHBCLK(uint32_t AHBClk_Type, FunctionalState CmdState) 12: { 13: if(CmdState) LPC_SYSCON->SYSAHBCLKCTRL |= AHBClk_Type; 14: else LPC_SYSCON->SYSAHBCLKCTRL &= ~(AHBClk_Type); 15: }(4)
GPIO_SetLowLevel(LED_PORT, LED_PINS, ENABLE);?????????????????????? /* 輸出低電平,點亮LED????????? */
? GPIO_SetHighLevel(LED_PORT, LED_PINS, ENABLE);????????????????????? /* 輸出高電平,熄滅LED????????? */
設置GPIO口為高電平還是低電平,(根據原理圖,低電平點亮LED)
在Lpc12xx_gpio.c中,
1: /** 2: * @brief Set Value for bits that have output direction on GPIO port. 3: 設置GPIO端口的輸出值. 4: * @param pGPIO LPC_GPIOn 哪個GPIO 5: Port Number selection, should be LPC_GPIO0, LPC_GPIO1, LPC_GPIO2 6: * @param bitPosi n 哪個引腳號 7: bit position value, should be from 0 to 31, if it is oxff, set 'value' 8: * @param value 值:0/1 9: if bitPosi is from 0 to 31, Direction value, should be: 10: * - 0: no effect 11: * - 1: high level. 12: * if bitPosi is oxff, set the all 32-bits of register 13: * @return None 14: * 15: * Note: 16: * - For all bits that has been set as input direction, this function will 17: * not effect. 18: * - For all remaining bits that are not activated in bitValue (value '0') 19: * will not be effected by this function. 20: */ 21: void GPIO_SetHighLevel(LPC_GPIO_TypeDef* pGPIO, uint8_t bitPosi, uint32_t value) 22: { 23: CHECK_PARAM(PARAM_GPIOx(pGPIO)); 24:? 25: gpio_setvalue((uint32_t *)&pGPIO->SET, bitPosi, value); 26: } 27:? 28: /** 29: * @brief Clear Value for bits that have output direction on GPIO port. 30: * @param pGPIO Port Number selection, should be LPC_GPIO0, LPC_GPIO1, LPC_GPIO2 31: * @param bitPosi bit position value, should be from 0 to 31, if it is oxff, set 'value' 32: * @param value if bitPosi is from 0 to 31, Direction value, should be: 33: * - 0: No effect. 34: * - 1: low level. 35: * if bitPosi is oxff, set the all 32-bits of register 36: * @return None 37: * 38: * Note: 39: * - For all bits that has been set as input direction, this function will 40: * not effect. 41: * - For all remaining bits that are not activated in bitValue (value '0') 42: * will not be effected by this function. 43: */ 44: void GPIO_SetLowLevel(LPC_GPIO_TypeDef* pGPIO, uint8_t bitPosi, uint32_t value) 45: { 46: CHECK_PARAM(PARAM_GPIOx(pGPIO)); 47:? 48: gpio_setvalue((uint32_t *)&pGPIO->CLR, bitPosi, value); 49: }- 在這里的CHECK_PARM用于檢查某個函數的參數檢查,在Lpc12xx_libcfg.h中有,
16: #endif /* DEBUG */
1: #define PARAM_GPIOx(n) ((((uint32_t *)n)==((uint32_t *)LPC_GPIO0)) || \ 2: (((uint32_t *)n)==((uint32_t *)LPC_GPIO1)) || \ 3: (((uint32_t *)n)==((uint32_t *)LPC_GPIO2))) 4:? 5: volatile uint32_t GPIOShadowPort[3];這里的關鍵是調用了這個函數:
1:? 2: /** 3: * @brief Set GPIO register. 4: * @param pGPIO LPC_GPIOn Port Number selection, should be LPC_GPIO0, LPC_GPIO1, LPC_GPIO2 5: * @param offset 寄存器的偏移值 Register offset value. 6: * @param bitPosi 操作比特位/直接設置值 bit position value, should be from 0 to 31, if it is oxff, set 'value' 7: * @param value 如果操作比特位,那么值為0/1 8: * if bitPosi is from 0 to 31, Direction value, should be: 9: * - 0: no mask. 10: * - 1: mask. 11: * if bitPosi is oxff, set the all 32-bits of register 12: * @return None 13: * 14: * Note: All remaining bits that are not activated in bitValue (value '0') 15: * will not be effected by this function. 16: */ 17: static void gpio_setvalue(uint32_t *p, uint8_t bitPosi, uint32_t value) 18: { 19: CHECK_PARAM(( bitPosi < 0x20 ) || ( bitPosi == 0xFF )); 20: CHECK_PARAM(( value == 0 ) || ( value == 1 )); 21: 22: if(bitPosi < 0x20){ 23: if (value) { 24: *(uint32_t *)p |= (0x1<<bitPosi); 25: } 26: else { 27: *(uint32_t *)p &= ~(0x1<<bitPosi); 28: } 29: } 30: else if(bitPosi == 0xFF){ 31: *(uint32_t *)p = value; 32: } 33: }調用關系:
GPIO_SetLowLevel(LED_PORT, LED_PINS, ENABLE);????????????????? LedOn()??????????? main.c
?? |-CHECK_PARAM(PARAM_GPIOx(pGPIO));???????????????????????????????????
?? |-gpio_setvalue((uint32_t *)&pGPIO->CLR, bitPosi, value);??
(5)設置IOCON_PIOn_m的值
LED引腳的寄存器 在 E:\kpan\Desktop\LPC\LPC1200系列微控制器產品數據手冊(英) LPC1200_ds_en page112頁.
6.4.48 PIO1_2 register
?
(6)LED初始化函數
void ledInit (void)
{
IOCON_PIO_CFG_Type PIO_mode;
IOCON_StructInit(&PIO_mode); /* 初始化端口模式 */
PIO_mode.type = GusGPIO1Port[LED_PINS];
IOCON_SetFunc(&PIO_mode); /* 設置LED引腳為GPIO功能 */
GPIO_SetDir(LED_PORT, LED_PINS, GPIO_DIR_OUTPUT); /* 設置LED引腳為輸出 */
ledOff(); /* 熄滅LED */
}
分析:
第一句,IOCON_PIO_CFG_Type PIO_mode;定義了一個IO引腳配置的結構體,描述了上面的? 6.4.48 PIO1_2 register 的值.
typedef struct {
??? uint16_t??? type;???? /*!< low 8 bits is address offset, other is func >
???????????????????????????????????????????????? This parameter can be a value of @ref PIO_type */
??? uint8_t???? pinmode;? /*!<? Pin mode >
???????????????????????????????????????????????? This parameter can be a value of @ref PIO_pin_mode */
??? uint8_t???? invert;?? /*!<? Inverted function >
???????????????????????????????????????????????? This parameter can be a value of @ref Invert_input_mode */
??? uint8_t???? ad;?????? /*!<? analog/digital mode >
???????????????????????????????????????????????? This parameter can be a value of @ref Analog_digital_mode */
??? uint16_t??? pmode;??? /*!<? Push/pull mode for I2C >
???????????????????????????????????????????????? This parameter can be a value of @ref Push_Pull_mode */
??? uint16_t??? od;?????? /*!<? Open drive >
???????????????????????????????????????????????? This parameter can be a value of @ref Open_drain_mode */
??? uint16_t??? drive;??? /*!<? Pin driver function >
???????????????????????????????????????????????? This parameter can be a value of @ref Drive_current_mode */
??? uint16_t??? sm;?????? /*!<? Sample mode >
???????????????????????????????????????????????? This parameter can be a value of @ref Sample_mode */
??? uint32_t??? cd;?????? /*!<? Clock Divider >
???????????????????????????????????????????????? This parameter can be a value of @ref clock_divider_num */
} IOCON_PIO_CFG_Type;
第二句,IOCON_StructInit(&PIO_mode);? 使用默認值來初始化了結構體.
void IOCON_StructInit ( IOCON_PIO_CFG_Type *mode)
{
?? mode->type = 0x0;
?? mode->pinmode = IOCON_PIO_MODE_PULLUP;
?? mode->invert = IOCON_PIO_INV_NOT;
?? mode->pmode = IOCON_PIO_PMODE_DISABLE;
?? mode->od = IOCON_PIO_OD_DISABLE;
?? mode->drive = IOCON_PIO_DRV_2MA_12MA;
?? mode->ad = IOCON_PIO_AD_DIGITAL;
?? mode->sm = IOCON_PIO_SMODE_BYPASS;
?? mode->cd = IOCON_PIO_CLKDIV_0;
}
第三句,PIO_mode.type = GusGPIO1Port[LED_PINS];設置結構體的type成員的值
PIO_mode.type = GusGPIO1Port[LED_PINS];
LED_PINS=2,那么根據定義的數組
static uint16_t GusGPIO1Port[7] = {???????????????????????????????????? /* GPIO1端口引腳類型??????????? */
??? IOCON_PIO_1_0,
??? IOCON_PIO_1_1,
??? IOCON_PIO_1_2,
??? IOCON_PIO_1_3,
??? IOCON_PIO_1_4,
??? IOCON_PIO_1_5,
??? IOCON_PIO_1_6
};
有PIO_mode.type =IOCON_PIO_1_2? ,
根據宏定義
#define IOCON_PIO_1_2 ((uint16_t)0x0C40 )
所以,最后有,IO引腳結構體type成員變量為
PIO_mode.type=0x0C40
第四句,IOCON_SetFunc(&PIO_mode); /* 設置LED引腳為GPIO功能 */
根據結構體指針來設置引腳的功能
?
void IOCON_SetFunc ( IOCON_PIO_CFG_Type *mode)
{
??? uint32_t offset;
??? uint32_t func;?
??? uint32_t tmp;
??? uint32_t *p = (uint32_t *)&LPC_IOCON->PIO2_28;
??? CHECK_PARAM( PARAM_IOCON_PIO_TYPE(mode->type) );
??? CHECK_PARAM( PARAM_IOCON_PIO_MODE(mode->pinmode));
??? CHECK_PARAM( PARAM_IOCON_PIO_DRV(mode->drive) );
??? CHECK_PARAM( PARAM_IOCON_PIO_AD(mode->ad) );
??? CHECK_PARAM( PARAM_IOCON_PIO_OD(mode->od));??
??? CHECK_PARAM( PARAM_IOCON_PIO_INV(mode->invert) );
??? CHECK_PARAM( PARAM_IOCON_PIO_SMODE(mode->sm));
??? CHECK_PARAM( PARAM_IOCON_PIO_CLKDIV(mode->cd));
??? offset = (mode->type >> 6);
??? func = (mode->type & 0xf);
??? if(offset == 0x24 || offset == 0x25){ //0x90, 0x94 right shift 2 bit
?????? tmp = (uint32_t)(func|(mode->pmode)|(mode->od)|(mode->invert)|(mode->sm)|(mode->cd));
??? }else{
?????? tmp = (uint32_t)(func|(mode->pinmode)|(mode->drive)|(mode->ad)|(mode->od)|(mode->invert)|(mode->sm)|(mode->cd));
??? }
??? *(uint32_t *)(p + offset) = tmp;
}
第五句,GPIO_SetDir(LED_PORT, LED_PINS, GPIO_DIR_OUTPUT);???? 設置端口方向,輸出
/**
? * @brief?????? Set Direction for GPIO port.
? * @param pGPIO??? Port Number selection, should be LPC_GPIO0, LPC_GPIO1, LPC_GPIO2
? * @param bitPosi?????? bit position value, should be from 0 to 31, if it is oxff, set 'value'
? * @param value??????? if bitPosi is from 0 to 31, Direction value, should be:
? *???????????????????????????? - 0: Input.
? *???????????????????????????? - 1: Output.
? *???????????????????????????? if bitPosi is oxff, set the all 32-bits of register
? * @return????? None
? *
? * Note: All remaining bits that are not activated in bitValue (value '0')
? * will not be effected by this function.
? */
void GPIO_SetDir(LPC_GPIO_TypeDef* pGPIO, uint8_t bitPosi, uint32_t value)
{
??? CHECK_PARAM(PARAM_GPIOx(pGPIO));
??? gpio_setvalue((uint32_t *)&pGPIO->DIR, bitPosi, value);??????
?
}
第六句,LedOff(),燈滅.
2013年3月30日 17:13:44
轉載于:https://www.cnblogs.com/xilifeng/archive/2013/03/30/2989914.html
總結
以上是生活随笔為你收集整理的TKStudio-LPC1220_GPIO_LED工程后记的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 对齐内容验证码和图片
- 下一篇: 不用加减乘除符号计算两数之和