本帖最后由 gcxzhz 于 2014-1-4 13:51 编辑
前两天在做课程设计,又回到了以前的实验室。该努力了,研究生自己没去考,2014自己马上就去工作了,可能是没找工作吧,有点忧伤。主要是实验室的同学都保研了,说实话,心里很失落。考研就那么重要吗,家里人都让考研,我感觉好累啊。 先工作吧,以后有机会一定要上研,不要被别人看不起。
言归正传吧 忙里偷闲 又看了看 TIVA C Launchpad gpio模块 书上有点亮led的程序。都能理解,做做课后题复习一下(课后题就是使用key点亮led)
刚开始直接调用 GPIOPinRead 函数读取按键值 发现老是不对 最后又看了看 button.c的函数 发现初始化的时候不是我想的那样简单(时钟初始化,使能外设时钟,设置io模式,进入while(1)key扫描)
看完它的程序后明白了 要解锁寄存器CR 程序中//Unlock PF0....处 才可以设置输入
运行结果是 默认全亮 即为白色 按左键红灯亮 按右键蓝灯亮 两个键都按是绿色亮
我是在源程序的基础上改的 加了
#include "inc/hw_gpio.h"
#define GPIO_LOCK_KEY 0x4C4F434B // Unlocks the GPIO_CR register
程序如下
#include <stdint.h>
#include <stdbool.h>
#include "inc/hw_types.h"
#include "inc/hw_memmap.h"
#include "inc/hw_gpio.h"
#include "driverlib/pin_map.h"
#include "driverlib/sysctl.h"
#include "driverlib/gpio.h"
#define GPIO_LOCK_KEY 0x4C4F434B // Unlocks the GPIO_CR register
int main(void)
{
uint8_t ui8button;
SysCtlClockSet(SYSCTL_SYSDIV_4|SYSCTL_USE_PLL|SYSCTL_XTAL_16MHZ|SYSCTL_OSC_MAIN); //set up timer 50mhz
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF); //enable gpio f
GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3); //setup gpiof PIN1 PIN2 PIN3 OUTPUT
// Unlock PF0 so we can change it to a GPIO input
// Once we have enabled (unlocked) the commit register then re-lock it
// to prevent further changes. PF0 is muxed with NMI thus a special case.
HWREG(GPIO_PORTF_BASE + GPIO_O_LOCK) = GPIO_LOCK_KEY; //UNLOCK CR REGISTER
HWREG(GPIO_PORTF_BASE + GPIO_O_CR) |= 0x01;
HWREG(GPIO_PORTF_BASE + GPIO_O_LOCK) = 0;
GPIODirModeSet(GPIO_PORTF_BASE, GPIO_PIN_0|GPIO_PIN_4, GPIO_DIR_MODE_IN); //SET UP GPIO PIN0 PIN5 INPUT
GPIOPadConfigSet(GPIO_PORTF_BASE, GPIO_PIN_0|GPIO_PIN_4,
GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPU); //SET UP GPIOF weak pull-up WITH 2MA
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3, 0xff);
while(1)
{
ui8button = GPIOPinRead(GPIO_PORTF_BASE, GPIO_PIN_0|GPIO_PIN_4); //READ KEY
if(ui8button !=0x11) // IF KEY WAS RRESSED
{
SysCtlDelay(200); //debounced
if(ui8button !=0x11)
{
if(ui8button ==0x01) // LEFT BUTTON WAS PRESSED
{GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3, 0x02);} //RED
else if(ui8button ==0x10) //RIGHT BUTTON WAS PRESSED
{GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3, 0x04);} //BLUD
else
{GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3, 0x08);} // RLEFT AND RIGHA WARE PRESSED GREEN
}
}
SysCtlDelay(2000000);
}
}
|