![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
EricKoh1985
Joined: 03 Mar 2008 Posts: 8
|
Problem in using "fgets()" for UART |
Posted: Fri Mar 14, 2008 3:57 am |
|
|
Here, i would like to do a password system to turn ON RB0's LED. For example, the password is "1111".
My problem is i don't know how to use "fgets()" for UART.
Below is my code (with missing code for "fgets()"):
Code: |
#include <16F628A.h>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP,HS
#include <stdio.h>
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_B2, rcv=PIN_B1)
void main()
{
printf("Please enter password\n");
while(1)
{
if (fgets( )) //inside the () should be the password code "1111"
output_a(0b00000001);
else
printf("Password Wrong\n");
}
}
|
Picture of Cirucit Design: http://www.imagehosting.com/show.php/1629177_UART.JPG.html
Thanks. |
|
![](templates/subSilver/images/spacer.gif) |
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Fri Mar 14, 2008 7:38 am |
|
|
Here is the example right out of the help files:
Code: | char string[30];
printf("Password: ");
gets(string);
if(strcmp(string, password))
printf("OK");
|
You only need fgets() instead of gets() if you are using streams. _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
![](templates/subSilver/images/spacer.gif) |
EricKoh1985
Joined: 03 Mar 2008 Posts: 8
|
|
Posted: Sun Mar 16, 2008 10:02 am |
|
|
Since my LED will only turn ON when password is '1111'. So, is it i need to replace 'password' with '1111'?
i have modified the code, as below
Code: |
#include <16F628A.h>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP,HS
#include <stdio.h>
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_B2, rcv=PIN_B1)
void main()
{
char string[30];
printf("Password: ");
gets(string);
if (strcmp(string,1111))
{
output_b(0b00000001);
printf("OK");
}
else
{
printf("Password Wrong");
}
}
|
Why when the password is wrong, the RB0 still can turn ON and program display "OK"? (Password command seem likes is not working)
How can i make my program will only display "OK" when password is '1111', other password will display "Password Wrong"? |
|
![](templates/subSilver/images/spacer.gif) |
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Mon Mar 17, 2008 2:25 am |
|
|
1111 in your code is not a string but a number. To change it to a string so that strcmp will work put " around it.
Also, strcmp returns 0 for a match so your code will produce the wrong result.
Change to
if (strcmp(string, "1111") == 0)
I also had the following problem, this may not be an issue with your version of the compiler but.
strcmp does not work with string litterals "abcde" but will only work with pointers so I had to do the following.
char match[80];
strcpy(match, "1111"); // Copy "1111" to match array
if (strcmp(string, match) == 0) {
hope this helps. |
|
![](templates/subSilver/images/spacer.gif) |
|
|
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
|