can someone help me correct this C code?

psycobreed

Member
Dec 7, 2000
102
0
0
#include <stdio.h>
#include <hc11.h>

unsigned int volt;
unsigned char adcData;

void main(){
initialization();
while(1){
input();
calculate();
output();
}
}

void initialization(){
OPTION = 0x80;
}

void input() {
ADCTL = 0x10;
while (!(ADCTL & 0x80));
adcData = ADR2;
}

void calculate () {
volt = (125*adcData) / 64;
}
void output () {
printf("voltage = %d%d \r ", volt/100, volt%100);
}



thanks, david
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
That's it? You're just gonna dump your code with a thanks and let other people stump themselves on it?

It would help if, say, you told us what the problem is.
 

ProviaFan

Lifer
Mar 17, 2001
14,993
1
0
Originally posted by: BingBongWongFooey
That's it? You're just gonna dump your code with a thanks and let other people stump themselves on it?

It would help if, say, you told us what the problem is.
True, do you expect us to copy your code into a file, then somehow find that other file that you're including (it doesn't look standard to me), then try to compile it, just to get an error message? You're underestimating the laziness of AT members. ;)
 

JetBlack69

Diamond Member
Sep 16, 2001
4,580
1
0
Originally posted by: psycobreed
#include <stdio.h>
#include <hc11.h>

unsigned int volt;
unsigned char adcData;

void main(){
initialization();

while(1){
input();
calculate();
output();
}

}

void initialization(){
OPTION = 0x80;
}

void input() {
ADCTL = 0x10;
while (!(ADCTL & 0x80));
adcData = ADR2;
}

void calculate () {
volt = (125*adcData) / 64;
}
void output () {
printf("voltage = %d%d \r ", volt/100, volt%100);
}



thanks, david

Are you getting an infinite loop error?
 

helppls

Senior member
Jun 19, 2001
216
0
76
like others have said, you'd undoubtebly get better help by describing how you know something is wrong. i'd help, but i'd be useless as i don't know anythign about these apis. i'll still make a suggestion though ;). in your while(1) loop, add a Sleep(100) or anything reasonable and include <windows.h>
 

PrincessGuard

Golden Member
Feb 5, 2001
1,435
0
0
That infinite loop is intentional. This is an embedded system that repeatedly polls an ADC and displays the result.

I'm not sure what the printf() is doing there, considering that the HC11 has no console.
 

JetBlack69

Diamond Member
Sep 16, 2001
4,580
1
0
Originally posted by: glugglug
don't know what this line is supposed to be, but fairly certain it's NOT supposed to be this:

while (!(ADCTL & 0x80));

that might want to be

while (!(ADCTL == 0x80));

but I don't know what ADCTL is. :confused:
 

glugglug

Diamond Member
Jun 9, 2002
5,340
1
81
Unless there is some other thread modifying ADCTL, it is an endless loop as ADCTL = 0x10 from the previous line.
 

oog

Golden Member
Feb 14, 2002
1,721
0
0
Personally, I don't see anything that wrong with the code. As others have said, it would help if you said what you're trying to do and what behavior you're seeing.

I don't know what happens when you set the value of OPTION. It appears you set adcData when ADCTL reaches a certain value. It also looks like you don't care what happens until ADCTL has the 0x80 bit set.

Finally, you end up performing a calculation on adcData and printing something strange: volt/100 followed by volt%100. Don't you just end up printing volt? volt/100 will give you the hundreds place, and volt%100 will give you the tens and ones place.

Anyway, I don't know if that's the behavior you want, but that's how I read the program.