|
|
View previous topic :: View next topic |
Author |
Message |
jeremy ragsdale Guest
|
Formatting result of ADC to Binary? |
Posted: Fri Feb 07, 2003 2:39 pm |
|
|
I am trying to get the binary 8 bit representation of the ADC and send it to the PC. I can manage to get a 0-255 value to the PC but I need the 0b00000000 representation. I'm sure this is really stupid and I have just been working to long to see the problem but if anyone could help I would really appreciate it.
#include <16f876.h>
#fuses HS,NOWDT,NOPROTECT,NOBROWNOUT,NOLVP,PUT
#use Delay(Clock=4000000)
#use rs232(baud=19200,xmit=pin_c6,rcv=pin_c7,parity=n,bits=8)
void main(void)
{
long sensor1;
setup_adc(adc_clock_div_32);
setup_adc_ports(all_analog);
while(1)
{
set_adc_channel(1);
delay_us(10);
sensor1 = read_adc();
printf("\%ld",sensor1);
}
delay_ms(1000);
}
}</font>
___________________________
This message was ported from CCS's old forum
Original Post ID: 11409 |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
Re: Formatting result of ADC to Binary? |
Posted: Fri Feb 07, 2003 2:56 pm |
|
|
:= I have just been working to long to see the problem...
---------------------------------------------------------
Well, I didn't see any of your code for the routine...
But anyway, here is a routine.
Code: |
#include "c:\program files\picc\devices\16F877.h"
#fuses HS,NOWDT,NOPROTECT,PUT,BROWNOUT, NOLVP
#use Delay(clock=8000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
void display_binary(char c);
//===============================
main()
{
display_binary(0x5A);
printf("\n\r");
display_binary(0xA5);
printf("\n\r");
while(1);
}
//================================
void display_binary(char c)
{
char i;
putc('0');
putc('b');
for(i = 0; i < 8; i++)
{
if(c & 0x80)
putc('1');
else
putc('0');
c <<= 1;
}
}
|
---
Edit: Put code in a code block.
___________________________
This message was ported from CCS's old forum
Original Post ID: 11411
Last edited by PCM programmer on Fri Mar 13, 2009 2:23 pm; edited 1 time in total |
|
|
jeremy ragsdale Guest
|
Re: Formatting result of ADC to Binary? |
Posted: Fri Feb 07, 2003 3:54 pm |
|
|
Lets say I have a PIC! I have one PIN set as Analoge in and assign the digital converted number to a long variable called sensor1. This variable goes from 0-255, this much is clear. when I use printf("\%d"); I send some number from 0-255 to the RS232 chip. I have a c program on the PC that gets this input, but only in the form of a string. The problem is I want the string to be sent in binary rather than decimalThe main question is, is there a simple way to send a decimal number in binary. Have a look at the code and please help.
#include <16f876.h>
#fuses HS,NOWDT,NOPROTECT,NOBROWNOUT,NOLVP,PUT
#use Delay(Clock=4000000)
#use rs232(baud=19200,xmit=pin_c6,rcv=pin_c7,parity=n,bits=8)
void main(void)
{
float sensor0,sensor1,sensor2,sensor3;
setup_adc(adc_clock_div_32);
setup_adc_ports(all_analog);
while(1)
{
/*
set_adc_channel(0);
delay_us(10);
sensor0 = read_adc();
//sensor0 = sensor0 * 0.02;
printf("\%ld",sensor0);
*/
set_adc_channel(1);
delay_us(10);
sensor1 = read_adc();
sensor1 = sensor1 * 0.02;
printf("\%f",sensor1);
/*
set_adc_channel(2);
delay_us(10);
sensor2 = read_adc();
//sensor2 = sensor2 * 0.02;
printf("\%ld",sensor2);
set_adc_channel(3);
delay_us(10);
sensor3 = read_adc();
//sensor3 = sensor3 * 0.02;
printf("\%ld",sensor3);
*/
delay_ms(1000);
}
}
___________________________
This message was ported from CCS's old forum
Original Post ID: 11414 |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
Re: Formatting result of ADC to Binary? |
Posted: Fri Feb 07, 2003 4:03 pm |
|
|
How about using putc() ?
The following test program will send the MSB
and LSB of a "long" to the RS-232 terminal.
It does not convert them to ascii. It just
sends the binary values.
#include "c:\program files\picc\devices\16F877.h"
#fuses HS,NOWDT,NOPROTECT,PUT,BROWNOUT, NOLVP
#use Delay(clock=8000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
//===============================================
main()
{
long k;
// For a test value, use something that will
// be readable when it's displayed on the terminal.
k = 0x3132;
putc(k >> 8); // Send MSB first
putc(k); // Send LSB
while(1);
}
___________________________
This message was ported from CCS's old forum
Original Post ID: 11415 |
|
|
ze.vana
Joined: 11 Jun 2011 Posts: 15
|
how to see status in binary |
Posted: Thu Nov 15, 2012 1:39 pm |
|
|
Maybe I am late here but I was looking for this and did not find anything good.
I know should have solved your problem but it can serve for others!
code: Code: | #include <16f628a.h>
#fuses INTRC_IO, NOLVP, NOWDT, PUT, BROWNOUT
#use delay(clock=4M)
#use RS232(baud=9600, xmit=PIN_b2,rcv=PIN_b1)
#define a0 input(pin_a0)
void stat_bit(byte val_bit)//print binary value
{
int i; printf("\ \n\r");
for (i=0; i<8; ++i)//run 8 time bit by bit
{
printf( "%d",( shift_left(&val_bit,1,0)));
delay_ms(10);
}
}
void main(void){
char cmd=0;
int a=1,b=2,c=4;
set_tris_a(0xff);//in
set_tris_b(0x00);//out
while(true)
{
// if (a0) {cmd++; delay_ms(200);}
switch (cmd) {
case 0: stat_bit(a);
printf(":STATUS-> A:%02u",a);//print A in decimal
delay_ms(200);
cmd++;
break;
case 1: stat_bit(b);
printf(":STATUS-> B:%02u",b);
delay_ms(200);
cmd++;
break;
case 2: stat_bit(c);//printbin(c);
printf(":STATUS-> C:%02u",c);
delay_ms(200);
cmd++;
break;
case 3: cmd=0;
break;
}
}
} /* You'll see this print:
00000001:STATUS-> A:01
00000010:STATUS-> B:02
00000100:STATUS-> C:04
00000001:STATUS-> A:01
00000010:STATUS-> B:02
00000100:STATUS-> C:04
00000001:STATUS-> A:01
00000010:STATUS-> B:02
*/
|
|
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
|
Posted: Thu Nov 15, 2012 6:09 pm |
|
|
If you waited until Feb, it would have been a decade!
But code contributions are always welcome!
(grin) _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19512
|
|
Posted: Fri Nov 16, 2012 4:08 am |
|
|
Except that the thread stopped, because it had been answered.
The original poster wanted to be able to send _raw binary_, _not_ ASCII text.....
Better to have contributed the code as a generic binary _text_ output routine in the code library, not here.
Best Wishes |
|
|
|
|
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
|