• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

can someone help me correct this C code?

#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
 
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.
 
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. 😉
 
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?
 
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>
 
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.
 
Unless there is some other thread modifying ADCTL, it is an endless loop as ADCTL = 0x10 from the previous line.
 
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.
 
Back
Top