文章目录
- 1 板子资源
- 2 开发环境
- 3 例程
- 3.1 PLL FPU 延时
- 3.1.1 PLL
- 3.1.2 FPU
- 3.1.3 延时
- 3.2 GPIO中,LED,KEY
- 3.3 按键外部中断控制灯
- 3.4 串口收发
- 3.5 Flash写入与读取
- 3.6 定时器 定时中断
- 3.7 定时器 PWM输出
- 3.8 定时器 计数捕获
- 3.9 定时器 时间捕获
- 3.10 读取模拟输入
- 3.11 两个按键+定时器 实验
1 板子资源
板子用户手册
https://www.ti.com/lit/ug/spmu296/spmu296.pdf
2 开发环境
https://blog.csdn.net/x1131230123/article/details/103263355
3 例程
芯片手册
库函数包
3.1 PLL FPU 延时
外设编程教程
http://shukra.dese.iisc.ernet.in/edwiki/EmSys:Lab_Practicals_Using_TivaC_LaunchPad_Board
本节程序概览
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 | #include <stdint.h> /*包含了例如uint8_t等的定义*/ #include <stdbool.h> #include <math.h> #include "inc/hw_memmap.h" /*外设和内存的基地址定义*/ #include "inc/hw_types.h" /* 常用类型和宏 */ #include "driverlib/fpu.h" /* FPU */ #include "driverlib/sysctl.h" /* 时钟 宏 */ #include "driverlib/rom.h" /* ROM 宏 */ void delay_ms( int ms ) { while ( ms-- ) SysCtlDelay( SysCtlClockGet() / 3000 ); /* 延时1ms */ } void main( void ) { FPUEnable(); /* 浮点计算使能 TM4C固有 */ FPULazyStackingEnable(); SysCtlClockSet( SYSCTL_SYSDIV_5 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ ); /* 设置系统时钟为80MHZ */ delay_ms( 2 ); /* 延时2ms */ while ( 1 ) { } } |
3.1.1 PLL
TM4C123GH6PM MCU具有四个不同的时钟源:
精密内部振荡器(PIOSC): 16 MHz。
主振荡器(MOSC):它可以使用外部时钟源或外部晶体。
低频内部振荡器(LFIOSC):片上内部30 kHz振荡器,用于深度睡眠节能模式。
休眠RTC振荡器(RTCOSC):可以配置为来自休眠(HIB)模块或HIB低频时钟源(HIB LFIOSC)的32.768 KHz外部振荡器源,旨在为系统提供实际的时钟源。
系统时钟由PLL产生,该PLL可以由5至25 MHz之间运行的任何晶体或振荡器驱动。PLL的输出频率始终为400 MHz,并且与输入时钟源无关。
两个时钟源,主OSC(MOSC)和精密内部osc(PIOSC)16 MHz,可以用作PLL的时钟源,并且可以通过MUX选择该时钟源。两个多路复用器(MUX)用于选择不同的时钟源,并且可以使用两种方法来创建要由CPU使用的系统时钟:
一种方法是使用锁相环(PLL)时钟发生器,该时钟发生器需要一个时钟源作为输入源来创建此系统时钟。
另一种方法是直接使用四个时钟源中的任何一个,并且可以通过MUX选择。一种简单的方法是使用精密内部OSC(16 MHz)除以4得到4 MHz的系统时钟。
库函数
1 2 | #include "driverlib/sysctl.h" /* 时钟 宏 */ SysCtlClockSet(SYSCTL_SYSDIV_5 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ);//设置为80MHZ工作频率 |
寄存器配置
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 | #include "inc/tm4c123gh6pm.h" #define SYSCTL_RIS_PLLLRIS 0x00000040 /* PLL Lock Raw Interrupt Status */ #define SYSCTL_RCC_XTAL_M 0x000007C0 /* Crystal Value */ #define SYSCTL_RCC_XTAL_6MHZ 0x000002C0 /* 6 MHz Crystal */ #define SYSCTL_RCC_XTAL_8MHZ 0x00000380 /* 8 MHz Crystal */ #define SYSCTL_RCC_XTAL_16MHZ 0x00000540 /* 16 MHz Crystal */ #define SYSCTL_RCC2_USERCC2 0x80000000 /* Use RCC2 */ #define SYSCTL_RCC2_DIV400 0x40000000 /* Divide PLL as 400 MHz vs. 200 MHz */ #define SYSCTL_RCC2_SYSDIV2_M 0x1F800000 /* System Clock Divisor 2 */ #define SYSCTL_RCC2_SYSDIV2LSB 0x00400000 /* Additional LSB for SYSDIV2 */ #define SYSCTL_RCC2_PWRDN2 0x00002000 /* Power-Down PLL 2 */ #define SYSCTL_RCC2_BYPASS2 0x00000800 /* PLL Bypass 2 */ #define SYSCTL_RCC2_OSCSRC2_M 0x00000070 /* Oscillator Source 2 */ #define SYSCTL_RCC2_OSCSRC2_MO 0x00000000 /* MOSC */ #define Bus80MHz 4 void PLL_Init(void) { /* 1) configure the system to use RCC2 for advanced features such as 400 MHz PLL and non-integer System Clock Divisor */ SYSCTL_RCC2_R |= SYSCTL_RCC2_USERCC2; /* 2) bypass PLL while initializing */ SYSCTL_RCC2_R |= SYSCTL_RCC2_BYPASS2; /* 3) select the crystal value and oscillator source */ SYSCTL_RCC_R &= ~SYSCTL_RCC_XTAL_M; /* clear XTAL field */ SYSCTL_RCC_R += SYSCTL_RCC_XTAL_16MHZ; /* configure for 16 MHz crystal */ SYSCTL_RCC2_R &= ~SYSCTL_RCC2_OSCSRC2_M; /* clear oscillator source field */ SYSCTL_RCC2_R += SYSCTL_RCC2_OSCSRC2_MO; /* configure for main oscillator source */ /* 4) activate PLL by clearing PWRDN */ SYSCTL_RCC2_R &= ~SYSCTL_RCC2_PWRDN2; /* 5) set the desired system divider and the system divider least significant bit */ SYSCTL_RCC2_R |= SYSCTL_RCC2_DIV400; /* use 400 MHz PLL */ SYSCTL_RCC2_R = (SYSCTL_RCC2_R & ~0x1FC00000) /* clear system clock divider field */ + (Bus80MHz << 22); /* configure for 80 MHz clock */ /* 6) wait for the PLL to lock by polling PLLLRIS */ while ((SYSCTL_RIS_R & SYSCTL_RIS_PLLLRIS) == 0) { ; } /* 7) enable use of PLL by clearing BYPASS */ SYSCTL_RCC2_R &= ~SYSCTL_RCC2_BYPASS2; } |
3.1.2 FPU
下面说一下开启FPU的方法:
首先,需要在编译器上开启FPU功能。CCS:默认为开启状态。可以在propertise——bulid——arm compiler——processor options 里的specify floating point support里配置,默认为FPv4SPD16 ,即开启状态。
其次,需要在代码里加上这两句 FPUEnable();FPULazyStackingEnable();(最好加在main函数入口处,具体原因我也不清楚。)
https://blog.csdn.net/chun_1959/article/details/50946392
http://home.eeworld.com.cn/my/space-uid-525682-blogid-242407.html
总而言之,单片机核心不是和运算符点运算,设计了FPU专门应用于浮点运算,这需要编译器的支持,编译器将程序中所有浮点运算都放置于FPU模块,这样可以减少CPU的负担,从而提升整体性能。
3.1.3 延时
1 2 3 4 5 6 | #include "driverlib/sysctl.h" /* 时钟 宏 */ void delay_ms( int ms ) { while ( ms-- ) SysCtlDelay( SysCtlClockGet() / 3000 ); /* 延时1ms */ } |
3.2 GPIO中,LED,KEY
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 | #include <stdint.h> /*包含了例如uint8_t等的定义*/ #include <stdbool.h> #include <math.h> #include "inc/hw_memmap.h" /*外设和内存的基地址定义*/ #include "inc/hw_types.h" /* 常用类型和宏 */ #include "driverlib/fpu.h" /* FPU */ #include "driverlib/sysctl.h" /* 时钟 宏 */ #include "driverlib/rom.h" /* ROM 宏 */ #include "driverlib/gpio.h" void delay_ms(int ms) { while (ms--) SysCtlDelay(SysCtlClockGet() / 3000); /* 延时1ms */ } void init_LED(void) { SysCtlPeripheralEnable( SYSCTL_PERIPH_GPIOF); /* 使能GPIOF时钟 */ while (!SysCtlPeripheralReady( SYSCTL_PERIPH_GPIOF)) ; /* 等待使能完成 */ GPIOPinTypeGPIOOutput( GPIO_PORTF_BASE, GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3); /* PF1 PF2 PF3设置为输出模式 */ GPIOPinWrite( GPIO_PORTF_BASE, GPIO_PIN_1, GPIO_PIN_1); /* PF1输出高电平 */ GPIOPinWrite( GPIO_PORTF_BASE, GPIO_PIN_1, 0); /* PF1输出低电平 */ } void init_key(void) { SysCtlPeripheralEnable( SYSCTL_PERIPH_GPIOF); /* 使能GPIOF时钟 */ while (!SysCtlPeripheralReady( SYSCTL_PERIPH_GPIOF)) ; /* 等待使能完成 */ GPIOPinTypeGPIOInput( GPIO_PORTF_BASE, GPIO_PIN_0 | GPIO_PIN_4); /* PF0 PF4设置为输入模式 */ GPIOPadConfigSet( GPIO_PORTF_BASE, GPIO_PIN_0 | GPIO_PIN_4, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPU); /* PF0 PF4设置为上拉电阻 */ } void main(void) { uint8_t ledifg = 0; FPUEnable(); /* 浮点计算使能 TM4C固有 */ FPULazyStackingEnable(); SysCtlClockSet( SYSCTL_SYSDIV_5 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ); /* 设置系统时钟为80MHZ */ init_LED(); init_key(); GPIOPinWrite( GPIO_PORTF_BASE, GPIO_PIN_1, 0); /* PF1输出低电平 */ delay_ms(100); /* 延时 */ GPIOPinWrite( GPIO_PORTF_BASE, GPIO_PIN_1, GPIO_PIN_1); /* PF1输出高电平 */ delay_ms(100); /* 延时 */ GPIOPinWrite( GPIO_PORTF_BASE, GPIO_PIN_1, 0); /* PF1输出低电平 */ while (1) { if (GPIOPinRead( GPIO_PORTF_BASE, GPIO_PIN_4) == 0) /*按键按下 */ { delay_ms(5); if (GPIOPinRead( GPIO_PORTF_BASE, GPIO_PIN_4) == 0) { while (GPIOPinRead( GPIO_PORTF_BASE, GPIO_PIN_4) == 0) ; /* 等待按键松开 */ ledifg = !ledifg; if (ledifg) GPIOPinWrite( GPIO_PORTF_BASE, GPIO_PIN_1, GPIO_PIN_1); /* PF1输出高电平 */ else GPIOPinWrite( GPIO_PORTF_BASE, GPIO_PIN_1, 0); /* PF1输出低电平 */ } } } } |
3.3 按键外部中断控制灯
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 | #include <stdint.h> /*包含了例如uint8_t等的定义*/ #include <stdbool.h> #include <math.h> #include "inc/hw_memmap.h" /*外设和内存的基地址定义*/ #include "inc/hw_types.h" /* 常用类型和宏 */ #include "inc/hw_ints.h" /* 中断分配宏 */ #include "driverlib/fpu.h" /* FPU */ #include "driverlib/sysctl.h" /* 时钟 宏 */ #include "driverlib/rom.h" /* ROM 宏 */ #include "driverlib/gpio.h" #include "driverlib/pin_map.h" #include "driverlib/interrupt.h" /* GPIOF中断服务函数 */ void vector_PortFIntHandler( void ) { GPIOPinWrite( GPIO_PORTF_BASE, GPIO_PIN_1, (GPIOPinRead( GPIO_PORTF_BASE, GPIO_PIN_1 ) == 0) ? GPIO_PIN_1 : 0 ); /* 翻转PF1电平 */ GPIOIntClear( GPIO_PORTF_BASE, GPIO_INT_PIN_4 ); /* 清除PF4中断标志 */ } void delay_ms( int ms ) { while ( ms-- ) SysCtlDelay( SysCtlClockGet() / 3000 ); /* 延时1ms */ } void init_LED( void ) { SysCtlPeripheralEnable( SYSCTL_PERIPH_GPIOF ); /* 使能GPIOF时钟 */ while ( !SysCtlPeripheralReady( SYSCTL_PERIPH_GPIOF ) ) ; /* 等待使能完成 */ GPIOPinTypeGPIOOutput( GPIO_PORTF_BASE, GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3 ); /* PF1 PF2 PF3设置为输出模式 */ GPIOPinWrite( GPIO_PORTF_BASE, GPIO_PIN_1, GPIO_PIN_1 ); /* PF1输出高电平 */ GPIOPinWrite( GPIO_PORTF_BASE, GPIO_PIN_1, 0 ); /* PF1输出低电平 */ } void init_key( void ) { SysCtlPeripheralEnable( SYSCTL_PERIPH_GPIOF ); /* 使能GPIOF时钟 */ while ( !SysCtlPeripheralReady( SYSCTL_PERIPH_GPIOF ) ) ; /* 等待使能完成 */ GPIOPinTypeGPIOInput( GPIO_PORTF_BASE, GPIO_PIN_0 | GPIO_PIN_4 ); /* PF0 PF4设置为输入模式 */ GPIOPadConfigSet( GPIO_PORTF_BASE, GPIO_PIN_0 | GPIO_PIN_4, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPU ); /* PF0 PF4设置为上拉电阻 */ GPIOIntRegister( GPIO_PORTF_BASE, vector_PortFIntHandler ); /* 注册GPIOF的中断服务函数 */ GPIOIntTypeSet( GPIO_PORTF_BASE, GPIO_PIN_4, GPIO_RISING_EDGE ); /*上升沿触发 */ GPIOIntEnable( GPIO_PORTF_BASE, GPIO_INT_PIN_4 ); /* PF4中断使能 */ IntEnable( INT_GPIOF ); /* PF中断使能 */ IntPrioritySet( INT_GPIOF, 0 ); /* 设置中断优先级 */ GPIOIntClear( GPIO_PORTF_BASE, GPIO_INT_PIN_4 ); /* 清除PF4中断标志 */ } void main( void ) { uint8_t ledifg = 0; FPUEnable(); /* 浮点计算使能 TM4C固有 */ FPULazyStackingEnable(); SysCtlClockSet( SYSCTL_SYSDIV_5 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ ); /* 设置系统时钟为80MHZ */ init_LED(); init_key(); GPIOPinWrite( GPIO_PORTF_BASE, GPIO_PIN_1, 0 ); /* PF1输出低电平 */ delay_ms( 100 ); /* 延时 */ GPIOPinWrite( GPIO_PORTF_BASE, GPIO_PIN_1, GPIO_PIN_1 ); /* PF1输出高电平 */ delay_ms( 100 ); /* 延时 */ GPIOPinWrite( GPIO_PORTF_BASE, GPIO_PIN_1, 0 ); /* PF1输出低电平 */ IntMasterEnable(); /* 使能总中断 */ while ( 1 ) { } } |
3.4 串口收发
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 | #include <stdint.h> /*包含了例如uint8_t等的定义*/ #include <stdbool.h> #include <math.h> #include "inc/hw_memmap.h" /*外设和内存的基地址定义*/ #include "inc/hw_types.h" /* 常用类型和宏 */ #include "inc/hw_ints.h" /* 中断分配宏 */ #include "driverlib/fpu.h" /* FPU */ #include "driverlib/sysctl.h" /* 时钟 宏 */ #include "driverlib/rom.h" /* ROM 宏 */ #include "driverlib/gpio.h" #include "driverlib/pin_map.h" #include "driverlib/interrupt.h" #include "driverlib/uart.h" /* GPIOF中断服务函数 */ void vector_PortFIntHandler( void ) { GPIOPinWrite( GPIO_PORTF_BASE, GPIO_PIN_1, (GPIOPinRead( GPIO_PORTF_BASE, GPIO_PIN_1 ) == 0) ? GPIO_PIN_1 : 0 ); /* 翻转PF1电平 */ GPIOIntClear( GPIO_PORTF_BASE, GPIO_INT_PIN_4 ); /* 清除PF4中断标志 */ } /* 串口接收中断 */ void vector_UART_IntHandler( void ) { uint8_t i_UartReceiveData = 0; UARTIntClear( UART0_BASE, UARTIntStatus( UART0_BASE, true ) ); /* 清除中断状态 */ /* 如果接受FIFO有数据就不断接收 */ while ( UARTCharsAvail( UART0_BASE ) ) { /* 接收数据 */ i_UartReceiveData = UARTCharGetNonBlocking( UART0_BASE ); /* 将数据发送出去 */ UARTCharPutNonBlocking( UART0_BASE, i_UartReceiveData ); } } void delay_ms( int ms ) { while ( ms-- ) SysCtlDelay( SysCtlClockGet() / 3000 ); /* 延时1ms */ } void init_LED( void ) { SysCtlPeripheralEnable( SYSCTL_PERIPH_GPIOF ); /* 使能GPIOF时钟 */ while ( !SysCtlPeripheralReady( SYSCTL_PERIPH_GPIOF ) ) ; /* 等待使能完成 */ GPIOPinTypeGPIOOutput( GPIO_PORTF_BASE, GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3 ); /* PF1 PF2 PF3设置为输出模式 */ GPIOPinWrite( GPIO_PORTF_BASE, GPIO_PIN_1, GPIO_PIN_1 ); /* PF1输出高电平 */ GPIOPinWrite( GPIO_PORTF_BASE, GPIO_PIN_1, 0 ); /* PF1输出低电平 */ } void init_key( void ) { SysCtlPeripheralEnable( SYSCTL_PERIPH_GPIOF ); /* 使能GPIOF时钟 */ while ( !SysCtlPeripheralReady( SYSCTL_PERIPH_GPIOF ) ) ; /* 等待使能完成 */ GPIOPinTypeGPIOInput( GPIO_PORTF_BASE, GPIO_PIN_0 | GPIO_PIN_4 ); /* PF0 PF4设置为输入模式 */ GPIOPadConfigSet( GPIO_PORTF_BASE, GPIO_PIN_0 | GPIO_PIN_4, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPU ); /* PF0 PF4设置为上拉电阻 */ GPIOIntRegister( GPIO_PORTF_BASE, vector_PortFIntHandler ); /* 注册GPIOF的中断服务函数 */ GPIOIntTypeSet( GPIO_PORTF_BASE, GPIO_PIN_4, GPIO_RISING_EDGE ); /*上升沿触发 */ GPIOIntEnable( GPIO_PORTF_BASE, GPIO_INT_PIN_4 ); /* PF4中断使能 */ IntEnable( INT_GPIOF ); /* PF中断使能 */ IntPrioritySet( INT_GPIOF, 0 ); /* 设置中断优先级 */ GPIOIntClear( GPIO_PORTF_BASE, GPIO_INT_PIN_4 ); /* 清除PF4中断标志 */ } void init_UART( void ) { SysCtlPeripheralEnable( SYSCTL_PERIPH_GPIOA ); /* 使能时钟 */ SysCtlPeripheralEnable( SYSCTL_PERIPH_UART0 ); /* 使能时钟 */ GPIOPinTypeUART( GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1 ); /* 配置PA0,PA1为GPIO串口模式 */ GPIOPinConfigure( GPIO_PA0_U0RX ); /* 配置PA0和PA1的功能 */ GPIOPinConfigure( GPIO_PA1_U0TX ); UARTConfigSetExpClk( UART0_BASE, SysCtlClockGet(), 115200, (UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE | UART_CONFIG_PAR_NONE) ); /* UART0工作模式 */ UARTIntRegister( UART0_BASE, vector_UART_IntHandler ); /* 注册中断函数 */ IntPrioritySet( INT_UART0, 0 ); /* 设置中断优先级 */ UARTIntEnable( UART0_BASE, UART_INT_RX | UART_INT_RT ); /* 使能接收完成中断和接收超时中断 */ IntEnable( INT_UART0 ); /* 使能UART0中断 */ } void main( void ) { FPUEnable(); /* 浮点计算使能 TM4C固有 */ FPULazyStackingEnable(); SysCtlClockSet( SYSCTL_SYSDIV_5 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ ); /* 设置系统时钟为80MHZ */ init_LED(); init_key(); init_UART(); GPIOPinWrite( GPIO_PORTF_BASE, GPIO_PIN_1, 0 ); /* PF1输出低电平 */ delay_ms( 100 ); /* 延时 */ GPIOPinWrite( GPIO_PORTF_BASE, GPIO_PIN_1, GPIO_PIN_1 ); /* PF1输出高电平 */ delay_ms( 100 ); /* 延时 */ GPIOPinWrite( GPIO_PORTF_BASE, GPIO_PIN_1, 0 ); /* PF1输出低电平 */ IntMasterEnable(); /* 使能总中断 */ while ( 1 ) { } } |
3.5 Flash写入与读取
SRAM存临时数据,当CPU缓存。
EEPROM 一般操作这个存储系统数据,而不是操作Flash,操作Flash需要知道自己写的代码的大小占了多少,从而去写入没用到的Flash区域。
FLASH按块(BLOCK)操作,EEPROM按字节操作。FLASH成本低。
ROM 存储固化了的数据,比如一些固定指令和固定变量。
flash读写
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 | #include <stdint.h> /*包含了例如uint8_t等的定义*/ #include <stdbool.h> #include <math.h> #include <stdio.h> #include "inc/hw_memmap.h" /*外设和内存的基地址定义*/ #include "inc/hw_types.h" /* 常用类型和宏 */ #include "inc/hw_ints.h" /* 中断分配宏 */ #include "driverlib/fpu.h" /* FPU */ #include "driverlib/sysctl.h" /* 时钟 宏 */ #include "driverlib/rom.h" /* ROM 宏 */ #include "driverlib/gpio.h" #include "driverlib/pin_map.h" #include "driverlib/interrupt.h" #include "driverlib/uart.h" #include "driverlib/flash.h" void init_LED(void) { SysCtlPeripheralEnable( SYSCTL_PERIPH_GPIOF); /* 使能GPIOF时钟 */ while (!SysCtlPeripheralReady( SYSCTL_PERIPH_GPIOF)) ; /* 等待使能完成 */ GPIOPinTypeGPIOOutput( GPIO_PORTF_BASE, GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3); /* PF1 PF2 PF3设置为输出模式 */ GPIOPinWrite( GPIO_PORTF_BASE, GPIO_PIN_1, GPIO_PIN_1); /* PF1输出高电平 */ GPIOPinWrite( GPIO_PORTF_BASE, GPIO_PIN_1, 0); /* PF1输出低电平 */ } char flashRead(unsigned long ulAdress) { char *pcData; pcData = (char *) (ulAdress); return (*pcData); } #define SECTION 200 //flash一块是1kb=1024字节,按块操作,一共256kb char yanzheng[20]; void main(void) { char cString[] = "abcdefghijklmn"; char c; uint32_t *pulData; long size; int i; FPUEnable(); /* 浮点计算使能 TM4C固有 */ FPULazyStackingEnable(); SysCtlClockSet( SYSCTL_SYSDIV_5 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ); /* 设置系统时钟为80MHZ */ init_LED(); pulData = (uint32_t *) cString; //必须按unsigned long操作 size = 4 * (1 + sizeof(cString) / 4); // 编程的字节数必须是4的倍数 if (FlashErase(SECTION * 1024)) //擦除Flash { //擦除失败了 while (1) ; //无限循环 } if (FlashProgram(pulData, SECTION * 1024, size)) { //写入失败了 while (1) ; //无限循环 } for (i = 0; i < sizeof(cString); i++) { c = flashRead(SECTION * 1024 + i); yanzheng[i] = c; } GPIOPinWrite( GPIO_PORTF_BASE, GPIO_PIN_1, (GPIOPinRead( GPIO_PORTF_BASE, GPIO_PIN_1) == 0) ? GPIO_PIN_1 : 0); /* 翻转PF1电平 */ while (1) { } } |
3.6 定时器 定时中断
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 | #include <stdint.h> /*包含了例如uint8_t等的定义*/ #include <stdbool.h> #include <math.h> #include <stdio.h> #include "inc/hw_memmap.h" /*外设和内存的基地址定义*/ #include "inc/hw_types.h" /* 常用类型和宏 */ #include "inc/hw_ints.h" /* 中断分配宏 */ #include "driverlib/fpu.h" /* FPU */ #include "driverlib/sysctl.h" /* 时钟 宏 */ #include "driverlib/rom.h" /* ROM 宏 */ #include "driverlib/gpio.h" #include "driverlib/pin_map.h" #include "driverlib/interrupt.h" #include "driverlib/uart.h" #include "driverlib/timer.h" void init_LED(void) { SysCtlPeripheralEnable( SYSCTL_PERIPH_GPIOF); /* 使能GPIOF时钟 */ while (!SysCtlPeripheralReady( SYSCTL_PERIPH_GPIOF)) ; /* 等待使能完成 */ GPIOPinTypeGPIOOutput( GPIO_PORTF_BASE, GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3); /* PF1 PF2 PF3设置为输出模式 */ GPIOPinWrite( GPIO_PORTF_BASE, GPIO_PIN_1, GPIO_PIN_1); /* PF1输出高电平 */ GPIOPinWrite( GPIO_PORTF_BASE, GPIO_PIN_1, 0); /* PF1输出低电平 */ } void init_key(void) { SysCtlPeripheralEnable( SYSCTL_PERIPH_GPIOF); /* 使能GPIOF时钟 */ while (!SysCtlPeripheralReady( SYSCTL_PERIPH_GPIOF)) ; /* 等待使能完成 */ GPIOPinTypeGPIOInput( GPIO_PORTF_BASE, GPIO_PIN_0 | GPIO_PIN_4); /* PF0 PF4设置为输入模式 */ GPIOPadConfigSet( GPIO_PORTF_BASE, GPIO_PIN_0 | GPIO_PIN_4, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPU); /* PF0 PF4设置为上拉电阻 */ } void Timer0IntHandler(void) { TimerIntClear( TIMER0_BASE, TIMER_TIMA_TIMEOUT); /* 清除标志位 */ GPIOPinWrite( GPIO_PORTF_BASE, GPIO_PIN_1, (GPIOPinRead( GPIO_PORTF_BASE, GPIO_PIN_1) == 0) ? GPIO_PIN_1 : 0); /* 翻转PF1电平 */ } void init_timer(void) { SysCtlPeripheralEnable( SYSCTL_PERIPH_TIMER0); /* 使能时钟 */ /* TimerConfigure(TIMER0_BASE, TIMER_CFG_ONE_SHOT);//单次计数模式 */ TimerConfigure( TIMER0_BASE, TIMER_CFG_PERIODIC); /* 周期性计数模式 */ TimerLoadSet( TIMER0_BASE, TIMER_A, SysCtlClockGet() / 10 - 1); /* 计数频率10HZ */ TimerIntRegister( TIMER0_BASE, TIMER_A, Timer0IntHandler); /* 注册中断函数 */ IntPrioritySet( INT_TIMER0A, 0); /* 设置中断优先级 */ TimerIntEnable( TIMER0_BASE, TIMER_TIMA_TIMEOUT); /* TIMEOUT标志位触发中断 */ TimerEnable( TIMER0_BASE, TIMER_A); /* TIMER0A开始计数,当计数值等于TimerLoadSet,触发中断 */ IntEnable( INT_TIMER0A); /* 使能TIMER0A中断 */ } void main(void) { FPUEnable(); /* 浮点计算使能 TM4C固有 */ FPULazyStackingEnable(); SysCtlClockSet( SYSCTL_SYSDIV_5 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ); /* 设置系统时钟为80MHZ */ init_LED(); init_key(); init_timer(); IntMasterEnable(); /* 开总中断 */ while (1) { } } |
3.7 定时器 PWM输出
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 | #include <stdint.h> /*包含了例如uint8_t等的定义*/ #include <stdbool.h> #include <math.h> #include <stdio.h> #include "inc/hw_memmap.h" /*外设和内存的基地址定义*/ #include "inc/hw_types.h" /* 常用类型和宏 */ #include "inc/hw_ints.h" /* 中断分配宏 */ #include "driverlib/fpu.h" /* FPU */ #include "driverlib/sysctl.h" /* 时钟 宏 */ #include "driverlib/rom.h" /* ROM 宏 */ #include "driverlib/gpio.h" #include "driverlib/pin_map.h" #include "driverlib/interrupt.h" #include "driverlib/uart.h" #include "driverlib/pwm.h" // 具体函数内容 void M1PWM7_init(uint32_t freq, float duty) { //设置PWM时钟为系统时钟的1分频 SysCtlPWMClockSet(SYSCTL_PWMDIV_1); SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM1); //配置引脚为PWM功能 SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF); GPIOPinConfigure(GPIO_PF3_M1PWM7); GPIOPinTypePWM(GPIO_PORTF_BASE, GPIO_PIN_3); //M1PWM7 //配置 PWM1 Generator3·发生器 PWMGenConfigure(PWM1_BASE, PWM_GEN_3, PWM_GEN_MODE_UP_DOWN|PWM_GEN_MODE_NO_SYNC); //配置 PWM1 Generator3 周期 PWMGenPeriodSet(PWM1_BASE, PWM_GEN_3, SysCtlClockGet()/freq - 1); //配置 PWM1 Generator3 占空比 PWMPulseWidthSet(PWM1_BASE, PWM_OUT_7, PWMGenPeriodGet(PWM1_BASE, PWM_GEN_3)*duty - 1); //使能 PWM1 输出 PWMOutputState(PWM1_BASE, PWM_OUT_7_BIT, true); //使能 PWM1 发生器模块 PWMGenEnable(PWM1_BASE, PWM_GEN_3); } void M1PWM7_set_duty(float duty) { //配置 PWM1 Generator3 占空比 PWMPulseWidthSet(PWM1_BASE, PWM_OUT_7, PWMGenPeriodGet(PWM1_BASE, PWM_GEN_3)*duty - 1); PWMOutputState(PWM1_BASE, PWM_OUT_7_BIT, true); //使能 PWM1 ·发生器模块 PWMGenEnable(PWM1_BASE, PWM_GEN_3); } void main(void) { FPUEnable(); /* 浮点计算使能 TM4C固有 */ FPULazyStackingEnable(); SysCtlClockSet( SYSCTL_SYSDIV_5 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ); /* 设置系统时钟为80MHZ */ M1PWM7_init(1000, 0); // PWM 初始化,频率为1000HZ ,占空比为0 M1PWM7_set_duty(0.5); IntMasterEnable(); /* 开总中断 */ while (1) { } } |
3.8 定时器 计数捕获
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 | #include <stdint.h> /*包含了例如uint8_t等的定义*/ #include <stdbool.h> #include <math.h> #include <stdio.h> #include "inc/hw_memmap.h" /*外设和内存的基地址定义*/ #include "inc/hw_types.h" /* 常用类型和宏 */ #include "inc/hw_ints.h" /* 中断分配宏 */ #include "driverlib/fpu.h" /* FPU */ #include "driverlib/sysctl.h" /* 时钟 宏 */ #include "driverlib/rom.h" /* ROM 宏 */ #include "driverlib/gpio.h" #include "driverlib/pin_map.h" #include "driverlib/interrupt.h" #include "driverlib/uart.h" #include "driverlib/timer.h" void init_LED( void ) { SysCtlPeripheralEnable( SYSCTL_PERIPH_GPIOF ); /* 使能GPIOF时钟 */ while ( !SysCtlPeripheralReady( SYSCTL_PERIPH_GPIOF ) ) ; /* 等待使能完成 */ GPIOPinTypeGPIOOutput( GPIO_PORTF_BASE, GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3 ); /* PF1 PF2 PF3设置为输出模式 */ GPIOPinWrite( GPIO_PORTF_BASE, GPIO_PIN_1, GPIO_PIN_1 ); /* PF1输出高电平 */ GPIOPinWrite( GPIO_PORTF_BASE, GPIO_PIN_1, 0 ); /* PF1输出低电平 */ } void init_key( void ) { SysCtlPeripheralEnable( SYSCTL_PERIPH_GPIOF ); /* 使能GPIOF时钟 */ while ( !SysCtlPeripheralReady( SYSCTL_PERIPH_GPIOF ) ) ; /* 等待使能完成 */ GPIOPinTypeGPIOInput( GPIO_PORTF_BASE, GPIO_PIN_0 | GPIO_PIN_4 ); /* PF0 PF4设置为输入模式 */ GPIOPadConfigSet( GPIO_PORTF_BASE, GPIO_PIN_0 | GPIO_PIN_4, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPU ); /* PF0 PF4设置为上拉电阻 */ } void Timer0IntHandler( void ) { TimerIntClear( TIMER0_BASE, TIMER_CAPA_MATCH ); /* 清空标志位 */ GPIOPinWrite( GPIO_PORTF_BASE, GPIO_PIN_1, (GPIOPinRead( GPIO_PORTF_BASE, GPIO_PIN_1 ) == 0) ? GPIO_PIN_1 : 0 ); /* 翻转PF1电平 */ } void init_timer( void ) { SysCtlPeripheralEnable( SYSCTL_PERIPH_TIMER0 ); /* 使能时钟 */ /* 捕获引脚PB6 */ SysCtlPeripheralEnable( SYSCTL_PERIPH_GPIOB ); /* 使能时钟 */ GPIOPinTypeGPIOInput( GPIO_PORTB_BASE, GPIO_PIN_6 ); GPIOPinConfigure( GPIO_PB6_T0CCP0 ); /* #define GPIO_PC4_WT0CCP0 0x00021007 */ GPIOPinTypeTimer( GPIO_PORTB_BASE, GPIO_PIN_6 ); TimerConfigure( TIMER0_BASE, TIMER_CFG_A_CAP_COUNT_UP | TIMER_CFG_SPLIT_PAIR ); /* 计数捕获模式,上升沿捕获,Two half-width timers */ TimerControlEvent( TIMER0_BASE, TIMER_A, TIMER_EVENT_POS_EDGE ); /* 捕获模式,A定时器,上升沿捕获,增计数模式,到达匹配值后可自动清零 */ /* TimerLoadSet(TIMER0_BASE, TIMER_A, 6);//溢出值6 */ TimerMatchSet( TIMER0_BASE, TIMER_A, 3 ); /* 匹配值3 */ TimerIntRegister( TIMER0_BASE, TIMER_A, Timer0IntHandler ); /* 注册中断函数 */ IntPrioritySet( INT_TIMER0A, 0 ); /* 设置中断优先级 */ TimerIntEnable( TIMER0_BASE, TIMER_CAPA_MATCH ); /* 捕获事件中断 */ TimerEnable( TIMER0_BASE, TIMER_A ); /* TIMER0A开始计数,当计数值等于TimerLoadSet,触发中断 */ IntEnable( INT_TIMER0A ); /* 使能TIMER0A中断 */ } void main( void ) { FPUEnable(); /* 浮点计算使能 TM4C固有 */ FPULazyStackingEnable(); SysCtlClockSet( SYSCTL_SYSDIV_5 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ ); /* 设置系统时钟为80MHZ */ init_LED(); init_key(); init_timer(); IntMasterEnable(); /* 开总中断 */ while ( 1 ) { } } |
3.9 定时器 时间捕获
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 | #include <stdint.h> /*包含了例如uint8_t等的定义*/ #include <stdbool.h> #include <math.h> #include <stdio.h> #include "inc/hw_memmap.h" /*外设和内存的基地址定义*/ #include "inc/hw_types.h" /* 常用类型和宏 */ #include "inc/hw_ints.h" /* 中断分配宏 */ #include "driverlib/fpu.h" /* FPU */ #include "driverlib/sysctl.h" /* 时钟 宏 */ #include "driverlib/rom.h" /* ROM 宏 */ #include "driverlib/gpio.h" #include "driverlib/pin_map.h" #include "driverlib/interrupt.h" #include "driverlib/uart.h" #include "driverlib/timer.h" void init_LED(void) { SysCtlPeripheralEnable( SYSCTL_PERIPH_GPIOF); /* 使能GPIOF时钟 */ while (!SysCtlPeripheralReady( SYSCTL_PERIPH_GPIOF)) ; /* 等待使能完成 */ GPIOPinTypeGPIOOutput( GPIO_PORTF_BASE, GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3); /* PF1 PF2 PF3设置为输出模式 */ GPIOPinWrite( GPIO_PORTF_BASE, GPIO_PIN_1, GPIO_PIN_1); /* PF1输出高电平 */ GPIOPinWrite( GPIO_PORTF_BASE, GPIO_PIN_1, 0); /* PF1输出低电平 */ } void init_key(void) { SysCtlPeripheralEnable( SYSCTL_PERIPH_GPIOF); /* 使能GPIOF时钟 */ while (!SysCtlPeripheralReady( SYSCTL_PERIPH_GPIOF)) ; /* 等待使能完成 */ GPIOPinTypeGPIOInput( GPIO_PORTF_BASE, GPIO_PIN_0 | GPIO_PIN_4); /* PF0 PF4设置为输入模式 */ GPIOPadConfigSet( GPIO_PORTF_BASE, GPIO_PIN_0 | GPIO_PIN_4, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPU); /* PF0 PF4设置为上拉电阻 */ } void Timer0IntHandler(void) { TimerIntClear(TIMER0_BASE, TIMER_CAPA_EVENT); /* 清空标志位 */ GPIOPinWrite( GPIO_PORTF_BASE, GPIO_PIN_1, (GPIOPinRead( GPIO_PORTF_BASE, GPIO_PIN_1) == 0) ? GPIO_PIN_1 : 0); /* 翻转PF1电平 */ } void init_timer(void) { SysCtlPeripheralEnable( SYSCTL_PERIPH_TIMER0); /* 使能时钟 */ /* 捕获引脚PB6 */ SysCtlPeripheralEnable( SYSCTL_PERIPH_GPIOB); /* 使能时钟 */ GPIOPinTypeGPIOInput( GPIO_PORTB_BASE, GPIO_PIN_6); GPIOPinConfigure( GPIO_PB6_T0CCP0); /* #define GPIO_PC4_WT0CCP0 0x00021007 */ GPIOPinTypeTimer( GPIO_PORTB_BASE, GPIO_PIN_6); TimerConfigure( TIMER0_BASE,TIMER_CFG_A_CAP_TIME_UP | TIMER_CFG_SPLIT_PAIR); /* 计时捕获模式,上升沿捕获 */ TimerControlEvent( TIMER0_BASE, TIMER_A, TIMER_EVENT_POS_EDGE); /* 捕获模式,A定时器,上升沿捕获 */ TimerIntRegister( TIMER0_BASE, TIMER_A, Timer0IntHandler); /* 注册中断函数 */ IntPrioritySet( INT_TIMER0A, 0); /* 设置中断优先级 */ TimerIntEnable(TIMER0_BASE, TIMER_CAPA_EVENT); /* 捕获事件中断 */ TimerEnable( TIMER0_BASE, TIMER_A); /* TIMER0A开始计数,当计数值等于TimerLoadSet,触发中断 */ IntEnable( INT_TIMER0A); /* 使能TIMER0A中断 */ } void main(void) { FPUEnable(); /* 浮点计算使能 TM4C固有 */ FPULazyStackingEnable(); SysCtlClockSet( SYSCTL_SYSDIV_5 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ); /* 设置系统时钟为80MHZ */ init_LED(); init_key(); init_timer(); IntMasterEnable(); /* 开总中断 */ while (1) { } } |
3.10 读取模拟输入
3.11 两个按键+定时器 实验
//实验考试要求:定时0.8S,初始状态红灯闪烁,亮灭各0.8S; 按左键RGB轮流亮;按右键,三灯闪烁
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 | #include <stdint.h> /*包含了例如uint8_t等的定义*/ #include <stdbool.h> #include <math.h> #include <stdio.h> #include "inc/hw_memmap.h" /*外设和内存的基地址定义*/ #include "inc/hw_types.h" /* 常用类型和宏 */ #include "inc/hw_ints.h" /* 中断分配宏 */ #include "driverlib/fpu.h" /* FPU */ #include "driverlib/sysctl.h" /* 时钟 宏 */ #include "driverlib/rom.h" /* ROM 宏 */ #include "driverlib/gpio.h" #include "inc/hw_gpio.h" #include "driverlib/pin_map.h" #include "driverlib/interrupt.h" #include "driverlib/uart.h" #include "driverlib/PIN_MAP.h" #include "driverlib/timer.h" //实验考试要求:定时0.8S,初始状态红灯闪烁,亮灭各0.8S; 按左键RGB轮流亮;按右键,三灯闪烁 int mode = 0; int count = 0; void delay_ms(int ms) { while (ms--) SysCtlDelay(SysCtlClockGet() / 3000); /* 延时1ms */ } void init_LED(void) { SysCtlPeripheralEnable( SYSCTL_PERIPH_GPIOF); /* 使能GPIOF时钟 */ while (!SysCtlPeripheralReady( SYSCTL_PERIPH_GPIOF)) ; /* 等待使能完成 */ GPIOPinTypeGPIOOutput( GPIO_PORTF_BASE, GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3); /* PF1 PF2 PF3设置为输出模式 */ GPIOPinWrite( GPIO_PORTF_BASE, GPIO_PIN_1, 0); /* PF1输出低电平 */ GPIOPinWrite( GPIO_PORTF_BASE, GPIO_PIN_2, 0); /* PF2输出低电平 */ GPIOPinWrite( GPIO_PORTF_BASE, GPIO_PIN_3, 0); /* PF3输出低电平 */ } #define BUTTONS_GPIO_BASE GPIO_PORTF_BASE #define GPIO_LOCK_KEY 0x4C4F434B void init_key(void) { HWREG(BUTTONS_GPIO_BASE + GPIO_O_LOCK) = GPIO_LOCK_KEY; HWREG(BUTTONS_GPIO_BASE + GPIO_O_CR) |= 0x01; HWREG(BUTTONS_GPIO_BASE + GPIO_O_LOCK) = 0; SysCtlPeripheralEnable( SYSCTL_PERIPH_GPIOF); /* 使能GPIOF时钟 */ while (!SysCtlPeripheralReady( SYSCTL_PERIPH_GPIOF)) ; /* 等待使能完成 */ GPIOPinTypeGPIOInput( GPIO_PORTF_BASE, GPIO_PIN_4); /* PF0 PF4设置为输入模式 */ GPIOPadConfigSet( GPIO_PORTF_BASE, GPIO_PIN_4, GPIO_STRENGTH_4MA, GPIO_PIN_TYPE_STD_WPU); /* 设置为上拉电阻 */ GPIOPinTypeGPIOInput( GPIO_PORTF_BASE, GPIO_PIN_0); /* PF0 PF4设置为输入模式 */ GPIOPadConfigSet( GPIO_PORTF_BASE, GPIO_PIN_0, GPIO_STRENGTH_4MA, GPIO_PIN_TYPE_STD_WPU); /* 设置为上拉电阻 */ } void Timer0IntHandler(void) { TimerIntClear( TIMER0_BASE, TIMER_TIMA_TIMEOUT); /* 清除标志位 */ if (mode == 0) //初始状态 { GPIOPinWrite( GPIO_PORTF_BASE, GPIO_PIN_1, (GPIOPinRead( GPIO_PORTF_BASE, GPIO_PIN_1) == 0) ? GPIO_PIN_1 : 0); /* 翻转PF1电平 */ } else if (mode == 1) //RGB轮流亮 { if (count == 0) { GPIOPinWrite( GPIO_PORTF_BASE, GPIO_PIN_1, GPIO_PIN_1); //r GPIOPinWrite( GPIO_PORTF_BASE, GPIO_PIN_3, 0); //g GPIOPinWrite( GPIO_PORTF_BASE, GPIO_PIN_2, 0); //b } else if (count == 1) { GPIOPinWrite( GPIO_PORTF_BASE, GPIO_PIN_1, 0); //r GPIOPinWrite( GPIO_PORTF_BASE, GPIO_PIN_3, GPIO_PIN_3); //g GPIOPinWrite( GPIO_PORTF_BASE, GPIO_PIN_2, 0); //b } else if (count == 2) { GPIOPinWrite( GPIO_PORTF_BASE, GPIO_PIN_1, 0); //r GPIOPinWrite( GPIO_PORTF_BASE, GPIO_PIN_3, 0); //g GPIOPinWrite( GPIO_PORTF_BASE, GPIO_PIN_2, GPIO_PIN_2); //b } count = (count + 1) % 3; //改变count 0 1 2 } else if (mode == 2) //三灯闪烁 { if (count == 0) { GPIOPinWrite( GPIO_PORTF_BASE, GPIO_PIN_1, 0); //r GPIOPinWrite( GPIO_PORTF_BASE, GPIO_PIN_3, 0); //g GPIOPinWrite( GPIO_PORTF_BASE, GPIO_PIN_2, 0); //b } else if (count == 1) { GPIOPinWrite( GPIO_PORTF_BASE, GPIO_PIN_1, GPIO_PIN_1); //r GPIOPinWrite( GPIO_PORTF_BASE, GPIO_PIN_3, GPIO_PIN_3); //g GPIOPinWrite( GPIO_PORTF_BASE, GPIO_PIN_2, GPIO_PIN_2); //b } count = (count + 1) % 2; //改变count 0 1 } } void init_timer(void) { SysCtlPeripheralEnable( SYSCTL_PERIPH_TIMER0); /* 使能时钟 */ /* TimerConfigure(TIMER0_BASE, TIMER_CFG_ONE_SHOT);//单次计数模式 */ TimerConfigure( TIMER0_BASE, TIMER_CFG_PERIODIC); /* 周期性计数模式 */ TimerLoadSet( TIMER0_BASE, TIMER_A, SysCtlClockGet() / 1.25 - 1); /* 1.25HZ 周期就是0.8S */ TimerIntRegister( TIMER0_BASE, TIMER_A, Timer0IntHandler); /* 注册中断函数 */ IntPrioritySet( INT_TIMER0A, 0); /* 设置中断优先级 */ TimerIntEnable( TIMER0_BASE, TIMER_TIMA_TIMEOUT); /* TIMEOUT标志位触发中断 */ TimerEnable( TIMER0_BASE, TIMER_A); /* TIMER0A开始计数,当计数值等于TimerLoadSet,触发中断 */ IntEnable( INT_TIMER0A); /* 使能TIMER0A中断 */ } void main(void) { FPUEnable(); /* 浮点计算使能 TM4C固有 */ FPULazyStackingEnable(); SysCtlClockSet( SYSCTL_SYSDIV_5 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ); /* 设置系统时钟为40MHZ */ init_LED(); init_key(); init_timer(); IntMasterEnable(); /* 开总中断 */ while (1) { delay_ms(10); if (GPIOPinRead( GPIO_PORTF_BASE, GPIO_PIN_4) == 0) //左按键 { //左按键 mode = 1; count = 0; GPIOPinWrite( GPIO_PORTF_BASE, GPIO_PIN_1, 0); /* PF1输出低电平 */ GPIOPinWrite( GPIO_PORTF_BASE, GPIO_PIN_2, 0); /* PF2输出低电平 */ GPIOPinWrite( GPIO_PORTF_BASE, GPIO_PIN_3, 0); /* PF3输出低电平 */ } if (GPIOPinRead( GPIO_PORTF_BASE, GPIO_PIN_0) == 0) /*右按键按下 */ { /*右按键按下 */ mode = 2; count = 0; GPIOPinWrite( GPIO_PORTF_BASE, GPIO_PIN_1, 0); /* PF1输出低电平 */ GPIOPinWrite( GPIO_PORTF_BASE, GPIO_PIN_2, 0); /* PF2输出低电平 */ GPIOPinWrite( GPIO_PORTF_BASE, GPIO_PIN_3, 0); /* PF3输出低电平 */ } } } |