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 first = 1; //is this first time?
- int ROWS = 4; // Four rows
- int COLS = 4; // Three columns
- // Define the Keymap
- char keys[4][4] = {{'1','2','3','A'},{'4','5','6','B'},{'7','8','9','C'},{'*','0','#','D'}};
- int debounceTime = 10;
- int rowPins[4] = { 0, 1, 2, 3 }; //set all High //B
- int colPins[4] = { 0, 1, 2, 3 }; //A
- #define A_PORT GPIOA
- #define B_PORT GPIOB
- int main(void)
- {
- INTTIM_Config();
- GPIO_Config();
- init_USART1(115200); // 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);
- while (1)
- {
- USART_puts(USART1, "Start!\r\n"); // just send a message to indicate that it works
- // char key = getKey();
- // sprintf (text, "%s\r\n", key);
- // USART_puts(USART1, text);
- //test LED
- 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);
- }
- }
- void getKey(void)
- {
- // char temp;
- char key = 0; // 0 indicates no key pressed
- int column;
- int row;
- for(column = 0; column < 4; column++)
- {
- //** digitalWrite(colPins[column],LOW); // Activate the current column.
- sprintf (temp, "GPIO_Pin_%d\r\n", column);
- GPIO_WriteBit(A_PORT,temp,Bit_RESET);
- for(row = 0; row < 4; row++) // Scan all rows for
- // a key press.
- {
- sprintf (temp, "GPIO_Pin_%d\r\n", row);
- //** if(digitalRead(rowPins[row]) == LOW) // Is a key pressed?
- if( GPIO_ReadInputDataBit(GPIOB,temp) == 0 )
- {
- Delay(debounceTime); // debounce
- //** while(digitalRead(rowPins[row]) == LOW)
- while(GPIO_ReadInputDataBit(GPIOB,temp) == 0)
- ; // wait for key to be released
- key = keys[row][column]; // Remember which key
- // was pressed.
- }
- }
- //** digitalWrite(colPins[column],HIGH); // De-activate the current column.
- }
- return key; // returns the key pressed or 0 if none
- }
- /*
- //if( GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_4) != 0 ) >> CHECKREAD
- void getKey(void) {
- char key_pressed = 0;
- int i;
- int j;
- //byte rowPins[ROWS] = { B0, B1, B2, B3 }; //set all High
- //byte colPins[COLS] = { A0, A1, A2,A3 };
- ///*if ((GPIOC->IDR & GPIO_Pin_13) == (uint32_t)0){
- for (j=0; j < 4; j++) { // scan the j-th row (j=0,1,2,3)
- for (i=0; i < 4; i++) {
- // output HIGH to all rows, except the j-th row
- // digitalWrite( rowPins[i], (i==j) ? LOW : HIGH );
- char index1 = "GPIO_Pin_%d"+i;
- if(i==j){
- GPIO_WriteBit(B_PORT,index1,Bit_RESET);
- }else{
- GPIO_WriteBit(B_PORT,index1,Bit_SET);
- }
- }
- for (i=0; i < 4; i++) {
- // if ( digitalRead( colPins[i] ) == LOW ) { // Button at (R,C)=(j,i) is pressed
- char index2 = "GPIO_Pin_%d"+i;
- if ((GPIOA->IDR & index2) == (uint32_t)0){
- // wait until the button is released.
- // while ( digitalRead( colPins[i] ) != HIGH ) ; // blocking
- while ((GPIOA->IDR & index2) != (uint32_t)1 );
- key_pressed = keys[j][i]; // get the associated key for that button
- break;
- }
- }
- // digitalWrite( rowPins[j], HIGH );
- char index3 = "GPIO_Pin_%d"+j;
- GPIO_WriteBit(B_PORT,index3,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
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
- 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);
- //EXTI0 (PA0)
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- 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