I am attempting to write a basic C code program to simulate in Proteus 8 Professional. The program’s function is to take input from an LM35 temperature sensor, and output the temperature to an LCD screen. I believe I am very close to succeeding, but I am now getting an error when building the code. The full code is below.
/* Main.c file generated by New Project wizard
*
* Processor: PIC16F877A
* Compiler: MPLAB XC8
*/
// CONFIG
#pragma config FOSC = HS // Oscillator Selection bits (HS 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 (Single-Supply) In-Circuit Serial Programming Enable bit (RB3/PGM pin has PGM function; low-voltage programming enabled)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
#define RS RB2
#define EN RB3
#define B4 RB4
#define B5 RB5
#define B6 RB6
#define B7 RB7
#define _XTAL_FREQ 4096000 //20000000
// In this exmple the clock has been set to 4.096 MHz and used for generating a 1 ms interrupt with Timer 0
#include <xc.h>
#include <stdio.h>
#include <stdlib.h>
#include <iso646.h>
#include <stdint.h>
#include "lcd.h";
//now start with a variable!
//unsigned char counter;
//void setup_hardware (void);
// counter,clk_cnt=5,clk_sec;
unsigned char counter=0,clk_cnt=0,seconds=0,up_date=0,bcd_low=0,bcd_high=0,datum=0,array_cnt = 0,LSB_datum=0,MSB_datum=0;
unsigned int clk_sec=0;
//Note: clk_cnt is defined as an integer in this example
// and 'passed' to the function below which is used for displaying the 'time'
void display_time(int time)
{
char str[20];
Lcd_Clear();
sprintf(str, "Time = %u", time);
Lcd_Set_Cursor(1,1);
Lcd_Write_String(str);
return;
}
//void display_time_f(float time) //JUST ADDED THIS TO TEST PRINTING A FLOAT
//{
// char str[20];
// Lcd_Clear();
// sprintf(str, "Time = %f", time);
// Lcd_Set_Cursor(2,3);
// Lcd_Write_String(str);
// return;
//}
void setup_hardware (void)
{
ADCON0=0x00;
ADCON1=0x00;
ADCON0bits.ADCS=2; //Two bits set for clock select Fosc/32 assuming 20 Mhz
//ADCON0bits.CHS0=2; //Three bits set to 000 for channel 0
// I HAVE SET THE CHANNELS INDIVIDUALLY HERE
ADCON0bits.CHS0=0;
ADCON0bits.CHS1=1; //010 A2
ADCON0bits.CHS2=0;
ADCON1bits.ADFM=1;
//I have turned ON module here instead of in the 'main'
//section
ADCON0bits.ADON=1;
PORTB = 0x00;
TRISB = 0x00;
//TIMER 0 SET_UP
INTCONbits.TMR0IE=1;
INTCONbits.TMR0IF=0;
INTCONbits.PEIE=1; //not required for the basic TO but T1 would require ths
OPTION_REGbits.PS0=1; //option register set for prescsler of 4 in this example
OPTION_REGbits.PS1=0;
OPTION_REGbits.PS2=0;
OPTION_REGbits.PSA=0;
OPTION_REGbits.T0CS=0;
//the following lines relate to the external interrupt if it was being used
// OPTION_REGbits.INTEDG=1; // The interrupting signal is caused by a 0 to 1 transition (edge triggered)
// so the INT EDGE select bit is set to 1. A '0' in this option bit would allow a 1 to 0 EDGE
// to cause the interrupt.
//INTCONbits.INTF=0; // Clear the external interrupt flag as a precaution
//INTCONbits.INTE=1; // set the ENABLE bit to activate external interrupt
INTCONbits.GIE=1;
// THE GLOBAL INTERRUPT ENABLE/MASK BIT MUST BE SET to allow any CPU interrupt
clk_cnt=0;
clk_sec=0;
}
// Interrupt function
void __interrupt()timer_isr(void)
{
if(INTCONbits.TMR0IF==1) //THIS CHECKS THAT TIMER 0 GENERATED INTERRUPT
{
//It is possible to load a timer register and not count from 00
// so if a partiular count was required then load timer register at this point
INTCONbits.TMR0IF=0;
clk_cnt++; //incremented every millisecond
if(clk_cnt==100) //every millisecond a timer generated interrupt with 4.096 MHz clock and prescaler =4
{ // 0.1 seconds after 100 interrupts
clk_sec++; // so clk_sec here is 1/10 of a second
clk_cnt=0;
}
if(clk_sec%10==0) //Allow update to set every second
{up_date=1;}
}
}
int read_adc0 ( void )
{
unsigned char hi, lo ;
int result;
ADCON0bits.GO_DONE=1; //I changed this slightly from the original
since I was inadvertently resetting the channel 0 al
while ( ADCON0bits.GO_DONE==1){
/* read the high byte */
hi = ADRESH;
/* read the low byte. */
lo = ADRESL;
/* return the result. */
result = (256 * hi) + lo; //Convert the two 8 bit results into a 16 bit integer
return result;
}
}
void main(void)
{
setup_hardware () ;
Lcd_Init();
while(1)
{
if(up_date==1) // flag tells me 1 second has elapsed so update display
{ // no point in constantly refreshing display...maybe only every minute in some clocks
up_date=0;
display_time(clk_sec); //call up lcd display function
__delay_ms(500); // just to prevent the LCD from being updated too quickly
}
}
}
The compiling error I receive when attempting to build the program is “can’t open include file “xc.h”. Too many open files”.
additionally, i get the following errors;
make: *** [main.p1] Error 1
Error code 2
Firmware file missing from the project directory: PIC16F877A_1/Debug/Debug.cof
Any help would be greatly appreciated.
also, I apologize for the messy code. we were told to cut and paste sections from example files we were provided with, and not to make any other edits to the program.
4