Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

arithmetic operation using microC

Status
Not open for further replies.

zulkifliaziz

Junior Member level 1
Joined
Mar 27, 2008
Messages
18
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,409
Hi all,

I have problem understand the this.

I'm using pic18f452, The idea is that i want to multiply or divide some value.

let says that , i read adc value from an0 using ADC_Read(0)>>2,

off course, the adc value in digital is from 0-255 rite?

Below is my code

temp_res = ADC_Read(0) >> 2;
delay_ms(100);
if (temp_res>0)
{
temp_res = temp_res/2; //divide by 2
UART1_Write(temp_res);
....
....
}

so basically, if the code and my understanding is correct, if the value read by the adc(0) is max which is 255, does it mean when it being divided by 2, the output should be 128 or something.. but what i got i far more than 128. i do not understand. can anyone please help. thanks
 

Code:
temp_res = ADC_Read(0) >> 2;

You are already dividing by 4 in the above statement. So, you can use the same technique to divide by 2.

Code:
temp_res = temp_res >> 1;

Hope this helps.
 
Last edited:

Hi, how about if i want to divide by three or four and how about if want to divide by 1.3456?
 

hi

for that you have to type the equation
like
temp_res/=3; which is equal to temp_res = temp_res/3;

is this ok for you?

ml
 

Hi,
Your above statement should be right. >>2 means divide by 4, not divide by 2. /2 is divide by 2, so for 3, it's /3, for 4, it's /4, for 1.3456, it's /1.3456

Can you post your entire code. It should be fine.
 

Hi,
Your above statement should be right. >>2 means divide by 4, not divide by 2. /2 is divide by 2, so for 3, it's /3, for 4, it's /4, for 1.3456, it's /1.3456

Can you post your entire code. It should be fine.

Hi, below is the following code i'm trying to develop.

unsigned char temp_res;
unsigned char temp_res1;
unsigned char x;
unsigned int y;
unsigned char Start;
void main()
{
UART1_Init(9600);
ADCON1 = 0x80;
TRISA = 0xFF;
TRISB=0;
PORTB=0B00000000;
i=0b00000000;
do {
if (Uart1_Data_Ready())
{
Start=Uart1_Read();
if (Start!=0)
{
delay_ms(3000);
Portb=0b00000001;
temp_res = ADC_Read(0) >> 2;
delay_ms(100);
if (temp_res>0)
{
x = 1*temp_res;
UART1_Write(x);
delay_ms(5000);
Portb=0b00000011;
temp_res1 = ADC_Read(1) >> 2;
delay_ms(100);
if (temp_res1>0)
{
y = 0.13725*temp_res1;
y = y + 36;
UART1_Write(y);
delay_ms(5000);
portb=0b00000111;
}
else
for(;;)
{
delay_ms(100) ;
Portb=0b00000011;
delay_ms(100);
portb=0b00000001;
}while (1);

}
else
for (;;)
{
delay_ms(100) ;
Portb=0b00000001;
delay_ms(100);
portb=0b00000000;
}while (1);
}
}
} while (1); // endless loop

}

The result is not like expected. I'm using MATLAB simulink toolbox to query instrument to get the rs232 data and seems like the matlab cannot read the number with few decimals.
 

Hi zulkifliaziz,

At first glance on your code several bugs can be noted;

Too many endless loops ( several while (1) & For(;;) ),
i is not defined as a variable,
y is defined as int, but it holds floating-point value (y= 0.13725*temp_res1),
Before sending y via UART1_write, it has not converted to text, (you may use FloatToStr function),

Thank you,
 
Hello,

The best way to represent number in float is using Q format, it is a fixed point format. For example Q12.4 means that you have an unsigned float using 12 bits for integer part and 4 bits for fractional. Try to use as resolution of phisican signal as power of 2 , so you can reduce amount of computation involved.

**broken link removed**)
 
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top