View previous topic :: View next topic |
Author |
Message |
piire
Joined: 04 Mar 2009 Posts: 19
|
bcd output to rs232 |
Posted: Sat Mar 07, 2009 4:47 am |
|
|
hi there,
im a bit stuck on how to get four digital inputs from a bcd counter on to the rs232, and display them in decimal.
i was wondering to to combine all the four input to get one decimal output.
im using a pic16f788a, the inputs are connected to B0, B1, B2, and B3. the ouput to the RS232 is E0, E1.
this is the code i have been tring but, it just diplay the same numbers on the hyperterminal
Code: |
#include <16F877A.h>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
#use rs232(baud=2400,xmit=PIN_E0,rcv=PIN_E1)
int bcd1;
int bcd2;
int bcd3;
int bcd4;
int count;
void main()
{
bcd1= input(PIN_B0);
bcd2= input(PIN_B1);
bcd3= input(PIN_B2);
bcd4= input(PIN_B3);
count= bcd1 + bcd2 + bcd3 + bcd4;
while(1)
{
printf("counter = %d",count);
delay_ms(100);
}
}
|
i no im missing something out i cant figure what, if someone can help i would be greatful
thanks in advance
piire! |
|
|
Ttelmah Guest
|
|
Posted: Sat Mar 07, 2009 5:17 am |
|
|
If these are four 'bits' representing a BCD number, then the they can only represent a number from 0-9. Is this all the counter does?. If not, then you need to rethink how the interface actually works.
If this is all it does, then you need:
count= bcd1 + (bcd2*2) + (bcd3*4) + (bcd4*8);
However much simpler, to just do:
count=input_b() & 0xF;
Best Wishes |
|
|
Sydney
Joined: 13 Feb 2009 Posts: 71
|
|
Posted: Sat Mar 07, 2009 5:22 am |
|
|
Code: | count= bcd1 + bcd2*2 + bcd3*4 + bcd4*8; |
Would be the easiest way to fix your problem. There are more efficient ways to do it.
Code: | count=input_b() & 0x0f //mask of upper bits |
Beware the 2nd method will make the upper bit of portb inputs, unless you are using #use fast_io |
|
|
Sydney
Joined: 13 Feb 2009 Posts: 71
|
|
Posted: Sat Mar 07, 2009 5:24 am |
|
|
|
|
|
piire
Joined: 04 Mar 2009 Posts: 19
|
|
Posted: Sat Mar 07, 2009 6:31 am |
|
|
Thanks for the help i will give it a try!
What i have is: a circuit which has a switch a bcd and a few gates and two seven segment displays. When i press the switch the number on the display increases. So what i have done is taken the output of the bcd and put them into the input port b, so that i can also get the reading on the computer as well. The maximum number is 99. Is it better to connect the 7 segment displays to the pic and have a switch on another digital port and just write a program that display the number on the displays and computer?
If anyone has an example i would be grateful.
thanks
piire |
|
|
Sydney
Joined: 13 Feb 2009 Posts: 71
|
|
Posted: Sat Mar 07, 2009 10:21 am |
|
|
Well given it can be done with just the main components, 7 segments, switch, and pic, and very little else(a couple of transistors for the digit sinks), the hardware will be much simpler, but obviously the software will be a little more involved, but still pretty simple, presumably you will multiplex the displays so will need 9/10 I/O depending if you need the DP and 1 I/O for the switch. |
|
|
|