Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ==========================
- joy.c
- ==========================
- #include "joy.h"
- #include "lpc17xx_clkpwr.h"
- #define SPEED_Y 48
- #define SPEED_X (SPEED_Y * 2)
- void UART_Printer(uint8_t text[], LPC_UART_TypeDef *UARTx);
- void UART_Initialize(LPC_UART_TypeDef *UARTx);
- static void handleJoy(void);
- void startJoy(void)
- {
- // GPIO power
- CLKPWR_ConfigPPWR(CLKPWR_PCONP_PCGPIO, ENABLE);
- GPIO_SetDir(KEY_PORT1, KEY_SELECT, 0);
- GPIO_SetDir(KEY_PORT1, KEY_DOWN, 0);
- GPIO_SetDir(KEY_PORT1, KEY_LEFT, 0);
- GPIO_SetDir(KEY_PORT1, KEY_RIGHT, 0);
- GPIO_SetDir(KEY_PORT1, KEY_DOWN, 0);
- UART_Initialize((LPC_UART_TypeDef *)LPC_UART2);
- while (1) {
- handleJoy();
- }
- }
- static void handleJoy(void)
- {
- uint32_t KeyValue;
- KeyValue = GPIO_ReadValue(KEY_PORT1);
- int key_left_c, key_right_c, key_up_c, key_down_c;
- key_down_c = key_up_c = key_right_c = key_left_c = 0;
- if ((KeyValue & KEY_LEFT) == 0) {
- key_left_c = 1;
- } else if((KeyValue & KEY_RIGHT) == 0){
- key_right_c = 1;
- } else if((KeyValue & KEY_UP) == 0){
- key_up_c = 1;
- } else if(((KeyValue & KEY_DOWN) == 0)){
- key_down_c = 1;
- }
- static int x,y;
- static char buffer[10];
- if(key_down_c){
- x++;
- sprintf(buffer,"\033[%d;%df",x/SPEED_X,y/SPEED_Y);
- UART_putstring(buffer,(LPC_UART_TypeDef *)LPC_UART2);
- } else if(key_up_c){
- if(x > 0) x--;
- sprintf(buffer,"\033[%d;%df",x/SPEED_X,y/SPEED_Y);
- UART_putstring(buffer,(LPC_UART_TypeDef *)LPC_UART2);
- } else if(key_right_c){
- y++;
- sprintf(buffer,"\033[%d;%df",x/SPEED_X,y/SPEED_Y);
- UART_putstring(buffer,(LPC_UART_TypeDef *)LPC_UART2);
- } else if(key_left_c){
- if(y > 0) y--;
- sprintf(buffer,"\033[%d;%df",x/SPEED_X,y/SPEED_Y);
- UART_putstring(buffer,(LPC_UART_TypeDef *)LPC_UART2);
- }
- }
- ==========================
- joy.h
- ==========================
- #ifndef __JOY_H__
- #define __JOY_H__
- #include "lpc17xx_gpio.h"
- /* define KEY PORT1 */
- #define KEY_PORT1 (1)
- #define KEY_SELECT _BIT(25)
- #define KEY_DOWN _BIT(26)
- #define KEY_LEFT _BIT(27)
- #define KEY_RIGHT _BIT(28)
- #define KEY_UP _BIT(29)
- #endif /* __JOY_H__ */
- ==========================
- UART_functions.c
- ==========================
- #include "lpc17xx_uart.h"
- #include "lpc17xx_pinsel.h"
- void UART_Initialize(LPC_UART_TypeDef *UARTx){
- /* UART Configuration structure variable */
- UART_CFG_Type UARTConfigStruct;
- /* UART FIFO configuration Struct variable */
- UART_FIFO_CFG_Type UARTFIFOConfigStruct;
- /* Pin configuration for UART0 */
- PINSEL_CFG_Type PinCfg;
- if((uint32_t)UARTx == (uint32_t)LPC_UART0) {
- /*
- * Initialize UART0 pin connect
- */
- PinCfg.Funcnum = 1;
- PinCfg.OpenDrain = 0;
- PinCfg.Pinmode = 2;
- PinCfg.Pinnum = 2;
- PinCfg.Portnum = 0;
- PINSEL_ConfigPin(&PinCfg);
- PinCfg.Pinnum = 3;
- PINSEL_ConfigPin(&PinCfg);
- }
- else if ((uint32_t)UARTx == (uint32_t)LPC_UART2) {
- /*
- * Initialize UART2 pin connect
- */
- PinCfg.Funcnum = 1;
- PinCfg.OpenDrain = 0;
- PinCfg.Pinmode = 2;
- PinCfg.Pinnum = 10;
- PinCfg.Portnum = 0;
- PINSEL_ConfigPin(&PinCfg);
- PinCfg.Pinnum = 11;
- PINSEL_ConfigPin(&PinCfg);
- }
- /* Initialize UART Configuration parameter structure to default state:
- * Baudrate = 9600bps
- * 8 data bit
- * 1 Stop bit
- * None parity
- */
- UART_ConfigStructInit(&UARTConfigStruct);
- /* Set Baudrate to 115200 */
- UARTConfigStruct.Baud_rate = 115200;
- /* Initialize UART0 peripheral with given to corresponding parameter */
- UART_Init(UARTx, &UARTConfigStruct);
- /* Initialize FIFOConfigStruct to default state:
- * - FIFO_DMAMode = DISABLE
- * - FIFO_Level = UART_FIFO_TRGLEV0
- * - FIFO_ResetRxBuf = ENABLE
- * - FIFO_ResetTxBuf = ENABLE
- * - FIFO_State = ENABLE
- */
- UART_FIFOConfigStructInit(&UARTFIFOConfigStruct);
- /* Initialize FIFO for UART0 peripheral */
- UART_FIFOConfig(UARTx, &UARTFIFOConfigStruct);
- /* Enable UART Transmit */
- UART_TxCmd(UARTx, ENABLE);
- }
- void UART_putchar(char c,LPC_UART_TypeDef *UARTx){
- while ((UARTx->LSR & 0x20) == 0){
- // do nothing
- }
- //write character
- UARTx->THR = c;
- }
- void UART_putstring(const char *str, LPC_UART_TypeDef *UARTx){
- while(*str){
- UART_putchar(*str++,UARTx);
- }
- }
- void UART_Printer(uint8_t text[], LPC_UART_TypeDef *UARTx){
- /* print a string */
- UART_Send(UARTx, text, sizeof(text), NONE_BLOCKING);
- }
- ==========================
- main.c
- ==========================
- #include "lpc17xx_uart.h"
- #include "lpc17xx_pinsel.h"
- void startJoy();
- int main(void)
- {
- // system initialization
- SystemInit();
- SystemCoreClockUpdate();
- startJoy();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement