Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stm32f10x.h"
- #include "stm32f10x_tim.h"
- #include "stm32f10x_rcc.h"
- #include "stm32f10x_gpio.h"
- #include "stm32f10x_usart.h"
- #include "misc.h"
- #include "stm32f10x_exti.h"
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- USART_InitTypeDef USART_InitStruct; // this is for the USART1 initilization
- GPIO_InitTypeDef GPIO_InitStructure;
- void GPIO_Config(void);
- void init_USART1(uint32_t baudrate);
- void USART_puts(USART_TypeDef* USARTx, volatile char *s);
- char text[100] = "";
- int data[100];
- int i = 0;
- int j = 0;
- int state = 0;
- uint32_t last_time = 0;
- uint32_t temp_time = 0;
- uint32_t temp = 0; //store data to check
- int value;
- int column;
- int row;
- // constant values
- const int ROWS = 4;
- const int COLS = 4;
- int keys[4][4] = {{'1','2','3','A'},{'4','5','6','B'},{'7','8','9','C'},{'*','0','#','D'}};
- //int keys[4][4] = {{1,2,3,3},{4,5,6,6},{7,8,9,9},{0,0,0,0}};
- //int input[4] = {(uint16_t)0x0001,(uint16_t)0x0002,(uint16_t)0x0004,(uint16_t)0x0008};
- int input[4] = {((uint16_t)0x0010),((uint16_t)0x0020),((uint16_t)0x0040),((uint16_t)0x0080)};
- int output[4] = {((uint16_t)0x0004),((uint16_t)0x0020),((uint16_t)0x0040),((uint16_t)0x0080)};
- int rowPins[4] = {0,1,2,3}; // connect B0,1,2,3 to Rows 1-4 (the left four pins)
- int colPins[4] = {0,1,2,3}; // connect A0,1,2,3 to Column 1-4 (the right four pins)
- #define A_PORT GPIOA
- #define B_PORT GPIOB
- int key;
- int main(void)
- {
- GPIO_Config();
- init_USART1(9600); // initialize USART1 @ 115200 baud
- // USART_puts(USART1, "Start!\r\n"); // just send a message to indicate that it works
- GPIO_WriteBit(B_PORT,GPIO_Pin_2,Bit_SET);
- GPIO_WriteBit(B_PORT,GPIO_Pin_5,Bit_SET);
- GPIO_WriteBit(B_PORT,GPIO_Pin_6,Bit_SET);
- GPIO_WriteBit(B_PORT,GPIO_Pin_7,Bit_SET);
- USART_puts(USART1, "Start!\r\n"); // just send a message to indicate that it works
- while (1)
- {
- value = getKey();
- sprintf (text, "%c\r\n", value);
- /* delay */
- // for(i=0;i<0x330000;i++); // is 1 second
- USART_puts(USART1, text);
- }
- }
- int j;
- int i;
- int getKey(void)
- {
- int key_pressed = 0;
- for (j=0; j < ROWS; j++) { // scan the j-th row (j=0,1,2,3)
- for (i=0; i < ROWS; i++) {
- // output HIGH to all rows, except the j-th row
- if(i==j){
- GPIO_WriteBit(B_PORT,output[i],Bit_RESET);
- }else{
- GPIO_WriteBit(B_PORT,output[i],Bit_SET);
- }
- }
- for (i=0; i < COLS; i++) {
- if(GPIO_ReadInputDataBit(GPIOA,input[i]) == 0){ // Button at (R,C)=(j,i) is pressed
- // wait until the button is released.
- while ( GPIO_ReadInputDataBit(GPIOA,input[i]) == 0 ) ; // blocking
- key_pressed = keys[j][i]; // get the associated key for that button
- break;
- }
- }
- GPIO_WriteBit(B_PORT,output[j],Bit_SET);
- if ( key_pressed != 0 ) {
- return key_pressed;
- }
- }
- return 0; // no key pressed
- }
- void GPIO_Config(void)
- {
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_USART1 |
- RCC_APB2Periph_GPIOA, ENABLE);
- //INPUT A = COLUMN
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- //OUTPUT B = ROW
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- //USART1 (PA9)
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //USART1_TX
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- }
- void init_USART1(uint32_t baudrate){
- USART_InitStruct.USART_BaudRate = baudrate; // the baudrate is set to the value we passed into this init function
- USART_InitStruct.USART_WordLength = USART_WordLength_8b; // we want the data frame size to be 8 bits (standard)
- USART_InitStruct.USART_StopBits = USART_StopBits_1; // we want 1 stop bit (standard)
- USART_InitStruct.USART_Parity = USART_Parity_No; // we don't want a parity bit (standard)
- USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None; // we don't want flow control (standard)
- USART_InitStruct.USART_Mode = USART_Mode_Tx; // we want to enable the transmitter and the receiver
- USART_Init(USART1, &USART_InitStruct); // again all the properties are passed to the USART_Init function which takes care of all the bit setting
- USART_ITConfig(USART1, USART_IT_RXNE, DISABLE); // enable the USART1 receive interrupt
- USART_Cmd(USART1, ENABLE);
- }
- void USART_puts(USART_TypeDef* USARTx, volatile char *s){
- while(*s){
- // wait until data register is empty
- while( !(USARTx->SR & 0x00000040) );
- USART_SendData(USARTx, *s);
- *s++;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement