PIC16F886 ADC and UART with for loop.

General discussion on mikroC PRO for PIC.
Post Reply
Author
Message
mago999
Posts: 4
Joined: 13 Aug 2010 13:58

PIC16F886 ADC and UART with for loop.

#1 Post by mago999 » 14 Aug 2010 06:41

I'm making following codes.
1) To get sample by A/D converter and store it to "capture1[40]" for 40 times.
2) To send above data to PC by UART.

But for loop doesn't work as my expectation.

Code: Select all

void main() {
char buffer[4];
unsigned short i;
unsigned int capture1[40];

ANSEL = 0b00000001; //A/D Converter disable except RA0
ANSELH = 0b00000000; //A/D Converter disable
ADCON0.ADCS1= 1; //Fosc/32
ADCON0.ADCS0= 0;

UART1_Init(115200);
UART1_write_text("START");
UART1_write(0x0d);
UART1_write(0x0a);

for (i=0; i<40; i++)
{
    capture1[i] = adc_read(0);
    delay_us(300);
}

UART1_write_text("A/D COMPL");
UART1_write(0x0d);
UART1_write(0x0a);

for (i=0; i<40; i++)
{
    bytetostr(i, buffer);
    UART1_write_text(buffer);
    UART1_write_text(",");

    WordToStr(capture1[i], buffer);
    UART1_write_text(buffer);
    UART1_write(0x0d);
    UART1_write(0x0a);
}

UART1_write_text("END");
UART1_write(0x0d);
UART1_write(0x0a);

}
The result of above code is follows:
START
A/D COMPL
0, 0
END

Only I could get one data. Not 40 data.
I connected LCD and confirmed the final value of "i". And it was 49 or 48 (unstable). I cannot understand why "i" was set as 49.
Is there any careless mistake? Would you give me any advices?

Thank you.

braus
Posts: 167
Joined: 25 Jul 2007 22:55
Location: Mexico, city.

Re: PIC16F886 ADC and UART with for loop.

#2 Post by braus » 14 Aug 2010 06:51

Hello mago999, in order to use correctly conversion functions you should take care about arrays length, this is true because to translate a byte to string array has to be 4 bytes long and to translate a word to string array that stores the resulting string must be 6 bytes long.
Best Regards
Omar Galicia
Mexico City

mago999
Posts: 4
Joined: 13 Aug 2010 13:58

Re: PIC16F886 ADC and UART with for loop.

#3 Post by mago999 » 14 Aug 2010 16:22

Thank you very much for your advice!

I've changed variable type and I confirmed that for loop work correctly..

Code: Select all

char buffer[6]; // from buffer[4];
In MikroC Help, I found following description.
>WordToStr
>Destination string should be at least 6 characters in length.
I understood the cause of this problem. I didn't follow the requirement of function. So, it's natural that it does not work correctly.
But I wonder why this function influence to i value. Must I read and understand LST file, if I would like to know the reason of this problem?

Anyway, thank you very much for your help.
Best regards,

braus
Posts: 167
Joined: 25 Jul 2007 22:55
Location: Mexico, city.

Re: PIC16F886 ADC and UART with for loop.

#4 Post by braus » 15 Aug 2010 00:48

As you know every PIC MCU has RAM memory which is used to store special function registers and variables, let's think in RAM memory as a large stack of bytes where every byte is beside other byte and so on. When a programmer declares a variable compiler assign it in a specific place among RAM memory, then, remembering the case we are talking about you declared a 4 element array and a single variable called i, sure thing is that compiler lay them out together so when you call WordToString function, which uses a 6 element array to store the resulting string, that function thought that i was the 5th element of the array it works with and manipulated it, changing its value against your will. :shock:
Best Regards
Omar Galicia
Mexico City

mago999
Posts: 4
Joined: 13 Aug 2010 13:58

Re: PIC16F886 ADC and UART with for loop.

#5 Post by mago999 » 18 Aug 2010 16:30

Thank you for the detail explanation for my elementary question.

I confirmed RAM lay-out and I found each variables are assigned as following address.
0x003B buffer (4byte)
0x003F i (1byte)

I understood the cause of this unintentional operation. "i" is assigned next to buffer[4].
I thought the compiler will notify the wrong usage of type declaration, but it was always not so.
If there was not your indication, I might not notice a difference, and I might not understand the logic of this operation.
Thank you :!:

Best regards,

User avatar
slavisa.zlatanovic
mikroElektronika team
Posts: 1321
Joined: 07 Apr 2009 09:39

Re: PIC16F886 ADC and UART with for loop.

#6 Post by slavisa.zlatanovic » 19 Aug 2010 14:44

Hi mago999!
I understood the cause of this problem. I didn't follow the requirement of function. So, it's natural that it does not work correctly.
But I wonder why this function influence to i value. Must I read and understand LST file, if I would like to know the reason of this problem?
In order to improve our compiler I would like to know what do you think compiler should do to make programming easier.
I guess that it should give warning when using WordToString function if your destination string is smaller then the minimal necessary size.
Currently, that information is located in the Help file. So, we taught that it would be enough to ensure successful operation.

Thanks in advance!

Best regards
Slavisa
Best regards
Slavisa

mago999
Posts: 4
Joined: 13 Aug 2010 13:58

Re: PIC16F886 ADC and UART with for loop.

#7 Post by mago999 » 19 Aug 2010 17:00

As you said, it's very helpful to find the mistake of variable length, if compiler provides any warning/caution when build.

I'm a beginner of C language and MikroC PRO Compiler. So, I don't have a confidence with regard to my proposal.
But if there was any warning or caution, I might have confirmed WordToStr function help again, I guess.

Maybe almost users will confirm the detail specification of function before using. So current description is enough for almost users, I guess.
But, it influences to other RAM value and it provides unanticipated result, if improper length variable was specified. In such case, to find the mistake of code was difficult for me.

Is above enough for your question?
I hope this comment helps to evolution of Mikro compilers still more..

User avatar
slavisa.zlatanovic
mikroElektronika team
Posts: 1321
Joined: 07 Apr 2009 09:39

Re: PIC16F886 ADC and UART with for loop.

#8 Post by slavisa.zlatanovic » 23 Aug 2010 10:22

Hi!

Thanks!
I will report this issue to our software developers.

Best regards
Slavisa
Best regards
Slavisa

Post Reply

Return to “mikroC PRO for PIC General”