View previous topic :: View next topic |
Author |
Message |
Lobobranco
Joined: 26 Jul 2010 Posts: 10
|
Problems about gets function and PWM variable via RS232 |
Posted: Mon Jul 26, 2010 11:03 am |
|
|
Hello
I have got some problems about the gets function, but after some researches i found that after sending a string i must send the Carriage Return to confirm.
I using a program that i downloaded that is possible to choose if you wanna send the CR/LF characters or not.
But, what if i want to do this by myself? I mean, after i write a string to send, what i have to write after the string that means the CR/LF? I tried to add \r and \n but nothing happens...
But considering that i using the option of CR/LF of the program i told before.
My program is supposed to read a string(a valor between 0 and 1023), convert it to a Int type, and change the value of the PWM, it works, but only once!
When i send the first time everything works Ok. But when i try to send a new string(value) the PWM value become 0, does not matter what value i put on it.
My program here
Sorry for my very bad english, I am brazilian.
Code: |
#include <16F628A.h>
#include <stdlib.h>
#include <stdio.h>
#include<string.h>
#include<math.h>
#fuses HS,NOWDT,NOPROTECT,PUT,NOLVP,NOBROWNOUT
#use delay (clock=20000000)
#use rs232(baud=9600,parity=N,xmit=PIN_B2,rcv=PIN_B1,bits=8)
void main(void)
{
char pwm[5]={0,0,0,0,0};
int16 pwmvalor=0;
setup_timer_2(T2_DIV_BY_4, 248, 1);
setup_ccp1(ccp_pwm);
set_pwm1_duty(0);
while(true)
{
delay_ms(100);
printf("Valor do PWM");
gets(pwm);
pwmvalor = atol(pwm);
set_pwm1_duty(pwmvalor);
printf("%s",pwm); // This confirms that the program received the string //and this always works, but the PWM does not work after the first //attempt.
}
} |
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Jul 26, 2010 1:11 pm |
|
|
Post the values that you get from your printf statement. Post several of
them, that come in sequentially to the serial port on the PIC. Tell us
which one of them causes the PWM duty cycle to become 0. |
|
|
Lobobranco
Joined: 26 Jul 2010 Posts: 10
|
|
Posted: Mon Jul 26, 2010 3:22 pm |
|
|
Every value that i send to PIC is sent to me normally, but doubled, like if i send 500 it's sent to me like 500 500 and the PWM works just the first time.
And does not matter the value that i send, the PWM becomes 0 at the second time and so on. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
Lobobranco
Joined: 26 Jul 2010 Posts: 10
|
|
Posted: Mon Jul 26, 2010 7:54 pm |
|
|
I am not using any terminal, i using a program that i downloaded. I can send my strings(values 0 ->1024) throught it.
I found that if i type Alt 013 before the value and send, this works =)
But, what if i create a program(like in Java) to send a serial string plus Carriage Return, what should i write to the PIC to it reconize as a CR? Is it 0x0D? or 13? I tried this with the program that i told you before, but did not work.
Thanks for the help man |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Jul 26, 2010 8:06 pm |
|
|
Quote: | using a program that i downloaded. |
That's exactly what TeraTerm is. TeraTerm will send 0x13 when you
press the Enter key.
Download TeraTerm (using the link I gave you), and it's likely that all
your problems will be gone. |
|
|
Lobobranco
Joined: 26 Jul 2010 Posts: 10
|
|
Posted: Tue Jul 27, 2010 2:46 pm |
|
|
I understood.
But I must create a program (that is a part of my project) to send these strings, that is why I wanna know how to send the CR after the strings.
I tried to send, for example, 1000 0x0D and 1000 \n but did not work... |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19552
|
|
Posted: Tue Jul 27, 2010 3:04 pm |
|
|
'\n', is 'new line' (Line feed, not carriage return). '\r', is 'return' (yes, the carriage return....). |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Jul 27, 2010 3:05 pm |
|
|
If you have a question about what the compiler is doing, just make a
little test program and look at the .LST file. Here's a test program to
answer the question, "What byte does the compiler generate for \n
and \r " ?
Code: |
#include <16F877.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
//===========================
void main()
{
putc('\n');
putc('\r');
while(1);
}
|
Then compile it and look at what you get:
Code: |
.................... putc('\n');
0015: MOVLW 0A
0016: BCF STATUS.RP0
0017: BTFSS PIR1.TXIF
0018: GOTO 017
0019: MOVWF TXREG
.................... putc('\r');
001A: MOVLW 0D
001B: BTFSS PIR1.TXIF
001C: GOTO 01B
001D: MOVWF TXREG
....................
|
From this you can see that '\r' gives you the carriage return (0x0D or 13)
value. |
|
|
Lobobranco
Joined: 26 Jul 2010 Posts: 10
|
|
Posted: Tue Jul 27, 2010 9:33 pm |
|
|
Ttelmah, I did a mistake, I meant \r instead \n, I did \r and did not work.
PCM Programmer, thanks for this information, I did not know that =)
But now, my program is working, if I send the string followed by a Alt 013...
When I create my program to send these strings to the PIC, I will try to send using \r 13 0x0D and Alt 013...Lets see what it will do.
Thank you very much for all! |
|
|
|