ADC Read RS232

General discussion on mikroC.
Post Reply
Author
Message
yturgut
Posts: 7
Joined: 04 Jun 2010 23:00

ADC Read RS232

#1 Post by yturgut » 04 Jun 2010 23:09

Hi everyone,

I am trying to get analog data from AN0, convert it and send through rs232.

My circuit is working fine and simulations return as expected.

My only problem is that when I start the circuit, hyperterminal displays weird chars. I know hyperterm is not suppose to view binary data thats where I am stuck.

My code is ;

Code: Select all

short temp_res    ;
char buf[4];
void main() {
Usart_Init(19200);              

 ADCON1 = 0;                     
 TRISA  = 0xFF;             

  do {
   temp_res = ADC_Read(0) >> 2 ; 
   IntToStr(temp_res, buf);
   Usart_Write(buf);  
   Delay_ms(100);
  } while (1);                   
}
it is actually the example adc+usart in default install. I added IntToStr to convert data to string and the send to usart, but hyperterm output is ;

oooooooooooooooooooooooooooooo

My input is 2-4V AC, circuit and everything is fine except this.

How can I view meaningful stuff in hyperterm ?

Thank you!

womai
Posts: 239
Joined: 16 Apr 2008 07:45

Re: ADC Read RS232

#2 Post by womai » 04 Jun 2010 23:39

First, the buffer you allocate is too short, so InttoStr will overwrite random parts of your memory. It should be 7 bytes, not 4 (see help for the Inttostr function);

char buf[7];

Second, Usart_write only writes a single char (unsigned short) value, but what you pass it is a pointer to a char. So it will always print the LSB of the pointer value (which is pointing to the address of the first element in buf[]).

What you need instead is something like

char i;
for (i = 0; i < 7; i++) Usart_Write ([buf ); // write one character at a time
Usart_Write (13); // CR
Usart_Write (10); // LF

Wolfgang

yturgut
Posts: 7
Joined: 04 Jun 2010 23:00

Re: ADC Read RS232

#3 Post by yturgut » 05 Jun 2010 00:12

same retarded outputs, nothing changed.
thanks for the reply though.

this is my output ;
+ë+ë+ëVëåëåëåë+ë+ëey=dy=åë

and no my input is not arabic (:

womai
Posts: 239
Joined: 16 Apr 2008 07:45

Re: ADC Read RS232

#4 Post by womai » 05 Jun 2010 03:51

Are you sure the baud rates match? If you enter a wrong frequency into the oscillator frequency box then the PIC will not drive 19200 baud. I'd start out with a simple LED blinking routine and make sure the LED blinks at the speed you expect - that will show if the PIC is running at the expected clock frequency.

yturgut
Posts: 7
Joined: 04 Jun 2010 23:00

Re: ADC Read RS232

#5 Post by yturgut » 06 Jun 2010 00:26

baud rates are correct at both ends, I know this is an easy trick but can't seem to handle :evil:

Sobrietytest
Posts: 619
Joined: 05 Jul 2008 06:05
Location: Thailand

Re: ADC Read RS232

#6 Post by Sobrietytest » 06 Jun 2010 11:12

That isn't what he said, he indicted that you must double check your master oscillator settings, if the oscillator control registers are not setup properly the PIC will generate the wrong baud rate irrespective of what you specify in the UART Init.

Also, if you are using a USB-RS232 converter to interface with your PC these can give some very strange results.

yturgut
Posts: 7
Joined: 04 Jun 2010 23:00

Re: ADC Read RS232

#7 Post by yturgut » 06 Jun 2010 15:41

I am using mikroC 8.2.0 and directly to rs232 port - no converters.

I am pretty sure that this is a software problem because simulation and implementation return same (wrong) results.

Anyone can give me a hand here ? :oops:

womai
Posts: 239
Joined: 16 Apr 2008 07:45

Re: ADC Read RS232

#8 Post by womai » 06 Jun 2010 17:23

Can you post your modified program? Otherwise it's like fishing in the dark.

How can you check your program in simulation? adc_read does not even simulate - the simulator will get stuck if you try.

Again, what makes you claim with such certainty that the baud rates are correct? Just because the numbers match for Uasrt_Init and you terminal program does not mean anything. Did you try my suggestion to check if the PIC is actually running at the frequency that you think (and that you entered in the frequency control box in the compiler)?

Wolfgang

yturgut
Posts: 7
Joined: 04 Jun 2010 23:00

Re: ADC Read RS232

#9 Post by yturgut » 06 Jun 2010 18:43

My code is as following ;

Code: Select all

unsigned short temp_res    ;
char buf[7];
int i;
void main() {
  Usart_Init(19200);
  ADCON1 = 0x80;                  
  TRISA  = 0xFF;                 

  do {
   temp_res = ADC_Read(0) >> 2 ; 
   ByteToStr(temp_res,&buf);
    for(i = 0; i < 7; i++) {
      Usart_Write(buf[i]);

   Delay_ms(10);   }
  } while (1);                  
}
Terminal Output and Simulation Settings are in the attachement.

Can't seem to spot the mistake.

Btw, input is 1V 50Hz sinusoidal source.
Attachments
2.jpg
2.jpg (23.52 KiB) Viewed 6158 times
1.jpg
1.jpg (255.06 KiB) Viewed 6158 times

womai
Posts: 239
Joined: 16 Apr 2008 07:45

Re: ADC Read RS232

#10 Post by womai » 06 Jun 2010 19:58

I guess I spotted the error.

ByteToStr(temp_res,&buf);

It should rather be

ByteToStr(temp_res,buf);

buf is already a pointer, pointing to the first element of your char array (string). If you absolutely want to use the address operator, you could write

ByteToStr(temp_res,&(buf[0]));

but of course that would be needlessly complicated.

Wolfgang

yturgut
Posts: 7
Joined: 04 Jun 2010 23:00

Re: ADC Read RS232

#11 Post by yturgut » 07 Jun 2010 08:58

nop, not that :)

It was about max232 IC, when I removed it, whole system works perfectly! So it was a hardware issue, my mistake.

Thank you for your help :wink:

womai
Posts: 239
Joined: 16 Apr 2008 07:45

Re: ADC Read RS232

#12 Post by womai » 08 Jun 2010 00:16

Yet the pointer issue I mentioned is real. Right now you are NOT writing the string into the space reserved for buf[7]. You are writing it to a random location in your memory. That MAY seem to work right now, but if it does, that's pure luck. Expand the program and you may end up having other information overwritten.

yturgut
Posts: 7
Joined: 04 Jun 2010 23:00

Re: ADC Read RS232

#13 Post by yturgut » 08 Jun 2010 20:57

Code gives correct outputs now.

0V=0 and 5V=255, 3v=153 and so on..

Code: Select all

unsigned short temp_res    ;
unsigned char buf[7];

void main() {
  Usart_Init(19200);
  ADCON1 = 0x80;                  
  TRISA  = 0xFF;               

  do {
   temp_res = ADC_Read(0) >> 2 ; 

     ByteToStr(temp_res,&buf[i]);

    for(i = 0; i < 7; i++) {

      Usart_Write(buf[i]);

   Delay_ms(100);   }
  } while (1);                  
}
I would like to view decimal values not 8bit values.

I could basicly do a /51 operation and it would be done but since buf is a char array I will need to convert this into integer first to do this.

atoi() seems to fit but couldn't figure it out yet :?

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

Re: ADC Read RS232

#14 Post by slavisa.zlatanovic » 09 Jun 2010 11:26

Hi!

Visit the link below and find many ADC examples:
http://www.mikroe.com/eng/downloads/get ... c_v100.zip

Also, if you upgrade to PRO version of our mikroC compiler
http://www.mikroe.com/eng/products/view ... o-for-pic/
you could use the UARTx_Write_Text(char * UART_text) routine.

Best regards
Slavisa
Best regards
Slavisa

Post Reply

Return to “mikroC General”