Advertisement
pongfactory

LCD + KEYPAD V.3 OK

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