View previous topic :: View next topic |
Author |
Message |
art
Joined: 21 May 2015 Posts: 181
|
Output B problem |
Posted: Sun Aug 02, 2015 6:18 pm |
|
|
Hi, i would like to send my input character to output_B. If i type in '00000111' why output B cannot show binary 00000111 ?
Below is my code:
Code: |
#include <18F452.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(c20000000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#include <stdio.h>
#include <string.h>
void main()
{
char x[8];
while(true)
{
printf("Enter number: \n");
get_string(x,8);
output_B(0bx)
}
} |
|
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Aug 02, 2015 11:57 pm |
|
|
1. Your array is too small for the whole string. A string always has a 0x00
byte at the end. This requires a 9-element array.
2. output_b() doesn't take a string as the parameter. It takes an integer.
This is in the CCS manual.
3. You need a routine to convert an ascii binary string to an integer.
CCS doesn't have one, so I have provided it below. The routine is
called abtoi(). It means "ascii binary to integer".
Code: | #include <16F887.h>
#fuses INTRC_IO, NOWDT
#use delay(clock=4M)
#use rs232(baud=9600, UART1, ERRORS)
#include <stdlib.h>
int8 binary_string[9];
int8 abtoi(char *ptr)
{
long retval;
char c;
char i;
retval = 0;
for(i = 0; i < 8; i++) // Read up to 8 characters
{
c = *ptr++; // Read char from string
if(c < '0') // Test if it's an ascii binary value
break; // If not, we are done
if(c > '1')
break;
retval <<= 1; // Shift left by 1
if(c == '1')
retval |= 1;
}
return(retval);
}
//======================
void main()
{
int8 result;
strcpy(binary_string, "00000111");
result = abtoi(binary_string);
printf("result = %u \r", result);
while(TRUE);
} |
|
|
![](templates/subSilver/images/spacer.gif) |
art
Joined: 21 May 2015 Posts: 181
|
|
Posted: Mon Aug 03, 2015 1:00 am |
|
|
Thank you for your reply. If i want to send result to port b, is it :
printf("result = %u \r", result);
output_B(result);
Kindly please reply. |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Aug 03, 2015 1:25 am |
|
|
Get the string. Then do this:
Code: | result = abtoi(binary_string);
output_B(result); |
|
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19369
|
|
Posted: Mon Aug 03, 2015 1:59 am |
|
|
As a comment, 'C' does have a binary to integer function. So you can do the conversion using:
Code: |
#include <16F887.h>
#fuses INTRC_IO, NOWDT
#use delay(clock=4M)
#use rs232(baud=9600, UART1, ERRORS)
#include <stdlib.h>
#include <input.c> //This is what contains the get_string function
#include <string.h>
int8 binary_string[10];
//======================
void main()
{
while (TRUE)
{
printf("Enter number: \n");
get_string(binary_string,9);
output_B((int8)strtoul(binary_string,0,2));
}
}
|
The 'downside', is that because this is a 'universal' function, it probably results in bulkier code (though the compiler optimiser is quite good).
It says convert the string, up to the first non numeric character into an unsigned long integer. Any remaining characters would be stored at the location given by the second argument (0 says to ignore these), and to convert the number from base 2 (that's what the '2' says).
I then cast this to an int8 (not really necessary, but I tend to play safe...). |
|
![](templates/subSilver/images/spacer.gif) |
art
Joined: 21 May 2015 Posts: 181
|
|
Posted: Mon Aug 03, 2015 4:30 am |
|
|
Thank you very much both of you for solving my problem. |
|
![](templates/subSilver/images/spacer.gif) |
|