1
\$\begingroup\$

I am writing a program to control three DC motor levels and display speed (RPM) on a 7-segment LED display using an 8051. I use an external interrupt to read pulse from encoder. When compiling, the program reports an error:

C141: syntax error near '=', expected ';'.

Error at row: led1 = on; P0 = so[thousand]; delay_ms(100); led1 = off;

Only when I delete all the statements in the for loop void hienthi, the program does not report an error.

Here is the code:

#include <reg51.h> #include <stdio.h> #define led1 P2^0 #define led2 P2^1 #define led3 P2^2 #define led4 P2^3 #define on 0 #define off 1 sbit pwm_out = P3^2; sbit low = P1^0; sbit medium = P1^1; sbit high = P1^2; sbit stop = P1^3; unsigned int count = 0, f, t = 0; char so[] = {0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90}; // for number 0->9 void init() { TMOD = 0x10; pwm_out = 1; EX0 = 1; ET1 = 1; IT0 = 1; TR1 = 1; EA = 1; } /*increments counter whenever falling edge is detected*/ void demxung() interrupt 0 { count++; } void time() interrupt 3 { t++; TH1 = 0xfc; TL1 = 0x18; TR1 = 1; if (t >= 1000) { f = count; count = 0; t = 0; } } void hienthi (unsigned int dem) { unsigned char thousand, hundred, tens, unit; int i; thousand = dem/1000; hundred = (dem%1000)/100; tens = (dem%100)/10; unit = dem%10; for (i=0; i<20; i++) { led1 = on; P0 = so[thousand]; delay_ms(100); led1 = off; led2 = on; P0 = so[hundred]; delay_ms(100); led2 = off; led3 = on; P0 = so[tens]; delay_ms(100); led3 = off; led4 = on; P0 = so[unit]; delay_ms(100); led4 = off; } } void delay_ms (unsigned int t) { unsigned int i,j; for (i=1; i<=t; i++) { for (j=1; j<125; j++); //delay_ms 1ms } } void main() { pwm_out = 0; init(); while (1) { if (low == 0) { medium = high = 1; low = 0; pwm_out = 1; delay_ms(30); pwm_out = 0; delay_ms(60); } if (medium == 0) { low = high = 1; medium = 0; pwm_out = 1; delay_ms(50); pwm_out = 0; delay_ms(50); } if (high == 0) { low = medium = 1; high = 0; pwm_out = 1; delay_ms(90); pwm_out = 0; delay_ms(10); } if (stop == 0) { low = medium = high = 1; pwm_out = 0; } hienthi (f); } } 
\$\endgroup\$
3
  • \$\begingroup\$ The error should have line number. But I am guessing it comes from one of the sbit pwm_out = P3^2; lines, which I have no idea what are supposed to achieve. This is definitely not a standard C syntax. If it is an extension, are you sure your compiler is supporting it? \$\endgroup\$ Commented Jun 2, 2022 at 18:13
  • \$\begingroup\$ You have Directive syntax errors not C errors in missing line# \$\endgroup\$ Commented Jun 2, 2022 at 18:35
  • \$\begingroup\$ I have edited the post. \$\endgroup\$ Commented Jun 2, 2022 at 18:55

2 Answers 2

1
\$\begingroup\$

In addition to brhans' answer, you should define single SFR bits like this:

sbit led1 = P2^0; 

instead of this:

#define led1 P2^0 

The syntax with ^ is an extension of Keil C51, and it is only allowed in such definitions.

You did this already for other bits.

\$\endgroup\$
4
  • \$\begingroup\$ It still gives the same error \$\endgroup\$ Commented Jun 3, 2022 at 6:18
  • \$\begingroup\$ Have you changed all LED definitions? Please edit your question and add your current code. Our crystal balls didn't make it through the pandemic. \$\endgroup\$ Commented Jun 3, 2022 at 6:20
  • \$\begingroup\$ I have changed all LED definitons. \$\endgroup\$ Commented Jun 3, 2022 at 6:27
  • \$\begingroup\$ As I wrote: show your new code in your question, please. \$\endgroup\$ Commented Jun 3, 2022 at 7:41
0
\$\begingroup\$

Your error results from these pieces of code:

#define led1 P2^0 ... led1 = on; P0 = so[thousand]; delay_ms(100); led1 = off; 

being pre-processed by your C compiler into:

P2^0 = on; P0 = so[thousand]; delay_ms(100); P2^0 = off; 

This is not valid C code - P2^0 = on; and P2^0 = off;.
P2^0 is not something you can assign a value to.

The ^ operator in C is the Exclusive OR operator.
I can guess from you code that you're trying to set or clear individual bits in the P2 register, and thereby set the individual P2 port pins high or low, but that is not the way to achieve this result.

One way to achieve this would be:
Instead of P2^0 = on;, write P2 |= (1 << 0); (use the bitwise OR operator to set the 0 bit in P2)
and instead of P2^0 = off;, write P2 &= ~(1 << 0); (use the bitwise AND operator to clear the 0 bit in P2)

\$\endgroup\$
1
  • \$\begingroup\$ I tried changing your way, but the program still gives the same error \$\endgroup\$ Commented Jun 3, 2022 at 3:17

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.