Mikro c code with pic16f887

Thread Starter

lema

Joined Apr 5, 2014
1
hi all, I have written the following code on mikro c and when i'm compiling, it shows an error on preprocessors.Please help me to solve this error and send me the correct solution.

Rich (BB code):
//==========
//    Project  : Temperature and Presssure Control System for oxygen concentrator
//    Project description : This project will use PIC16F887
//======include====
#include"pic.h"
//======configuration====
__CONFIG (0x3F32);
//==========define IO port=========  
#define        lcd            PORTC
#define        RS            RA2
#define        E            RA5
#define        CHANNEL0    0b10000001    // AN0
#define        CHANNEL1    0b10001001    // AN1
#define        buzzer      RB5
#define        fanA        RB4
#define        fanB        RB3
#define        ledA        RB2
#define        ledB        RB1
//=======FUNCTION PTOTOTYPE===
void e_pulse(void);
void delay(unsigned short i);
void send_char(unsigned char data);
void send_config(unsigned char data);
void lcd_goto(unsigned char data);
void lcd_clr(void);
void dis_num(unsigned long data);
void increment(unsigned long data);
void read_adc(void);
unsigned short read_temp(void);
unsigned short read_pres(void);
unsigned short result;
unsigned short temp,pres,tempA,presB;

void main(void)
{
    ADRESH=0;             //clear A/D result
    ADRESL=0;             //clear A/D result
    ADCON1=0b11000101;     // A/D result right justified,
    PWM_Init(5000)         //Set PWM frequency at 5kHz
    PWM_Set_Duty(128)    //Set PWM duty cycle at 50%
    PWM_Start()          //Start PWM
    TRISA=0b11011011;    //configure PORTA I/O direction
    TRISB=0b00000000;    //configure PORTB as output
    TRISC=0b00000000;    //configure PORTC as output
    PORTA=0;
    PORTB=0;
    while(1)                    
    {
        send_config(0b00000001);     //clear display at lcd
        send_config(0b00000010);     //Lcd Return to home
        send_config(0b00000110);     //entry mode-cursor increase 1
        send_config(0b00001100);     //diplay on, cursor off and cursor blink off
        send_config(0b00111000);     //function set
        lcd_goto(0);            //cursor start from beginning
        //display character on LCD
        send_char(' ');
        send_char('T');
        send_char('E');
        send_char('M');
        send_char('P');
        send_char('.');
        send_char('A');
        send_char('=');    

        lcd_goto(20);
        send_char(' ');
        send_char('P');
        send_char('R');
        send_char('E');
        send_char('S');
        send_char('.');
        send_char('B');
        send_char('=');    

        while(1)                        //infinity loop
        {
        //sensor A
        ADCON0=CHANNEL0;                //CHANNEL1=0b10001001
        lcd_goto(8);        
        read_adc();
        temp=read_temp();
        dis_num(temp/10);
        send_char('.');
        dis_num(temp%10);
        send_char(0b11011111);
        send_char('C');
        send_char(' ');
        send_char(' ');
        tempA=temp;
        //sensor B
        ADCON0=CHANNEL1;                //CHANNEL0=0b10000001            
        lcd_goto(28);        
        read_adc();
        pres=read_pres();
        dis_num(pres/10);
        send_char('.');
        dis_num(pres%10);
        send_char(0b11011111);
        send_char('pa');
        send_char(' ');
        send_char(' ');
        presB=pres;
            if((tempA>400)&&(presB>2100))
                {

                    ledA=1;
                    ledB=0;                                           
                    fanA=1;
                    fanB=0;
                    buzzer=0;
                }    

            else if((tempA>350)&&(tempA<400)&&(presB<2100)&&(presB>200))
                {
                    ledA=0;
                    ledB=1;
                    fanA=0;
                    fanB=1;
                    buzzer=0;
                }    

            else if((tempA<350)&&(presB<200))
                {
                    ledB=1;
                    ledA=1;
                    fanA=0;
                    fanB=1;
                    buzzer=1;
                }    
        delay(2000);
    
        }
    
    }
        
}
//=====subroutine LCD setting =====

void send_config(unsigned char data)
{
     RS=0;
    lcd=data;
    delay(500);
    e_pulse();
}
void e_pulse(void)
{
    E=1;
    delay(500);
    E=0;
    delay(500);
}
void send_char(unsigned char data)
{
     RS=1;
    lcd=data;
    delay(500);
    e_pulse();
}
void lcd_goto(unsigned char data)
{
     if(data<16)
    {
         send_config(0x80+data);
    }
    else
    {
         data=data-20;
        send_config(0xc0+data);
    }
}


void lcd_clr(void)
{
    RS=0;
     send_config(0x01);
    delay(600);    
}


void dis_num(unsigned long data)
{
    unsigned char hundred_thousand;
    unsigned char ten_thousand;
    unsigned char thousand;
    unsigned char hundred;
    unsigned char tenth;

    hundred_thousand = data/100000;                    
    data = data % 100000;
    ten_thousand = data/10000;
    data = data % 10000;
    thousand = data / 1000;
    data = data % 1000;
    hundred = data / 100;
    data = data % 100;
    tenth = data / 10;
    data = data % 10;

    if(hundred_thousand>0)
    {
        send_char(hundred_thousand + 0x30); //0x30 added to become ASCII code
        send_char(ten_thousand + 0x30);
        send_char(thousand + 0x30);
        send_char(hundred + 0x30);
        send_char(tenth + 0x30);
        send_char(data + 0x30);
    }

    else if(ten_thousand>0) 
    {
        send_char(ten_thousand + 0x30);//0x30 added to become ASCII code
        send_char(thousand + 0x30);
        send_char(hundred + 0x30);
        send_char(tenth + 0x30);
        send_char(data + 0x30);
    }
    else if(thousand>0)
    {
         send_char(thousand + 0x30);//0x30 added to become ASCII code
        send_char(hundred + 0x30);
        send_char(tenth + 0x30);
        send_char(data + 0x30);
    }
    else if(hundred>0)
    {
         send_char(hundred + 0x30);//0x30 added to become ASCII code
        send_char(tenth + 0x30);
        send_char(data + 0x30);
    }
    else if(tenth>0)
     {
        send_char(tenth + 0x30); //0x30 added to become ASCII code
        send_char(data + 0x30);
    }
    else send_char(data + 0x30);
}
void increment(unsigned long data)
{    
    unsigned short j;
    for(j=10;j>0;j--)
    {    lcd_goto(32);
        data=data+1;
        dis_num(data);
        delay(10000);
    }
}

//=====subroutine ADC=======

void read_adc(void)
{
    unsigned short i;
    unsigned short r;
    unsigned long result_temp=0;
    unsigned long result_pres=0;
    for(i=2000;i>0;i-=1)
    {
        ADGO = 1;
        while(ADGO==1);
        result=ADRESH;
        result=result<<8;
        result=result|ADRESL;
        result_temp+=result;        
    }
    result = result_temp/2000;
 for(r=2000;r>0;r-=1)
    {
        ADGO = 1;
        while(ADGO==1);
        result=ADRESH;
        result=result<<8;
        result=result|ADRESL;
        result_pres+=result;        
    }
   result = result_pres/2000; 
}
unsigned short read_temp(void)
{
    unsigned short temp;
    temp=result;
    return temp;
}
unsigned short read_pres(void)
{
    unsigned short pres;
    pres=result;
    return pres;
}
void delay(unsigned short i)
{    
    for(;i>0;i--);
}
void delay(unsigned short r)
{    
    for(;r>0;r--);
}
 
Last edited by a moderator:

JohnInTX

Joined Jun 26, 2012
4,787
I think the main problem is that this code isn't written for MikroC. If you look at chapter 4 of the user's manual you'll see that the port defs have different names and syntax. MikroC does not require the #include PIC.h nor allow __CONFIG. There are several others.

You can't do this:
void send_config(unsigned char data);
in MikroC; data is a reserved memory specifier (chapter 5 of the manual). Change 'data' to 'd' or something like that.



It looks more like HitechC.
 
Last edited:

THE_RB

Joined Feb 11, 2008
5,438
Also I'm not sure if MikroC accepts "RB4" like this;
#define fanA RB4

try changing those defines like this;
#define fanA PORTB.F4
 
Top