0
\$\begingroup\$

I am programming a PIC16F887 with MPLAB X, the compiler is XC8 v2.10 and the hardware tool is PICkit3.

When compiling the program I get these errors in the following constants of the program.

control.c: 125: 12: error: expression is not assignable
ADON_bit = 1;

control.c: 126: 3: error: invalid digit 'F' in octal constant
C1IE_bit = 1; // enable analog coparator interrupt

control.c: 126: 12: error: expression is not assignable
C1IE_bit = 1; // enable analog coparator interrupt

control.c: 130: 5: error: invalid digit 'F' in octal constant
GO_DONE_bit = 1; // start analog-to-digital conversion

control.c: 130: 17: error: expression is not assignable
GO_DONE_bit = 1; // start analog-to-digital conversion

My program code is as follows:

#define PWM_MIN_DUTY 80
#define PWM_START_DUTY 200

#include <xc.h>
#include <pic16f887.h>
#include "config.h"

void AH_BL();
void AH_CL();
void BH_CL();
void BH_AL();
void CH_AL();
void CH_BL();
void bldc_move();

unsigned int bldc_step = 0;
unsigned int motor_speed, i, j;

void Interrupt()
{     
  int j;
  for(j = 0; j < 9; j++) 
  {      
    if(bldc_step & 1) 
    {
      if(!C1OUT_bit) 
      {   
         j -= 1;
      }
    }
    else 
    {
      if(C1OUT_bit) 
      {
         j -= 1;
      }
    }
 }
 bldc_move();
 C1ON_bit = 1;      // clear the mismatch condition
 C1IF_bit = 0;      // Clear comparator 1 interrupt flag bit
}

void bldc_move()  
{
   switch(bldc_step)
   {
     case 0:
     AH_BL();
     CM1CON0 = 0xA2;
     break;
     case 1:
     AH_CL();
     CM1CON0 = 0xA1;
     break;
     case 2:
     BH_CL();
     CM1CON0 = 0xA0;
     break;
     case 3:
     BH_AL();
     CM1CON0 = 0xA2; 
     break;
     case 4:
     CH_AL();
     CM1CON0 = 0xA1; 
     break;
     case 5:
     CH_BL();
     CM1CON0 = 0xA0; 
     break;
   }
   bldc_step++;
   if(bldc_step >= 6)
     bldc_step = 0;
  }

  void set_pwm_duty(unsigned int pwm_duty)
  {
    CCP1CON = ((pwm_duty << 4) & 0x30) | 0x0C;
    CCPR1L  = pwm_duty >> 2;
  }

  void main()
  { 
    ANSEL = 0x10;
    PORTD = 0;
    TRISD = 0;  
    ADCON0 = 0xD0;
    ADFM_bit = 0;

    INTCON = 0xC0;
    C1IF_bit = 0;

    CCP1CON = 0x0C;
    CCPR1L  = 0;  
    TMR2IF_bit = 0;
    T2CON = 0x04;
    PR2 = 0xFF;

    set_pwm_duty(PWM_START_DUTY); 
    i = 5000;
    while(i > 100)
    {
      j = i;
      while(j--);
      bldc_move();
      i = i - 50;
    }

    ADON_bit = 1;
    C1IE_bit = 1; 

    while(1)
    {
      GO_DONE_bit = 1;
      __delay_ms(50); 

      motor_speed = (ADRESH << 2) | (ADRESL >> 6);

      if(motor_speed < PWM_MIN_DUTY)
         motor_speed = PWM_MIN_DUTY;
      set_pwm_duty(motor_speed);            
    }  
  }

  void AH_BL()
  {
    CCP1CON = 0;       
    PORTD   = 0x08;
    PSTRCON = 0x08; 
    CCP1CON = 0x0C;
  }

  void AH_CL()
  {
    PORTD = 0x04;
  }

  void BH_CL()
  {
    CCP1CON = 0;        
    PORTD   = 0x04;
    PSTRCON = 0x04;     
    CCP1CON = 0x0C;     
  }

  void BH_AL()
  {
    PORTD = 0x10;
  }

  void CH_AL()
  {
     CCP1CON = 0;        
     PORTD   = 0x10;
     PSTRCON = 0x02;    
     CCP1CON = 0x0C;    
  }

  void CH_BL()
  {
    PORTD = 0x08;
  }
\$\endgroup\$
  • 2
    \$\begingroup\$ Did you notice that all your errors come from lines where you're using xxxx_bit ? I think you'll find (if you look for the definition of those _bit's) is that these are not registers, they are the bit number in the associated register. so instead of ADON_bit = 1; you should be doing SOME_REGISTER |= 1 << ADON_bit; where SOME_REGISTER is whichever register holds the ADON bit. \$\endgroup\$ – brhans May 7 at 19:30
  • \$\begingroup\$ The errors are in a file called "control.c", starting at line 125. The comments in the error messages don't appear in the file you posted, so "control.c" must be another file linked in the project. \$\endgroup\$ – Peter Bennett May 7 at 19:45
  • \$\begingroup\$ You have to include xc.hin control.c \$\endgroup\$ – Mike May 8 at 6:14
  • \$\begingroup\$ And never doubke include your header. xc.h is enough.Remove pic16f887.h \$\endgroup\$ – Mike May 8 at 6:28
1
\$\begingroup\$

Perhaps you didn't write this code.. you've got constants for some other compiler (not xc8) mixed in there.

Check out the pic16f883.h header file for the proper bitfield and other definitions for xc8.

| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ There is a #definestatement in pic16f883.h for ADON_bit. I guess he had not inlude xc.hin control.c \$\endgroup\$ – Mike May 8 at 6:12
  • 1
    \$\begingroup\$ @Mike so there is \$\endgroup\$ – Spehro Pefhany May 8 at 6:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.