Thai microtron community

Electronic Knowledge Base => Microcontoller & Electronics => Topic started by: muya on February 02, 2014, 05:30:17 PM



Title: PIC 18F258 กับจอ LCD 16x2
Post by: muya on February 02, 2014, 05:30:17 PM
(http://upic.me/i/34/5capture.png)

// LCD Module //
sbit LCD_RS at LATB4_bit;
sbit LCD_EN at LATB5_bit;
sbit LCD_D4 at LATB0_bit;
sbit LCD_D5 at LATB1_bit;
sbit LCD_D6 at LATB2_bit;
sbit LCD_D7 at LATB3_bit;

sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;


// Define Messages
char message0[] = "LCD Initialized";
char message1[] = "Room Temperature";

// String array to store temperature value to display
char *tempC = "000.0";
char *tempF = "000.0";

// Variables to store temperature register values
unsigned int temp_whole, temp_fraction, temp_value;
signed int tempinF, tempinC;
unsigned short C_Neg=0, F_Neg=0, TempH, TempL;

void Display_Temperature() {
  // convert Temp to characters
 if (!C_Neg) {
     if (tempinC/1000)
   // 48 is the decimal character code value for displaying 0 on LCD
     tempC[0] = tempinC/1000  + 48;
     else tempC[0] = ' ';
  }
  tempC[1] = (tempinC/100)%10 + 48;             // Extract tens digit
  tempC[2] =  (tempinC/10)%10 + 48;             // Extract ones digit

  // convert temp_fraction to characters
  tempC[4] =  tempinC%10  + 48;         // Extract tens digit

  // print temperature on LCD
  Lcd_Out(2, 1, tempC);

  if (!F_Neg) {
     if (tempinF/1000)
      tempF[0] = tempinF/1000  + 48;
     else tempF[0] = ' ';
  }

  tempF[1] = (tempinF/100)%10 + 48;             // Extract tens digit
  tempF[2] =  (tempinF/10)%10 + 48;
  tempF[4] =  tempinF%10  + 48;
  // print temperature on LCD
  Lcd_Out(2, 10, tempF);
}

void main()
{
ADCON0 = 0;
ADCON1 = 0x07;
  TRISB = 0b00100000;
  PORTB =   0b00000000;
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
 Lcd_Out(1,1,message0);
  Delay_ms(1000);
  Lcd_Out(1,1,message1);             // Write message1 in 1st row
  // Print degree character
  Lcd_Chr(2,6,223);
  Lcd_Chr(2,15,223);
 // different LCD displays have different char code for degree
 // if you see greek alpha letter try typing 178 instead of 223

  Lcd_Chr(2,7,'C');
  Lcd_Chr(2,16,'F');
  do {
  //--- perform temperature reading
    Ow_Reset(&PORTB, 5);      // Onewire reset signal
    Ow_Write(&PORTB, 5, 0xCC);   // Issue command SKIP_ROM
    Ow_Write(&PORTB, 5, 0x44);   // Issue command CONVERT_T
    INTCON.GIE  = 1;     // 1-wire library disables interrpts
    Delay_us(120);
    Ow_Reset(&PORTB, 5);
    Ow_Write(&PORTB, 5, 0xCC);    // Issue command SKIP_ROM
    Ow_Write(&PORTB, 5, 0xBE);    // Issue command READ_SCRATCHPAD
    Delay_ms(400) ;

    // Read Byte 0 from Scratchpad
    TempL =  Ow_Read(&PORTB, 5);
    // Then read Byte 1 from Scratchpad
    TempH = Ow_Read(&PORTB, 5);
    temp_value = (TempH << 8)+ TempL ;
    // check if temperature is negative
    if (temp_value & 0x8000) {
      C_Neg = 1;
      tempC[0] = '-';
      // Negative temp values are stored in 2's complement form
      temp_value = ~temp_value + 1;
      }
    else C_Neg = 0;
    // Get temp_whole by dividing by 2
    temp_whole = temp_value >> 1 ;
    if (temp_value & 0x0001){  // LSB is 0.5C
       temp_fraction = 1;
       }
    else temp_fraction = 0;
    tempinC = temp_whole*10+temp_fraction;

    if(C_Neg)  {
     tempinF = 320-9*tempinC/5;
     if (tempinF < 0) {
      F_Neg = 1;
      tempF[0] = '-';
      tempinF = abs(tempinF);
      }
     else F_Neg = 0;
     }
    else tempinF = 9*tempinC/5 + 320;
    //--- Format and display result on Lcd
    Display_Temperature();

  } while(1);
}

มีปัญหาตรงจอ LCD
คือพอต่อกับวงจรจริงไฟเข้า LCD แต่ไม่แสดงค่าใดๆเลยคะ
แล้วก็ต่อ R ปรับค่าได้ที่ขา 1 2 3 ของ LCD แล้วนะคะ
รบกวนผู้รู้ช่วยตอบคำถามด้วยคะ ขอบคุณคะ