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 CCS Technical Support

Output B problem

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



Joined: 21 May 2015
Posts: 181

View user's profile Send private message

Output B problem
PostPosted: Sun Aug 02, 2015 6:18 pm     Reply with quote

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)
       
    }

}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Aug 02, 2015 11:57 pm     Reply with quote

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);   
}
art



Joined: 21 May 2015
Posts: 181

View user's profile Send private message

PostPosted: Mon Aug 03, 2015 1:00 am     Reply with quote

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.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Aug 03, 2015 1:25 am     Reply with quote

Get the string. Then do this:
Code:
result = abtoi(binary_string);
output_B(result);
Ttelmah



Joined: 11 Mar 2010
Posts: 19520

View user's profile Send private message

PostPosted: Mon Aug 03, 2015 1:59 am     Reply with quote

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...).
art



Joined: 21 May 2015
Posts: 181

View user's profile Send private message

PostPosted: Mon Aug 03, 2015 4:30 am     Reply with quote

Thank you very much both of you for solving my problem.
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