|
8个led 共阳接法,阴极接到M058的P1口,P3.2为INT0中断,有可接地按键。
充分利用CoIDE的导航功能及实例,只要对代码进行少量修改,就可以成功。共2个文件:
main.c ledblink.c
main.c:
////////////////////////////////////////////////
void Init(void); //初始化函数声明
void myBlinkExp( void );//led函数声明
int main(void)
{
Init(); //初始化
printf("+++++++++++++++\n\r"); //串口打印字符
printf("+hellow coocox+\n\r");
printf("+++++++++++++++\n\r");
myBlinkExp(); //流水灯,本身死循环
while(1)
{
}
}
/////////////////////////////////////////////
ledblink.c:
/////////////////////////////////////////////
//以下包含文件在CoIDE导航按照工程选项自动添加
#include "M051Series.h"
#include "DrvGPIO.h"
#include "DrvSYS.h"
#include "DrvUART.h"
/**Define the port and pin number.*/
#define GPIOPORT1 E_PORT1 //led接P1口
#define GPIOPORT3 E_PORT3 //P3口有中断按键
void EINT0Callback(void); //int0中断回调函数声明
int i32BitValue=1; //控制位,int0中断改变1,0,控制流水灯方向
/***************************************************************************//**
* @brief Blink LED example 以下多数代码从led例程复制改写
* @param None
* @return None
*******************************************************************************/
E_UART_PORT UartNum=UART_PORT0;//串口变量,现在是串口0
STR_UART_T UartParam;
void Init(void) //初始化函数
{
/* Unlock the protected registers */
UNLOCKREG();
/* Enable the 12MHz oscillator oscillation */
SYSCLK->PWRCON.XTL12M_EN = 1;
/* Waiting for 12M Xtal stable */
while (DrvSYS_GetChipClockSourceStatus(E_SYS_XTL12M) != 1);
////GPIO初始化
DrvGPIO_Open(GPIOPORT3, GPIOPIN_2, E_IO_INPUT);//P3.2为INT0中断输入按键
DrvGPIO_InitFunction(E_FUNC_EXTINT0);
DrvGPIO_SetDebounceTime(6, E_DBCLKSRC_HCLK); //按键弹跳时间
DrvGPIO_EnableDebounce(E_PORT3, E_PIN2); //按键弹跳回避使能
DrvGPIO_EnableEINT(E_EINT0_PIN, E_IO_FALLING, E_MODE_EDGE, EINT0Callback);//中断使能,引脚,下降沿,边沿模式,回调函数
DrvGPIO_SetPortBits (E_PORT1, 0x12); //led口初值,随便选一个
/* UART initial function */ //串口初始化,复制的,意义自明
DrvGPIO_InitFunction(E_FUNC_UART0);
UartParam.u32BaudRate = 115200;
UartParam.u8cDataBits = DRVUART_DATABITS_8;
UartParam.u8cStopBits = DRVUART_STOPBITS_1;
UartParam.u8cParity = DRVUART_PARITY_NONE;
UartParam.u8cRxTriggerLevel = DRVUART_FIFO_1BYTES;
UartParam.u8TimeOut = 0;
/* Open the UART */ //开启串口
DrvUART_Open(UART_PORT0, &UartParam);
/* Write data to the UART */
}
////
void myBlinkExp( void )
{
int i, j;
int leds=0x01; //流水灯,注意是32bit!
while(1)
{
if (i32BitValue ) //由int0按键切换流水方向
{/*unticlockwise*/
DrvGPIO_SetPortBits (E_PORT1, ~leds); //1个led 亮起
DrvSYS_Delay(200000); //延时函数最大335000us
DrvSYS_Delay(200000);
DrvSYS_Delay(200000);
leds<<=1;
if(leds>0x80)
leds=0x01;
}
else
{
DrvSYS_Delay(200000);
DrvSYS_Delay(200000);
DrvSYS_Delay(200000);
leds>>=1;
if(leds==0)
leds=0x80;
}
}
}
//////////////////////////////////////中断回调函数
void EINT0Callback(void)
{
i32BitValue =!i32BitValue ; //改变流水
DrvGPIO_DisableEINT (E_EINT0_PIN);//中断关闭
printf("EINT0 Interrupttt!\n\r"); //串口打印字符
DrvGPIO_EnableEINT(E_EINT0_PIN, E_IO_FALLING, E_MODE_EDGE, EINT0Callback);//重启中断
}
编好后链接下载立即可以运行,不需要另外的下载程序(已经集成在CoIDE中)
以上大部分代码均有实例可循,要不是CoIDE的导航功能和示例程序,对初学者真不知如何下手。 |
阿莫论坛20周年了!感谢大家的支持与爱护!!
知道什么是神吗?其实神本来也是人,只不过神做了人做不到的事情 所以才成了神。 (头文字D, 杜汶泽)
|