Advertisement
pongfactory

Keypad Demo V.4 OK All KEY

Feb 25th, 2014
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.06 KB | None | 0 0
  1. #include "stm32f10x.h"
  2. #include "stm32f10x_tim.h"
  3. #include "stm32f10x_rcc.h"
  4. #include "stm32f10x_gpio.h"
  5. #include "stm32f10x_usart.h"
  6. #include "misc.h"
  7. #include "stm32f10x_exti.h"
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11.  
  12. USART_InitTypeDef USART_InitStruct; // this is for the USART1 initilization
  13. GPIO_InitTypeDef  GPIO_InitStructure;
  14.  
  15. void GPIO_Config(void);
  16. void init_USART1(uint32_t baudrate);
  17. void USART_puts(USART_TypeDef* USARTx, volatile char *s);
  18.  
  19. char text[100] = "";
  20. int data[100];
  21. int i = 0;
  22. int j = 0;
  23. int state = 0;
  24. uint32_t last_time = 0;
  25. uint32_t temp_time = 0;
  26. uint32_t temp = 0; //store data to check
  27. int value;
  28. int column;
  29. int row;
  30. // constant values
  31. const int ROWS = 4;
  32. const int COLS = 4;
  33. int keys[4][4] = {{'1','2','3','A'},{'4','5','6','B'},{'7','8','9','C'},{'*','0','#','D'}};
  34. //int keys[4][4] = {{1,2,3,3},{4,5,6,6},{7,8,9,9},{0,0,0,0}};
  35. //int input[4] = {(uint16_t)0x0001,(uint16_t)0x0002,(uint16_t)0x0004,(uint16_t)0x0008};
  36. int input[4] = {((uint16_t)0x0010),((uint16_t)0x0020),((uint16_t)0x0040),((uint16_t)0x0080)};
  37. int output[4] = {((uint16_t)0x0004),((uint16_t)0x0020),((uint16_t)0x0040),((uint16_t)0x0080)};
  38. int rowPins[4] = {0,1,2,3};   // connect B0,1,2,3 to Rows 1-4 (the left four pins)
  39. int colPins[4] = {0,1,2,3};   // connect A0,1,2,3 to Column 1-4 (the right four pins)
  40. #define A_PORT GPIOA
  41. #define B_PORT GPIOB
  42. int key;
  43.  
  44. int main(void)
  45. {
  46.  
  47.     GPIO_Config();
  48.     init_USART1(9600); // initialize USART1 @ 115200 baud
  49.    // USART_puts(USART1, "Start!\r\n"); // just send a message to indicate that it works
  50.  
  51.         GPIO_WriteBit(B_PORT,GPIO_Pin_2,Bit_SET);
  52.         GPIO_WriteBit(B_PORT,GPIO_Pin_5,Bit_SET);
  53.         GPIO_WriteBit(B_PORT,GPIO_Pin_6,Bit_SET);
  54.         GPIO_WriteBit(B_PORT,GPIO_Pin_7,Bit_SET);
  55.  
  56.     USART_puts(USART1, "Start!\r\n"); // just send a message to indicate that it works
  57.     while (1)
  58.     {
  59.                 value = getKey();
  60.                 sprintf (text, "%c\r\n", value);
  61.                 /* delay */
  62.             //  for(i=0;i<0x330000;i++); // is 1 second
  63.                 USART_puts(USART1, text);
  64.  
  65.     }
  66. }
  67.  
  68. int j;
  69. int i;
  70. int getKey(void)
  71. {
  72.      int key_pressed = 0;
  73.  
  74.       for (j=0; j < ROWS; j++) { // scan the j-th row (j=0,1,2,3)
  75.         for (i=0; i < ROWS; i++) {
  76.           // output HIGH to all rows, except the j-th row
  77.             if(i==j){
  78.                  GPIO_WriteBit(B_PORT,output[i],Bit_RESET);
  79.             }else{
  80.                  GPIO_WriteBit(B_PORT,output[i],Bit_SET);
  81.             }
  82.         }
  83.         for (i=0; i < COLS; i++) {
  84.             if(GPIO_ReadInputDataBit(GPIOA,input[i]) == 0){ // Button at (R,C)=(j,i) is pressed
  85.              // wait until the button is released.
  86.              while ( GPIO_ReadInputDataBit(GPIOA,input[i]) == 0 ) ; // blocking
  87.              key_pressed = keys[j][i]; // get the associated key for that button
  88.              break;
  89.           }
  90.         }
  91.         GPIO_WriteBit(B_PORT,output[j],Bit_SET);
  92.         if ( key_pressed != 0 ) {
  93.           return key_pressed;
  94.         }
  95.       }
  96.       return 0; // no key pressed
  97.     }
  98.  
  99. void GPIO_Config(void)
  100. {
  101.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_USART1 |
  102.             RCC_APB2Periph_GPIOA, ENABLE);
  103.     //INPUT A = COLUMN
  104.           GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
  105.           GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  106.           GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  107.           GPIO_Init(GPIOA, &GPIO_InitStructure);
  108.  
  109.     //OUTPUT B = ROW
  110.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
  111.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  112.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  113.     GPIO_Init(GPIOB, &GPIO_InitStructure);
  114.  
  115.     //USART1 (PA9)
  116.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //USART1_TX
  117.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  118.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  119.     GPIO_Init(GPIOA, &GPIO_InitStructure);
  120.  
  121.  
  122. }
  123.  
  124. void init_USART1(uint32_t baudrate){
  125.     USART_InitStruct.USART_BaudRate = baudrate;  // the baudrate is set to the value we passed into this init function
  126.     USART_InitStruct.USART_WordLength = USART_WordLength_8b;  // we want the data frame size to be 8 bits (standard)
  127.     USART_InitStruct.USART_StopBits = USART_StopBits_1;  // we want 1 stop bit (standard)
  128.     USART_InitStruct.USART_Parity = USART_Parity_No;  // we don't want a parity bit (standard)
  129.     USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None; // we don't want flow control (standard)
  130.     USART_InitStruct.USART_Mode = USART_Mode_Tx;  // we want to enable the transmitter and the receiver
  131.     USART_Init(USART1, &USART_InitStruct);  // again all the properties are passed to the USART_Init function which takes care of all the bit setting
  132.  
  133.     USART_ITConfig(USART1, USART_IT_RXNE, DISABLE);             // enable the USART1 receive interrupt
  134.     USART_Cmd(USART1, ENABLE);
  135. }
  136.  
  137. void USART_puts(USART_TypeDef* USARTx, volatile char *s){
  138.     while(*s){
  139.         // wait until data register is empty
  140.         while( !(USARTx->SR & 0x00000040) );
  141.         USART_SendData(USARTx, *s);
  142.         *s++;
  143.     }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement