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;
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- void INTTIM_Config(void);
- void GPIO_Config(void);
- void init_USART1(uint32_t baudrate);
- void USART_puts(USART_TypeDef* USARTx, volatile char *s);
- //void init_EXTI(void);
- void init_NVIC(void);
- //void EXTI0_IRQHandler(void);
- void Delay(uint32_t nTime);
- 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 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)
- {
- INTTIM_Config();
- 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(A_PORT,GPIO_Pin_0,Bit_SET);
- GPIO_WriteBit(A_PORT,GPIO_Pin_1,Bit_SET);
- GPIO_WriteBit(A_PORT,GPIO_Pin_2,Bit_SET);
- GPIO_WriteBit(A_PORT,GPIO_Pin_3,Bit_SET);
- GPIO_WriteBit(B_PORT,GPIO_Pin_0,Bit_SET);
- GPIO_WriteBit(B_PORT,GPIO_Pin_1,Bit_SET);
- GPIO_WriteBit(B_PORT,GPIO_Pin_2,Bit_SET);
- GPIO_WriteBit(B_PORT,GPIO_Pin_3,Bit_SET);
- /*testLED
- GPIO_WriteBit(B_PORT,GPIO_Pin_8,Bit_SET);
- GPIO_WriteBit(B_PORT,GPIO_Pin_9,Bit_RESET);
- GPIO_WriteBit(B_PORT,GPIO_Pin_10,Bit_RESET);
- GPIO_WriteBit(B_PORT,GPIO_Pin_11,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,input[i],Bit_RESET);
- }else{
- GPIO_WriteBit(B_PORT,input[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,input[j],Bit_SET);
- if ( key_pressed != 0 ) {
- return key_pressed;
- }
- }
- return 0; // no key pressed
- }
- void INTTIM_Config(void)
- {
- /* TIM3 clock enable */
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
- /* Time base configuration */
- TIM_TimeBaseStructure.TIM_Period = 0xFFFF - 1; // 1 MHz down to 1 KHz (1 ms)
- TIM_TimeBaseStructure.TIM_Prescaler = 720 - 1; // 72 MHz Clock down to 100 kHz (adjust per your clock)
- TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
- TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
- /* TIM IT enable */
- TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);
- /* TIM3 enable counter */
- TIM_Cmd(TIM3, ENABLE);
- }
- void GPIO_Config(void)
- {
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_USART1 |
- RCC_APB2Periph_GPIOA, ENABLE);
- //OUTPUT B = ROW
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
- 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);
- //INPUT A = COLUMN
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
- 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++;
- }
- }
- static __IO uint32_t TimingDelay;
- void Delay(uint32_t nTime){
- TimingDelay = nTime;
- while(TimingDelay != 0);
- }
- void SysTick_Handler(void){
- if (TimingDelay != 0x00)
- TimingDelay--;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement