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

How use UART?

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



Joined: 16 Oct 2010
Posts: 20

View user's profile Send private message

How use UART?
PostPosted: Wed Oct 20, 2010 11:40 am     Reply with quote

Hello
I want to make a program as follow:

while(1){1- Analog signal is input in RA0
2- ADC convert it to digital and store 8 bits at Temp(unsigned variable)
3 - UART takes temp and send it serial via TX pin to MAX232, then to serial port.}

well, I can make steps 1,2 but how can i use UART? how can i make it read the ADC output(temp)?
Thanks
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Oct 20, 2010 12:01 pm     Reply with quote

See this sample program:
http://www.ccsinfo.com/forum/viewtopic.php?t=42763&start=1
doryme



Joined: 16 Oct 2010
Posts: 20

View user's profile Send private message

PostPosted: Wed Oct 20, 2010 8:55 pm     Reply with quote

PCM programmer wrote:
See this sample program:
http://www.ccsinfo.com/forum/viewtopic.php?t=42763&start=1


Thank you very much for your help. I can now program my adc using this code:

Code:
void main()
{
int16 adc_value;
 
setup_adc_ports(AN0);
setup_adc(ADC_CLOCK_DIV_4);
set_adc_channel(0);
delay_us(20);

while(1)
  {
   adc_value = read_adc();
   printf("%lu", adc_value);
   delay_ms(500);
  }
}

my question now how to send adc_value to UART to send it serial to RS232 Cable?
also what is the use of printf?
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Thu Oct 21, 2010 5:38 am     Reply with quote

Look up the function #use_RS232() to see how to set up the UART for your pins, clock speed, and baud rate. This must be done before using printf().
The printf() function takes the binary value you get from the ADC and converts it to ASCII characters to send out the UART.
_________________
The search for better is endless. Instead simply find very good and get the job done.
doryme



Joined: 16 Oct 2010
Posts: 20

View user's profile Send private message

PostPosted: Thu Oct 21, 2010 6:03 am     Reply with quote

SherpaDoug wrote:
Look up the function #use_RS232() to see how to set up the UART for your pins, clock speed, and baud rate. This must be done before using printf().
The printf() function takes the binary value you get from the ADC and converts it to ASCII characters to send out the UART.


This is my full code:

Code:

#INCLUDE <16F877A.h>
#device adc=10
#fuses HS, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#USE DELAY (CLOCK=10000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
void main() {
int16 adc_value;
setup_adc_ports(AN0);
setup_adc(ADC_CLOCK_DIV_4);
set_adc_channel(0);
delay_us(20);
while(1)
  {
   adc_value = read_adc();
   printf("%lu", adc_value);
   delay_ms(500);
  }
}




When i run simulation on Proteus, I get one on pin_c6..always one and doesn't change
so what is the error with my code?
I use square wave as my input signal
Douglas Kennedy



Joined: 07 Sep 2003
Posts: 755
Location: Florida

View user's profile Send private message AIM Address

PostPosted: Thu Oct 21, 2010 7:25 am     Reply with quote

I'm not sure this is the board for Proteus issues. Frankly there are way to many inquiries about RS232 that have been asked and answered several times on this board. Even a small effort at search should bring the solution.

For RS232 input take a very serious look at an isr circular buffer solution.
For timing critical output use an isr circular buffer there as well. Never ever pack a lot of code in an isr.
PICs have timers for a reason use them instead of inline delay except in the simplest of circumstances
Those who don't care to understand what asynchronous means shouldn't care if their code has timing and lost data issues.
If Proteus fails to simulate CCS code that is proven to work in a real PIC device then Proteus has the issue.
doryme



Joined: 16 Oct 2010
Posts: 20

View user's profile Send private message

PostPosted: Fri Oct 22, 2010 3:21 am     Reply with quote

Douglas Kennedy wrote:
I'm not sure this is the board for Proteus issues. Frankly there are way to many inquiries about RS232 that have been asked and answered several times on this board. Even a small effort at search should bring the solution.

For RS232 input take a very serious look at an isr circular buffer solution.
For timing critical output use an isr circular buffer there as well. Never ever pack a lot of code in an isr.
PICs have timers for a reason use them instead of inline delay except in the simplest of circumstances
Those who don't care to understand what asynchronous means shouldn't care if their code has timing and lost data issues.
If Proteus fails to simulate CCS code that is proven to work in a real PIC device then Proteus has the issue.


I run many CCS codes on Proteus and simulation was successful so I guess that the error is due to my code not Proteus. Any way I will search about UART questions in the forum. Thank you!
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