为pic微控制器编译C程序 - 错误消息

时间:2013-10-09 01:52:34

标签: c microcontroller pic

我有一个C程序,我试图把我的照片,但我得到错误“3.17无法打开包含文件”main.h“:没有这样的文件或目录”。这是我第一次尝试编写照片。

这是我试图加入的程序。

#include <htc.h>
#include <stdio.h>
#include "main.h"

__CONFIG(FOSC_INTOSC & WDTE_OFF & PWRTE_OFF & MCLRE_ON & CP_OFF & CPD_OFF & BOREN_OFF & CLKOUTEN_OFF & IESO_OFF & FCMEN_OFF);
__CONFIG(WRT_OFF & PLLEN_OFF & STVREN_OFF & BORV_HI & LVP_OFF);

#define _XTAL_FREQ 4000000

int main()
{
  int     t = 0;
  char    outStr[8];
  char    ch;

  init();

  Lcd_PutStr(0, 0, "Option 1");
  Lcd_PutStr(1, 0, "Spec Analyzer");

  while (1)
  {
    // do stuff here
  }
}

int init()
{
    // Hardware
    OSCCON = 0x6A;        // 4 MHz oscillator

    PORTA = 0;          // Clear Port A
    LATA = 0;

    PORTB = 0;          // Clear Port B
    LATB = 0;

    PORTC = 0;          // Clear Port C
    LATC = 0;

    ANSELA = 0;         // Set Port A to digital
    ANSELB = 0;         // Set Port B to digital
    ANSELC = 0;         // Set Port C to digital

    TRISB = 0;          // Set port B to output
    TRISC = 0;          // Set port C to output

    // LCD display
    Lcd_Init();

    return 0;
}

void ENPulse()
{
  LATC |= 0x20;
  __delay_us(1);
  LATC &= 0xdf;
}

void Lcd_Init()
{
    LATC = 0;
    __delay_ms(40);

    LATC = 0x03;
    ENPulse();
    __delay_us(37);

    Lcd_Cmd(0x28);
    __delay_us(37);

    Lcd_Cmd(0x28);
    __delay_us(37);

    Lcd_Cmd(0x0C);
    __delay_ms(2);

    Lcd_Cmd(0x01);
    __delay_us(37);
}

void Lcd_Cmd(byte cmd)
{
    LATC = cmd >> 4;
    ENPulse();
    LATC = cmd & 0x0F;      // clear RS (LATC,4)
    ENPulse();
    __delay_us(100);
}

void Lcd_Data(byte data)
{
    LATC = (data >> 4) | 0x10;    // set RS (LATC,4)
    ENPulse();
    LATC = (data & 0x0F) | 0x10;  // set RS (LATC,4)
    ENPulse();
    __delay_us(100);
}

void Lcd_PutStr(int row, int col, char* str)
{
    int   pos;

    // use row and column=-1 for no positioning, i.e. position where last ended
    if (row >= 0 && col >= 0)
    {
        if (row > 0)
            row = 0x40; // row 1
        else
            row = 0;  // row 0

        pos = row | col | 0x80;   // 0x80 is cmd for positioning cursor
        Lcd_Cmd(pos);
    }

    while (*str)
    {
        Lcd_Data(*str);
        str++;
    }
}

我正在使用带有Pickit3的MPLab和Hi-Tech C.我根据给我的样本来编写此代码。我不知道main.h文件的用途是什么。任何帮助表示赞赏。

2 个答案:

答案 0 :(得分:1)

在查看错误后,您可以在没有main.h的情况下运行代码。它看起来像main.h主要是原型文件底部的函数。尝试将此代码放在main和#define之间:

int init();
void ENPulse();
void Lcd_Init();
void Lcd_Cmd(byte cmd);
void Lcd_Data(byte data);
void Lcd_PutStr(int row, int col, char* str);

这应该可以减少大多数错误。它可以完全解决它。这个想法是,当编译器第一次遇到这些函数时,它不知道它们是什么,它会在找到错误之前抛出错误。如果仍有错误让我知道,我会尝试提供更多帮助。

答案 1 :(得分:1)

试试这段代码:

#include <htc.h>
#include <stdio.h>
//#include "main.h"

__CONFIG(FOSC_INTOSC & WDTE_OFF & PWRTE_OFF & MCLRE_ON & CP_OFF & CPD_OFF & BOREN_OFF & CLKOUTEN_OFF & IESO_OFF & FCMEN_OFF);
__CONFIG(WRT_OFF & PLLEN_OFF & STVREN_OFF & BORV_HI & LVP_OFF);

#define _XTAL_FREQ 4000000



int init()
{
    // Hardware
    OSCCON = 0x6A;        // 4 MHz oscillator

    PORTA = 0;          // Clear Port A
    LATA = 0;

    PORTB = 0;          // Clear Port B
    LATB = 0;

    PORTC = 0;          // Clear Port C
    LATC = 0;

    ANSELA = 0;         // Set Port A to digital
    ANSELB = 0;         // Set Port B to digital
    ANSELC = 0;         // Set Port C to digital

    TRISB = 0;          // Set port B to output
    TRISC = 0;          // Set port C to output

    // LCD display
    Lcd_Init();

    return 0;
}

void ENPulse()
{
  LATC |= 0x20;
  __delay_us(1);
  LATC &= 0xdf;
}

void Lcd_Init()
{
    LATC = 0;
    __delay_ms(40);

    LATC = 0x03;
    ENPulse();
    __delay_us(37);

    Lcd_Cmd(0x28);
    __delay_us(37);

    Lcd_Cmd(0x28);
    __delay_us(37);

    Lcd_Cmd(0x0C);
    __delay_ms(2);

    Lcd_Cmd(0x01);
    __delay_us(37);
}

void Lcd_Cmd(byte cmd)
{
    LATC = cmd >> 4;
    ENPulse();
    LATC = cmd & 0x0F;      // clear RS (LATC,4)
    ENPulse();
    __delay_us(100);
}

void Lcd_Data(byte data)
{
    LATC = (data >> 4) | 0x10;    // set RS (LATC,4)
    ENPulse();
    LATC = (data & 0x0F) | 0x10;  // set RS (LATC,4)
    ENPulse();
    __delay_us(100);
}

void Lcd_PutStr(int row, int col, char* str)
{
    int   pos;

    // use row and column=-1 for no positioning, i.e. position where last ended
    if (row >= 0 && col >= 0)
    {
        if (row > 0)
            row = 0x40; // row 1
        else
            row = 0;  // row 0

        pos = row | col | 0x80;   // 0x80 is cmd for positioning cursor
        Lcd_Cmd(pos);
    }

    while (*str)
    {
        Lcd_Data(*str);
        str++;
    }
}

int main()
{
  int     t = 0;
  char    outStr[8];
  char    ch;

  init();

  Lcd_PutStr(0, 0, "Option 1");
  Lcd_PutStr(1, 0, "Spec Analyzer");

  while (1)
  {
    // do stuff here
  }
}