Advertisement
phillip_bourdon234

LCD_Driver.c

Feb 28th, 2021
618
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.33 KB | None | 0 0
  1. #include "stm32f10x.h"
  2. #include "system_stm32f10x.h"
  3. #include "GPIO_Driver.h"
  4. #include "delay.h"
  5. #include "LCD_Driver.h"
  6.  
  7. GPIO_TYPE static D4;
  8. GPIO_TYPE static D5;
  9. GPIO_TYPE static D6;
  10. GPIO_TYPE static D7;
  11. GPIO_TYPE static EN;
  12. GPIO_TYPE static RW;
  13. GPIO_TYPE static RS;
  14.  
  15. void lcd_init(GPIO_TypeDef *D4port, uint8_t D4pin, GPIO_TypeDef *D5port, uint8_t D5pin,
  16.                             GPIO_TypeDef *D6port, uint8_t D6pin, GPIO_TypeDef *D7port, uint8_t D7pin,
  17.                             GPIO_TypeDef *ENport, uint8_t ENpin, GPIO_TypeDef *RWport, uint8_t RWpin,
  18.                             GPIO_TypeDef *RSport, uint8_t RSpin)
  19. {
  20.     //==========================================
  21.     D4.port = D4port;
  22.     D4.pin = D4pin;
  23.     D4.mode = OUTPUT;
  24.   D4.mode_type = OUTPUT_GEN_PURPOSE;
  25.     D4.speed = SPEED_2MHZ;
  26.     //==========================================
  27.    
  28.     //==========================================
  29.     D5.port = D5port;
  30.     D5.pin = D5pin;
  31.     D5.mode = OUTPUT;
  32.   D5.mode_type = OUTPUT_GEN_PURPOSE;
  33.     D5.speed = SPEED_2MHZ;
  34.     //==========================================
  35.    
  36.     //==========================================
  37.     D6.port = D6port;
  38.     D6.pin = D6pin;
  39.     D6.mode = OUTPUT;
  40.   D6.mode_type = OUTPUT_GEN_PURPOSE;
  41.     D6.speed = SPEED_2MHZ;
  42.     //==========================================
  43.    
  44.     //==========================================
  45.     D7.port = D7port;
  46.     D7.pin = D7pin;
  47.     D7.mode = OUTPUT;
  48.   D7.mode_type = OUTPUT_GEN_PURPOSE;
  49.     D7.speed = SPEED_2MHZ;
  50.     //==========================================
  51.    
  52.     //==========================================
  53.     EN.port = ENport;
  54.     EN.pin = ENpin;
  55.     EN.mode = OUTPUT;
  56.   EN.mode_type = OUTPUT_GEN_PURPOSE;
  57.     EN.speed = SPEED_2MHZ;
  58.     //==========================================
  59.    
  60.     //==========================================
  61.     RW.port = RWport;
  62.     RW.pin = RWpin;
  63.     RW.mode = OUTPUT;
  64.   RW.mode_type = OUTPUT_GEN_PURPOSE;
  65.     RW.speed = SPEED_2MHZ;
  66.     //==========================================
  67.    
  68.     //==========================================
  69.     RS.port = RSport;
  70.     RS.pin = RSpin;
  71.     RS.mode = OUTPUT;
  72.   RS.mode_type = OUTPUT_GEN_PURPOSE;
  73.     RS.speed = SPEED_2MHZ;
  74.     //==========================================
  75.    
  76.     init_gpio(D4);
  77.     init_gpio(D5);
  78.     init_gpio(D6);
  79.     init_gpio(D7);
  80.     init_gpio(EN);
  81.     init_gpio(RW);
  82.     init_gpio(RS);
  83.    
  84.     //==============init LCD=================
  85.     delayMs(50);
  86.     pin_write(RS.port, RS.pin, LOW);
  87.     pin_write(RW.port, RW.pin, LOW);
  88.     pin_write(D4.port, D4.pin, HIGH);
  89.     pin_write(D5.port, D5.pin, HIGH);
  90.     pin_write(D6.port, D6.pin, LOW);
  91.     pin_write(D7.port, D7.pin, LOW);
  92.    
  93.     lcd_clock();
  94.    
  95.     delayMs(10);
  96.     pin_write(RS.port, RS.pin, LOW);
  97.     pin_write(RW.port, RW.pin, LOW);
  98.     pin_write(D4.port, D4.pin, HIGH);
  99.     pin_write(D5.port, D5.pin, HIGH);
  100.     pin_write(D6.port, D6.pin, LOW);
  101.     pin_write(D7.port, D7.pin, LOW);
  102.    
  103.     lcd_clock();
  104.    
  105.     delayMs(1);
  106.     pin_write(RS.port, RS.pin, LOW);
  107.     pin_write(RW.port, RW.pin, LOW);
  108.     pin_write(D4.port, D4.pin, HIGH);
  109.     pin_write(D5.port, D5.pin, HIGH);
  110.     pin_write(D6.port, D6.pin, LOW);
  111.     pin_write(D7.port, D7.pin, LOW);
  112.    
  113.     lcd_clock();   
  114.    
  115.     //==============init 4 bit mode=================
  116.     pin_write(RS.port, RS.pin, LOW);
  117.     pin_write(RW.port, RW.pin, LOW);
  118.     pin_write(D4.port, D4.pin, LOW);
  119.     pin_write(D5.port, D5.pin, HIGH);
  120.     pin_write(D6.port, D6.pin, LOW);
  121.     pin_write(D7.port, D7.pin, LOW);
  122.    
  123.     lcd_clock();
  124.    
  125.     //==============init 2 line 5X8=================
  126.     lcd_command(FOUR_BIT_MODE);
  127.  
  128.     //==============display off=================
  129.     lcd_command(DISPLAY_OFF);
  130.    
  131.     //==============clear display=================
  132.     lcd_command(DISPLAY_CLEAR);
  133.    
  134.     //===============entry mode set==========================
  135.     lcd_command(EM_INCREMENT);
  136.    
  137.     //===================display on==============================
  138.     lcd_command(DISPLAY_ON_COFF);
  139. }
  140.  
  141. void lcd_clock()
  142. {
  143.     pin_write(EN.port, EN.pin, HIGH);
  144.     delayMs(1);
  145.     pin_write(EN.port, EN.pin, LOW);
  146.     delayMs(1);
  147. }
  148.  
  149. void lcd_command(uint8_t command)
  150. {
  151.     //high bits
  152.     pin_write(RS.port, RS.pin, LOW);
  153.     pin_write(RW.port, RW.pin, LOW);
  154.    
  155.     pin_write(D4.port, D4.pin, (command>>4)&0x01);
  156.     pin_write(D5.port, D5.pin, (command>>5)&0x01);
  157.     pin_write(D6.port, D6.pin, (command>>6)&0x01);
  158.     pin_write(D7.port, D7.pin, (command>>7)&0x01);
  159.    
  160.     lcd_clock();
  161.     //low bits
  162.     pin_write(RS.port, RS.pin, LOW);
  163.     pin_write(RW.port, RW.pin, LOW);
  164.    
  165.     pin_write(D4.port, D4.pin, (command)&0x01);
  166.     pin_write(D5.port, D5.pin, (command>>1)&0x01);
  167.     pin_write(D6.port, D6.pin, (command>>2)&0x01);
  168.     pin_write(D7.port, D7.pin, (command>>3)&0x01);
  169.    
  170.     lcd_clock();   
  171. }
  172.  
  173. void lcd_write_char(uint8_t character)
  174. {
  175.     pin_write(RS.port, RS.pin, HIGH);
  176.     pin_write(RW.port, RW.pin, LOW);
  177.    
  178.     pin_write(D4.port, D4.pin, (character>>4)&0x01);
  179.     pin_write(D5.port, D5.pin, (character>>5)&0x01);
  180.     pin_write(D6.port, D6.pin, (character>>6)&0x01);
  181.     pin_write(D7.port, D7.pin, (character>>7)&0x01);
  182.    
  183.     lcd_clock();
  184.     //low bits
  185.     pin_write(RS.port, RS.pin, HIGH);
  186.     pin_write(RW.port, RW.pin, LOW);
  187.    
  188.     pin_write(D4.port, D4.pin, (character)&0x01);
  189.     pin_write(D5.port, D5.pin, (character>>1)&0x01);
  190.     pin_write(D6.port, D6.pin, (character>>2)&0x01);
  191.     pin_write(D7.port, D7.pin, (character>>3)&0x01);
  192.     lcd_clock();   
  193. }
  194.  
  195. void lcd_write_number(uint16_t number)
  196. {
  197.     uint8_t numberArray[4] = {0, 0, 0, 0};
  198.     uint8_t numOfDigits = 0;
  199.    
  200.     if(number < 10)
  201.     {
  202.             numOfDigits = 1;
  203.             numberArray[0] = (uint8_t)number;
  204.     }
  205.     else if(number < 100)
  206.     {
  207.         numOfDigits = 2;
  208.         numberArray[1] = (uint8_t)(number % 10);
  209.         numberArray[0] = (uint8_t)(number/10);
  210.     }
  211.     else if( number < 1000)
  212.     {
  213.         numOfDigits = 3;
  214.         numberArray[2] = (uint8_t)(number % 10);
  215.         numberArray[1] = (uint8_t)((number/10)%10);
  216.         numberArray[0] = (uint8_t)(number/100);    
  217.     }
  218.     else if( number < 10000)
  219.     {
  220.         numOfDigits = 4;
  221.         numberArray[3] = (uint8_t)(number % 10);
  222.         numberArray[2] = (uint8_t)((number/10)%10);
  223.         numberArray[1] = (uint8_t)((number/100)%10);
  224.         numberArray[0] = (uint8_t)(number/1000);       
  225.     }
  226.    
  227.    
  228.    
  229.     for(int i = 0; i < numOfDigits; i++)
  230.     {
  231.         numberArray[i] = numberArray[i] | 0x30;
  232.        
  233.         pin_write(RS.port, RS.pin, HIGH);
  234.         pin_write(RW.port, RW.pin, LOW);
  235.    
  236.         pin_write(D4.port, D4.pin, (numberArray[i]>>4)&0x01);
  237.         pin_write(D5.port, D5.pin, (numberArray[i]>>5)&0x01);
  238.         pin_write(D6.port, D6.pin, (numberArray[i]>>6)&0x01);
  239.         pin_write(D7.port, D7.pin, (numberArray[i]>>7)&0x01);
  240.    
  241.         lcd_clock();
  242.         //low bits
  243.         pin_write(RS.port, RS.pin, HIGH);
  244.         pin_write(RW.port, RW.pin, LOW);
  245.    
  246.         pin_write(D4.port, D4.pin, (numberArray[i])&0x01);
  247.         pin_write(D5.port, D5.pin, (numberArray[i]>>1)&0x01);
  248.         pin_write(D6.port, D6.pin, (numberArray[i]>>2)&0x01);
  249.         pin_write(D7.port, D7.pin, (numberArray[i]>>3)&0x01);
  250.         lcd_clock();   
  251.     }
  252.  
  253. }
  254.    
  255. void lcd_clear(uint8_t row, uint8_t start_collumn, uint8_t end_collumn)
  256. {
  257.     lcd_set_cursor(row, start_collumn);
  258.    
  259.     for(int i = 0; i < (end_collumn-start_collumn)+1; i++)
  260.     {
  261.         lcd_write_char(' ');
  262.     }
  263.    
  264. }
  265.  
  266. void lcd_set_cursor(uint8_t yPos, uint8_t xPos)
  267. {
  268.     if(yPos == 1)
  269.     {
  270.         xPos += 0x40;
  271.     }
  272.    
  273.     lcd_command(0x80 | xPos);
  274.    
  275. }
  276.  
  277. void lcd_create_character(uint8_t address, uint8_t character[])
  278. {
  279.     address = 8*address; //the CG address is 6 bits, the first 3 bits represent the specific
  280.                                              //address of the custom character, and the last 3  bits represent
  281.                                              //the address of the graphical bit values
  282.    
  283.     lcd_command(0x40 | address); //the 0x40 is the write to CGRAM command, followed by the address
  284.    
  285.     for(int i = 0; i < 8; i++)
  286.     {
  287.         lcd_write_char(character[i]);
  288.     }
  289. }
  290.  
  291.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement