Mikro c code with pic16f887

Discussion in 'Embedded Systems and Microcontrollers' started by lema, Apr 6, 2014.

  1. lema

    Thread Starter New Member

    Apr 5, 2014
    1
    0
    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.

    Code ( (Unknown Language)):
    1.  
    2. //==========
    3. //    Project  : Temperature and Presssure Control System for oxygen concentrator
    4. //    Project description : This project will use PIC16F887
    5. //======include====
    6. #include"pic.h"
    7. //======configuration====
    8. __CONFIG (0x3F32);
    9. //==========define IO port=========  
    10. #define        lcd            PORTC
    11. #define        RS            RA2
    12. #define        E            RA5
    13. #define        CHANNEL0    0b10000001    // AN0
    14. #define        CHANNEL1    0b10001001    // AN1
    15. #define        buzzer      RB5
    16. #define        fanA        RB4
    17. #define        fanB        RB3
    18. #define        ledA        RB2
    19. #define        ledB        RB1
    20. //=======FUNCTION PTOTOTYPE===
    21. void e_pulse(void);
    22. void delay(unsigned short i);
    23. void send_char(unsigned char data);
    24. void send_config(unsigned char data);
    25. void lcd_goto(unsigned char data);
    26. void lcd_clr(void);
    27. void dis_num(unsigned long data);
    28. void increment(unsigned long data);
    29. void read_adc(void);
    30. unsigned short read_temp(void);
    31. unsigned short read_pres(void);
    32. unsigned short result;
    33. unsigned short temp,pres,tempA,presB;
    34.  
    35. void main(void)
    36. {
    37.     ADRESH=0;             //clear A/D result
    38.     ADRESL=0;             //clear A/D result
    39.     ADCON1=0b11000101;     // A/D result right justified,
    40.     PWM_Init(5000)         //Set PWM frequency at 5kHz
    41.     PWM_Set_Duty(128)    //Set PWM duty cycle at 50%
    42.     PWM_Start()          //Start PWM
    43.     TRISA=0b11011011;    //configure PORTA I/O direction
    44.     TRISB=0b00000000;    //configure PORTB as output
    45.     TRISC=0b00000000;    //configure PORTC as output
    46.     PORTA=0;
    47.     PORTB=0;
    48.     while(1)                    
    49.     {
    50.         send_config(0b00000001);     //clear display at lcd
    51.         send_config(0b00000010);     //Lcd Return to home
    52.         send_config(0b00000110);     //entry mode-cursor increase 1
    53.         send_config(0b00001100);     //diplay on, cursor off and cursor blink off
    54.         send_config(0b00111000);     //function set
    55.         lcd_goto(0);            //cursor start from beginning
    56.         //display character on LCD
    57.         send_char(' ');
    58.         send_char('T');
    59.         send_char('E');
    60.         send_char('M');
    61.         send_char('P');
    62.         send_char('.');
    63.         send_char('A');
    64.         send_char('=');    
    65.  
    66.         lcd_goto(20);
    67.         send_char(' ');
    68.         send_char('P');
    69.         send_char('R');
    70.         send_char('E');
    71.         send_char('S');
    72.         send_char('.');
    73.         send_char('B');
    74.         send_char('=');    
    75.  
    76.         while(1)                        //infinity loop
    77.         {
    78.         //sensor A
    79.         ADCON0=CHANNEL0;                //CHANNEL1=0b10001001
    80.         lcd_goto(8);        
    81.         read_adc();
    82.         temp=read_temp();
    83.         dis_num(temp/10);
    84.         send_char('.');
    85.         dis_num(temp%10);
    86.         send_char(0b11011111);
    87.         send_char('C');
    88.         send_char(' ');
    89.         send_char(' ');
    90.         tempA=temp;
    91.         //sensor B
    92.         ADCON0=CHANNEL1;                //CHANNEL0=0b10000001            
    93.         lcd_goto(28);        
    94.         read_adc();
    95.         pres=read_pres();
    96.         dis_num(pres/10);
    97.         send_char('.');
    98.         dis_num(pres%10);
    99.         send_char(0b11011111);
    100.         send_char('pa');
    101.         send_char(' ');
    102.         send_char(' ');
    103.         presB=pres;
    104.             if((tempA>400)&&(presB>2100))
    105.                 {
    106.  
    107.                     ledA=1;
    108.                     ledB=0;                                          
    109.                     fanA=1;
    110.                     fanB=0;
    111.                     buzzer=0;
    112.                 }    
    113.  
    114.             else if((tempA>350)&&(tempA<400)&&(presB<2100)&&(presB>200))
    115.                 {
    116.                     ledA=0;
    117.                     ledB=1;
    118.                     fanA=0;
    119.                     fanB=1;
    120.                     buzzer=0;
    121.                 }    
    122.  
    123.             else if((tempA<350)&&(presB<200))
    124.                 {
    125.                     ledB=1;
    126.                     ledA=1;
    127.                     fanA=0;
    128.                     fanB=1;
    129.                     buzzer=1;
    130.                 }    
    131.         delay(2000);
    132.    
    133.         }
    134.    
    135.     }
    136.        
    137. }
    138. //=====subroutine LCD setting =====
    139.  
    140. void send_config(unsigned char data)
    141. {
    142.      RS=0;
    143.     lcd=data;
    144.     delay(500);
    145.     e_pulse();
    146. }
    147. void e_pulse(void)
    148. {
    149.     E=1;
    150.     delay(500);
    151.     E=0;
    152.     delay(500);
    153. }
    154. void send_char(unsigned char data)
    155. {
    156.      RS=1;
    157.     lcd=data;
    158.     delay(500);
    159.     e_pulse();
    160. }
    161. void lcd_goto(unsigned char data)
    162. {
    163.      if(data<16)
    164.     {
    165.          send_config(0x80+data);
    166.     }
    167.     else
    168.     {
    169.          data=data-20;
    170.         send_config(0xc0+data);
    171.     }
    172. }
    173.  
    174.  
    175. void lcd_clr(void)
    176. {
    177.     RS=0;
    178.      send_config(0x01);
    179.     delay(600);    
    180. }
    181.  
    182.  
    183. void dis_num(unsigned long data)
    184. {
    185.     unsigned char hundred_thousand;
    186.     unsigned char ten_thousand;
    187.     unsigned char thousand;
    188.     unsigned char hundred;
    189.     unsigned char tenth;
    190.  
    191.     hundred_thousand = data/100000;                    
    192.     data = data % 100000;
    193.     ten_thousand = data/10000;
    194.     data = data % 10000;
    195.     thousand = data / 1000;
    196.     data = data % 1000;
    197.     hundred = data / 100;
    198.     data = data % 100;
    199.     tenth = data / 10;
    200.     data = data % 10;
    201.  
    202.     if(hundred_thousand>0)
    203.     {
    204.         send_char(hundred_thousand + 0x30); //0x30 added to become ASCII code
    205.         send_char(ten_thousand + 0x30);
    206.         send_char(thousand + 0x30);
    207.         send_char(hundred + 0x30);
    208.         send_char(tenth + 0x30);
    209.         send_char(data + 0x30);
    210.     }
    211.  
    212.     else if(ten_thousand>0)
    213.     {
    214.         send_char(ten_thousand + 0x30);//0x30 added to become ASCII code
    215.         send_char(thousand + 0x30);
    216.         send_char(hundred + 0x30);
    217.         send_char(tenth + 0x30);
    218.         send_char(data + 0x30);
    219.     }
    220.     else if(thousand>0)
    221.     {
    222.          send_char(thousand + 0x30);//0x30 added to become ASCII code
    223.         send_char(hundred + 0x30);
    224.         send_char(tenth + 0x30);
    225.         send_char(data + 0x30);
    226.     }
    227.     else if(hundred>0)
    228.     {
    229.          send_char(hundred + 0x30);//0x30 added to become ASCII code
    230.         send_char(tenth + 0x30);
    231.         send_char(data + 0x30);
    232.     }
    233.     else if(tenth>0)
    234.      {
    235.         send_char(tenth + 0x30); //0x30 added to become ASCII code
    236.         send_char(data + 0x30);
    237.     }
    238.     else send_char(data + 0x30);
    239. }
    240. void increment(unsigned long data)
    241. {    
    242.     unsigned short j;
    243.     for(j=10;j>0;j--)
    244.     {    lcd_goto(32);
    245.         data=data+1;
    246.         dis_num(data);
    247.         delay(10000);
    248.     }
    249. }
    250.  
    251. //=====subroutine ADC=======
    252.  
    253. void read_adc(void)
    254. {
    255.     unsigned short i;
    256.     unsigned short r;
    257.     unsigned long result_temp=0;
    258.     unsigned long result_pres=0;
    259.     for(i=2000;i>0;i-=1)
    260.     {
    261.         ADGO = 1;
    262.         while(ADGO==1);
    263.         result=ADRESH;
    264.         result=result<<8;
    265.         result=result|ADRESL;
    266.         result_temp+=result;        
    267.     }
    268.     result = result_temp/2000;
    269.  for(r=2000;r>0;r-=1)
    270.     {
    271.         ADGO = 1;
    272.         while(ADGO==1);
    273.         result=ADRESH;
    274.         result=result<<8;
    275.         result=result|ADRESL;
    276.         result_pres+=result;        
    277.     }
    278.    result = result_pres/2000;
    279. }
    280. unsigned short read_temp(void)
    281. {
    282.     unsigned short temp;
    283.     temp=result;
    284.     return temp;
    285. }
    286. unsigned short read_pres(void)
    287. {
    288.     unsigned short pres;
    289.     pres=result;
    290.     return pres;
    291. }
    292. void delay(unsigned short i)
    293. {    
    294.     for(;i>0;i--);
    295. }
    296. void delay(unsigned short r)
    297. {    
    298.     for(;r>0;r--);
    299. }
    300.  
     
    Last edited by a moderator: Apr 6, 2014
  2. JohnInTX

    Moderator

    Jun 26, 2012
    2,339
    1,021
    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: Apr 6, 2014
  3. THE_RB

    AAC Fanatic!

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

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