اطلاعیه

Collapse
No announcement yet.

درست کردن هدر فایل LCD (حل شد)

Collapse
X
 
  • فیلتر
  • زمان
  • Show
Clear All
new posts

    درست کردن هدر فایل LCD (حل شد)

    سلام به همه دوستان
    چطور میتونم هدر فایل LCD کارکتری برای کامپایلر XC8 درست کنم؟ البته برای سری PIC16F
    تشکر

    #2
    پاسخ : درست کردن هدر فایل LCD

    با استفاده از مطالب اینترنت یک هدر فایل نوشتم اما متاسفانه کار نمیکه! نمیدونم مشکل دقیقا چی هست، کسی میتونه راهنمایی کنه؟

    lcd.h
    [code=c]
    #ifndef XC_HEADER_TEMPLATE_H

    #define XC_HEADER_TEMPLATE_H
    #include <xc.h> // include processor files - each processor file is guarded.
    #define _XTAL_FREQ 4000000
    // TODO Insert appropriate #include <>

    // TODO Insert C++ class definitions if appropriate

    // TODO Insert declarations

    // TODO Insert declarations or function prototypes (right here) to leverage
    // live documentation

    #ifdef __cplusplus
    extern "C" {
    #endif /* __cplusplus */

    // TODO If C++ is being used, regular C code needs function names to have C
    // linkage so the functions can be used by the c code.
    #define LCD_RS RA3
    #define LCD_EN RA4
    #define LCD_DATA4 RB0
    #define LCD_DATA5 RB1
    #define LCD_DATA6 RB2
    #define LCD_DATA7 RB3
    #define LCD_RS_Direction TRISA3
    #define LCD_EN_Direction TRISA4
    #define LCD_DATA4_Direction TRISB0
    #define LCD_DATA5_Direction TRISB1
    #define LCD_DATA6_Direction TRISB2
    #define LCD_DATA7_Direction TRISB3
    void LCD_STROBE(void){
    LCD_EN = 1;
    __delay_ms(1);
    LCD_EN = 0;
    __delay_ms(50);
    }
    void LCD_write(unsigned char data){
    if(data & 1)
    LCD_DATA4 = 1;
    else
    LCD_DATA4 = 0;
    if(data & 2)
    LCD_DATA5 = 1;
    else
    LCD_DATA5 = 0;
    if(data & 4)
    LCD_DATA6 = 1;
    else
    LCD_DATA6 = 0;
    if(data & 8)
    LCD_DATA7 = 1;
    else
    LCD_DATA7 = 0;
    //return;
    }
    void LCD_cmd(unsigned char cmd){
    //unsigned char cmd_entry = cmd;
    /*LCD_DATA4 = (cmd >> 0) & 0x01;
    LCD_DATA5 = (cmd >> 1) & 0x01;
    LCD_DATA6 = (cmd >> 2) & 0x01;
    LCD_DATA7 = (cmd >> 3) & 0x01;*/
    LCD_RS = 0;
    LCD_write(cmd);
    LCD_STROBE();
    //return;
    }
    void LCD_char(char c){
    char temp,y;
    temp = c&0x0F;
    y = c&0xF0;
    LCD_RS = 1;
    LCD_write(y>>4);
    LCD_STROBE();
    LCD_write(temp);
    LCD_STROBE();
    }
    void LCD_string(char *s){
    //LCD_RS = 1;
    for (int i = 0; s[i] != '\0'; i++) {
    LCD_char(s[i]);
    }
    }
    void LCD_clear(void){
    LCD_cmd(0x01); //clear
    }
    void LCD_goto(void){
    LCD_cmd(0x80); //cursor(0,0)
    }
    void LCD_init(void){
    LCD_RS_Direction = 0;
    LCD_EN_Direction = 0;
    LCD_DATA4_Direction = 0;
    LCD_DATA5_Direction = 0;
    LCD_DATA6_Direction = 0;
    LCD_DATA7_Direction = 0;
    LCD_cmd(0x28); //4-bit
    __delay_ms(1);
    LCD_cmd(0x01); //clear
    __delay_ms(1);
    LCD_cmd(0x0E); //cursor on
    __delay_ms(1);
    LCD_cmd(0x80); //cursor(0,0)
    __delay_ms(1);
    //return;
    }
    #ifdef __cplusplus
    }
    #endif /* __cplusplus */
    #endif /* XC_HEADER_TEMPLATE_H */
    [/code]

    main.c
    [code=c]
    #define _XTAL_FREQ 4000000
    #include <pic16f88.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <xc.h>
    #include "lcd.h"
    #pragma config FOSC = XT // Oscillator Selection bits (XT oscillator)
    #pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
    #pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
    #pragma config BOREN = OFF // Brown-out Reset Enable bit (BOR disabled)
    #pragma config LVP = OFF // Low-Voltage Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
    #pragma config CPD = OFF // Data EE Memory Code Protection bit (Code protection off)
    #pragma config CP = OFF
    //TRISB
    void main(void){
    //TRISB6 = 0;
    LCD_init();
    LCD_goto();
    while (1) {
    //LED();
    LCD_string("Hello World"
    }
    }
    [/code]

    دیدگاه


      #3
      پاسخ : درست کردن هدر فایل LCD (حل شد)

      بالاخره تونستم حلش کنم!!!
      مشکل از این بود که من از پورت A برای EN و RS استفاده میکردم، پورت A آنالوگ است باید مقدار بیت های دو پین EN و RS را صفر در نظر میگرفتم تا پورت دیجیتال ست بشه!

      وقتی که بیت ها دیجیتال ست نشده!


      کد رو هم کمی تغییر دادم

      lcd.h
      [code=c]
      #ifndef XC_HEADER_TEMPLATE_H

      #define XC_HEADER_TEMPLATE_H
      #include <xc.h> // include processor files - each processor file is guarded.
      #define _XTAL_FREQ 4000000
      #include <pic16f88.h>
      // TODO Insert appropriate #include <>

      // TODO Insert C++ class definitions if appropriate

      // TODO Insert declarations

      // Comment a function and leverage automatic documentation with slash star star
      /**
      */
      // TODO Insert declarations or function prototypes (right here) to leverage
      // live documentation

      #ifdef __cplusplus
      extern "C" {
      #endif /* __cplusplus */

      // TODO If C++ is being used, regular C code needs function names to have C
      // linkage so the functions can be used by the c code.
      #define LCD_RS RA3
      #define LCD_EN RA4
      #define LCD_DATA4 RB0
      #define LCD_DATA5 RB1
      #define LCD_DATA6 RB2
      #define LCD_DATA7 RB3
      #define LCD_PORT PORTB

      void LCD_STROBE(void){
      LCD_EN = 1;
      __delay_us(1);
      LCD_EN = 0;
      }
      void write(unsigned char value){
      LCD_DATA4 = (value >> 0) & 0x01;
      LCD_DATA5 = (value >> 1) & 0x01;
      LCD_DATA6 = (value >> 2) & 0x01;
      LCD_DATA7 = (value >> 3) & 0x01;
      LCD_EN = 1;
      __delay_us(1);
      LCD_EN = 0;
      }
      void LCD_cmd(unsigned char data){
      LCD_RS = 0;
      __delay_us(50);
      LCD_PORT = (data >> 4);
      LCD_STROBE();
      LCD_PORT = (data);
      LCD_STROBE();
      }
      void LCD_goto(void){
      LCD_cmd(0x80);
      __delay_ms(2);
      }
      void LCD_clear(void){
      LCD_cmd(0x01);
      __delay_ms(2);
      }
      void LCD_char(unsigned char data){
      LCD_RS = 1;
      __delay_us(50);
      write(data >> 4);
      write(data);
      }
      void LCD_putc(unsigned char data){
      LCD_RS = 1;
      __delay_us(50);
      LCD_PORT = (data >> 4);
      LCD_STROBE();
      LCD_PORT = (data);
      LCD_STROBE();
      }
      void LCD_putcs(const char *data){
      while (*data) {
      LCD_putc(*data++);
      }
      }
      void LCD_init(void){
      TRISA3 = 0;
      TRISA4 = 0;
      TRISB = 0b11110000;

      LCD_cmd(0x20); //4-bit 1 line, 2 line= 0x28
      __delay_ms(1);
      LCD_cmd(0x0c);
      __delay_ms(1);
      LCD_cmd(0x06); //cursor on
      __delay_ms(2);
      LCD_cmd(0x80); //cursor(0,0)
      __delay_ms(2);
      LCD_cmd(0x01); //clear
      __delay_ms(2);
      }
      #ifdef __cplusplus
      }
      #endif /* __cplusplus */
      #endif /* XC_HEADER_TEMPLATE_H */
      [/code]

      main.c
      [code=c]
      #define _XTAL_FREQ 4000000
      #include <pic16f88.h>
      #include <stdio.h>
      #include <stdlib.h>
      #include <xc.h>
      #include "lcd.h"
      #pragma config FOSC = XT // Oscillator Selection bits (XT oscillator)
      #pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
      #pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
      #pragma config BOREN = OFF // Brown-out Reset Enable bit (BOR disabled)
      #pragma config LVP = OFF // Low-Voltage Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
      #pragma config CPD = OFF // Data EE Memory Code Protection bit (Code protection off)
      #pragma config CP = OFF
      //TRISB
      void main(void){
      //TRISB6 = 0;
      ANS3 = 0; //set RA3 to digital I/O
      ANS4 = 0; //set RA4 to digital I/O
      LCD_init();
      LCD_goto();
      LCD_putc('2'
      __delay_ms(1000);
      while (1) {
      LCD_clear();
      LCD_goto();
      LCD_char('P'
      __delay_ms(5000);
      LCD_clear();
      LCD_putcs("HELLO WORLD"
      __delay_ms(5000);
      LCD_clear();
      }
      }
      [/code]

      دیدگاه

      لطفا صبر کنید...
      X