Advertisement
pongfactory

LCD+KEYPAD V.2 OK

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