Advertisement
pongfactory

Keypad Demo 1

Feb 23rd, 2014
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.63 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 first = 1; //is this first time?
  34.  
  35. int ROWS = 4; // Four rows
  36. int COLS = 4; // Three columns
  37. // Define the Keymap
  38. char keys[4][4] = {{'1','2','3','A'},{'4','5','6','B'},{'7','8','9','C'},{'*','0','#','D'}};
  39. int debounceTime = 10;
  40.  
  41. int rowPins[4] = { 0, 1, 2, 3 }; //set all High //B
  42. int colPins[4] = { 0, 1, 2, 3 };  //A
  43. #define A_PORT GPIOA
  44. #define B_PORT GPIOB
  45.  
  46.  
  47.  
  48. int main(void)
  49. {
  50.  
  51.     INTTIM_Config();
  52.     GPIO_Config();
  53.     init_USART1(115200); // initialize USART1 @ 115200 baud
  54.     USART_puts(USART1, "Start!\r\n"); // just send a message to indicate that it works
  55.     GPIO_WriteBit(A_PORT,GPIO_Pin_0,Bit_SET);
  56.     GPIO_WriteBit(A_PORT,GPIO_Pin_1,Bit_SET);
  57.     GPIO_WriteBit(A_PORT,GPIO_Pin_2,Bit_SET);
  58.     GPIO_WriteBit(A_PORT,GPIO_Pin_3,Bit_SET);
  59.     GPIO_WriteBit(B_PORT,GPIO_Pin_0,Bit_SET);
  60.     GPIO_WriteBit(B_PORT,GPIO_Pin_1,Bit_SET);
  61.     GPIO_WriteBit(B_PORT,GPIO_Pin_2,Bit_SET);
  62.     GPIO_WriteBit(B_PORT,GPIO_Pin_3,Bit_SET);
  63.  
  64.  
  65.  
  66.     while (1)
  67.     {
  68.          USART_puts(USART1, "Start!\r\n"); // just send a message to indicate that it works
  69.     //  char key = getKey();
  70.     //  sprintf (text, "%s\r\n", key);
  71.     //    USART_puts(USART1, text);
  72.  
  73.         //test LED
  74.                 GPIO_WriteBit(B_PORT,GPIO_Pin_8,Bit_SET);
  75.                 GPIO_WriteBit(B_PORT,GPIO_Pin_9,Bit_RESET);
  76.                 GPIO_WriteBit(B_PORT,GPIO_Pin_10,Bit_RESET);
  77.                 GPIO_WriteBit(B_PORT,GPIO_Pin_11,Bit_SET);
  78.  
  79.     }
  80. }
  81.  
  82. void getKey(void)
  83. {
  84.  // char temp;
  85.   char key = 0;                                  // 0 indicates no key pressed
  86.   int column;
  87.   int row;
  88.  
  89.   for(column = 0; column < 4; column++)
  90.   {
  91.   //**  digitalWrite(colPins[column],LOW);         // Activate the current column.
  92.  
  93.       sprintf (temp, "GPIO_Pin_%d\r\n", column);
  94.       GPIO_WriteBit(A_PORT,temp,Bit_RESET);
  95.  
  96.     for(row = 0; row < 4; row++)     // Scan all rows for
  97.                                                // a key press.
  98.     {
  99.         sprintf (temp, "GPIO_Pin_%d\r\n", row);
  100.   //**    if(digitalRead(rowPins[row]) == LOW)     // Is a key pressed?
  101.         if( GPIO_ReadInputDataBit(GPIOB,temp) == 0 )
  102.       {
  103.         Delay(debounceTime);                   // debounce
  104.   //**      while(digitalRead(rowPins[row]) == LOW)
  105.             while(GPIO_ReadInputDataBit(GPIOB,temp) == 0)
  106.             ;                                  // wait for key to be released
  107.         key = keys[row][column];             // Remember which key
  108.                                                // was pressed.
  109.       }
  110.     }
  111.   //** digitalWrite(colPins[column],HIGH);     // De-activate the current column.
  112.   }
  113.   return key;  // returns the key pressed or 0 if none
  114. }
  115.  
  116.  
  117.  
  118. /*
  119. //if( GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_4) != 0 )  >> CHECKREAD
  120. void getKey(void) {
  121.   char key_pressed = 0;
  122.   int i;
  123.   int j;
  124.   //byte rowPins[ROWS] = { B0, B1, B2, B3 }; //set all High
  125.   //byte colPins[COLS] = { A0, A1, A2,A3 };
  126.   ///*if ((GPIOC->IDR & GPIO_Pin_13) == (uint32_t)0){
  127.   for (j=0; j < 4; j++) { // scan the j-th row (j=0,1,2,3)
  128.     for (i=0; i < 4; i++) {
  129.       // output HIGH to all rows, except the j-th row
  130.      // digitalWrite( rowPins[i], (i==j) ? LOW : HIGH );
  131.         char index1 = "GPIO_Pin_%d"+i;
  132.         if(i==j){
  133.  
  134.               GPIO_WriteBit(B_PORT,index1,Bit_RESET);
  135.         }else{
  136.               GPIO_WriteBit(B_PORT,index1,Bit_SET);
  137.         }
  138.     }
  139.     for (i=0; i < 4; i++) {
  140.    //   if ( digitalRead( colPins[i] ) == LOW ) { // Button at (R,C)=(j,i) is pressed
  141.          char index2 = "GPIO_Pin_%d"+i;
  142.         if ((GPIOA->IDR & index2) == (uint32_t)0){
  143.  
  144.          // wait until the button is released.
  145.       //   while ( digitalRead( colPins[i] ) != HIGH ) ; // blocking
  146.  
  147.            while ((GPIOA->IDR & index2) != (uint32_t)1  );
  148.  
  149.          key_pressed = keys[j][i]; // get the associated key for that button
  150.          break;
  151.       }
  152.     }
  153.    // digitalWrite( rowPins[j], HIGH );
  154.     char index3 = "GPIO_Pin_%d"+j;
  155.     GPIO_WriteBit(B_PORT,index3,Bit_SET);
  156.  
  157.  
  158.  
  159.     if ( key_pressed != 0 ) {
  160.       return key_pressed;
  161.     }
  162.   }
  163.   return 0; // no key pressed
  164. }
  165. */
  166.  
  167. void INTTIM_Config(void)
  168. {
  169.     /* TIM3 clock enable */
  170.     RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
  171.     /* Time base configuration */
  172.     TIM_TimeBaseStructure.TIM_Period = 0xFFFF - 1;  // 1 MHz down to 1 KHz (1 ms)
  173.     TIM_TimeBaseStructure.TIM_Prescaler = 720 - 1; // 72 MHz Clock down to 100 kHz (adjust per your clock)
  174.     TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
  175.     TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  176.     TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
  177.     /* TIM IT enable */
  178.     TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);
  179.     /* TIM3 enable counter */
  180.     TIM_Cmd(TIM3, ENABLE);
  181. }
  182.  
  183. void GPIO_Config(void)
  184. {
  185.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_USART1 |
  186.             RCC_APB2Periph_GPIOA, ENABLE);
  187.     //Output B
  188.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
  189.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  190.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  191.     GPIO_Init(GPIOB, &GPIO_InitStructure);
  192.  
  193.     //USART1 (PA9)
  194.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //USART1_TX
  195.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  196.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  197.     GPIO_Init(GPIOA, &GPIO_InitStructure);
  198.  
  199.     //EXTI0 (PA0)
  200.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
  201.     GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_IN_FLOATING;
  202.     GPIO_Init(GPIOA, &GPIO_InitStructure);
  203. }
  204.  
  205. void init_USART1(uint32_t baudrate){
  206.     USART_InitStruct.USART_BaudRate = baudrate;  // the baudrate is set to the value we passed into this init function
  207.     USART_InitStruct.USART_WordLength = USART_WordLength_8b;  // we want the data frame size to be 8 bits (standard)
  208.     USART_InitStruct.USART_StopBits = USART_StopBits_1;  // we want 1 stop bit (standard)
  209.     USART_InitStruct.USART_Parity = USART_Parity_No;  // we don't want a parity bit (standard)
  210.     USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None; // we don't want flow control (standard)
  211.     USART_InitStruct.USART_Mode = USART_Mode_Tx;  // we want to enable the transmitter and the receiver
  212.     USART_Init(USART1, &USART_InitStruct);  // again all the properties are passed to the USART_Init function which takes care of all the bit setting
  213.  
  214.     USART_ITConfig(USART1, USART_IT_RXNE, DISABLE);             // enable the USART1 receive interrupt
  215.     USART_Cmd(USART1, ENABLE);
  216. }
  217.  
  218. void USART_puts(USART_TypeDef* USARTx, volatile char *s){
  219.     while(*s){
  220.         // wait until data register is empty
  221.         while( !(USARTx->SR & 0x00000040) );
  222.         USART_SendData(USARTx, *s);
  223.         *s++;
  224.     }
  225. }
  226.  
  227. static __IO uint32_t TimingDelay;
  228.  
  229. void Delay(uint32_t nTime){
  230.     TimingDelay = nTime;
  231.     while(TimingDelay != 0);
  232. }
  233. void SysTick_Handler(void){
  234.     if (TimingDelay != 0x00)
  235.         TimingDelay--;
  236. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement