2013-02-25 94 views
1

我想了解Pic16F887上的这个LCD示例; http://www.mikroe.com/chapters/view/17/chapter-4-examples/#c4v12LCD库 - MPLAB X

但是编译器一直显示错误我:

lcdpic16.c:32: warning: function declared implicit int 
lcdpic16.c:33: warning: function declared implicit int 
lcdpic16.c:33: error: undefined identifier "_LCD_CURSOR_OFF" 
lcdpic16.c:34: error: undefined identifier "_LCD_CLEAR" 
lcdpic16.c:36: warning: illegal conversion between pointer types 
pointer to const unsigned char -> pointer to unsigned char 
lcdpic16.c:37: warning: function declared implicit int 
lcdpic16.c:38: warning: illegal conversion between pointer types 
pointer to const unsigned char -> pointer to unsigned char 
lcdpic16.c:43: warning: function declared implicit int 
lcdpic16.c:45: warning: illegal conversion between pointer types 
pointer to const unsigned char -> pointer to unsigned char 
lcdpic16.c:48: warning: function declared implicit int 
lcdpic16.c:54: warning: function declared implicit int 
lcdpic16.c:55: warning: function declared implicit int 
(908) exit status = 1 
make: *** [build/default/production/lcdpic16.p1] Error 1 

BUILD FAILED (exit value 2, total time: 22s) 

源代码(lcdp16.c)

/*Header******************************************************/ 
#include <pic16f887.h> 


// LCD module connections 
#define LCD_RS RB4 
#define LCD_RS RB4 
#define LCD_EN RB5 
#define LCD_D4 RB0 
#define LCD_D5 RB1 
#define LCD_D6 RB2 
#define LCD_D7 RB3 
#define LCD_RS_Direction TRISB4 
#define LCD_EN_Direction TRISB5 
#define LCD_D4_Direction TRISB0 
#define LCD_D5_Direction TRISB1 
#define LCD_D6_Direction TRISB2 
#define LCD_D7_Direction TRISB3 
// End LCD module connections 

unsigned char ch;     // 
unsigned int adc_rd;     // Declare variables 
char *text;       // 
long tlong;       // 

void main() { 
    INTCON = 0;      // All interrupts disabled 
    ANSEL = 0x04;     // Pin RA2 is configured as an analog input 
    TRISA = 0x04; 
    ANSELH = 0;      // Rest of pins are configured as digital 

    Lcd_Init();      // LCD display initialization 
    Lcd_Cmd(_LCD_CURSOR_OFF);  // LCD command (cursor off) 
    Lcd_Cmd(_LCD_CLEAR);    // LCD command (clear LCD) 

    text = "mikroElektronika";  // Define the first message 
    Lcd_Out(1,1,text);    // Write the first message in the first line 
    text = "LCD example";   // Define the second message 
    Lcd_Out(2,1,text);    // Define the first message 

    ADCON1 = 0x82;     // A/D voltage reference is VCC 
    TRISA = 0xFF;     // All port A pins are configured as inputs 
    Delay_ms(2000); 

    text = "voltage:";    // Define the third message 

    while (1) { 
     adc_rd = ADC_Read(2);  // A/D conversion. Pin RA2 is an input. 
     Lcd_Out(2,1,text);   // Write result in the second line 
     tlong = (long)adc_rd * 5000; // Convert the result in millivolts 
     tlong = tlong/1023;  // 0..1023 -> 0-5000mV 
     ch = tlong/1000;   // Extract volts (thousands of millivolts) 
            // from result 
     Lcd_Chr(2,9,48+ch);   // Write result in ASCII format 
     Lcd_Chr_CP('.'); 
     ch = (tlong/100) % 10;  // Extract hundreds of millivolts 
     Lcd_Chr_CP(48+ch);   // Write result in ASCII format 
     ch = (tlong/10) % 10;  // Extract tens of millivolts 
     Lcd_Chr_CP(48+ch);   // Write result in ASCII format 
     ch = tlong % 10;    // Extract digits for millivolts 
     Lcd_Chr_CP(48+ch);   // Write result in ASCII format 
     Lcd_Chr_CP('V'); 
     Delay_ms(1); 
    } 
} 

任何人都可以向我解释发生了什么?我应该创建一个自定义的LCD库,以便编译器可以识别像Lcd_Init()或什么的方法? (Windows 7/XC8/MPLAB X)

+0

您缺少一些#include文件(从LCD库声明函数和常量的文件)。 'pic16f887.h'只包含像SFR这样的控制器特定的定义。您的教程链接不起作用(页面未找到),所以我不能更具体地 – 2013-02-25 15:33:38

+0

是的,某事缺少,我找不到它_ _ <也许这是某种特定的库> _ < 这里是链接:http://www.mikroe.com/chapters/view/17/chapter-4-examples/#c4v12 – user1428734 2013-02-25 20:02:54

+0

“为了使这个例子正常工作,有必要勾选库管理器中的以下库在编译之前:'ADC''LCD'“ – 2013-02-25 20:22:10

回答

0

嗯,首先要说的是第一件事。从理论上讲,你所做的一切都是正确的。但是,您需要设计很多专门调用的函数:LCD_Init(),LCD_CMD(),LCD_OUT()。这些不是本机命令,因此您必须自己编写代码。

如果你想得到这个启动和运行。做到这一点的最佳方法是找到您的LCD的文档并找出执行操作所需的位/字节。由于您已将LCD_RS定义为RB4,因此您必须确保LCD上的RS引脚连接到RB4。并对所有其他引脚执行此检查。在LCD的文档中,他们会告诉您需要设置哪些引脚来执行特定操作。即LCD_EN可能会启用写入屏幕,以便显示字符。虽然D7-D4可能是您的数据总线,但是您可以一次从您的字节发送4位数据。由于LCD的响应时间不能立即发送数据,您可能需要插入延迟。

延迟相当简单,您只需设置一个空闲计时器来测量时间(以微秒为单位)并提升一个标记,一旦提升标记,就可以发送下一个命令。您也可以进行投票延迟。这是通过将定时器值重置为零并等待,直到它达到某个预定值,代码为2000.

最后,您必须编写ADC_Read(),ADC_Init() [这不是在你的代码中,但我认为写一个比在你的代码中公开注册操作更好]。这也许看起来很吓人,但不用担心。一旦你编写了一个LCD,这就是其中的一件事,你已经将它们编程了。就我个人而言,我认为你应该先编写你的定时器例程,然后转到LCD屏幕。希望有所帮助。

1

简单!您正在使用MPLAB,但您正在查看的库旨在用于完全不同的称为MikroC的IDE。

你将不得不写你自己的功能或改变你的IDE。

Interfacing PIC to HD44780 LCD

0

对于MPLAB XC8使用这个库下面的链接: ​​

在那篇文章中的例子使用BTW,书写LCD功能并不难,看看这个例子PIC 16F877A微控制器。您只需在MPLAB项目设置中将其更改即可将其转换为PIC 16F877 ..