Advertisement
pongfactory

Keypad Demo V.3 OK

Feb 25th, 2014
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.04 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 rowPins[4] = {0,1,2,3};   // connect B0,1,2,3 to Rows 1-4 (the left four pins)
  37. int colPins[4] = {0,1,2,3};   // connect A0,1,2,3 to Column 1-4 (the right four pins)
  38. #define A_PORT GPIOA
  39. #define B_PORT GPIOB
  40. int key;
  41.  
  42. int main(void)
  43. {
  44.  
  45.     GPIO_Config();
  46.     init_USART1(9600); // initialize USART1 @ 115200 baud
  47.    // USART_puts(USART1, "Start!\r\n"); // just send a message to indicate that it works
  48.  
  49.     GPIO_WriteBit(A_PORT,GPIO_Pin_0,Bit_SET);
  50.     GPIO_WriteBit(A_PORT,GPIO_Pin_1,Bit_SET);
  51.     GPIO_WriteBit(A_PORT,GPIO_Pin_2,Bit_SET);
  52.     GPIO_WriteBit(A_PORT,GPIO_Pin_3,Bit_SET);
  53.         GPIO_WriteBit(B_PORT,GPIO_Pin_0,Bit_SET);
  54.         GPIO_WriteBit(B_PORT,GPIO_Pin_1,Bit_SET);
  55.         GPIO_WriteBit(B_PORT,GPIO_Pin_2,Bit_SET);
  56.         GPIO_WriteBit(B_PORT,GPIO_Pin_3,Bit_SET);
  57.  
  58.     USART_puts(USART1, "Start!\r\n"); // just send a message to indicate that it works
  59.     while (1)
  60.     {
  61.                 value = getKey();
  62.                 sprintf (text, "%c\r\n", value);
  63.                 /* delay */
  64.             //  for(i=0;i<0x330000;i++); // is 1 second
  65.                 USART_puts(USART1, text);
  66.  
  67.     }
  68. }
  69.  
  70. int j;
  71. int i;
  72. int getKey(void)
  73. {
  74.      int key_pressed = 0;
  75.  
  76.       for (j=0; j < ROWS; j++) { // scan the j-th row (j=0,1,2,3)
  77.         for (i=0; i < ROWS; i++) {
  78.           // output HIGH to all rows, except the j-th row
  79.             if(i==j){
  80.                  GPIO_WriteBit(B_PORT,input[i],Bit_RESET);
  81.             }else{
  82.                  GPIO_WriteBit(B_PORT,input[i],Bit_SET);
  83.             }
  84.         }
  85.         for (i=0; i < COLS; i++) {
  86.             if(GPIO_ReadInputDataBit(GPIOA,input[i]) == 0){ // Button at (R,C)=(j,i) is pressed
  87.              // wait until the button is released.
  88.              while ( GPIO_ReadInputDataBit(GPIOA,input[i]) == 0 ) ; // blocking
  89.              key_pressed = keys[j][i]; // get the associated key for that button
  90.              break;
  91.           }
  92.         }
  93.         GPIO_WriteBit(B_PORT,input[j],Bit_SET);
  94.         if ( key_pressed != 0 ) {
  95.           return key_pressed;
  96.         }
  97.       }
  98.       return 0; // no key pressed
  99.     }
  100.  
  101. void GPIO_Config(void)
  102. {
  103.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_USART1 |
  104.             RCC_APB2Periph_GPIOA, ENABLE);
  105.     //OUTPUT B = ROW
  106.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
  107.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  108.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  109.     GPIO_Init(GPIOB, &GPIO_InitStructure);
  110.  
  111.     //USART1 (PA9)
  112.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //USART1_TX
  113.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  114.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  115.     GPIO_Init(GPIOA, &GPIO_InitStructure);
  116.  
  117.     //INPUT A = COLUMN
  118.        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
  119.        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  120.        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  121.        GPIO_Init(GPIOA, &GPIO_InitStructure);
  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