Advertisement
Guest User

Mikro - projekt2

a guest
Jan 29th, 2015
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.74 KB | None | 0 0
  1. ==========================
  2. joy.c
  3. ==========================
  4. #include "joy.h"
  5. #include "lpc17xx_clkpwr.h"
  6. #define SPEED_Y 48
  7. #define SPEED_X (SPEED_Y * 2)
  8.  
  9.  
  10. void UART_Printer(uint8_t text[], LPC_UART_TypeDef *UARTx);
  11. void UART_Initialize(LPC_UART_TypeDef *UARTx);
  12. static void handleJoy(void);
  13.  
  14. void startJoy(void)
  15. {
  16.         // GPIO power
  17.         CLKPWR_ConfigPPWR(CLKPWR_PCONP_PCGPIO, ENABLE);
  18.  
  19.         GPIO_SetDir(KEY_PORT1, KEY_SELECT, 0);
  20.         GPIO_SetDir(KEY_PORT1, KEY_DOWN, 0);
  21.         GPIO_SetDir(KEY_PORT1, KEY_LEFT, 0);
  22.         GPIO_SetDir(KEY_PORT1, KEY_RIGHT, 0);
  23.         GPIO_SetDir(KEY_PORT1, KEY_DOWN, 0);
  24.  
  25.         UART_Initialize((LPC_UART_TypeDef *)LPC_UART2);
  26.  
  27.         while (1) {
  28.             handleJoy();
  29.         }
  30. }
  31.  
  32. static void handleJoy(void)
  33. {
  34.         uint32_t KeyValue;
  35.         KeyValue = GPIO_ReadValue(KEY_PORT1);
  36.  
  37.         int key_left_c, key_right_c, key_up_c, key_down_c;
  38.         key_down_c = key_up_c = key_right_c = key_left_c = 0;
  39.  
  40.         if ((KeyValue & KEY_LEFT) == 0) {
  41.             key_left_c = 1;
  42.         } else if((KeyValue & KEY_RIGHT) == 0){
  43.             key_right_c = 1;
  44.         } else if((KeyValue & KEY_UP) == 0){
  45.             key_up_c = 1;
  46.         } else if(((KeyValue & KEY_DOWN) == 0)){
  47.             key_down_c = 1;
  48.         }
  49.  
  50.         static int x,y;
  51.         static char buffer[10];
  52.  
  53.         if(key_down_c){
  54.             x++;
  55.             sprintf(buffer,"\033[%d;%df",x/SPEED_X,y/SPEED_Y);
  56.             UART_putstring(buffer,(LPC_UART_TypeDef *)LPC_UART2);
  57.         } else if(key_up_c){
  58.             if(x > 0) x--;
  59.             sprintf(buffer,"\033[%d;%df",x/SPEED_X,y/SPEED_Y);
  60.             UART_putstring(buffer,(LPC_UART_TypeDef *)LPC_UART2);
  61.         } else if(key_right_c){
  62.             y++;
  63.             sprintf(buffer,"\033[%d;%df",x/SPEED_X,y/SPEED_Y);
  64.             UART_putstring(buffer,(LPC_UART_TypeDef *)LPC_UART2);
  65.         } else if(key_left_c){
  66.             if(y > 0) y--;
  67.             sprintf(buffer,"\033[%d;%df",x/SPEED_X,y/SPEED_Y);
  68.             UART_putstring(buffer,(LPC_UART_TypeDef *)LPC_UART2);
  69.         }
  70. }
  71. ==========================
  72. joy.h
  73. ==========================
  74. #ifndef __JOY_H__
  75. #define __JOY_H__
  76.  
  77. #include "lpc17xx_gpio.h"
  78.  
  79. /* define KEY PORT1 */
  80. #define KEY_PORT1       (1)
  81. #define KEY_SELECT _BIT(25)
  82. #define KEY_DOWN _BIT(26)
  83. #define KEY_LEFT _BIT(27)
  84. #define KEY_RIGHT _BIT(28)
  85. #define KEY_UP _BIT(29)
  86.  
  87.  
  88. #endif /* __JOY_H__ */
  89. ==========================
  90. UART_functions.c
  91. ==========================
  92.  
  93. #include "lpc17xx_uart.h"
  94. #include "lpc17xx_pinsel.h"
  95.  
  96. void UART_Initialize(LPC_UART_TypeDef *UARTx){
  97.  
  98.     /* UART Configuration structure variable */
  99.     UART_CFG_Type UARTConfigStruct;
  100.     /* UART FIFO configuration Struct variable */
  101.     UART_FIFO_CFG_Type UARTFIFOConfigStruct;
  102.     /* Pin configuration for UART0 */
  103.     PINSEL_CFG_Type PinCfg;
  104.  
  105.     if((uint32_t)UARTx == (uint32_t)LPC_UART0) {
  106.         /*
  107.          * Initialize UART0 pin connect
  108.          */
  109.         PinCfg.Funcnum = 1;
  110.         PinCfg.OpenDrain = 0;
  111.         PinCfg.Pinmode = 2;
  112.         PinCfg.Pinnum = 2;
  113.         PinCfg.Portnum = 0;
  114.         PINSEL_ConfigPin(&PinCfg);
  115.         PinCfg.Pinnum = 3;
  116.         PINSEL_ConfigPin(&PinCfg);
  117.     }
  118.     else if ((uint32_t)UARTx == (uint32_t)LPC_UART2) {
  119.         /*
  120.          * Initialize UART2 pin connect
  121.          */
  122.         PinCfg.Funcnum = 1;
  123.         PinCfg.OpenDrain = 0;
  124.         PinCfg.Pinmode = 2;
  125.         PinCfg.Pinnum = 10;
  126.         PinCfg.Portnum = 0;
  127.         PINSEL_ConfigPin(&PinCfg);
  128.         PinCfg.Pinnum = 11;
  129.         PINSEL_ConfigPin(&PinCfg);
  130.     }
  131.  
  132.     /* Initialize UART Configuration parameter structure to default state:
  133.      * Baudrate = 9600bps
  134.      * 8 data bit
  135.      * 1 Stop bit
  136.      * None parity
  137.      */
  138.     UART_ConfigStructInit(&UARTConfigStruct);
  139.  
  140.     /* Set Baudrate to 115200 */
  141.     UARTConfigStruct.Baud_rate = 115200;
  142.  
  143.     /* Initialize UART0 peripheral with given to corresponding parameter */
  144.     UART_Init(UARTx, &UARTConfigStruct);
  145.  
  146.     /* Initialize FIFOConfigStruct to default state:
  147.      *              - FIFO_DMAMode = DISABLE
  148.      *              - FIFO_Level = UART_FIFO_TRGLEV0
  149.      *              - FIFO_ResetRxBuf = ENABLE
  150.      *              - FIFO_ResetTxBuf = ENABLE
  151.      *              - FIFO_State = ENABLE
  152.      */
  153.     UART_FIFOConfigStructInit(&UARTFIFOConfigStruct);
  154.  
  155.     /* Initialize FIFO for UART0 peripheral */
  156.     UART_FIFOConfig(UARTx, &UARTFIFOConfigStruct);
  157.  
  158.     /*  Enable UART Transmit */
  159.     UART_TxCmd(UARTx, ENABLE);
  160. }
  161.  
  162. void UART_putchar(char c,LPC_UART_TypeDef *UARTx){
  163.     while ((UARTx->LSR & 0x20) == 0){
  164.         // do nothing
  165.     }
  166.     //write character
  167.     UARTx->THR = c;
  168. }
  169.  
  170. void UART_putstring(const char *str, LPC_UART_TypeDef *UARTx){
  171.     while(*str){
  172.         UART_putchar(*str++,UARTx);
  173.     }
  174. }
  175.  
  176. void UART_Printer(uint8_t text[], LPC_UART_TypeDef *UARTx){
  177.     /* print a string */
  178.     UART_Send(UARTx, text, sizeof(text), NONE_BLOCKING);
  179. }
  180. ==========================
  181. main.c
  182. ==========================
  183. #include "lpc17xx_uart.h"
  184. #include "lpc17xx_pinsel.h"
  185.  
  186. void startJoy();
  187.  
  188. int main(void)
  189. {
  190.     // system initialization
  191.     SystemInit();
  192.     SystemCoreClockUpdate();
  193.  
  194.     startJoy();
  195.     return 0;
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement