Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

how to send ADC data to USART??

Status
Not open for further replies.

Cheetos

Member level 3
Joined
May 26, 2011
Messages
57
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Activity points
1,700
how do i send my data from ADC to Usart? This is my code, and i am not able to send the data that i need. I am not that good in programming microcontrollers. i am currently using PIC16F877A and MikroC



void main(){

ADCON1 = 0x00; // 8-bit converter, 0000 Ports E and A are analog inputs and Vref+ is VDD Vref- is Vss,
TRISA = 0xFF; // Port A input
PORTB = 0x00;
TRISB = 0x00;
TRISC = 0xC0; // only TX pin is made output on port C
TRISD = 0x00;
Usart_Init(9600);
PORTB = 0xFF;
delay_ms(1000);
PORTB = 0x00;
while(1){

Vout=Adc_read(0);

Usart_Write('C');
Usart_Write('U');
Usart_Write('R');
Usart_Write('R');
Usart_Write('E');
Usart_Write('N');
Usart_Write('T');
Usart_Write('\r');
Usart_Write(Adc_read(0));

}


}
 

i'm having trouble converting the ADC value to character string, how do i convert it? i used InttoStr(myvalue ,stringvalue ) still i get the wrong data
 

Using the sprint library, would most likely be the easiest, but not the most efficient, method of converting an integer to character array/buffer.

Reference MikroC Pro User Manual, pg 596 & 600 , Section SPRINT LIBRARY

The library code example on pg 600 demonstration the conversion along with transmission using the UART:

Code:
double ww = -1.2587538e+1;
char buffer[15];

// Function for sending string to UART
void UartWriteText(char *txt) 
{
       while(*txt)
       UART1_Write(*txt++);
}

// Function for sending const string to UART
void UartWriteConstText(const char *txt) 
{
       while(*txt)
       UART1_Write(*txt++);
}

void main()
{
      UART1_Init(4800); // Initialize UART module at 4800 bps
      Delay_ms(10);
   
      UartWriteConstText("Floating point number representation"); //Write message on UART
      sprintf(buffer, "%12e", ww); // Format ww and store it to buffer
      UartWriteConstText("\r\ne format:"); // Write message on UART
      UartWriteText(buffer); // Write buffer on UART
      sprintf(buffer, "%12f", ww); // Format ww and store it to buffer
      UartWriteConstText("\r\nf format:"); // Write message on UART
      UartWriteText(buffer); // Write buffer on UART
      sprintf(buffer, "%12g", ww); // Format ww and store it to buffer
      UartWriteConstText("\r\ng format:"); // Write message on UART
      UartWriteText(buffer); // Write buffer on UART

     while(1);
}

You could use the sprinti() form of the library function and save some flash memory.


BigDog
 

is this applicable for Usart_write? hmm i need to transmit it at the TX pin of my microcontroller
 

is this applicable for Usart_write? hmm i need to transmit it at the TX pin of my microcontroller

The meaning of your question is not clear. Can you elaborate?

The ADC value is a integer, so the sprintf() conversion will be slightly different, you need to use "%d" instead of "%f" or "%g".

BigDog
 

sorry about that, my ADC value will be integer, and i want it to be converted to a string, otherwise, my receiving node will display a character corresponding to the hex equivalent of the ADC integer. i see that the code you gave me is for UART_write, but i am currently using USART_write(), i am using the RX and TX pin of my microcontroller.
 

Actually I see very little discussed in the user's manual concerning USART_write(). Try it and see. Both routines use the devices USART and TX/RX pins.

BigDog
 

ok, i think my question is wrong :???: may should have asked how to convert int to string for Usart_write() transmission.
 

Actually I see very little discussed in the user's manual concerning USART_write(). Try it and see. Both routines use the devices USART and TX/RX pins.

BigDog

I was able to search for a code that can transmit what i want, but the problem is when i tested it, it only transmits 1 decimal place, i would like it to transmit two decimal places, how can i do it?

This is my

ad_value = Adc_Read(0);
Vin = adc_value*(5.00000/1023.00000);
d1=((Vin%1)*100)/100;
d2=((d1%1)*10);
inter= Vin;
deci = d1;
deci2= d2;
usart_write(inter +0x30);
delay_ms(50);
usart_write('.');
delay_ms(50);
usart_write(deci + 0x30);
delay_ms(50);
usart_write(deci2 +0x30;
delay_ms(5000)
usart_write(13);
usart_write(10);
 

Try this my UART function
Code:
void uart_transmit(char data){
while(TXIF==0) continue;
TXREG=data;
}

void uart_string(const char *s)
{
while(*s)
uart_transmit(*s++);
}

void uart_number(unsigned int no, char base, char digit)
{char i,di[10];
for(i=0;i<=9;i++) di[i]=0;
i=0;
do{
	di[i]=no%base;
	no=no/base;
	i++;}
while(no!=0);
for(i=digit;i>0;i--){
	if(di[i-1]<=9) uart_transmit(di[i-1]+'0');
	else uart_transmit(di[i-1]-10+'A');}
}


---------- Post added at 04:02 ---------- Previous post was at 03:55 ----------

I was able to search for a code that can transmit what i want, but the problem is when i tested it, it only transmits 1 decimal place, i would like it to transmit two decimal places, how can i do it?

This is my

ad_value = Adc_Read(0);
Vin = adc_value*(5.00000/1023.00000);
Vin maximum will be 5, so it always 1 digit values. You should multiply with 5000 instead of 5.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top