/*Cortex M0学习断断续续,抽空玩下GPIO,利用下助学板上的LED_1~4及KEY1~2,单片机菜鸟,望各位不吝赐教!*/
/**************************************************
** 文件名称:NUC120_HOT_GPIO.c
** 文件说明:NUC120助学板练习程序
** 创建日期:2011-03-27
** 修改日期:
** 备 注:GPIO输入输出及中断程序
LED_1与LED_2交替闪烁
KEY_1下降沿中断 LED_3取反
KEY_2按下即为低电平 LED_4亮
弹起即为高电平 LED_4灭
**************************************************/
#include <stdio.h>
#include "NUC1xx.h"
#include "Driver\DrvGPIO.h"
#include "Driver\DrvSYS.h"
#include "Driver\DrvUART.h"
/***************
** 函数声明 **
***************/
void Init_System (void);
void Init_Uart (void);
void Init_GPIO (void);
void delay_loop(void);
void LED_FLASH (void);
void INPUT_CHARGE (void);
/*****************************
** Name: EINT0Callback
** Function: EINT0中断服务函数
** Input: None
** OutPut: None
** Data: 2011-03-27
** Note:
****************************/
void EINT0Callback(void)
{
printf("EINT0 Interrupt!\n");
/* Toggle LED (GPA4) */
//outpw((uint32_t)&GPIOA->DOUT, inpw((uint32_t)&GPIOA->IN) ^ (1 << 4)); //与下面if else语句相同功能
if (DrvGPIO_GetBit(E_GPA,4))
DrvGPIO_ClrBit(E_GPA,4);
else
DrvGPIO_SetBit(E_GPA,4);
}
/*****************************
** Name: delay_loop
** Function: 软件延时函数
** Input: None
** OutPut: None
** Data: 2011-03-27
** Note:
****************************/
void delay_loop(void)
{
uint32_t j;
for(j=0;j<60000;j++)
INPUT_CHARGE();
}
/*****************************
** Name: Init_System
** Function: 系统初始化函数
** Input: None
** OutPut: None
** Data: 2011-03-17
** Note:
****************************/
void Init_System(void)
{
/* Unlock the locked registers before access */
UNLOCKREG(); //寄存器锁定键地址寄存器(RegLockAddr) :有些系统控制寄存器需要被保护起来,以防止误操作而影响芯片运行,
//这些寄存器在上电复位到用户解锁定之前是锁定的。用户可以连续依次写入“59h”, “16h” “88h”到0x5000_0100解锁定.
/* Enable the 12MHz oscillator oscillation */
DrvSYS_SetOscCtrl(E_SYS_XTL12M, 1); //SYSCLK->WRCON.XTL12M_EN = 1;
/* Waiting for 12M Xtal stable */
//while (DrvSYS_GetChipClockSourceStatus(E_SYS_XTL12M) != 1); //SYSCLK->CLKSTATUS.XTL12M_STB
/*eClkSrc - [in] E_SYS_XTL12M / E_SYS_XTL32K / E_SYS_OSC22M / E_SYS_OSC10K / E_SYS_PLL */
// Note: Only some of NuMicro NUC100 Series support this function.
DrvSYS_Delay(5000);
LOCKREG();
//向“0x5000_0100”写入任何值,就可以重锁保护寄存器
}
/*****************************
** Name: Init_Uart
** Function: UART初始化函数
** Input: None
** OutPut: None
** Data: 2011-03-17
** Note:
****************************/
void Init_Uart(void)
{
STR_UART_T param;
/*
声明 UART设置的结构体 位于DRVUART.H
结构体如下
typedef struct DRVUART_STRUCT
{
uint32_t u32BaudRate;
E_DATABITS_SETTINS u8cDataBits;
E_STOPBITS_SETTINS u8cStopBits;
E_PARITY_SETTINS u8cParity;
E_FIFO_SETTINGS u8cRxTriggerLevel;
uint8_t u8TimeOut ;
}STR_UART_T;
*/
DrvSYS_SelectIPClockSource(E_SYS_UART_CLKSRC,0); //使能UART时钟
//SYSCLK->CLKSEL1.UART_S = 0; //UART时钟源选择. 00 =外部12MHz 晶振 01 = PLL 1x =内部 22MHz 振荡器
DrvGPIO_InitFunction(E_FUNC_UART0); //GPB_MFP0-1-2-3置位 GPIO使能UART功能
//outpw(&SYS->GPBMFP, inpw(&SYS->GPBMFP) | (0xF<<0));
param.u32BaudRate = 115200; // 波特率
param.u8cDataBits = DRVUART_DATABITS_8; // 数据位
param.u8cStopBits = DRVUART_STOPBITS_1; // 停止位
param.u8cParity = DRVUART_PARITY_NONE; // 校验位
param.u8cRxTriggerLevel = DRVUART_FIFO_1BYTES; // FIFO存储深度 1 字节
param.u8TimeOut = 0; // FIFO超时设定
/* Set UART Configuration */
if(DrvUART_Open(UART_PORT0,¶m) != E_SUCCESS) // 串口开启、结构体整体赋值
printf("UART0 open failed\n");
/* u32Port -[in] UART Channel: UART_PORT0 / UART_PORT1 /UART_PORT2 */
/* sParam -[in] the struct parameter to configure UART */
}
/*****************************
** Name: Init_GPIO
** Function: GPIO初始化函数
** Input: None
** OutPut: None
** Data: 2011-03-27
** Note:
****************************/
void Init_GPIO (void)
{
/*set GPI0 output*/
DrvGPIO_Open(E_GPA,2, E_IO_OUTPUT); // 设置GPA2端口为输出模式 连接LED_1
DrvGPIO_Open(E_GPA,3, E_IO_OUTPUT); // 设置GPA3端口为输出模式 连接LED_2
DrvGPIO_Open(E_GPA,4, E_IO_OUTPUT); // 设置GPA4端口为输出模式 连接LED_3
DrvGPIO_Open(E_GPA,5, E_IO_OUTPUT); // 设置GPA5端口为输出模式 连接LED_4
DrvGPIO_Open(E_GPB,14,E_IO_INPUT); // 设置GPB14端口为输入模式 连接按KEY1
DrvGPIO_Open(E_GPB,15,E_IO_INPUT); // 设置GPB15端口为输入模式 连接按KEY2
/* port - [in] E_DRVGPIO_PORT, specify GPIO port. It could be E_GPA, E_GPB, E_GPC, E_GPD and E_GPE. */
/* i32Bit - [in] Specify pin of the GPIO port. It could be 0~15. */
/* mode - [in] E_DRVGPIO_IO, set the specified GPIO pin to be E_IO_INPUT, E_IO_OUTPUT,E_IO_OPENDRAIN or E_IO_QUASI mode.*/
/* Configure external interrupt */
DrvGPIO_InitFunction(E_FUNC_EXTINT0);
/* Parameter: */
/* function - [in] */
/* E_DRVGPIO_FUNC, specified the relative GPIO pins as special function pins. */
/* It could be : */
/* E_FUNC_GPIO */
/* E_FUNC_CLKO */
/* E_FUNC_I2C0 / E_FUNC_I2C1 */
/* E_FUNC_I2S */
/* E_FUNC_CAN0 */
/* E_FUNC_ACMP0 / E_FUNC_ACMP1 */
/* E_FUNC_SPI0 /E_ FUNC_SPI1 / E_FUNC_SPI2 / E_FUNC_SPI3 / */
/* E_FUNC_ADC0 / E_FUNC_ADC1 / E_FUNC_ADC2 / E_FUNC_ADC3 / */
/* E_FUNC_ADC4 / E_FUNC_ADC5 / E_FUNC_ADC6 / E_FUNC_ADC7 */
/* E_FUNC_EXTINT0 / E_FUNC_EXTINT1 */
/* E_FUNC_TMR0 / E_FUNC_TMR1 / E_FUNC_TMR2 / E_FUNC_TMR3 */
/* E_FUNC_UART0 / E_FUNC_UART1 / E_FUNC_UART2 */
/* E_FUNC_PWM01 / E_FUNC_PWM23 / E_FUNC_PWM45 / E_FUNC_PWM67 */
/* E_FUNC_EBI_8B / E_FUNC_EBI_16B */
/* E_FUNC_SPI0_QFN36PIN */
//DrvGPIO_InitFunction(E_FUNC_EXTINT1);
DrvGPIO_EnableEINT0(E_IO_FALLING, E_MODE_EDGE, EINT0Callback);
//若设置为上升沿中断即E_IO_RISING,则在每次按键弹起时响应中断
//若设置为下降沿中断即E_IO_FALLING,则在每次按键按下时相应中断
/* Parameter: */
/* TriggerType - [in] */
/* E_DRVGPIO_INT_TYPE, specify the interrupt trigger type. */
/* It could be E_IO_RISING, E_IO_FALLING or E_IO_BOTH_EDGE and */
/* it's meaning the interrupt function enable by rising egde/high level, */
/* falling edge/low level or both riging edge and falling egde. */
/* If the interrupt mode is E_MODE_LEVEL and interrupt type is */
/* E_BOTH_EDGEthen calling this API is ignored. */
/* Mode - [in] */
/* E_DRVGPIO_INT_MODE, specify the interrupt mode. */
/* It could be E_MODE_EDGE or E_MODE_LEVEL to control the interrupt is by */
/* edge trigger or by level trigger. */
/* If the interrupt mode is E_MODE_LEVEL and interrupt type is */
/* E_BOTH_EDGEthen calling this API is ignored. */
/* pfEINT0Callback - [in] */
/* It's the function pointer of the external INT0 callback function. */
//DrvGPIO_EnableEINT1(E_IO_BOTH_EDGE, E_MODE_EDGE, EINT1Callback);
}
/*****************************
** Name: LED_FLASH
** Function: LED闪烁函数
** Input: None
** OutPut: None
** Data: 2011-03-27
** Note:
****************************/
void LED_FLASH (void)
{
DrvGPIO_ClrBit(E_GPA,2);
/* port - [in] E_DRVGPIO_PORT, specify GPIO port. It could be E_GPA, E_GPB, E_GPC, E_GPD and E_GPE. */
/* i32Bit - [in] Specify pin of the GPIO port. It could be 0~15. */
delay_loop();
DrvGPIO_ClrBit(E_GPA,3);
delay_loop();
DrvGPIO_SetBit(E_GPA,2);
DrvGPIO_SetBit(E_GPA,3);
delay_loop();
DrvGPIO_ClrBit(E_GPA,3);
delay_loop();
DrvGPIO_ClrBit(E_GPA,2);
delay_loop();
DrvGPIO_SetBit(E_GPA,2);
DrvGPIO_SetBit(E_GPA,3);
delay_loop();
}
/*****************************
** Name: INPUT_CHARGE
** Function: GPIO输入判断函数
** Input: None
** OutPut: None
** Data: 2011-03-27
** Note: 查询法
****************************/
void INPUT_CHARGE (void)
{
if(DrvGPIO_GetBit(E_GPB, 15)==0)
/*Description :Get the pin value from the specified input GPIO pin. */
/*port [in] :E_DRVGPIO_PORT, specify GPIO port. It could be E_GPA, E_GPB, E_GPC, E_GPD and E_GPE. */
/*i32Bit [in] : Specify pin of the GPIO port. It could be 0~15. */
DrvGPIO_ClrBit(E_GPA,5);
else
DrvGPIO_SetBit(E_GPA,5);
}
int main (void)
{
uint8_t test = 250;
Init_System();
Init_Uart();
Init_GPIO();
printf("\n");
printf("/*==========================\n");
printf("======菜农 %d 助学计划======\n",test);
printf("========NUC120助学板========\n");
printf("==程序参考Cube教程及BSP例程=\n");
printf("=======2010年03月27日=======\n");
printf("==========GPIO实验==========\n");
printf("==========================*/\n");
while(1)
{
LED_FLASH();
}
}