使用mikroc开发的PIC16F877A LCD无法正常工作

时间:2015-09-04 16:55:45

标签: c microcontroller pic lcd mikroc

我一直在使用PIC微控制器设计一个简单的交通信号代码,它应该有一个控制面板,您可以用它来输入每个红绿灯的时间,使其更安全我添加了一个密码来阻止任何人弄乱随着时间的推移。

我使用Proteus 8进行模拟。问题发生在有时当我在液晶显示器上打印出来的东西不正确时,我仍然无法确定为什么会发生这种情况。当输入红色时间并且黄色开始时,显示: http://i.stack.imgur.com/lnbrI.png

同样的事情发生在绿色时间,我尝试而不是自动输入字符串将其放入变量中,研究没有得到任何特定于我的问题! 这是我的代码:

char kp;
char entry[16];
int i = 0;
char password[] = "12345";
char *Redd = "Red";
char *Yelloww = "Yellow";
char *Greenn = "Green";
int Red;

int Green;
int Yellow;
char test[5];


sbit LCD_RS at RD4_bit;
sbit LCD_EN at RD6_bit; 
sbit LCD_D4 at RD0_bit;
sbit LCD_D5 at RD1_bit;
sbit LCD_D6 at RD2_bit;
sbit LCD_D7 at RD3_bit;

sbit LCD_RS_Direction at TRISD4_bit;
sbit LCD_EN_Direction at TRISD6_bit;
sbit LCD_D4_Direction at TRISD0_bit;
sbit LCD_D5_Direction at TRISD1_bit;
sbit LCD_D6_Direction at TRISD2_bit;
sbit LCD_D7_Direction at TRISD3_bit;

char keypadPort at PORTB;



char keypadchar () {
  int key = 0;
  while (!key) key = Keypad_Key_Click();
  switch (key) {
    case 1: return '1'; break;
    case 2: return '2'; break;
    case 3: return '3'; break;
    case 5: return '4'; break;
    case 6: return '5'; break;
    case 7: return '6'; break;
    case 9: return '7'; break;
    case 10: return '8'; break;
    case 11: return '9'; break;
    case 13: return '*'; break;
    case 14: return '0'; break;
    case 15: return '#'; break;
  }
}

void inputPassword() {
  int count = 0;
  i = 0;
  Lcd_out(1,1,"Enter Password");
  lcd_cmd(_LCD_SECOND_ROW);
  while(1) {
    kp = keypadchar();
    if (kp != '*' && kp != '#' && count < 15) {
      Lcd_Chr_Cp('*');
      while(entry[i]!=0) i++;    // break when find NULL
      entry[i] = kp;     // insert char where NULL was
      entry[i+1] = 0;   // and insert new NULL after it
      i = 0;
      count++;
    } else if (kp == '*') {
      if (strlen(entry) != 0) {
        LCD_Cmd(_LCD_MOVE_CURSOR_LEFT);
        Lcd_Chr_Cp(' ');
        LCD_Cmd(_LCD_MOVE_CURSOR_LEFT);
        while(entry[i]!=0) i++;    // break when find NULL
        entry[i-1] = 0;     // insert Null
        i = 0;
        count--;
      }
    } else if (kp == '#') {
      if (strlen(entry) != 0)
        if (strcmp(password,entry) == 0) {
          Lcd_out(2,1,"Password Correct  ");
          while(entry[i]!=0) {
            entry[i] = 0;
            i++;
          }
          i = 0;
          break;
        }
        else {
          Lcd_out(2,1,"Wrong password  ");
          delay_ms(1000);
          Lcd_out(2,1,"                  ");
          Lcd_out(2,1,"");
          while(entry[i]!=0) {
            entry[i] = 0;
            i++;
          }
          count = 0;
          i = 0;
        }
    }
  }
}

int entertiming(char *colorN) {
  int count = 0;
  i = 0;
  LCD_Cmd(_LCD_CLEAR);
  delay_ms(1);
  strcat(colorN," timing");
  delay_ms(1);
  LCD_Out(1,1,colorN);
  delay_ms(1);
  LCD_Cmd(_LCD_SECOND_ROW);
  while(entry[i]!=0) {
    entry[i] = 0;
    i++;
  }
  i = 0;
  while(1) {
    kp = keypadchar();
    if (kp != '*' && kp != '#' && count < 3) {
      Lcd_Chr_Cp(kp);
      while(entry[i]!=0) i++;    // break when find NULL
      entry[i] = kp;     // insert char where NULL was
      entry[i+1] = 0;   // and insert new NULL after it
      i = 0;
      count++;
    } else if (kp == '*') {
      if (strlen(entry) != 0) {
        LCD_Cmd(_LCD_MOVE_CURSOR_LEFT);
        Lcd_Chr_Cp(' ');
        LCD_Cmd(_LCD_MOVE_CURSOR_LEFT);
        while(entry[i]!=0) i++;    // break when find NULL
        entry[i-1] = 0;     // insert Null
        i = 0;
        count--;
      }
    } else if (kp == '#') {
      if (strlen(entry) != 0)
        return atoi(entry);
    }
  }
}

void startcountdown(int time) {
  while (time >= 0) {
    wordtostr(time,test);
    lcd_out(2,1,test);
    time--;
    delay_ms(1000);
    }
  } 

void main () {
  Keypad_init();
  Lcd_init();
  LCD_Cmd(_LCD_CLEAR) ;
  LCD_Out(1, 1, "Hello!") ;
  Lcd_Cmd(_LCD_SECOND_ROW);
  delay_ms(1000);
  while(1) {
    inputPassword();
    delay_ms(1);
    Red = entertiming(Redd);
    delay_ms(1);
    Yellow = entertiming(Yelloww);
    delay_ms(1);
    Green = entertiming(Greenn); 
    while(1) {
      Lcd_cmd(_LCD_CLEAR);
      Lcd_Out(1,1,"Red");
      startcountdown(Red);
      Lcd_cmd(_LCD_CLEAR);
      Lcd_Out(1,1,"Yellow");
      startcountdown(Yellow);
      Lcd_cmd(_LCD_CLEAR);
      Lcd_Out(1,1,"Green");
      startcountdown(Green);
      }
   }
}

编辑我用更新的代码编辑了代码,按照评论中的建议,我还完成了代码,除了一个小块,内部while循环保持倒计时应该有当发生中断以改变红色/黄色/绿色时间的值时发生的中断命令.. 并且出现新问题,我得到的黄色读数总是转换为0!无论我输入什么号码

第二次编辑:当我更改红色,黄色,绿色功能的序列时,某些序列有效,有些则没有意义

0 个答案:

没有答案