Advertisement
pongfactory

KEYPAD TO LCD New Demo V.6

Mar 1st, 2014
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 17.30 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. #include <stdint.h>
  12.  
  13. USART_InitTypeDef USART_InitStruct; // this is for the USART1 initilization
  14. GPIO_InitTypeDef  GPIO_InitStructure;
  15.  
  16. void GPIO_Config(void);
  17. void init_USART1(uint32_t baudrate);
  18. void USART_puts(USART_TypeDef* USARTx, volatile char *s);
  19.  
  20. #define EN  GPIO_Pin_10
  21. #define RS  GPIO_Pin_12
  22. #define RW  GPIO_Pin_11
  23.  
  24. const unsigned int SWAP_DATA[16] = { 0x0, 0x8, 0x4, 0xC, 0x2, 0xA, 0x6, 0xE,
  25.                                      0x1, 0x9, 0x5, 0xD, 0x3, 0xB, 0x7, 0xF};
  26.  
  27.  
  28. const char UserFont[8][8] = {  /* 8 user defined characters to be loaded into CGRAM (used for bargraph)*/
  29.     { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
  30.     { 0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10 },
  31.     { 0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18 },
  32.     { 0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C },
  33.     { 0x1E,0x1E,0x1E,0x1E,0x1E,0x1E,0x1E,0x1E },
  34.     { 0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F },
  35.     { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
  36.     { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }
  37. };
  38.  
  39. /***************************************************************************//**
  40.  * Declare function prototypes
  41.  ******************************************************************************/
  42. void RCC_Configuration(void);
  43. void LCD_DATA_DIR_OUT(void);
  44. void LCD_DATA_DIR_IN(void);
  45. void LCD_ALL_DIR_OUT(void);
  46. unsigned int LCD_DATA_IN(void);
  47. static unsigned char Wait_While_Busy(void);
  48. static unsigned char Lcd_Read_Status (void);
  49. void Lcd_Write_4bits(uc8 byte);
  50. void Delay(vu32 nCount);
  51. void Lcd_Write_Command(uc8 command);
  52. void Lcd_Write_Data(uc8 data);
  53. void Lcd_Init(void);
  54. void Lcd_Write_Line1(void);
  55. void Lcd_Write_Line2(void);
  56. void set_cursor(int, int);
  57. void lcd_print (char *string);
  58. void lcd_clear (void);
  59.  
  60. char text[100] = "";
  61. char text2[100] = "";
  62. int data[100];
  63. int i = 0;
  64. int j = 0;
  65. int state = 0;
  66. uint32_t last_time = 0;
  67. uint32_t temp_time = 0;
  68. uint32_t temp = 0; //store data to check
  69. int value,value1,value2,value3,value4;
  70. char valueS[100] = "";
  71. int tempValue;
  72. int column;
  73. int row;
  74. int loop1 = 0;
  75. // constant values
  76. const int ROWS = 4;
  77. const int COLS = 4;
  78. int keys[4][4] = {{'1','2','3','A'},{'4','5','6','B'},{'7','8','9','C'},{'*','0','#','D'}};
  79. //int keys[4][4] = {{1,2,3,3},{4,5,6,6},{7,8,9,9},{0,0,0,0}};
  80. //int input[4] = {(uint16_t)0x0001,(uint16_t)0x0002,(uint16_t)0x0004,(uint16_t)0x0008};
  81. int input[4] = {((uint16_t)0x0010),((uint16_t)0x0020),((uint16_t)0x0040),((uint16_t)0x0080)};
  82. int output[4] = {((uint16_t)0x0004),((uint16_t)0x0020),((uint16_t)0x0040),((uint16_t)0x0080)};
  83. int rowPins[4] = {0,1,2,3};   // connect B0,1,2,3 to Rows 1-4 (the left four pins)
  84. int colPins[4] = {0,1,2,3};   // connect A0,1,2,3 to Column 1-4 (the right four pins)
  85. #define A_PORT GPIOA
  86. #define B_PORT GPIOB
  87. int key;
  88. int loopLCD;
  89. void GPIO_LCD1602(void);
  90. int main(void)
  91. {
  92. char ans[4] = {'1','2','3','A'};
  93. char ansNumber[] = "1333";
  94. //char ansLCD[] = {"1745","1746","1747"};
  95. char* strAns[3];
  96. strAns[2] = "Set  :  ";
  97. char* strings[5];
  98. strings[0] = "Real ";
  99. strings[1] = "Time";
  100. strings[2] = "ByIC";
  101. strings[3] = "DS1307";
  102. strings[4] = "------";
  103.  
  104.     GPIO_Config();
  105.     init_USART1(9600); // initialize USART1 @ 115200 baud
  106.     RCC_Configuration();
  107.     Lcd_Init();
  108.  
  109.         GPIO_WriteBit(B_PORT,GPIO_Pin_2,Bit_SET);
  110.         GPIO_WriteBit(B_PORT,GPIO_Pin_5,Bit_SET);
  111.         GPIO_WriteBit(B_PORT,GPIO_Pin_6,Bit_SET);
  112.         GPIO_WriteBit(B_PORT,GPIO_Pin_7,Bit_SET);
  113.         lcd_clear();                        /* clean the LCD */
  114.         Delay(1000);
  115.  
  116.  
  117.     USART_puts(USART1, "Start!\r\n"); // just send a message to indicate that it works
  118.     char* setTime[5];
  119.     char* keepTime[5];
  120.     int tempINT = 0;
  121.     int cLoop;
  122.     while (1)
  123.     {
  124.         //** Function KEYPAD
  125.         /*
  126.         value = getKey();
  127.                         if(value == '1'){
  128.                             value = 'Y';
  129.                         }
  130.                         //sprintf (text, "%c\r\n", value);
  131.                         sprintf (text, "%c", value);
  132.                         USART_puts(USART1, text);
  133.                         */
  134.          //** END Function KEYPAD
  135.                 //valueinLCD = getKey();
  136.                     RCC_Configuration();
  137.                     Lcd_Init();
  138.                     lcd_clear();
  139.                     Delay(1000);
  140.  
  141.                     // Main LCD do
  142.                     set_cursor(0,0);
  143.                     lcd_print (" Time =  ");
  144.                     set_cursor(0, 1);
  145.                     lcd_print ("<<<< Set Time    ");
  146.  
  147.                     for(loopLCD=0;loopLCD<5;loopLCD++){
  148.                     value = getKey();
  149.                    //sprintf (text, "%c\r\n", value);
  150.  
  151.  
  152.  
  153.                     sprintf (text, "%c", value);
  154.                     USART_puts(USART1, text);
  155.  
  156.  
  157.                     set_cursor(8,0);
  158.                     lcd_print (strings[loopLCD]);
  159.                     set_cursor(0, 1);
  160.                     lcd_print (text);
  161.                     for(i=0;i<0x100000;i++); // is 1 second
  162.                     }
  163.  
  164.  
  165.                     set_cursor(0, 1);
  166.                     lcd_print ("        ");
  167.  
  168.     }
  169. }
  170.  
  171.  
  172. int j;
  173. int i;
  174. int getKey(void)
  175. {
  176.      int key_pressed = 0;
  177.  
  178.       for (j=0; j < ROWS; j++) { // scan the j-th row (j=0,1,2,3)
  179.         for (i=0; i < ROWS; i++) {
  180.           // output HIGH to all rows, except the j-th row
  181.             if(i==j){
  182.                  GPIO_WriteBit(B_PORT,output[i],Bit_RESET);
  183.             }else{
  184.                  GPIO_WriteBit(B_PORT,output[i],Bit_SET);
  185.             }
  186.         }
  187.         for (i=0; i < COLS; i++) {
  188.             if(GPIO_ReadInputDataBit(GPIOA,input[i]) == 0){ // Button at (R,C)=(j,i) is pressed
  189.              // wait until the button is released.
  190.              while ( GPIO_ReadInputDataBit(GPIOA,input[i]) == 0 ) ; // blocking
  191.              key_pressed = keys[j][i]; // get the associated key for that button
  192.              break;
  193.           }
  194.         }
  195.         GPIO_WriteBit(B_PORT,output[j],Bit_SET);
  196.         if ( key_pressed != 0 ) {
  197.           return key_pressed;
  198.         }
  199.       }
  200.       return 0; // no key pressed
  201.     }
  202.  
  203. void GPIO_Config(void)
  204. {
  205.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_USART1 |
  206.             RCC_APB2Periph_GPIOA, ENABLE);
  207.     //INPUT A = COLUMN
  208.           GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
  209.           GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  210.           GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  211.           GPIO_Init(GPIOA, &GPIO_InitStructure);
  212.  
  213.     //OUTPUT B = ROW
  214.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
  215.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  216.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  217.     GPIO_Init(GPIOB, &GPIO_InitStructure);
  218.  
  219.     //USART1 (PA9)
  220.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //USART1_TX
  221.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  222.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  223.     GPIO_Init(GPIOA, &GPIO_InitStructure);
  224.  
  225.  
  226. }
  227.  
  228. void init_USART1(uint32_t baudrate){
  229.     USART_InitStruct.USART_BaudRate = baudrate;  // the baudrate is set to the value we passed into this init function
  230.     USART_InitStruct.USART_WordLength = USART_WordLength_8b;  // we want the data frame size to be 8 bits (standard)
  231.     USART_InitStruct.USART_StopBits = USART_StopBits_1;  // we want 1 stop bit (standard)
  232.     USART_InitStruct.USART_Parity = USART_Parity_No;  // we don't want a parity bit (standard)
  233.     USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None; // we don't want flow control (standard)
  234.     USART_InitStruct.USART_Mode = USART_Mode_Tx;  // we want to enable the transmitter and the receiver
  235.     USART_Init(USART1, &USART_InitStruct);  // again all the properties are passed to the USART_Init function which takes care of all the bit setting
  236.  
  237.     USART_ITConfig(USART1, USART_IT_RXNE, DISABLE);             // enable the USART1 receive interrupt
  238.     USART_Cmd(USART1, ENABLE);
  239. }
  240.  
  241. void USART_puts(USART_TypeDef* USARTx, volatile char *s){
  242.     while(*s){
  243.         // wait until data register is empty
  244.         while( !(USARTx->SR & 0x00000040) );
  245.         USART_SendData(USARTx, *s);
  246.         *s++;
  247.     }
  248. }
  249.  
  250. void GPIO_LCD1602(void)
  251. {
  252.     //valueinLCD = getKey();
  253.     RCC_Configuration();
  254.     Lcd_Init();                         /* initial       */
  255.     lcd_clear();                        /* clean the LCD */
  256.     Delay(1000);
  257.     set_cursor(0,0);                    /* set cursor    */
  258.     lcd_print (" TestTest!  ");     /* display       */
  259.     set_cursor(0, 1);
  260.     lcd_print (" SetTime  ");
  261. }
  262.  
  263. /***************************************************************************//**
  264.  * @brief System clocks configuration
  265.  ******************************************************************************/
  266. void RCC_Configuration(void)
  267. {
  268.     RCC_DeInit ();                        /* RCC system reset(for debug purpose)*/
  269.     RCC_HSEConfig (RCC_HSE_ON);           /* Enable HSE                         */
  270.  
  271.     /* Wait till HSE is ready                                                   */
  272.     while (RCC_GetFlagStatus(RCC_FLAG_HSERDY) == RESET);
  273.  
  274.     RCC_HCLKConfig   (RCC_SYSCLK_Div1);   /* HCLK   = SYSCLK                    */
  275.     RCC_PCLK2Config  (RCC_HCLK_Div1);     /* PCLK2  = HCLK                      */
  276.     RCC_PCLK1Config  (RCC_HCLK_Div2);     /* PCLK1  = HCLK/2                    */
  277.     RCC_ADCCLKConfig (RCC_PCLK2_Div4);    /* ADCCLK = PCLK2/4                   */
  278.  
  279.     *(vu32 *)0x40022000 = 0x01;           /* Flash 2 wait state                 */
  280.  
  281.     /* PLLCLK = 8MHz * 9 = 72 MHz                                               */
  282.     RCC_PLLConfig (0x00010000, RCC_PLLMul_9);
  283.  
  284.     RCC_PLLCmd (ENABLE);                  /* Enable PLL                         */
  285.  
  286.     /* Wait till PLL is ready                                                   */
  287.     while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);
  288.  
  289.     /* Select PLL as system clock source                                        */
  290.     RCC_SYSCLKConfig (RCC_SYSCLKSource_PLLCLK);
  291.  
  292.     /* Wait till PLL is used as system clock source                             */
  293.     while (RCC_GetSYSCLKSource() != 0x08);
  294. }
  295.  
  296. /***************************************************************************//**
  297.  * @brief Delay some time
  298.  ******************************************************************************/
  299. void Delay(vu32 nCount)
  300. {
  301.     for(; nCount != 0; nCount--);
  302. }
  303.  
  304. /***************************************************************************//**
  305.  * @brief  Setting all pins to output mode
  306.  ******************************************************************************/
  307. void LCD_ALL_DIR_OUT(void)
  308. {
  309.     GPIO_InitTypeDef GPIO_InitStructure;
  310.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
  311.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  312.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  313.     GPIO_Init(GPIOC, &GPIO_InitStructure);
  314. }
  315.  
  316. /***************************************************************************//**
  317.  * @brief  Setting DATA pins to input mode
  318.  ******************************************************************************/
  319. void LCD_DATA_DIR_IN(void)
  320. {
  321.     GPIO_InitTypeDef GPIO_InitStructure;
  322.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
  323.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  324.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  325.     GPIO_Init(GPIOC, &GPIO_InitStructure);
  326. }
  327.  
  328. /***************************************************************************//**
  329.  * @brief  Setting DATA pins to output mode
  330.  ******************************************************************************/
  331. void LCD_DATA_DIR_OUT(void)
  332. {
  333.     GPIO_InitTypeDef GPIO_InitStructure;
  334.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
  335.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  336.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  337.     GPIO_Init(GPIOC, &GPIO_InitStructure);
  338. }
  339. /***************************************************************************//**
  340.  * @brief  Reading DATA pins
  341.  * @return the data value.
  342.  ******************************************************************************/
  343. unsigned int LCD_DATA_IN(void)
  344. {
  345.     uint16_t u16Temp=0;
  346.     u16Temp = GPIO_ReadInputData(GPIOC)&0x000F;
  347.     return SWAP_DATA[u16Temp];
  348. }
  349.  
  350. /***************************************************************************//**
  351.  * @brief  Read status of LCD controller
  352.  * @return status : Status of LCD controller
  353.  ******************************************************************************/
  354. static unsigned char Lcd_Read_Status (void)
  355. {
  356.     unsigned char status;
  357.  
  358.     LCD_DATA_DIR_IN();
  359.     GPIO_WriteBit(GPIOC, RS, Bit_RESET);
  360.     GPIO_WriteBit(GPIOC, RW, Bit_SET);
  361.     Delay(10);
  362.     GPIO_WriteBit(GPIOC, EN, Bit_SET);
  363.     Delay(10);
  364.     status  = LCD_DATA_IN() << 4;
  365.     GPIO_WriteBit(GPIOC, EN, Bit_RESET);
  366.     Delay(10);
  367.     GPIO_WriteBit(GPIOC, EN, Bit_SET);
  368.     Delay(10);
  369.     status |= LCD_DATA_IN();
  370.     GPIO_WriteBit(GPIOC, EN, Bit_RESET);
  371.     LCD_DATA_DIR_OUT();
  372.     return (status);
  373. }
  374.  
  375. /***************************************************************************//**
  376.  * @brief Wait while LCD is busy
  377.  * @return status : Status of LCD controller
  378.  ******************************************************************************/
  379. static unsigned char Wait_While_Busy()
  380. {
  381.     unsigned char status;
  382.     do{
  383.     status = Lcd_Read_Status();
  384.     }while(status & 0x80);
  385.  
  386.     return status;
  387. }
  388. /***************************************************************************//**
  389.  * @brief  Write 4-bits to LCD controller
  390.  ******************************************************************************/
  391. void Lcd_Write_4bits(uc8 byte)
  392. {
  393.     uint16_t u16Temp=0;
  394.     GPIO_WriteBit(GPIOC, RW, Bit_RESET);
  395.     GPIO_WriteBit(GPIOC, EN, Bit_SET);
  396.     u16Temp = GPIO_ReadOutputData(GPIOC)&0xFFF0;
  397.     u16Temp |=  SWAP_DATA[byte&0x0F];
  398.     GPIO_Write(GPIOC, u16Temp);
  399.     Delay(10);
  400.     GPIO_WriteBit(GPIOC, EN, Bit_RESET);
  401.     Delay(10);
  402. }
  403.  
  404. /***************************************************************************//**
  405.  * @brief:    Write command to LCD controller
  406.  * @param[in] command :  Command to be written
  407.  ******************************************************************************/
  408. void Lcd_Write_Command(uc8 command)
  409. {
  410.     Wait_While_Busy();
  411.     GPIO_WriteBit(GPIOC, RS, Bit_RESET);
  412.     Lcd_Write_4bits(command>>4);
  413.     Lcd_Write_4bits(command);
  414. }
  415.  
  416. /***************************************************************************//**
  417.  * @brief:     Write data to LCD controller
  418.   * @param[in] data :  Data to be written
  419.  ******************************************************************************/
  420. void Lcd_Write_Data(uc8 data)
  421. {
  422.     Wait_While_Busy();
  423.     GPIO_WriteBit(GPIOC, RS, Bit_SET);
  424.     Lcd_Write_4bits(data>>4);
  425.     Lcd_Write_4bits(data);
  426. }
  427.  
  428. /*******************************************************************************
  429. * @brief : Set cursor position on LCD display
  430. * @param[in] column : Column position
  431. * @param[in] line   : Line position
  432. *******************************************************************************/
  433. void set_cursor(int column, int line)
  434. {
  435.     unsigned char address;
  436.  
  437.     address = (line * 40) + column;
  438.     address = 0x80 + (address & 0x7F);
  439.     Lcd_Write_Command(address);               /* Set DDRAM address counter to 0     */
  440. }
  441.  
  442. /***************************************************************************//**
  443.  * @brief  Initial the LCD1602
  444.  ******************************************************************************/
  445. void Lcd_Init(void)
  446. {
  447.     char const *p;
  448.     int i;
  449.  
  450.     /* Enable clock for peripheral        */
  451.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
  452.  
  453.     /* Set all pins for LCD as outputs    */
  454.     LCD_ALL_DIR_OUT();
  455.     Delay(15000);
  456.     GPIO_WriteBit(GPIOC, RS, Bit_RESET);
  457.     Lcd_Write_4bits(0x3);  /* Select 4-bit interface  */
  458.     Delay(4100);
  459.     Lcd_Write_4bits(0x3);
  460.     Delay(100);
  461.     Lcd_Write_4bits(0x3);
  462.     Lcd_Write_4bits(0x2);
  463.  
  464.     Lcd_Write_Command(0x28); /* 2 lines, 5x8 character matrix      */
  465.     Lcd_Write_Command(0x0C); /* Display ctrl:Disp=ON,Curs/Blnk=OFF */
  466.     Lcd_Write_Command(0x06); /* Entry mode: Move right, no shift   */
  467.  
  468.     /* Load user-specific characters into CGRAM                                 */
  469.     Lcd_Write_Command(0x40);                  /* Set CGRAM address counter to 0     */
  470.     p = &UserFont[0][0];
  471.     for (i = 0; i < sizeof(UserFont); i++, p++)
  472.         lcd_print (*p);
  473.     Lcd_Write_Command(0x80);                 /* Set DDRAM address counter to 0     */
  474. }
  475.  
  476. /***************************************************************************//**
  477.  * @brief   print a string on LCD1602.
  478.  * @param[in] *string : point to the string which will be printed on LCD.
  479.  ******************************************************************************/
  480. void lcd_print (char *string)
  481. {
  482.     int i;
  483.  
  484.       for (i=0;i<16 && string[i]!=0;i++)                                               // 16 Character Print
  485.       {
  486.           Lcd_Write_Data(string[i]);                                                    // Print Byte to LCD
  487.       }
  488.     /*
  489.     while (*string)
  490.     {
  491.         Lcd_Write_Data (*string++);
  492.     }
  493.     */
  494. }
  495.  
  496. /*******************************************************************************
  497.  * @brief  Clear the LCD display                                                        *
  498. *******************************************************************************/
  499. void lcd_clear (void)
  500. {
  501.     Lcd_Write_Command(0x01);                  /* Display clear                      */
  502.     set_cursor (0, 0);
  503. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement