We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
I'm trying to read the data from an ADC. I'm getting the error message error C193 '+=' bad operand type. If I use '=+' I don't get the error but my data is shifting. My result is a 0 or 1. It should be in the range from 0 to 255. CONVST is the convert command I'm sending to the ADC, SCLK is the clock pulse I'm sending to the ADC, DOUT is the data coming from the ADC, and ad_in is my variable. Any help would be appreciated. Here it the part of the program that's giving me the trouble. void read_ad(void) { data byte i; SCLK = 1; CONVST = 0; ad_in = 0; CONVST = 1; i=8; do { SCLK = 0; ad_in<<=1; SCLK = 1; ad_in += DOUT; } while(--i); }
Try
ad_in += (byte)DOUT;
"I'm getting the error message error C193 '+=' bad operand type" When you get an error message, select it in the Build Output window, then press F1. This will bring up the help for that specific message. You can also open the help file directly; it's HLP\errors.chm in your C51 installation folder. The same text also appears in Chapter 7, "Error Messages" in the Manual
How is DOUT declared? BTW, a += b; adds a and b and stores the result in a. a =+ b stores +b in a. These are not the same thing. Jon
#pragma small #include <reg51.h> #include <stdio.h typedef unsigned char byte; typedef unsigned short word; sbit CONVST = P1^5; sbit SCLK = P1^6; sbit DOUT = P1^7; word ad_in; word Adata; void read_ad(void); void read_ad(void) { data byte i; SCLK = 1; CONVST = 0; ad_in = 0; CONVST = 1; i=8; do { SCLK = 0; ad_in<<=1; SCLK = 1; ad_in =+ DOUT; } while(--i); } void main(void) { read_ad(); Adata = ad_in; }
Hi Glenn, Even assuming the ad_in += DOUT worked did you mean to set bit7 or bit0 in ad_in?, if you want to clock the data into bit0 of ad_in, try replacing the line 'ad_in += DOUT' with :- if (DOUT) /* is the port bit a 1? */ { ad_in++; /* if so, 'shift' bit into result */ // ad_in |= 0x80; /* use this if you meant bit7 */ } Hope this helps, Mark.
Use ad_in |= DOUT; This will work. Jon
Thanks for the help. ad_in |= DOUT; works Glenn