Advertisement
pongfactory

LCD + KEYPAD V.4 OK

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