[技术问题解答] 请教下我的KL25开发板LCD为什么不显示

[复制链接]
 楼主| bigbossc43 发表于 2015-11-2 14:09 | 显示全部楼层 |阅读模式
     我是新手对这个板子不怎么熟悉,想请教一下。就觉得很奇怪为什么LCD(8位,16*2,HD44780)只能亮不能显示文字,因为我在LCD控制函数后面设置断点,并通过watch window观察了8位GPIO口的数据了,是跟我想输入的函数相符合的,另外也通过E置1再置0输出给LCD了,为什么还是不行。另外还有点很奇怪的是我的函数包含了一个系统延迟,如果不设置断点run了以后再reset是回到start up文件中的system_initial函数,并且怎么也跳不出来。如果把系统延迟换成麻瓜while延迟就不会卡在循环里,这是什么情况。。          后面附带我写的程序和问题截图,也是非常的菜。一开始我们组搞硬件的根据网上资料连的数据线,分散得乱七八糟,我lcd_write程序也是写得非常奇葩,但是debugger模式下观察gpio的结果还是对的。我同学说我没设置地址线,我也搞不清,需要地址线吗?不是直接把data传给lcd就行了嘛= =求指导,这个程序已经搞了2天了还没有头绪。。
  1. #include <MKL25Z4.h>

  2. // Program ID
  3. uint8_t program_author[]   = "Donald Weiman";
  4. uint8_t program_version[]  = "LCD-AVR-8d-s (gcc)";
  5. uint8_t program_date[]     = "Sep 16, 2013";

  6. // Function Prototypes
  7. void lcd_write_8(uint8_t);
  8. void lcd_write_instruction_8d(uint8_t);
  9. void lcd_write_character_8d(uint8_t);
  10. void lcd_write_string_8d(uint8_t *);
  11. void lcd_init_8d(void);

  12. //system delay
  13. volatile uint32_t msTicks;

  14. void SysTick_Handler(void) {
  15.   msTicks++;                        /* increment counter necessary in Delay() */
  16. }

  17. __INLINE static void Delay (uint32_t dlyTicks) {
  18.   uint32_t curTicks;
  19.   curTicks = msTicks;
  20. while ((msTicks - curTicks) < dlyTicks);
  21. }
  22. //simple delay
  23. //void Delay(int a){
  24. //        int i;
  25. //        for(i=0;i<a*100;i++);
  26. //}
  27.        
  28. /******************************* Main Program Code *************************/
  29. int main(void)
  30. {
  31.                 SystemCoreClockUpdate();
  32. // initialize the LCD controller as determined by the defines (LCD instructions)
  33.     lcd_init_8d();                                  // initialize the LCD display for an 8-bit interface
  34.        
  35.                 SysTick_Config(10000);
  36.    
  37. // display the first line of information
  38.     lcd_write_string_8d(program_author);

  39. // set cursor to start of second line
  40.     lcd_write_instruction_8d(0xC0);
  41.     Delay(0x1);                                  // 40 uS delay (min)

  42. // display the second line of information
  43.     lcd_write_string_8d(program_version);

  44. // endless loop
  45.     while(1);
  46. //    return 0;
  47. }
  48. /******************************* End of Main Program Code ******************/

  49. /*============================== 8-bit LCD Functions ======================*/
  50. /*
  51.   Name:     lcd_init_8d
  52.   Purpose:  initialize the LCD module for a 8-bit data interface
  53.   Entry:    equates (LCD instructions) set up for the desired operation
  54.   Exit:     no parameters
  55.   Notes:    uses time delays rather than checking the busy flag
  56. */



  57. void lcd_init_8d(void)
  58. {
  59.                 SIM->SCGC5|=(1<<9)|(1<<10)|(1<<12);
  60.                 PORTA->PCR[1]=(1<<8);
  61.                 PORTA->PCR[2]=(1<<8);
  62.                 PORTD->PCR[4]=(1<<8);
  63.                 PORTA->PCR[12]=(1<<8);
  64.                 PORTB->PCR[0]=(1<<8);
  65.                 PORTB->PCR[1]=(1<<8);
  66.                 PORTB->PCR[2]=(1<<8);
  67.                 PORTB->PCR[3]=(1<<8);
  68. // Power-up delay
  69.     Delay(0x100); // initial 40 mSec delay

  70. // configure the microprocessor pins for the data lines                          
  71.                 PTA->PDDR=(1<<1)|(1<<2)|(1<<12);                              // all 8 data lines - output
  72.                 PTB->PDDR=(1<<0)|(1<<1)|(1<<2)|(1<<3);
  73.                 PTD->PDDR=(1<<4);
  74. // configure the microprocessor pins for the control lines
  75.     PTD->PDDR|=(1<<0);                                                   // E line - output
  76.     PTA->PDDR|=(1<<13);                                           // RS line - output
  77.        
  78. // Reset the LCD controller
  79.                 PTD->PCOR=(1<<5);                                                                                                                           //  R/W=0     PCOR doesn't change
  80.                
  81.                 lcd_write_instruction_8d(0x38);                                               // 0011 1000=fucntion set
  82.     Delay(0x100);                                   // 4.1 mS delay (min)

  83.                 lcd_write_instruction_8d(0x01);                                                                        //0000 0001=clear display
  84.                 Delay(0x100);                                   // 40 uS delay (min)

  85. // Display On/Off Control instruction
  86.                 lcd_write_instruction_8d(0x0E);                                                                         // 0000 1110=turn the display ON
  87.     Delay(0x100);                                   // 40 uS delay (min)
  88.                
  89.                 lcd_write_instruction_8d(0x06);                                                                        // 0000 0110=entry mode set
  90. }

  91. /*...........................................................................
  92.   Name:     lcd_write_string_8d
  93. ; Purpose:  display a string of characters on the LCD
  94.   Entry:    (theString) is the string to be displayed
  95.   Exit:     no parameters
  96.   Notes:    uses time delays rather than checking the busy flag
  97. */
  98. void lcd_write_string_8d(uint8_t theString[])
  99. {
  100.     volatile int i = 0;                             // character counter*/
  101.     while (theString[i] != 0)
  102.     {
  103.         lcd_write_character_8d(theString[i]);
  104.         i++;
  105.         Delay(0x100);                              // 40 uS delay (min)
  106.     }
  107. }

  108. /*...........................................................................
  109.   Name:     lcd_write_character_8d
  110.   Purpose:  send a byte of information to the LCD data register
  111.   Entry:    (theData) is the information to be sent to the data register
  112.   Exit:     no parameters
  113.   Notes:    does not deal with RW (busy flag is not implemented)
  114. */
  115. void lcd_write_character_8d(uint8_t theData)
  116. {
  117.     PTA->PSOR=(1<<13);                 // select the Data Register (RS high)
  118.     PTD->PCOR=(1<<0);                  // make sure E is initially low
  119.     lcd_write_8(theData);                           // write the data
  120. }

  121. /*...........................................................................
  122.   Name:     lcd_write_instruction_8d
  123.   Purpose:  send a byte of information to the LCD instruction register
  124.   Entry:    (theInstruction) is the information to be sent to the instruction register
  125.   Exit:     no parameters
  126.   Notes:    does not deal with RW (busy flag is not implemented)
  127. */
  128. void lcd_write_instruction_8d(uint8_t theInstruction)
  129. {
  130.     PTA->PCOR=(1<<13);                // select the Instruction Register (RS low)
  131.     PTD->PCOR=(1<<0);                  // make sure E is initially low
  132.     lcd_write_8(theInstruction);                    // write the instruction
  133. }

  134. /*...........................................................................
  135.   Name:     lcd_write_8
  136.   Purpose:  send a byte of information to the LCD module
  137.   Entry:    (theByte) is the information to be sent to the desired LCD register
  138.             RS is configured for the desired LCD register
  139.             E is low
  140.             RW is low
  141.   Exit:     no parameters
  142.   Notes:    use either time delays or the busy flag
  143. */
  144. void lcd_write_8(uint8_t theByte)
  145. {
  146.                                                     // 'Address set-up time' (40 nS)
  147.     uint8_t a;
  148.                 int i;
  149.                 PTA->PDOR=(0<<1)|(0<<2)|(0<<12);                              // all 8 data lines - output
  150.                 PTB->PDOR=(0<<0)|(0<<1)|(0<<2)|(0<<3);
  151.                 PTD->PDOR=(0<<4);
  152.                 for(i=0;i<8;i++){
  153.                         a=theByte>>i;
  154.                         a=a&(0x01);
  155.                         switch(i){
  156.                                 case 0:
  157.                                         if(a==0x01){
  158.                                                 PTA->PDOR|=(1<<1);
  159.                                         }
  160.                                         break;
  161.                                 case 1:
  162.                                         if(a==0x01){
  163.                                                 PTA->PDOR|=(1<<2);
  164.                                         }
  165.                                         break;
  166.                                 case 2:
  167.                                         if(a==0x01){
  168.                                                 PTD->PDOR|=(1<<4);
  169.                                         }
  170.                                         break;
  171.                                 case 3:
  172.                                         if(a==0x01){
  173.                                                 PTA->PDOR|=(1<<12);
  174.                                         }
  175.                                         break;
  176.                                 case 4:
  177.                                         if(a==0x01){
  178.                                                 PTB->PDOR|=(1<<0);
  179.                                         }
  180.                                         break;
  181.                                 case 5:
  182.                                         if(a==0x01){
  183.                                                 PTB->PDOR|=(1<<1);
  184.                                         }
  185.                                         break;
  186.                                 case 6:
  187.                                         if(a==0x01){
  188.                                                 PTB->PDOR|=(1<<2);
  189.                                         }
  190.                                         break;
  191.                                 case 7:
  192.                                         if(a==0x01){
  193.                                                 PTB->PDOR|=(1<<3);
  194.                                         }
  195.                                         break;
  196.                         }
  197.                         a=theByte;
  198.                 }
  199.                                                                                                                                                                                                                 // put information on data lines
  200.     PTD->PSOR=(1);                                                                            // Enable pin high
  201.     Delay(0x100);                                   // implement 'Data set-up time' (80 nS) and 'Enable pulse width' (230 nS)
  202.     PTD->PCOR=(1);                                                                                         // Enable pin low
  203.     Delay(0x100);                                   // implement 'Data hold time' (10 nS) and 'Enable cycle time' (500 nS)
  204. }
QQ图片20151102010334.png
一旦使用系统延迟就卡BX在这里,很怪,单步执行挑不出来,暂停也跳不出来
FSL_TICS_ZJJ 发表于 2015-11-2 14:20 | 显示全部楼层
楼主你好!
和LCD结合,首先你要了解你使用的LCD,需要哪些信号线,控制的字码是怎么点亮的,然后再用程序去控制。
所以,你先检查下,你的LCD相关控制线,数据,命令等是否按照你的LCD屏来的,你可以找个你那款LCD的参考例程参考下。
 楼主| bigbossc43 发表于 2015-11-3 11:30 | 显示全部楼层
FSL_TICS_ZJJ 发表于 2015-11-2 14:20
楼主你好!
和LCD结合,首先你要了解你使用的LCD,需要哪些信号线,控制的字码是怎么点亮的,然后再用程序去 ...

好的,谢谢,我再试试
您需要登录后才可以回帖 登录 | 注册

本版积分规则

3

主题

6

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部