Facebook
From aska, 5 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 213
  1. /*----------------------------------------------------------------------------
  2.  *      
  3.  *----------------------------------------------------------------------------
  4.  *      Name:    slcd.c
  5.  *      Purpose: Microprocessors Laboratory
  6.  *----------------------------------------------------------------------------
  7.  *      
  8.  *---------------------------------------------------------------------------*/
  9.  
  10. #include "slcd.h"                                                                                               //Declarations
  11.  
  12. /*----------------------------------------------------------------------------
  13.   Function that initializes sLCD
  14.  *----------------------------------------------------------------------------*/
  15. void slcdInitialize(void){
  16.        
  17.         SIM->SCGC5 |= SIM_SCGC5_PORTB_MASK | SIM_SCGC5_PORTC_MASK | /* Enable Clock to Port B & C */
  18.                                                                 SIM_SCGC5_PORTD_MASK | SIM_SCGC5_PORTE_MASK | SIM_SCGC5_SLCD_MASK; /* Enable Clock to Port D & E and sLCD module*/
  19.         LCD->GCR |= LCD_GCR_PADSAFE_MASK; // Set PADSAFE during configuration
  20.         LCD->GCR &= ~LCD_GCR_LCDEN_MASK; // Clear LCDEN during configuration
  21.         //multiplexers configuration for ports to act as sLCD controller
  22.        
  23.         //TASK 2.3 - replace XXX with pin number, and YYY with proper alternative number
  24.         PORTB->PCR[7] = PORT_PCR_MUX(0UL);
  25.         PORTB->PCR[8] = PORT_PCR_MUX(0UL);
  26.         PORTB->PCR[10] = PORT_PCR_MUX(0UL);
  27.         PORTB->PCR[11] = PORT_PCR_MUX(0UL);
  28.         PORTB->PCR[21] = PORT_PCR_MUX(0UL);
  29.         PORTB->PCR[22] = PORT_PCR_MUX(0UL);
  30.         PORTB->PCR[23] = PORT_PCR_MUX(0UL);
  31.         PORTC->PCR[17] = PORT_PCR_MUX(0UL);
  32.         PORTC->PCR[18] = PORT_PCR_MUX(0UL);
  33.         PORTD->PCR[0] = PORT_PCR_MUX(0UL);
  34.         PORTE->PCR[4] = PORT_PCR_MUX(0UL);
  35.         PORTE->PCR[5] = PORT_PCR_MUX(0UL);
  36.        
  37.         //sLCD register configuration
  38.         LCD->GCR = LCD_GCR_RVTRIM(0x00) | LCD_GCR_CPSEL_MASK | LCD_GCR_LADJ(0x03) |
  39.                                                  LCD_GCR_VSUPPLY_MASK | LCD_GCR_ALTDIV(0x00) |
  40.                                                  LCD_GCR_SOURCE_MASK | LCD_GCR_LCLK(0x01) | LCD_GCR_DUTY(0x03);
  41.         //sLCD blinking configuration
  42.         LCD->AR = LCD_AR_BRATE(0x03);  
  43.         //FDCR register configuration
  44.         LCD->FDCR = 0x00000000;
  45.   //activation of 12 pins to control sLCD (2 registers, 32 bits each)
  46.         LCD->PEN[0] = LCD_PEN_PEN(1u<<7) | // LCD_P7
  47.                                                                 LCD_PEN_PEN(1u<<8) | // LCD_P8
  48.                                                                 LCD_PEN_PEN(1u<<10) | // LCD_P10
  49.                                                                 LCD_PEN_PEN(1u<<11) | // LCD_P11
  50.                                                                 LCD_PEN_PEN(1u<<17) | // LCD_P17
  51.                                                                 LCD_PEN_PEN(1u<<18) | // LCD_P18
  52.                                                                 LCD_PEN_PEN(1u<<19); // LCD_P19
  53.         LCD->PEN[1] = LCD_PEN_PEN(1u<<5) | // LCD_P37
  54.                                                                 LCD_PEN_PEN(1u<<6) | // LCD_P38
  55.                                                                 LCD_PEN_PEN(1u<<8) | // LCD_P40
  56.                                                                 LCD_PEN_PEN(1u<<20) | // LCD_P52
  57.                                                                 LCD_PEN_PEN(1u<<21); // LCD_P53
  58.         //configuration of 4 back plane pins (2 registers, 32 bits each)
  59.         LCD->BPEN[0] = LCD_BPEN_BPEN(1u<<18) | //LCD_P18
  60.                                                                  LCD_BPEN_BPEN(1u<<19); //LCD_P19
  61.         LCD->BPEN[1] = LCD_BPEN_BPEN(1u<<8) | //LCD_P40
  62.                                                                  LCD_BPEN_BPEN(1u<<20); //LCD_P52
  63.  
  64.         // waveform registers configuration – 4 active (because of 4 back planes)
  65.         // (44.3.7 in KL46 Reference Manual)
  66.         LCD->WF[4] =  LCD_WF_WF16(0x00) | LCD_WF_WF17(0x00) |
  67.                                                           LCD_WF_WF18(0x88) | // COM3 (10001000)
  68.                                                           LCD_WF_WF19(0x44);  // COM2 (01000100)
  69.         LCD->WF[10] = LCD_WF_WF40(0x11) | // COM0 (00010001)
  70.                                                                 LCD_WF_WF41(0x00) | LCD_WF_WF42(0x00) | LCD_WF_WF43(0x00);
  71.        
  72.         LCD->WF[13] = LCD_WF_WF52(0x22) | // COM1 (00100010)
  73.                                                                 LCD_WF_WF53(0x00) | LCD_WF_WF54(0x00) | LCD_WF_WF55(0x00);
  74.        
  75.         LCD->GCR &= ~LCD_GCR_PADSAFE_MASK; // Clear PADSAFE at the end of configuration
  76.         LCD->GCR |= LCD_GCR_LCDEN_MASK; // Set LCDEN at the end of configuration
  77. }
  78. /*----------------------------------------------------------------------------
  79.   Function that outputs on sLCD (number) error message for debug purpose
  80.  *----------------------------------------------------------------------------*/
  81. void slcdErr(uint8_t number){
  82.         LCD->WF8B[LCD_Front_Pin[0]] = (LCD_S_G | LCD_S_E | LCD_S_D | LCD_S_F); //1st digit
  83.         LCD->WF8B[LCD_Front_Pin[1]] = (LCD_S_A);
  84.        
  85.         LCD->WF8B[LCD_Front_Pin[2]] = (LCD_S_E | LCD_S_G); //2nd digit
  86.         LCD->WF8B[LCD_Front_Pin[3]] = (LCD_C);
  87.        
  88.         LCD->WF8B[LCD_Front_Pin[4]] = (LCD_S_E | LCD_S_G); //3rd digit
  89.         LCD->WF8B[LCD_Front_Pin[5]] = (LCD_C);
  90.        
  91.         switch(number){  //4 digit depends on provided number
  92.                 case 0x00:
  93.                         LCD->WF8B[LCD_Front_Pin[6]] = (LCD_S_D | LCD_S_E | LCD_S_F); /* To display '0' we must active segments: d, e & f on first front plane */
  94.                         LCD->WF8B[LCD_Front_Pin[7]] = (LCD_S_A | LCD_S_B | LCD_S_C); /* To display '0' we must active segments: a, b & c on second front plane */
  95.                         break;
  96.                 case 0x01:
  97.                         LCD->WF8B[LCD_Front_Pin[6]] = (LCD_C);  //TASK 2.5 - replace ZZZ with segment definition, do czyszczenia
  98.                         LCD->WF8B[LCD_Front_Pin[7]] = (LCD_S_B | LCD_S_C);  //TASK 2.5 - replace ZZZ with segment definition
  99.                         break;
  100.                 case 0x02:
  101.                         LCD->WF8B[LCD_Front_Pin[6]] = (LCD_S_G | LCD_S_E | LCD_S_D );  //TASK 2.5 - replace ZZZ with segment definition
  102.                         LCD->WF8B[LCD_Front_Pin[7]] = (LCD_S_A | LCD_S_B);  //TASK 2.5 - replace ZZZ with segment definition
  103.                         break;
  104.                 case 0x03:
  105.                         LCD->WF8B[LCD_Front_Pin[6]] = (LCD_S_G | LCD_S_D);  //TASK 2.5 - replace ZZZ with segment definition
  106.                         LCD->WF8B[LCD_Front_Pin[7]] = (LCD_S_A | LCD_S_B | LCD_S_C);  //TASK 2.5 - replace ZZZ with segment definition
  107.                         break;
  108.                 case 0x04:
  109.                         LCD->WF8B[LCD_Front_Pin[6]] = (LCD_S_G | LCD_S_F);
  110.                         LCD->WF8B[LCD_Front_Pin[7]] = (LCD_S_B | LCD_S_C);
  111.                         break;
  112.                 default:   //as default display 'r'
  113.                         LCD->WF8B[LCD_Front_Pin[6]] = (LCD_S_E | LCD_S_G);
  114.                         LCD->WF8B[LCD_Front_Pin[7]] = (LCD_C);
  115.                         break;
  116.         }
  117. }
  118. /*----------------------------------------------------------------------------
  119.   Writing on sLCD (value) on specified position (digit)
  120. *
  121. * FP0 - Front Plane 0
  122. *  f |_g
  123. *  e |_
  124. *    d
  125. *
  126. * FP1 - Front Plane 1
  127. *    a_
  128. *      | b
  129. *    c |.
  130. *        dot
  131.  *----------------------------------------------------------------------------*/
  132. void slcdSet(uint8_t value, uint8_t digit){    
  133.  
  134.         switch(value){
  135.                 case 0x00:
  136.                         LCD->WF8B[LCD_Front_Pin[((2*digit)-2)]] = (LCD_S_D | LCD_S_E | LCD_S_F); /* To display '0' we must active segments: d, e & f on first front plane */
  137.                         LCD->WF8B[LCD_Front_Pin[((2*digit)-1)]] = (LCD_S_A | LCD_S_B | LCD_S_C); /* To display '0' we must active segments: a, b & c on second front plane */
  138.                         break;
  139.                 case 0x01:
  140.                         LCD->WF8B[LCD_Front_Pin[((2*digit)-2)]] = (LCD_C);  //TASK 2.5 - replace ZZZ with segment definition
  141.                         LCD->WF8B[LCD_Front_Pin[((2*digit)-1)]] = (LCD_S_B | LCD_S_C);  //TASK 2.5 - replace ZZZ with segment definition
  142.                         break;
  143.                 case 0x02:
  144.                         LCD->WF8B[LCD_Front_Pin[((2*digit)-2)]] = (LCD_S_G | LCD_S_E | LCD_S_D );  //TASK 2.5 - replace ZZZ with segment definition
  145.                         LCD->WF8B[LCD_Front_Pin[((2*digit)-1)]] = (LCD_S_A | LCD_S_B);  //TASK 2.5 - replace ZZZ with segment definition
  146.                         break;
  147.                 case 0x03:
  148.                         LCD->WF8B[LCD_Front_Pin[((2*digit)-2)]] = (LCD_S_G | LCD_S_D);  //TASK 2.5 - replace ZZZ with segment definition
  149.                         LCD->WF8B[LCD_Front_Pin[((2*digit)-1)]] = (LCD_S_A | LCD_S_B | LCD_S_C);  //TASK 2.5 - replace ZZZ with segment definition
  150.                         break;
  151.                 case 0x04:
  152.                         LCD->WF8B[LCD_Front_Pin[((2*digit)-2)]] = (LCD_S_G | LCD_S_F);
  153.                         LCD->WF8B[LCD_Front_Pin[((2*digit)-1)]] = (LCD_S_B | LCD_S_C);
  154.                         break;
  155.                 case 0x05:
  156.                         LCD->WF8B[LCD_Front_Pin[((2*digit)-2)]] = (LCD_S_G | LCD_S_F | LCD_S_D);
  157.                         LCD->WF8B[LCD_Front_Pin[((2*digit)-1)]] = (LCD_S_A | LCD_S_C);
  158.                         break;
  159.                 case 0x06:
  160.                         LCD->WF8B[LCD_Front_Pin[((2*digit)-2)]] = (LCD_S_G | LCD_S_E | LCD_S_D  | LCD_S_F);
  161.                         LCD->WF8B[LCD_Front_Pin[((2*digit)-1)]] = (LCD_S_A | LCD_S_C);
  162.                         break;
  163.                 case 0x07:
  164.                         LCD->WF8B[LCD_Front_Pin[((2*digit)-2)]] = (LCD_C);
  165.                         LCD->WF8B[LCD_Front_Pin[((2*digit)-1)]] = (LCD_S_A | LCD_S_B | LCD_S_C);
  166.                         break;
  167.                 case 0x08:
  168.                         LCD->WF8B[LCD_Front_Pin[((2*digit)-2)]] = (LCD_S_G | LCD_S_E | LCD_S_D  | LCD_S_F);
  169.                         LCD->WF8B[LCD_Front_Pin[((2*digit)-1)]] = (LCD_S_A | LCD_S_B | LCD_S_C);
  170.                         break;
  171.                 case 0x09:
  172.                         LCD->WF8B[LCD_Front_Pin[((2*digit)-2)]] = (LCD_S_F | LCD_S_G | LCD_S_D);
  173.                         LCD->WF8B[LCD_Front_Pin[((2*digit)-1)]] = (LCD_S_A | LCD_S_B | LCD_S_C);
  174.                         break;
  175.                 case 0x0A:
  176.                         LCD->WF8B[LCD_Front_Pin[((2*digit)-2)]] = (LCD_S_F | LCD_S_E | LCD_S_G);
  177.                         LCD->WF8B[LCD_Front_Pin[((2*digit)-1)]] = (LCD_S_A | LCD_S_B | LCD_S_C);
  178.                         break;
  179.                 case 0x0B:
  180.                         LCD->WF8B[LCD_Front_Pin[((2*digit)-2)]] = (LCD_S_C);
  181.                         LCD->WF8B[LCD_Front_Pin[((2*digit)-1)]] = (LCD_S_G | LCD_S_E | LCD_S_D | LCD_S_F);
  182.                         break;
  183.                 case 0x0C:
  184.                         LCD->WF8B[LCD_Front_Pin[((2*digit)-2)]] = (LCD_S_F | LCD_S_E | LCD_S_D);
  185.                         LCD->WF8B[LCD_Front_Pin[((2*digit)-1)]] = (LCD_S_A);
  186.                         break;
  187.                 case 0x0D:
  188.                         LCD->WF8B[LCD_Front_Pin[((2*digit)-2)]] = (LCD_S_G | LCD_S_E | LCD_S_D);
  189.                         LCD->WF8B[LCD_Front_Pin[((2*digit)-1)]] = (LCD_S_B | LCD_S_C);
  190.                         break;
  191.                 case 0x0E:
  192.                         LCD->WF8B[LCD_Front_Pin[((2*digit)-2)]] = (LCD_S_G | LCD_S_E | LCD_S_D | LCD_S_F);
  193.                         LCD->WF8B[LCD_Front_Pin[((2*digit)-1)]] = (LCD_S_A);
  194.                         break;
  195.                 case 0x0F:
  196.                         LCD->WF8B[LCD_Front_Pin[((2*digit)-2)]] = (LCD_S_F | LCD_S_E | LCD_S_G);
  197.                         LCD->WF8B[LCD_Front_Pin[((2*digit)-1)]] = (LCD_S_A);
  198.                         break;
  199.                 default:   //as default display 'r'
  200.                         LCD->WF8B[LCD_Front_Pin[((2*digit)-2)]] = (LCD_S_E | LCD_S_G);
  201.                         LCD->WF8B[LCD_Front_Pin[((2*digit)-1)]] = (LCD_C);
  202.                         break;
  203.         }
  204.         if(digit>4){
  205.                 slcdErr(1);
  206.         }
  207. }
  208. /*----------------------------------------------------------------------------
  209.   Function for conversion purpose, displaying (value) in specified (format)
  210.  *----------------------------------------------------------------------------*/
  211. void slcdDisplay(uint16_t value ,uint16_t format){     
  212.         //uint16_t tab[4] = {1, 2, 3, 4};
  213.         //int i = 0;
  214.         uint16_t  tVariable = 0;
  215.         /* TASK 2.7: Write value to the table in specified format */
  216.         uint16_t x[4];
  217.         x[3] = value % format;
  218.         tVariable = value - x[3]; //full number of system
  219.  
  220.         x[2] = tVariable / format % format;
  221.         x[1] = tVariable / (format*format) %format;
  222.         x[0] = tVariable / (format*format*format) % format;
  223.        
  224.        
  225.         slcdSet(x[0], 1);
  226.         slcdSet(x[1], 2);
  227.         slcdSet(x[2], 3);
  228.         slcdSet(x[3], 4);
  229.        
  230.         /*for(i=0; i < 4; i++){        
  231.                 slcdClear();
  232.                 slcdSet(x[i], i+1);
  233.                 delay_mc(300);
  234.                 slcdClear();
  235.         }*/
  236. }
  237. /*----------------------------------------------------------------------------
  238.   Clear sLCD
  239.  *----------------------------------------------------------------------------*/
  240. void slcdClear(void){
  241.         uint8_t i = 0;
  242.        
  243.         for(i=0; i < 8; i++){
  244.                 LCD->WF8B[LCD_Front_Pin[i]] = LCD_C;
  245.         }
  246. }
  247. /*----------------------------------------------------------------------------
  248.   Function for demo purpose
  249.  *----------------------------------------------------------------------------*/
  250. void slcdDemo(void){   
  251.         uint8_t i;
  252.        
  253.         for(i=0; i < 0x10; i++){
  254.                 slcdClear();
  255.                 slcdSet(i, i%4 + 1);
  256.                 delay_mc(300);
  257.                 slcdClear();
  258.         }
  259. }
  260.