- Joined
- Jul 4, 2009
- Messages
- 16,243
- Helped
- 5,140
- Reputation
- 10,309
- Reaction score
- 5,123
- Trophy points
- 1,393
- Location
- Aberdyfi, West Wales, UK
- Activity points
- 137,461
I would guess your problem is therefore that the signals are inverted. With no segments in the data you get them all turned on instead of turned off. This is probably to do with the drive waveform to the original LEDs being active low.
If it's a problem, just invert the data in software before writing it out to the TX port.
Brian.
while(1)
{
if(segment1==1)
{
segment1_var=1;
seg_data1=P0;
UART_TxChar('1');
UART_TxChar(seg_data1);
}
if(segment2==1)
{
segment2_var=1;
seg_data2=P0;
UART_TxChar('2');
UART_TxChar(seg_data2);
}
if(segment3==1)
{
segment3_var=1;
seg_data3=P0;
UART_TxChar('3');
UART_TxChar(seg_data3);
}
if(segment4==1)
{
segment4_var=1;
seg_data4=P0;
UART_TxChar('4');
UART_TxChar(seg_data4);
}
}
if(segment1==1)
segment1_var=1;
seg_data1=P0;
UART_TxChar('1');
UART_TxChar(seg_data1);
Is not very useful information as long as we don´t know timer0 period time (and the timing mentioned before)i even tried timer0 interrupt for sending the data's
Hi,
our problem is: We see only snippets of code, we can´t know the timing and we can´t see your idea and how you use the variables.
example1:
* We don´t know what "segment1" is. It may be a variable, it may be a port...Code:if(segment1==1)
* you check for status "1". But we don´t see that it changes. Who changes this value? and what´s the timing? How long is it "1" and how long is it NOT "1"?
example2:
* We don´t know what this means. They seem to be two variables. But what are they used for?Code:segment1_var=1; seg_data1=P0;
* "segment1_var" set to "1" but never set to any other value. This makes no sense to me.
* what is "seg_data1" used for?
example3:
How do you prevent that this data is sent many times repeatedly?Code:UART_TxChar('1'); UART_TxChar(seg_data1);
example4:
Is not very useful information as long as we don´t know timer0 period time (and the timing mentioned before)
****
Please understand it´s really difficult to help with this lack of informations.
Klaus
#include<reg51.h>
sbit segment1 = P2^4; // SEGMENT - 1 SELECTION PIN
sbit segment2 = P2^5; // SEGMENT - 2 SELECTION PIN
sbit segment3 = P2^6; // SEGMENT - 3 SELECTION PIN
sbit segment4 = P2^7; // SEGMENT - 4 SELECTION PIN
unsigned int segment1_var=0, segment2_var=0, segment3_var=0, segment4_var=0;
char seg_data1, seg_data2, seg_data3, seg_data4, seg_data_copy;
void UART_Init()
{
SCON = 0x50; // Asynchronous mode, 8-bit data and 1-stop bit
TMOD = 0x20; //Timer1 in Mode2.
TH1 = 0xF7; // Baudrate 9600
TR1 = 1; //Turn ON the timer for Baud rate generation
PCON=0x80;
}
void UART_TxChar(char ch)
{
SBUF = ch; // Load the data to be transmitted
while(TI==0); // Wait till the data is trasmitted
TI = 0; //Clear the Tx flag for next cycle.
}
void main()
{
UART_Init();
while(1)
{
if(segment1==1)
{
segment1_var=1;
seg_data1=P0;
UART_TxChar('1');
UART_TxChar(seg_data1);
}
if(segment2==1)
{
segment2_var=1;
seg_data2=P0;
UART_TxChar('2');
UART_TxChar(seg_data2);
}
if(segment3==1)
{
segment3_var=1;
seg_data3=P0;
UART_TxChar('3');
UART_TxChar(seg_data3);
}
if(segment4==1)
{
segment4_var=1;
seg_data4=P0;
UART_TxChar('4');
UART_TxChar(seg_data4);
}
}
}
Convert the 10 possible combinations of segments (0-9) into the binary numbers 0-9.
Brian.
I agree with your concept.No problem, all it has to do is recognize the different segment patterns and encode them into the minimum number of bits. There are only 80 possible combinations of segments plus 'none' so every possible digit, character and shape can be represented in 7 bits at most but I suspect most combinations are meaningless so you can shorten it to 6, still leaving two bits to carry the digit number.
Look here for clues:
https://puzzling.stackexchange.com/...ou-create-with-only-one-seven-segment-display
Format the data through the UART as DDSSSSSS and it will still work.
Brian.
char seg_no
char seg_data
if(segment1==1) // scan is segment 1 is selected
{
seg_no=1; write 1 to seg_no as segment 1 is selected.
seg_data = P0; write port status to seg_data.
}
I understand what you are trying to do but you complicate things by confusing segments and digits.
A segment is an individual bar making up the shape of a number. A digit is the whole single number.
So for each digit there are 7 segments plus a dot. Your whole display has four digits.
That means you have to TX a value that identifies which segments are lit and also a value identifying which of the four digits the segment value came from. By encoding the values you can fit them into a single byte.
I can't help you with 8051 coding because the last time I used one was about 30 years ago and I do not presently have a compiler suitable for 8051 source code but the same principle applies to all processors.
At the TX end:
Step 1:
First list all the possible combinations of segments you are going to use, you should have a list like 0,1,2,3,4,5,6,7,8,9,A,b,c,C,d,E,f,H,h,-,L,P and possibly other special shapes.
Convert the segment patterns into binary 'on/off' bits so a '1' is for a segment lit up and '0' is for one un-powered. I strongly advise you to use the convention of segment 'A' being the top then working clockwise to 'F' with 'G' being the center bar. Make segment 'A' the least significant bit of the binary number so it looks like this 'GFEDCBA' and if you need the decimal dot add it as the MSB.
Step 2:
Sending the binary representation of segments can be done but is would need all 8 bits if you use one bit per segment. It makes sense to ignore the segment combinations you are not going to use. For example, if you only use the ones I listed earlier there are only 22 combinations used and you can squeeze that into 5 bits if you encode them. Use a look up table to convert each of the segment combinations you need into a unique number, something like this:
displayed -- segments -- binary -- unique number (x = not lit)
0 -- xxFEDCBA -- 00111111 -- 00000
1 -- xxxxxCBx -- 00000110 -- 00001
2 -- xGFxDxBA -- 01101011 -- 00010
.
.
H -- xGFExCBx -- 01110110 -- 01100
So now for each possible character you display there is a unique 5-bit number.
Step 3:
You have four digits so you need to identify which of them the segment data refers to. Read in the four digit select signals from the display driver. Encode the four signals into two bits:
0001 = 00
0010 = 01
0100 = 10
1000 = 11
so you now have a two bit number that identifies which display was active when the segment data was valid.
Step 4:
Combine the 5-bit segment data with the 2-bit digit data. Do this by shifting the digit data (call it DD) to the left then ANDing it with the segment data (call it SSSSS) so you get a single byte of '0DDSSSSS'. This contains 'which digit' and 'what it displayed' data.
Step 5:
Transmit the 0DDSSSSS byte through the UART.
Step 6:
The digit select signal and the segment data on the display is constantly changing so you need to be sure you don't read them while they are showing half of one number and half of another. The easiest way to do that is to wait until you see the next digit select signal appearing so monitor it for change. When you see it change, immediately read in the segment values.
At the RX end:
Step 1:
Read the next byte from the UART.
Step 2:
Split the bits so the two 'DD' bits and the five 'SSSSS' are in different variables.
Step 3:
Convert the DD bits back to four pin signals, these will drive the digit selects at the receiver display.
Use a look-up table to convert the SSSSS bits back to their original segment patterns (the reverse of what was done at the TX end)
Step 4:
Output the signals through appropriate drivers to the four digits at the RX end.
Brian.
comparing a byte with the informations form the segments with a table of - let´s say 32 bytes (table items) of possible codes (sadly you don´t give the count of different codes)rather than increasing the software part and making the controller busy in doing the calculations and conversions,
Bottom line: Yes you can do it in hardware but you already have a processor and software is free.
Follow Klaus's advice - define the problem accurately and you find the optimal solution.
Brian.
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?