Advertisement
pongfactory

LCD + KEYPAD V.5 OK

Mar 1st, 2014
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 17.85 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;
  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.  
  125.     for(cLoop=0;cLoop<5;cLoop++){
  126.         sprintf (text2, "%d", tempINT);
  127.         setTime[cLoop] = text2;
  128.         tempINT = tempINT + 1;
  129.         if(tempINT == 9999){
  130.             tempINT = 0;
  131.         }
  132.     }
  133.  
  134.         /*
  135.     setTime[0] = " 0000";
  136.     setTime[1] = " 0001";
  137.     setTime[2] = " 0002";
  138.     setTime[3] = " 0003";
  139.     setTime[4] = " 0004";
  140.     */
  141.                 value = getKey();
  142.                 if(value == '1'){
  143.                     value = 'Y';
  144.                 }
  145.                     keepTime[0] = value;
  146.                     keepTime[1] = value;
  147.                     keepTime[2] = value;
  148.                     keepTime[3] = value;
  149.                     keepTime[4] = value;
  150.                 //sprintf (text, "%c\r\n", value);
  151.                 sprintf (text, "%c", value);
  152.                 strAns[0] = value;
  153.  
  154.  
  155.                 //valueinLCD = getKey();
  156.                     RCC_Configuration();
  157.                     Lcd_Init();                         /* initial       */
  158.                     lcd_clear();                        /* clean the LCD */
  159.                     Delay(1000);
  160.  
  161.                     // Main LCD do
  162.                     set_cursor(0,0);                    /* set cursor    */
  163.                     lcd_print (" Time =  ");     /* display       */
  164.                     set_cursor(0, 1);
  165.                     lcd_print ("<<<< Set Time    ");
  166.  
  167.                     for(i=0;i<0x330000;i++); // wait // 100000
  168.                    // lcd_clear();
  169.                     for(i=0;i<0x80000;i++); // wait  // 40000
  170.  
  171.                     for(loopLCD=0;loopLCD<5;loopLCD++){
  172.                     set_cursor(8,0);                    /* set cursor    */
  173.                     lcd_print (strings[loopLCD]);     /* display       */
  174.                     set_cursor(0, 1);
  175.                     lcd_print (setTime[loopLCD]);
  176.                     for(i=0;i<0x330000;i++); // is 1 second
  177.                     }
  178.                     set_cursor(0, 1);
  179.                     lcd_print ("        ");
  180.  
  181.     }
  182. }
  183.  
  184.  
  185. int j;
  186. int i;
  187. int getKey(void)
  188. {
  189.      int key_pressed = 0;
  190.  
  191.       for (j=0; j < ROWS; j++) { // scan the j-th row (j=0,1,2,3)
  192.         for (i=0; i < ROWS; i++) {
  193.           // output HIGH to all rows, except the j-th row
  194.             if(i==j){
  195.                  GPIO_WriteBit(B_PORT,output[i],Bit_RESET);
  196.             }else{
  197.                  GPIO_WriteBit(B_PORT,output[i],Bit_SET);
  198.             }
  199.         }
  200.         for (i=0; i < COLS; i++) {
  201.             if(GPIO_ReadInputDataBit(GPIOA,input[i]) == 0){ // Button at (R,C)=(j,i) is pressed
  202.              // wait until the button is released.
  203.              while ( GPIO_ReadInputDataBit(GPIOA,input[i]) == 0 ) ; // blocking
  204.              key_pressed = keys[j][i]; // get the associated key for that button
  205.              break;
  206.           }
  207.         }
  208.         GPIO_WriteBit(B_PORT,output[j],Bit_SET);
  209.         if ( key_pressed != 0 ) {
  210.           return key_pressed;
  211.         }
  212.       }
  213.       return 0; // no key pressed
  214.     }
  215.  
  216. void GPIO_Config(void)
  217. {
  218.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_USART1 |
  219.             RCC_APB2Periph_GPIOA, ENABLE);
  220.     //INPUT A = COLUMN
  221.           GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
  222.           GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  223.           GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  224.           GPIO_Init(GPIOA, &GPIO_InitStructure);
  225.  
  226.     //OUTPUT B = ROW
  227.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
  228.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  229.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  230.     GPIO_Init(GPIOB, &GPIO_InitStructure);
  231.  
  232.     //USART1 (PA9)
  233.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //USART1_TX
  234.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  235.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  236.     GPIO_Init(GPIOA, &GPIO_InitStructure);
  237.  
  238.  
  239. }
  240.  
  241. void init_USART1(uint32_t baudrate){
  242.     USART_InitStruct.USART_BaudRate = baudrate;  // the baudrate is set to the value we passed into this init function
  243.     USART_InitStruct.USART_WordLength = USART_WordLength_8b;  // we want the data frame size to be 8 bits (standard)
  244.     USART_InitStruct.USART_StopBits = USART_StopBits_1;  // we want 1 stop bit (standard)
  245.     USART_InitStruct.USART_Parity = USART_Parity_No;  // we don't want a parity bit (standard)
  246.     USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None; // we don't want flow control (standard)
  247.     USART_InitStruct.USART_Mode = USART_Mode_Tx;  // we want to enable the transmitter and the receiver
  248.     USART_Init(USART1, &USART_InitStruct);  // again all the properties are passed to the USART_Init function which takes care of all the bit setting
  249.  
  250.     USART_ITConfig(USART1, USART_IT_RXNE, DISABLE);             // enable the USART1 receive interrupt
  251.     USART_Cmd(USART1, ENABLE);
  252. }
  253.  
  254. void USART_puts(USART_TypeDef* USARTx, volatile char *s){
  255.     while(*s){
  256.         // wait until data register is empty
  257.         while( !(USARTx->SR & 0x00000040) );
  258.         USART_SendData(USARTx, *s);
  259.         *s++;
  260.     }
  261. }
  262.  
  263. void GPIO_LCD1602(void)
  264. {
  265.     //valueinLCD = getKey();
  266.     RCC_Configuration();
  267.     Lcd_Init();                         /* initial       */
  268.     lcd_clear();                        /* clean the LCD */
  269.     Delay(1000);
  270.     set_cursor(0,0);                    /* set cursor    */
  271.     lcd_print (" TestTest!  ");     /* display       */
  272.     set_cursor(0, 1);
  273.     lcd_print (" SetTime  ");
  274. }
  275.  
  276. /***************************************************************************//**
  277.  * @brief System clocks configuration
  278.  ******************************************************************************/
  279. void RCC_Configuration(void)
  280. {
  281.     RCC_DeInit ();                        /* RCC system reset(for debug purpose)*/
  282.     RCC_HSEConfig (RCC_HSE_ON);           /* Enable HSE                         */
  283.  
  284.     /* Wait till HSE is ready                                                   */
  285.     while (RCC_GetFlagStatus(RCC_FLAG_HSERDY) == RESET);
  286.  
  287.     RCC_HCLKConfig   (RCC_SYSCLK_Div1);   /* HCLK   = SYSCLK                    */
  288.     RCC_PCLK2Config  (RCC_HCLK_Div1);     /* PCLK2  = HCLK                      */
  289.     RCC_PCLK1Config  (RCC_HCLK_Div2);     /* PCLK1  = HCLK/2                    */
  290.     RCC_ADCCLKConfig (RCC_PCLK2_Div4);    /* ADCCLK = PCLK2/4                   */
  291.  
  292.     *(vu32 *)0x40022000 = 0x01;           /* Flash 2 wait state                 */
  293.  
  294.     /* PLLCLK = 8MHz * 9 = 72 MHz                                               */
  295.     RCC_PLLConfig (0x00010000, RCC_PLLMul_9);
  296.  
  297.     RCC_PLLCmd (ENABLE);                  /* Enable PLL                         */
  298.  
  299.     /* Wait till PLL is ready                                                   */
  300.     while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);
  301.  
  302.     /* Select PLL as system clock source                                        */
  303.     RCC_SYSCLKConfig (RCC_SYSCLKSource_PLLCLK);
  304.  
  305.     /* Wait till PLL is used as system clock source                             */
  306.     while (RCC_GetSYSCLKSource() != 0x08);
  307. }
  308.  
  309. /***************************************************************************//**
  310.  * @brief Delay some time
  311.  ******************************************************************************/
  312. void Delay(vu32 nCount)
  313. {
  314.     for(; nCount != 0; nCount--);
  315. }
  316.  
  317. /***************************************************************************//**
  318.  * @brief  Setting all pins to output mode
  319.  ******************************************************************************/
  320. void LCD_ALL_DIR_OUT(void)
  321. {
  322.     GPIO_InitTypeDef GPIO_InitStructure;
  323.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
  324.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  325.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  326.     GPIO_Init(GPIOC, &GPIO_InitStructure);
  327. }
  328.  
  329. /***************************************************************************//**
  330.  * @brief  Setting DATA pins to input mode
  331.  ******************************************************************************/
  332. void LCD_DATA_DIR_IN(void)
  333. {
  334.     GPIO_InitTypeDef GPIO_InitStructure;
  335.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
  336.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  337.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  338.     GPIO_Init(GPIOC, &GPIO_InitStructure);
  339. }
  340.  
  341. /***************************************************************************//**
  342.  * @brief  Setting DATA pins to output mode
  343.  ******************************************************************************/
  344. void LCD_DATA_DIR_OUT(void)
  345. {
  346.     GPIO_InitTypeDef GPIO_InitStructure;
  347.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
  348.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  349.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  350.     GPIO_Init(GPIOC, &GPIO_InitStructure);
  351. }
  352. /***************************************************************************//**
  353.  * @brief  Reading DATA pins
  354.  * @return the data value.
  355.  ******************************************************************************/
  356. unsigned int LCD_DATA_IN(void)
  357. {
  358.     uint16_t u16Temp=0;
  359.     u16Temp = GPIO_ReadInputData(GPIOC)&0x000F;
  360.     return SWAP_DATA[u16Temp];
  361. }
  362.  
  363. /***************************************************************************//**
  364.  * @brief  Read status of LCD controller
  365.  * @return status : Status of LCD controller
  366.  ******************************************************************************/
  367. static unsigned char Lcd_Read_Status (void)
  368. {
  369.     unsigned char status;
  370.  
  371.     LCD_DATA_DIR_IN();
  372.     GPIO_WriteBit(GPIOC, RS, Bit_RESET);
  373.     GPIO_WriteBit(GPIOC, RW, Bit_SET);
  374.     Delay(10);
  375.     GPIO_WriteBit(GPIOC, EN, Bit_SET);
  376.     Delay(10);
  377.     status  = LCD_DATA_IN() << 4;
  378.     GPIO_WriteBit(GPIOC, EN, Bit_RESET);
  379.     Delay(10);
  380.     GPIO_WriteBit(GPIOC, EN, Bit_SET);
  381.     Delay(10);
  382.     status |= LCD_DATA_IN();
  383.     GPIO_WriteBit(GPIOC, EN, Bit_RESET);
  384.     LCD_DATA_DIR_OUT();
  385.     return (status);
  386. }
  387.  
  388. /***************************************************************************//**
  389.  * @brief Wait while LCD is busy
  390.  * @return status : Status of LCD controller
  391.  ******************************************************************************/
  392. static unsigned char Wait_While_Busy()
  393. {
  394.     unsigned char status;
  395.     do{
  396.     status = Lcd_Read_Status();
  397.     }while(status & 0x80);
  398.  
  399.     return status;
  400. }
  401. /***************************************************************************//**
  402.  * @brief  Write 4-bits to LCD controller
  403.  ******************************************************************************/
  404. void Lcd_Write_4bits(uc8 byte)
  405. {
  406.     uint16_t u16Temp=0;
  407.     GPIO_WriteBit(GPIOC, RW, Bit_RESET);
  408.     GPIO_WriteBit(GPIOC, EN, Bit_SET);
  409.     u16Temp = GPIO_ReadOutputData(GPIOC)&0xFFF0;
  410.     u16Temp |=  SWAP_DATA[byte&0x0F];
  411.     GPIO_Write(GPIOC, u16Temp);
  412.     Delay(10);
  413.     GPIO_WriteBit(GPIOC, EN, Bit_RESET);
  414.     Delay(10);
  415. }
  416.  
  417. /***************************************************************************//**
  418.  * @brief:    Write command to LCD controller
  419.  * @param[in] command :  Command to be written
  420.  ******************************************************************************/
  421. void Lcd_Write_Command(uc8 command)
  422. {
  423.     Wait_While_Busy();
  424.     GPIO_WriteBit(GPIOC, RS, Bit_RESET);
  425.     Lcd_Write_4bits(command>>4);
  426.     Lcd_Write_4bits(command);
  427. }
  428.  
  429. /***************************************************************************//**
  430.  * @brief:     Write data to LCD controller
  431.   * @param[in] data :  Data to be written
  432.  ******************************************************************************/
  433. void Lcd_Write_Data(uc8 data)
  434. {
  435.     Wait_While_Busy();
  436.     GPIO_WriteBit(GPIOC, RS, Bit_SET);
  437.     Lcd_Write_4bits(data>>4);
  438.     Lcd_Write_4bits(data);
  439. }
  440.  
  441. /*******************************************************************************
  442. * @brief : Set cursor position on LCD display
  443. * @param[in] column : Column position
  444. * @param[in] line   : Line position
  445. *******************************************************************************/
  446. void set_cursor(int column, int line)
  447. {
  448.     unsigned char address;
  449.  
  450.     address = (line * 40) + column;
  451.     address = 0x80 + (address & 0x7F);
  452.     Lcd_Write_Command(address);               /* Set DDRAM address counter to 0     */
  453. }
  454.  
  455. /***************************************************************************//**
  456.  * @brief  Initial the LCD1602
  457.  ******************************************************************************/
  458. void Lcd_Init(void)
  459. {
  460.     char const *p;
  461.     int i;
  462.  
  463.     /* Enable clock for peripheral        */
  464.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
  465.  
  466.     /* Set all pins for LCD as outputs    */
  467.     LCD_ALL_DIR_OUT();
  468.     Delay(15000);
  469.     GPIO_WriteBit(GPIOC, RS, Bit_RESET);
  470.     Lcd_Write_4bits(0x3);  /* Select 4-bit interface  */
  471.     Delay(4100);
  472.     Lcd_Write_4bits(0x3);
  473.     Delay(100);
  474.     Lcd_Write_4bits(0x3);
  475.     Lcd_Write_4bits(0x2);
  476.  
  477.     Lcd_Write_Command(0x28); /* 2 lines, 5x8 character matrix      */
  478.     Lcd_Write_Command(0x0C); /* Display ctrl:Disp=ON,Curs/Blnk=OFF */
  479.     Lcd_Write_Command(0x06); /* Entry mode: Move right, no shift   */
  480.  
  481.     /* Load user-specific characters into CGRAM                                 */
  482.     Lcd_Write_Command(0x40);                  /* Set CGRAM address counter to 0     */
  483.     p = &UserFont[0][0];
  484.     for (i = 0; i < sizeof(UserFont); i++, p++)
  485.         lcd_print (*p);
  486.     Lcd_Write_Command(0x80);                 /* Set DDRAM address counter to 0     */
  487. }
  488.  
  489. /***************************************************************************//**
  490.  * @brief   print a string on LCD1602.
  491.  * @param[in] *string : point to the string which will be printed on LCD.
  492.  ******************************************************************************/
  493. void lcd_print (char *string)
  494. {
  495.     int i;
  496.  
  497.       for (i=0;i<16 && string[i]!=0;i++)                                               // 16 Character Print
  498.       {
  499.           Lcd_Write_Data(string[i]);                                                    // Print Byte to LCD
  500.       }
  501.     /*
  502.     while (*string)
  503.     {
  504.         Lcd_Write_Data (*string++);
  505.     }
  506.     */
  507. }
  508.  
  509. /*******************************************************************************
  510.  * @brief  Clear the LCD display                                                        *
  511. *******************************************************************************/
  512. void lcd_clear (void)
  513. {
  514.     Lcd_Write_Command(0x01);                  /* Display clear                      */
  515.     set_cursor (0, 0);
  516. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement