View previous topic :: View next topic |
Author |
Message |
doryme
Joined: 16 Oct 2010 Posts: 20
|
How use UART? |
Posted: Wed Oct 20, 2010 11:40 am |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
![](templates/subSilver/images/spacer.gif) |
doryme
Joined: 16 Oct 2010 Posts: 20
|
|
Posted: Wed Oct 20, 2010 8:55 pm |
|
|
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? |
|
![](templates/subSilver/images/spacer.gif) |
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Thu Oct 21, 2010 5:38 am |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
doryme
Joined: 16 Oct 2010 Posts: 20
|
|
Posted: Thu Oct 21, 2010 6:03 am |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
Douglas Kennedy
Joined: 07 Sep 2003 Posts: 755 Location: Florida
|
|
Posted: Thu Oct 21, 2010 7:25 am |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
doryme
Joined: 16 Oct 2010 Posts: 20
|
|
Posted: Fri Oct 22, 2010 3:21 am |
|
|
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! |
|
![](templates/subSilver/images/spacer.gif) |
|