Advertisement
etilletas98

LCD 4-bit

Aug 15th, 2012
1,655
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.00 KB | None | 0 0
  1.  /**
  2.   ******************************************************************************
  3.   * @file    Project/STM32F10x_StdPeriph_Template/main.c
  4.   * @author  MCD Application Team
  5.   * @version V3.5.0
  6.   * @date    08-April-2011
  7.   * @brief   Main program body
  8.   ******************************************************************************
  9.   * @attention
  10.   *
  11.   * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  12.   * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  13.   * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  14.   * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  15.   * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  16.   * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  17.   *
  18.   * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
  19.   ******************************************************************************
  20.   */  
  21.  
  22. /* Includes ------------------------------------------------------------------*/
  23. #include "stm32f10x.h"
  24. #include <stdio.h>
  25.  
  26.  
  27. /* Private typedef -----------------------------------------------------------*/
  28. typedef struct{
  29.   uint16_t pin; // assign something like GPIO_Pin_8
  30.   GPIO_TypeDef * port; // assign someting like GPIOA
  31. } MyPin;
  32.  
  33. MyPin DB0,DB1,DB2,DB3,DB4,DB5,DB6,DB7,DB[8],RS,RW,EN,LED;
  34.  
  35. /* Private define ------------------------------------------------------------*/
  36. /*
  37.  
  38.  
  39. */
  40. /* Private macro -------------------------------------------------------------*/
  41. /* Private variables ---------------------------------------------------------*/
  42. GPIO_InitTypeDef GPIO_InitStructure;
  43.  
  44. /* Private function prototypes -----------------------------------------------*/
  45. #ifdef __GNUC__
  46. /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
  47.    set to 'Yes') calls __io_putchar() */
  48. #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
  49. #else
  50. #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
  51. #endif /* __GNUC__ */
  52.  
  53.  
  54. /* Private functions ---------------------------------------------------------*/
  55. void enable_gpio();
  56. void writePin(MyPin pin, BitAction value);
  57. BitAction readPin(MyPin pin);
  58. /*----------------- LCD functions ------------*/
  59. //Common LCD functions
  60. void lcdCommon_sendByte(int byte,int howManyPins);
  61. //8-bit mode LCD functions; you may delete ALL these 5 functions if you are using 4-bit
  62. void lcd8_init();
  63. void lcd8_sendCommand(int byte);
  64. void lcd8_sendData(int byte);
  65. void lcd8_print( char *s);
  66. void lcd8_printx(char *s, int line, int column);
  67. //4-bit mode LCD functions; you may delete ALL these 5 functions if you are using 8-bit
  68. void lcd4_init();
  69. void lcd4_sendCommand(int byte);
  70. void lcd4_sendData(int byte);
  71. void lcd4_print( char *s);
  72. void lcd4_printx(char *s, int line, int column);
  73.  
  74. /**
  75.   * @brief  Main program.
  76.   * @param  None
  77.   * @retval None
  78.   */
  79. void delay_ms(int ms){
  80.   while(ms--)
  81.     for(volatile uint16_t i = 0; i < 2300; i++);
  82. }
  83. void delay_us(volatile int ms){
  84.   ms *= 4;
  85.   while(ms--);
  86. }
  87.  
  88. int main(void)
  89. {
  90.   enable_gpio();
  91.   /* //8-bit example
  92.   lcd8_init();
  93.   lcd8_printx("8  Bit",0,8);
  94.   lcd8_printx("LCD Driver",1,6);
  95.   lcd8_printx("->",0,0);
  96.   lcd8_printx("aemre",3,15);//
  97.   /*/ // 4-bit example
  98.   lcd4_init();
  99.   lcd4_printx("4  Bit",0,8);
  100.   lcd4_printx("LCD Driver",1,6);
  101.   lcd4_printx("aemre",3,15);// */
  102.   while(1);
  103. }
  104.  
  105.  
  106. /**
  107. * bit0 = PA8#2
  108. * bit1 = PA9#4
  109. * bit2 = PC10#8
  110. * bit3 = PB5#12
  111. * bit4 = PB8#16
  112. * bit5 = PC1#20
  113. * bit6 = PC13#24
  114. * bit7 = PB1#26
  115. *
  116. * RS = PB9#17
  117. * RW = PC0#19
  118. * EN = PB0#21
  119. * LED = PC12#10
  120. */
  121. void enable_gpio(){
  122.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |
  123.                        RCC_APB2Periph_GPIOC, ENABLE);
  124.   /* enable lcdCommon_sendByte() function pins */
  125.   GPIO_InitTypeDef GPIO_InitStructureA, GPIO_InitStructureB,GPIO_InitStructureC;
  126.  
  127.   GPIO_InitStructureA.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
  128.   GPIO_InitStructureB.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_8 | GPIO_Pin_1 | GPIO_Pin_9 | GPIO_Pin_0;
  129.   GPIO_InitStructureC.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_1 | GPIO_Pin_13 | GPIO_Pin_12 | GPIO_Pin_0;
  130.  
  131.   GPIO_InitStructureA.GPIO_Speed = GPIO_InitStructureB.GPIO_Speed = GPIO_InitStructureC.GPIO_Speed = GPIO_Speed_50MHz;
  132.   GPIO_InitStructureA.GPIO_Mode = GPIO_InitStructureB.GPIO_Mode = GPIO_InitStructureC.GPIO_Mode = GPIO_Mode_Out_PP;
  133.  
  134.   GPIO_Init(GPIOA, &GPIO_InitStructureA);
  135.   GPIO_Init(GPIOB, &GPIO_InitStructureB);
  136.   GPIO_Init(GPIOC, &GPIO_InitStructureC);
  137.  
  138.   DB0.port = GPIOA; DB0.pin = GPIO_Pin_8;
  139.   DB1.port = GPIOA; DB1.pin = GPIO_Pin_9;
  140.   DB2.port = GPIOC; DB2.pin = GPIO_Pin_10;
  141.   DB3.port = GPIOB; DB3.pin = GPIO_Pin_5;
  142.   DB4.port = GPIOB; DB4.pin = GPIO_Pin_8;
  143.   DB5.port = GPIOC; DB5.pin = GPIO_Pin_1;
  144.   DB6.port = GPIOC; DB6.pin = GPIO_Pin_13;
  145.   DB7.port = GPIOB; DB7.pin = GPIO_Pin_1;
  146.  
  147.   RS.port = GPIOB; RS.pin = GPIO_Pin_9;
  148.   RW.port = GPIOC; RW.pin = GPIO_Pin_0;
  149.   EN.port = GPIOB; EN.pin = GPIO_Pin_0;
  150.  
  151.   LED.port = GPIOC; LED.pin = GPIO_Pin_12;
  152.  
  153.   DB[0]=DB0;DB[1]=DB1;DB[2]=DB2;DB[3]=DB3;DB[4]=DB4;DB[5]=DB5;DB[6]=DB6;DB[7]=DB7;
  154. }
  155.  
  156. void lcd8_init(){
  157.   delay_ms(42);
  158.   lcd8_sendCommand(0x3C);// 8-bit interface, 4 lines, 5x8
  159.   delay_us(48);
  160.   lcd8_sendCommand(0x3C);// 8-bit interface, 8 lines, 5x8  - again
  161.   delay_us(48);
  162.   lcd8_sendCommand(0x0C);// display ON, cursor off, blink off
  163.   delay_us(48);
  164.   lcd8_sendCommand(0x01);// clear display
  165.   delay_ms(2);
  166.   lcd8_sendCommand(0x06);// increment cursor, no shift
  167. }
  168.  
  169. void lcd4_init(){
  170.   delay_ms(42);
  171.   /* only most significant 4 bytes sent, lcd4_sendCommand is not used*/
  172.   writePin(RS,Bit_RESET);
  173.   lcdCommon_sendByte(0x30,4);
  174.   /* only most significant 4 bytes sent, lcd4_sendCommand is not used */
  175.  
  176.   delay_us(48);
  177.   lcd4_sendCommand(0x2C);// 4-bit interface, 8 lines, 5x8  - again
  178.   delay_us(48);
  179.   lcd4_sendCommand(0x0C);// display ON, cursor off, blink off
  180.   delay_us(48);
  181.   lcd4_sendCommand(0x01);// clear display
  182.   delay_ms(2);
  183.   lcd4_sendCommand(0x06);// increment cursor, no shift
  184. }
  185. /**
  186. * Convert boolean to BitAction
  187. */
  188. BitAction conv(int b){
  189.   return b ? Bit_SET : Bit_RESET;
  190. }
  191.  
  192. void writePin(MyPin pin, BitAction value){
  193.   GPIO_WriteBit(pin.port,pin.pin,value);
  194. }
  195.  
  196. BitAction readPin(MyPin pin){
  197.   return conv(GPIO_ReadInputDataBit(pin.port, pin.pin));
  198. }
  199.  
  200. void lcdCommon_sendByte(int byte,int howManyPins){
  201.   //First check if LCD is busy
  202.   writePin(EN,Bit_SET);// To send data we need high to low signal
  203.   for(int i = 0; i<howManyPins; i++)
  204.     writePin(DB[7-i],conv(byte & (0x01 << (7-i))));//writePin(DB[7-i],conv(byte & (int) pow(2,7-i)));
  205.   writePin(RW,Bit_RESET); // LCD is now in write mode
  206.   delay_us(100); //wait sometime
  207.   writePin(EN,Bit_RESET); // Now we send high to low signal
  208.   delay_us(50); //it may need some time for the next operation
  209. }
  210.  
  211. void lcd8_sendCommand(int byte){
  212.   writePin(RS,Bit_RESET);
  213.   lcdCommon_sendByte(byte,8);
  214. }
  215.  
  216. void lcd4_sendCommand(int byte){
  217.   writePin(RS,Bit_RESET);
  218.   lcdCommon_sendByte(byte,4);
  219.   lcdCommon_sendByte(byte<<4,4);
  220. }
  221.  
  222. void lcd8_sendData(int byte){
  223.   writePin(RS,Bit_SET);
  224.   lcdCommon_sendByte(byte,8);
  225. }
  226.  
  227. void lcd4_sendData(int byte){
  228.   writePin(RS,Bit_SET);
  229.   lcdCommon_sendByte(byte,4);
  230.   lcdCommon_sendByte(byte<<4,4);
  231. }
  232.  
  233. void lcd8_print( char *s){
  234.   while( *s) lcd8_sendData( *s++);
  235. }
  236.  
  237. void lcd4_print( char *s){
  238.   while( *s) lcd4_sendData( *s++);
  239. }
  240.  
  241. void lcd8_printx( char *s, int line, int column){
  242.   int lineStarts[4] = {128, 192, 148, 212};
  243.   lcd8_sendCommand(lineStarts[line%4]+column%20); //go to given position
  244.   while( *s) lcd8_sendData( *s++);
  245. }
  246.  
  247. void lcd4_printx( char *s, int line, int column){
  248.   int lineStarts[4] = {128, 192, 148, 212};
  249.   lcd4_sendCommand(lineStarts[line%4]+column%20); //go to given position
  250.   while( *s) lcd4_sendData( *s++);
  251. }
  252.  
  253. #ifdef  USE_FULL_ASSERT
  254. void assert_failed(uint8_t* file, uint32_t line)
  255. {
  256.   while (1);
  257. }
  258. #endif
  259.  
  260. /**
  261.   * @}
  262.   */
  263.  
  264.  
  265. /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/
  266. /*
  267.  
  268. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement