CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

sprintf(); writing arrays with a2d

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
newbieA2D
Guest







sprintf(); writing arrays with a2d
PostPosted: Wed Dec 13, 2006 11:23 pm     Reply with quote

I am trying to write an array based on light inputs. The code I was trying was this:
Code:

// fortest.c

#include <16F690.h>
#include <STDLIB>
#fuses INTRC_IO,NOWDT,NOPROTECT,NOMCLR,NOBROWNOUT,NOIESO,NOFCMEN //uses built-in clock only!
#use delay(clock=8000000)
#use RS232(Baud=9600,Xmit=PIN_B7) //PIN_B7 sends to LCD

#define LENGTH  6

     
void main(void){

    delay_ms(3000);             //wait for display to wake up
    printf("?B7F");             //set contrast adaptor
    delay_ms(2000);             //wait for config to take hold
    output_high(PIN_A2);        // turn on the led
    printf("?f");       //Home and clear the display.
   
    setup_adc_ports(sAN1);          //setup analog port
    setup_adc(ADC_CLOCK_INTERNAL);
   
   
    while(1){
        char sig[LENGTH];
        int i;
        int light;
       
        light = read_adc();
       
        set_adc_channel(1); 
        printf("?f");
        delay_ms(1000);
        printf("?f");
       
        for (i=0; i<5; i++){
            printf("i = %u",i);     //display the value of i
            delay_ms(2000);
            printf("?f");
           
            if(light<120){
                sig[i]= '1';

            }
            else{
                sig[i]= '0';
            }
        }
           
        printf("%s",sig);
           
        delay_ms(2000);
           
        }
}

and it did not seem to work. I was wondering if sprintf() should be used and could be used to write specific parts of the array. Any suggestions would be helpful.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Dec 13, 2006 11:58 pm     Reply with quote

Quote:
light = read_adc();

set_adc_channel(1);

Here, at least on the first pass, you're reading the A/D before you
set the channel. You should set the channel, delay for 20 us,
and then read the A/D.


Quote:

if(light<120){
sig[i]= '1';
}
else{
sig[i]= '0';
}
}
printf("%s",sig);

In the code above, you're trying to build a string which consists of
ascii 1's and 0's. But it's not a string until you write a string terminator
byte of zero at the end of the ascii digits. After you've finished filling
the array, you need to write 0x00 in the next available location.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group