Page 1 of 1
Can't Change Individual Ports
Posted: Wed Apr 04, 2012 2:42 pm
by rocket200
Hello,
So I I need to display a number on one 7 segment display. There is one problem though. When I go into the Connections and try to change one Port Letter (i.e. PORTA, or PORTB) it changes EVERY port letter. I need to change segment A Port to PORTD 0 and segment B and up to ports PORTB 0 - PORTB 6. The reason I need to change segment A to a different port is because that port is being used by something else.
I'd really appreciate any help! Thank you!!!
Re: Can't Change Individual Ports
Posted: Wed Apr 04, 2012 3:50 pm
by Benj
Hello,
Ok this is possible but you cannot do it directly through the component without using code customization.
Select the component on the panel and click the custom code button.
Select the Show digit function and click edit.
Scroll down to the section beginning like this.
Code: Select all
MX_7SEG1_COMMON_TRIS = MX_7SEG1_COMMON_TRIS & %o;
MX_7SEG1_SEGMENT_TRIS = MX_7SEG1_SEGMENT_TRIS & %p;
Edit it so that it matches your desired pin out. Basically you need to clear each bit in the tris register associated with the 8 outputs for the 7seg.
Code: Select all
MX_7SEG1_COMMON_TRIS = MX_7SEG1_COMMON_TRIS & %o;
TRISD = TRISD & ~(0x01);
TRISB = TRISB & ~(0x7E);
Next find this section.
Code: Select all
//common anode
MX_7SEG1_SEGMENT_PORT = (MX_7SEG1_SEGMENT_PORT & %p) | cSegmentValue;
MX_7SEG1_COMMON_PORT = (MX_7SEG1_COMMON_PORT | %n);
#else
//common cathode
MX_7SEG1_SEGMENT_PORT = (MX_7SEG1_SEGMENT_PORT & %p) | ~cSegmentValue;
MX_7SEG1_COMMON_PORT = (MX_7SEG1_COMMON_PORT & %o);
And change to this to route the data to the correct output pins.
Code: Select all
//common anode
PORTD = (PORTD & 0xFE) | (cSegmentValue & 0x01);
cSegmentValue = cSegmentValue >> 1;
PORTB = (PORTB & 0x80) | (cSegmentValue & 0x7F);
MX_7SEG1_COMMON_PORT = (MX_7SEG1_COMMON_PORT | %n);
#else
//common cathode
PORTD = (PORTD & 0xFE) | (~cSegmentValue & 0x01);
cSegmentValue = cSegmentValue >> 1;
PORTB = (PORTB & 0x80) | (~cSegmentValue & 0x7F);
MX_7SEG1_COMMON_PORT = (MX_7SEG1_COMMON_PORT & %o);
Click ok and then close and that's it your done.
Let me know how you get on.
Re: Can't Change Individual Ports
Posted: Wed Apr 04, 2012 6:03 pm
by rocket200
Benj wrote:Hello,
Ok this is possible but you cannot do it directly through the component without using code customization.
Select the component on the panel and click the custom code button.
Select the Show digit function and click edit.
Scroll down to the section beginning like this.
Code: Select all
MX_7SEG1_COMMON_TRIS = MX_7SEG1_COMMON_TRIS & %o;
MX_7SEG1_SEGMENT_TRIS = MX_7SEG1_SEGMENT_TRIS & %p;
Edit it so that it matches your desired pin out. Basically you need to clear each bit in the tris register associated with the 8 outputs for the 7seg.
Code: Select all
MX_7SEG1_COMMON_TRIS = MX_7SEG1_COMMON_TRIS & %o;
TRISD = TRISD & ~(0x01);
TRISB = TRISB & ~(0x7E);
Next find this section.
Code: Select all
//common anode
MX_7SEG1_SEGMENT_PORT = (MX_7SEG1_SEGMENT_PORT & %p) | cSegmentValue;
MX_7SEG1_COMMON_PORT = (MX_7SEG1_COMMON_PORT | %n);
#else
//common cathode
MX_7SEG1_SEGMENT_PORT = (MX_7SEG1_SEGMENT_PORT & %p) | ~cSegmentValue;
MX_7SEG1_COMMON_PORT = (MX_7SEG1_COMMON_PORT & %o);
And change to this to route the data to the correct output pins.
Code: Select all
//common anode
PORTD = (PORTD & 0xFE) | (cSegmentValue & 0x01);
cSegmentValue = cSegmentValue >> 1;
PORTB = (PORTB & 0x80) | (cSegmentValue & 0x7F);
MX_7SEG1_COMMON_PORT = (MX_7SEG1_COMMON_PORT | %n);
#else
//common cathode
PORTD = (PORTD & 0xFE) | (~cSegmentValue & 0x01);
cSegmentValue = cSegmentValue >> 1;
PORTB = (PORTB & 0x80) | (~cSegmentValue & 0x7F);
MX_7SEG1_COMMON_PORT = (MX_7SEG1_COMMON_PORT & %o);
Click ok and then close and that's it your done.
Let me know how you get on.
My code looks a little different so I can't figure out where exactly to put the new code. Also, can you explain how the hex values correspond to the pin?
My code:
Code: Select all
//Segment Port Definitions
#define MX_7SEG1_SEGMENT_PORT PORT%a
#define MX_7SEG1_SEGMENT_DDR DDR%a
//Common Port Definitions
#define MX_7SEG1_COMMON_PORT PORT%b
#define MX_7SEG1_COMMON_DDR DDR%b
//set pattern array for the display
const char cSegmentArray[] = {%c, %d, %e, %f, %g, %h, %i, %j, %k, %l};
//get pattern for digit
char cSegmentValue = cSegmentArray[Value % 10];
if (DecimalPoint)
cSegmentValue = cSegmentValue & %m;
//set up ports
MX_7SEG1_COMMON_DDR = MX_7SEG1_COMMON_DDR | ~%o;
MX_7SEG1_SEGMENT_DDR = MX_7SEG1_SEGMENT_DDR | ~%p;
//display the digit
#if (%q == 1)
//common anode
MX_7SEG1_SEGMENT_PORT = (MX_7SEG1_SEGMENT_PORT & %p) | cSegmentValue;
MX_7SEG1_COMMON_PORT = (MX_7SEG1_COMMON_PORT | %n);
#else
//common cathode
MX_7SEG1_SEGMENT_PORT = (MX_7SEG1_SEGMENT_PORT & %p) | ~cSegmentValue;
MX_7SEG1_COMMON_PORT = (MX_7SEG1_COMMON_PORT & %o);
#endif
//undefine symbols
#undef MX_7SEG1_SEGMENT_PORT
#undef MX_7SEG1_SEGMENT_DDR
#undef MX_7SEG1_COMMON_PORT
#undef MX_7SEG1_COMMON_DDR
Re: Can't Change Individual Ports
Posted: Thu Apr 05, 2012 11:16 am
by Benj
Aha your using AVR not PIC, sorry about that.
Ok this section.
Code: Select all
//set up ports
MX_7SEG1_COMMON_DDR = MX_7SEG1_COMMON_DDR | ~%o;
MX_7SEG1_SEGMENT_DDR = MX_7SEG1_SEGMENT_DDR | ~%p;
Change to this.
Code: Select all
MX_7SEG1_COMMON_DDR = MX_7SEG1_COMMON_DDR | ~%o;
DDRD = DDRD | 0x01;
DDRB = DDRB | 0x7E;
And this section.
Code: Select all
//common anode
MX_7SEG1_SEGMENT_PORT = (MX_7SEG1_SEGMENT_PORT & %p) | cSegmentValue;
MX_7SEG1_COMMON_PORT = (MX_7SEG1_COMMON_PORT | %n);
#else
//common cathode
MX_7SEG1_SEGMENT_PORT = (MX_7SEG1_SEGMENT_PORT & %p) | ~cSegmentValue;
MX_7SEG1_COMMON_PORT = (MX_7SEG1_COMMON_PORT & %o);
Change to this.
Code: Select all
//common anode
PORTD = (PORTD & 0xFE) | (cSegmentValue & 0x01);
cSegmentValue = cSegmentValue >> 1;
PORTB = (PORTB & 0x80) | (cSegmentValue & 0x7F);
MX_7SEG1_COMMON_PORT = (MX_7SEG1_COMMON_PORT | %n);
#else
//common cathode
PORTD = (PORTD & 0xFE) | (~cSegmentValue & 0x01);
cSegmentValue = cSegmentValue >> 1;
PORTB = (PORTB & 0x80) | (~cSegmentValue & 0x7F);
MX_7SEG1_COMMON_PORT = (MX_7SEG1_COMMON_PORT & %o);
Each Port register is made up of 8 bits. A hex value from 0x00 to 0xFF is an 8-bit value that contains all of the possible combinations of 0's and 1's, 256 different combinations to be precise and including 0.
cSegmentValue & 0x01
This masks off the first data bit allowing us to write the bit0 of portd.
cSegmentValue >> 1
cSegmentValue & 0x7F
This shifts the variable cSegmentValue to the right by one place essentially dropping the least significant bit.
We then use 7F to mask off the remaining 7 bits of data to pass to portb.
Re: Can't Change Individual Ports
Posted: Thu Apr 05, 2012 7:39 pm
by rocket200
Benj wrote:Aha your using AVR not PIC, sorry about that.
Ok this section.
Code: Select all
//set up ports
MX_7SEG1_COMMON_DDR = MX_7SEG1_COMMON_DDR | ~%o;
MX_7SEG1_SEGMENT_DDR = MX_7SEG1_SEGMENT_DDR | ~%p;
Change to this.
Code: Select all
MX_7SEG1_COMMON_DDR = MX_7SEG1_COMMON_DDR | ~%o;
DDRD = DDRD | 0x01;
DDRB = DDRB | 0x7E;
And this section.
Code: Select all
//common anode
MX_7SEG1_SEGMENT_PORT = (MX_7SEG1_SEGMENT_PORT & %p) | cSegmentValue;
MX_7SEG1_COMMON_PORT = (MX_7SEG1_COMMON_PORT | %n);
#else
//common cathode
MX_7SEG1_SEGMENT_PORT = (MX_7SEG1_SEGMENT_PORT & %p) | ~cSegmentValue;
MX_7SEG1_COMMON_PORT = (MX_7SEG1_COMMON_PORT & %o);
Change to this.
Code: Select all
//common anode
PORTD = (PORTD & 0xFE) | (cSegmentValue & 0x01);
cSegmentValue = cSegmentValue >> 1;
PORTB = (PORTB & 0x80) | (cSegmentValue & 0x7F);
MX_7SEG1_COMMON_PORT = (MX_7SEG1_COMMON_PORT | %n);
#else
//common cathode
PORTD = (PORTD & 0xFE) | (~cSegmentValue & 0x01);
cSegmentValue = cSegmentValue >> 1;
PORTB = (PORTB & 0x80) | (~cSegmentValue & 0x7F);
MX_7SEG1_COMMON_PORT = (MX_7SEG1_COMMON_PORT & %o);
Each Port register is made up of 8 bits. A hex value from 0x00 to 0xFF is an 8-bit value that contains all of the possible combinations of 0's and 1's, 256 different combinations to be precise and including 0.
cSegmentValue & 0x01
This masks off the first data bit allowing us to write the bit0 of portd.
cSegmentValue >> 1
cSegmentValue & 0x7F
This shifts the variable cSegmentValue to the right by one place essentially dropping the least significant bit.
We then use 7F to mask off the remaining 7 bits of data to pass to portb.
So I actually need to change Segment D instead. I made all of your modifications with a couple changes to make it change to Segment D instead of A. So far, all the segments are working except for Segment D.
Here is the code:
Code: Select all
//set up ports
MX_7SEG1_COMMON_DDR = MX_7SEG1_COMMON_DDR | ~%o;
DDRB = DDRB | 0xF7;
DDRD = DDRD | 0x01;
Code: Select all
//common cathode
PORTB = (PORTB & 0xFE) | (~cSegmentValue & 0x01);
PORTB = (PORTB & 0xFD) | (~cSegmentValue & 0x02);
PORTB = (PORTB & 0xFB) | (~cSegmentValue & 0x04);
PORTD = (PORTD & 0xF7) | (~cSegmentValue & 0x08);
PORTB = (PORTB & 0xEF) | (~cSegmentValue & 0x10);
PORTB = (PORTB & 0xDF) | (~cSegmentValue & 0x20);
PORTB = (PORTB & 0xBF) | (~cSegmentValue & 0x40);
PORTB = (PORTB & 0x7F) | (~cSegmentValue & 0x80);
MX_7SEG1_COMMON_PORT = (MX_7SEG1_COMMON_PORT & %o);
Thanks for all your help!
Re: Can't Change Individual Ports
Posted: Tue Apr 10, 2012 9:42 am
by Benj
Hello,
I think this line of code.
Code: Select all
PORTD = (PORTD & 0xF7) | (~cSegmentValue & 0x08);
Should look like this if you are using pin D0.
Code: Select all
PORTD = (PORTD & 0xFE) | ((~cSegmentValue & 0x08) >> 3);
Re: Can't Change Individual Ports
Posted: Tue Apr 10, 2012 7:08 pm
by rocket200
Benj wrote:Hello,
I think this line of code.
Code: Select all
PORTD = (PORTD & 0xF7) | (~cSegmentValue & 0x08);
Should look like this if you are using pin D0.
Code: Select all
PORTD = (PORTD & 0xFE) | ((~cSegmentValue & 0x08) >> 3);
It works! Thank you!
Re: Can't Change Individual Ports
Posted: Tue Jun 26, 2012 9:01 pm
by rocket200
Benj wrote:Hello,
I think this line of code.
Code: Select all
PORTD = (PORTD & 0xF7) | (~cSegmentValue & 0x08);
Should look like this if you are using pin D0.
Code: Select all
PORTD = (PORTD & 0xFE) | ((~cSegmentValue & 0x08) >> 3);
Hello! Sorry to bring this thread back from the dead, but I have a problem.
Basically I got a new 7-segment display and it has common anodes instead of cathodes. Could you help me make the code for the common anodes instead of the cathodes? Same exact as the common cathodes setup except now they need to be anodes. I am guessing the ports and segments would need to be inverted so the electricity can flow into the ports instead of out? I can't figure this out so could I get your help? Thank you so very much!
Also, if it helps, here is a pinout of what I need:
SEGMENT A > PORTB.0
SEGMENT B > PORTB.1
SEGMENT C > PORTB.2
SEGMENT D > PORTD.0
SEGMENT E > PORTB.4
SEGMENT F > PORTB.5
SEGMENT G > PORTB.6