Advertisement
pongfactory

Keypad Demo V.2 OK

Feb 25th, 2014
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.31 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. TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
  15.  
  16. void INTTIM_Config(void);
  17. void GPIO_Config(void);
  18. void init_USART1(uint32_t baudrate);
  19. void USART_puts(USART_TypeDef* USARTx, volatile char *s);
  20. //void init_EXTI(void);
  21. void init_NVIC(void);
  22. //void EXTI0_IRQHandler(void);
  23. void Delay(uint32_t nTime);
  24.  
  25. char text[100] = "";
  26. int data[100];
  27. int i = 0;
  28. int j = 0;
  29. int state = 0;
  30. uint32_t last_time = 0;
  31. uint32_t temp_time = 0;
  32. uint32_t temp = 0; //store data to check
  33. int value;
  34. int column;
  35. int row;
  36. // constant values
  37. const int ROWS = 4;
  38. const int COLS = 4;
  39. int keys[4][4] = {{'1','2','3','A'},{'4','5','6','B'},{'7','8','9','C'},{'*','0','#','D'}};
  40. //int keys[4][4] = {{1,2,3,3},{4,5,6,6},{7,8,9,9},{0,0,0,0}};
  41. int input[4] = {(uint16_t)0x0001,(uint16_t)0x0002,(uint16_t)0x0004,(uint16_t)0x0008};
  42. int rowPins[4] = {0,1,2,3};   // connect B0,1,2,3 to Rows 1-4 (the left four pins)
  43. int colPins[4] = {0,1,2,3};   // connect A0,1,2,3 to Column 1-4 (the right four pins)
  44. #define A_PORT GPIOA
  45. #define B_PORT GPIOB
  46. int key;
  47.  
  48.  
  49.  
  50. int main(void)
  51. {
  52.  
  53.     INTTIM_Config();
  54.     GPIO_Config();
  55.     init_USART1(9600); // initialize USART1 @ 115200 baud
  56.    // USART_puts(USART1, "Start!\r\n"); // just send a message to indicate that it works
  57.  
  58.     GPIO_WriteBit(A_PORT,GPIO_Pin_0,Bit_SET);
  59.     GPIO_WriteBit(A_PORT,GPIO_Pin_1,Bit_SET);
  60.     GPIO_WriteBit(A_PORT,GPIO_Pin_2,Bit_SET);
  61.     GPIO_WriteBit(A_PORT,GPIO_Pin_3,Bit_SET);
  62.         GPIO_WriteBit(B_PORT,GPIO_Pin_0,Bit_SET);
  63.         GPIO_WriteBit(B_PORT,GPIO_Pin_1,Bit_SET);
  64.         GPIO_WriteBit(B_PORT,GPIO_Pin_2,Bit_SET);
  65.         GPIO_WriteBit(B_PORT,GPIO_Pin_3,Bit_SET);
  66.  
  67.     /*testLED
  68.     GPIO_WriteBit(B_PORT,GPIO_Pin_8,Bit_SET);
  69.     GPIO_WriteBit(B_PORT,GPIO_Pin_9,Bit_RESET);
  70.     GPIO_WriteBit(B_PORT,GPIO_Pin_10,Bit_RESET);
  71.     GPIO_WriteBit(B_PORT,GPIO_Pin_11,Bit_SET);
  72.     */
  73.  
  74.     USART_puts(USART1, "Start!\r\n"); // just send a message to indicate that it works
  75.     while (1)
  76.     {
  77.                 value = getKey();
  78.                 sprintf (text, "%c\r\n", value);
  79.                 /* delay */
  80.             //  for(i=0;i<0x330000;i++); // is 1 second
  81.                 USART_puts(USART1, text);
  82.  
  83.     }
  84. }
  85.  
  86. int j;
  87. int i;
  88. int getKey(void)
  89. {
  90.      int key_pressed = 0;
  91.  
  92.       for (j=0; j < ROWS; j++) { // scan the j-th row (j=0,1,2,3)
  93.         for (i=0; i < ROWS; i++) {
  94.           // output HIGH to all rows, except the j-th row
  95.             if(i==j){
  96.                  GPIO_WriteBit(B_PORT,input[i],Bit_RESET);
  97.             }else{
  98.                  GPIO_WriteBit(B_PORT,input[i],Bit_SET);
  99.             }
  100.         }
  101.         for (i=0; i < COLS; i++) {
  102.             if(GPIO_ReadInputDataBit(GPIOA,input[i]) == 0){ // Button at (R,C)=(j,i) is pressed
  103.              // wait until the button is released.
  104.              while ( GPIO_ReadInputDataBit(GPIOA,input[i]) == 0 ) ; // blocking
  105.              key_pressed = keys[j][i]; // get the associated key for that button
  106.              break;
  107.           }
  108.         }
  109.         GPIO_WriteBit(B_PORT,input[j],Bit_SET);
  110.         if ( key_pressed != 0 ) {
  111.           return key_pressed;
  112.         }
  113.       }
  114.       return 0; // no key pressed
  115.     }
  116.  
  117. void INTTIM_Config(void)
  118. {
  119.     /* TIM3 clock enable */
  120.     RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
  121.     /* Time base configuration */
  122.     TIM_TimeBaseStructure.TIM_Period = 0xFFFF - 1;  // 1 MHz down to 1 KHz (1 ms)
  123.     TIM_TimeBaseStructure.TIM_Prescaler = 720 - 1; // 72 MHz Clock down to 100 kHz (adjust per your clock)
  124.     TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
  125.     TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  126.     TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
  127.     /* TIM IT enable */
  128.     TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);
  129.     /* TIM3 enable counter */
  130.     TIM_Cmd(TIM3, ENABLE);
  131. }
  132.  
  133. void GPIO_Config(void)
  134. {
  135.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_USART1 |
  136.             RCC_APB2Periph_GPIOA, ENABLE);
  137.     //OUTPUT B = ROW
  138.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
  139.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  140.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  141.     GPIO_Init(GPIOB, &GPIO_InitStructure);
  142.  
  143.     //USART1 (PA9)
  144.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //USART1_TX
  145.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  146.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  147.     GPIO_Init(GPIOA, &GPIO_InitStructure);
  148.  
  149.  
  150.  
  151.     //INPUT A = COLUMN
  152.        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
  153.        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  154.        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  155.        GPIO_Init(GPIOA, &GPIO_InitStructure);
  156. }
  157.  
  158. void init_USART1(uint32_t baudrate){
  159.     USART_InitStruct.USART_BaudRate = baudrate;  // the baudrate is set to the value we passed into this init function
  160.     USART_InitStruct.USART_WordLength = USART_WordLength_8b;  // we want the data frame size to be 8 bits (standard)
  161.     USART_InitStruct.USART_StopBits = USART_StopBits_1;  // we want 1 stop bit (standard)
  162.     USART_InitStruct.USART_Parity = USART_Parity_No;  // we don't want a parity bit (standard)
  163.     USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None; // we don't want flow control (standard)
  164.     USART_InitStruct.USART_Mode = USART_Mode_Tx;  // we want to enable the transmitter and the receiver
  165.     USART_Init(USART1, &USART_InitStruct);  // again all the properties are passed to the USART_Init function which takes care of all the bit setting
  166.  
  167.     USART_ITConfig(USART1, USART_IT_RXNE, DISABLE);             // enable the USART1 receive interrupt
  168.     USART_Cmd(USART1, ENABLE);
  169. }
  170.  
  171. void USART_puts(USART_TypeDef* USARTx, volatile char *s){
  172.     while(*s){
  173.         // wait until data register is empty
  174.         while( !(USARTx->SR & 0x00000040) );
  175.         USART_SendData(USARTx, *s);
  176.         *s++;
  177.     }
  178. }
  179.  
  180. static __IO uint32_t TimingDelay;
  181.  
  182. void Delay(uint32_t nTime){
  183.     TimingDelay = nTime;
  184.     while(TimingDelay != 0);
  185. }
  186. void SysTick_Handler(void){
  187.     if (TimingDelay != 0x00)
  188.         TimingDelay--;
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement