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>
- #include <stdint.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);
- #define EN GPIO_Pin_10
- #define RS GPIO_Pin_12
- #define RW GPIO_Pin_11
- const unsigned int SWAP_DATA[16] = { 0x0, 0x8, 0x4, 0xC, 0x2, 0xA, 0x6, 0xE,
- 0x1, 0x9, 0x5, 0xD, 0x3, 0xB, 0x7, 0xF};
- const char UserFont[8][8] = { /* 8 user defined characters to be loaded into CGRAM (used for bargraph)*/
- { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
- { 0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10 },
- { 0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18 },
- { 0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C },
- { 0x1E,0x1E,0x1E,0x1E,0x1E,0x1E,0x1E,0x1E },
- { 0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F },
- { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
- { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }
- };
- /***************************************************************************//**
- * Declare function prototypes
- ******************************************************************************/
- void RCC_Configuration(void);
- void LCD_DATA_DIR_OUT(void);
- void LCD_DATA_DIR_IN(void);
- void LCD_ALL_DIR_OUT(void);
- unsigned int LCD_DATA_IN(void);
- static unsigned char Wait_While_Busy(void);
- static unsigned char Lcd_Read_Status (void);
- void Lcd_Write_4bits(uc8 byte);
- void Delay(vu32 nCount);
- void Lcd_Write_Command(uc8 command);
- void Lcd_Write_Data(uc8 data);
- void Lcd_Init(void);
- void Lcd_Write_Line1(void);
- void Lcd_Write_Line2(void);
- void set_cursor(int, int);
- void lcd_print (char *string);
- void lcd_clear (void);
- 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;
- char valueS[100] = "";
- int tempValue;
- int column;
- int row;
- int loop1 = 0;
- // 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 loopLCD;
- void GPIO_LCD1602(void);
- int main(void)
- {
- char ans[4] = {'1','2','3','A'};
- char ansNumber[] = "1333";
- //char ansLCD[] = {"1745","1746","1747"};
- char* strAns[3];
- strAns[2] = "Set : ";
- char* strings[5];
- strings[0] = "Real ";
- strings[1] = "Time";
- strings[2] = "ByIC";
- strings[3] = "DS1307";
- strings[4] = ">>><<<";
- GPIO_Config();
- init_USART1(9600); // initialize USART1 @ 115200 baud
- RCC_Configuration();
- Lcd_Init();
- 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);
- lcd_clear(); /* clean the LCD */
- Delay(1000);
- USART_puts(USART1, "Start!\r\n"); // just send a message to indicate that it works
- char* setTime[5];
- char* keepTime[5];
- while (1)
- {
- setTime[0] = " 0000";
- setTime[1] = " 0001";
- setTime[2] = " 0002";
- setTime[3] = " 0003";
- setTime[4] = " 0004";
- value = getKey();
- if(value == '1'){
- value = 'Y';
- }
- keepTime[0] = value;
- keepTime[1] = value;
- keepTime[2] = value;
- keepTime[3] = value;
- keepTime[4] = value;
- //sprintf (text, "%c\r\n", value);
- sprintf (text, "%c", value);
- strAns[0] = value;
- //valueinLCD = getKey();
- RCC_Configuration();
- Lcd_Init(); /* initial */
- lcd_clear(); /* clean the LCD */
- Delay(1000);
- // Main LCD do
- set_cursor(0,0); /* set cursor */
- lcd_print (" Time = "); /* display */
- set_cursor(0, 1);
- lcd_print (" Set ");
- for(i=0;i<0x330000;i++); // wait // 100000
- // lcd_clear();
- for(i=0;i<0x80000;i++); // wait // 40000
- for(loopLCD=0;loopLCD<5;loopLCD++){
- set_cursor(8,0); /* set cursor */
- lcd_print (strings[loopLCD]); /* display */
- set_cursor(0, 1);
- lcd_print (setTime[loopLCD]);
- for(i=0;i<0x330000;i++); // is 1 second
- }
- set_cursor(0, 1);
- lcd_print (" ");
- }
- }
- 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++;
- }
- }
- void GPIO_LCD1602(void)
- {
- //valueinLCD = getKey();
- RCC_Configuration();
- Lcd_Init(); /* initial */
- lcd_clear(); /* clean the LCD */
- Delay(1000);
- set_cursor(0,0); /* set cursor */
- lcd_print (" TestTest! "); /* display */
- set_cursor(0, 1);
- lcd_print (" SetTime ");
- }
- /***************************************************************************//**
- * @brief System clocks configuration
- ******************************************************************************/
- void RCC_Configuration(void)
- {
- RCC_DeInit (); /* RCC system reset(for debug purpose)*/
- RCC_HSEConfig (RCC_HSE_ON); /* Enable HSE */
- /* Wait till HSE is ready */
- while (RCC_GetFlagStatus(RCC_FLAG_HSERDY) == RESET);
- RCC_HCLKConfig (RCC_SYSCLK_Div1); /* HCLK = SYSCLK */
- RCC_PCLK2Config (RCC_HCLK_Div1); /* PCLK2 = HCLK */
- RCC_PCLK1Config (RCC_HCLK_Div2); /* PCLK1 = HCLK/2 */
- RCC_ADCCLKConfig (RCC_PCLK2_Div4); /* ADCCLK = PCLK2/4 */
- *(vu32 *)0x40022000 = 0x01; /* Flash 2 wait state */
- /* PLLCLK = 8MHz * 9 = 72 MHz */
- RCC_PLLConfig (0x00010000, RCC_PLLMul_9);
- RCC_PLLCmd (ENABLE); /* Enable PLL */
- /* Wait till PLL is ready */
- while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);
- /* Select PLL as system clock source */
- RCC_SYSCLKConfig (RCC_SYSCLKSource_PLLCLK);
- /* Wait till PLL is used as system clock source */
- while (RCC_GetSYSCLKSource() != 0x08);
- }
- /***************************************************************************//**
- * @brief Delay some time
- ******************************************************************************/
- void Delay(vu32 nCount)
- {
- for(; nCount != 0; nCount--);
- }
- /***************************************************************************//**
- * @brief Setting all pins to output mode
- ******************************************************************************/
- void LCD_ALL_DIR_OUT(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_Init(GPIOC, &GPIO_InitStructure);
- }
- /***************************************************************************//**
- * @brief Setting DATA pins to input mode
- ******************************************************************************/
- void LCD_DATA_DIR_IN(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_Init(GPIOC, &GPIO_InitStructure);
- }
- /***************************************************************************//**
- * @brief Setting DATA pins to output mode
- ******************************************************************************/
- void LCD_DATA_DIR_OUT(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_Init(GPIOC, &GPIO_InitStructure);
- }
- /***************************************************************************//**
- * @brief Reading DATA pins
- * @return the data value.
- ******************************************************************************/
- unsigned int LCD_DATA_IN(void)
- {
- uint16_t u16Temp=0;
- u16Temp = GPIO_ReadInputData(GPIOC)&0x000F;
- return SWAP_DATA[u16Temp];
- }
- /***************************************************************************//**
- * @brief Read status of LCD controller
- * @return status : Status of LCD controller
- ******************************************************************************/
- static unsigned char Lcd_Read_Status (void)
- {
- unsigned char status;
- LCD_DATA_DIR_IN();
- GPIO_WriteBit(GPIOC, RS, Bit_RESET);
- GPIO_WriteBit(GPIOC, RW, Bit_SET);
- Delay(10);
- GPIO_WriteBit(GPIOC, EN, Bit_SET);
- Delay(10);
- status = LCD_DATA_IN() << 4;
- GPIO_WriteBit(GPIOC, EN, Bit_RESET);
- Delay(10);
- GPIO_WriteBit(GPIOC, EN, Bit_SET);
- Delay(10);
- status |= LCD_DATA_IN();
- GPIO_WriteBit(GPIOC, EN, Bit_RESET);
- LCD_DATA_DIR_OUT();
- return (status);
- }
- /***************************************************************************//**
- * @brief Wait while LCD is busy
- * @return status : Status of LCD controller
- ******************************************************************************/
- static unsigned char Wait_While_Busy()
- {
- unsigned char status;
- do{
- status = Lcd_Read_Status();
- }while(status & 0x80);
- return status;
- }
- /***************************************************************************//**
- * @brief Write 4-bits to LCD controller
- ******************************************************************************/
- void Lcd_Write_4bits(uc8 byte)
- {
- uint16_t u16Temp=0;
- GPIO_WriteBit(GPIOC, RW, Bit_RESET);
- GPIO_WriteBit(GPIOC, EN, Bit_SET);
- u16Temp = GPIO_ReadOutputData(GPIOC)&0xFFF0;
- u16Temp |= SWAP_DATA[byte&0x0F];
- GPIO_Write(GPIOC, u16Temp);
- Delay(10);
- GPIO_WriteBit(GPIOC, EN, Bit_RESET);
- Delay(10);
- }
- /***************************************************************************//**
- * @brief: Write command to LCD controller
- * @param[in] command : Command to be written
- ******************************************************************************/
- void Lcd_Write_Command(uc8 command)
- {
- Wait_While_Busy();
- GPIO_WriteBit(GPIOC, RS, Bit_RESET);
- Lcd_Write_4bits(command>>4);
- Lcd_Write_4bits(command);
- }
- /***************************************************************************//**
- * @brief: Write data to LCD controller
- * @param[in] data : Data to be written
- ******************************************************************************/
- void Lcd_Write_Data(uc8 data)
- {
- Wait_While_Busy();
- GPIO_WriteBit(GPIOC, RS, Bit_SET);
- Lcd_Write_4bits(data>>4);
- Lcd_Write_4bits(data);
- }
- /*******************************************************************************
- * @brief : Set cursor position on LCD display
- * @param[in] column : Column position
- * @param[in] line : Line position
- *******************************************************************************/
- void set_cursor(int column, int line)
- {
- unsigned char address;
- address = (line * 40) + column;
- address = 0x80 + (address & 0x7F);
- Lcd_Write_Command(address); /* Set DDRAM address counter to 0 */
- }
- /***************************************************************************//**
- * @brief Initial the LCD1602
- ******************************************************************************/
- void Lcd_Init(void)
- {
- char const *p;
- int i;
- /* Enable clock for peripheral */
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
- /* Set all pins for LCD as outputs */
- LCD_ALL_DIR_OUT();
- Delay(15000);
- GPIO_WriteBit(GPIOC, RS, Bit_RESET);
- Lcd_Write_4bits(0x3); /* Select 4-bit interface */
- Delay(4100);
- Lcd_Write_4bits(0x3);
- Delay(100);
- Lcd_Write_4bits(0x3);
- Lcd_Write_4bits(0x2);
- Lcd_Write_Command(0x28); /* 2 lines, 5x8 character matrix */
- Lcd_Write_Command(0x0C); /* Display ctrl:Disp=ON,Curs/Blnk=OFF */
- Lcd_Write_Command(0x06); /* Entry mode: Move right, no shift */
- /* Load user-specific characters into CGRAM */
- Lcd_Write_Command(0x40); /* Set CGRAM address counter to 0 */
- p = &UserFont[0][0];
- for (i = 0; i < sizeof(UserFont); i++, p++)
- lcd_print (*p);
- Lcd_Write_Command(0x80); /* Set DDRAM address counter to 0 */
- }
- /***************************************************************************//**
- * @brief print a string on LCD1602.
- * @param[in] *string : point to the string which will be printed on LCD.
- ******************************************************************************/
- void lcd_print (char *string)
- {
- int i;
- for (i=0;i<16 && string[i]!=0;i++) // 16 Character Print
- {
- Lcd_Write_Data(string[i]); // Print Byte to LCD
- }
- /*
- while (*string)
- {
- Lcd_Write_Data (*string++);
- }
- */
- }
- /*******************************************************************************
- * @brief Clear the LCD display *
- *******************************************************************************/
- void lcd_clear (void)
- {
- Lcd_Write_Command(0x01); /* Display clear */
- set_cursor (0, 0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement