Can't Change Individual Ports

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 4.
To post in this forum you must have a registered copy of Flowcode 4 or higher. To sign up for this forum topic please use the "Online Resources" link in the Flowcode Help Menu.

Moderator: Benj

Post Reply
rocket200
Posts: 68
Joined: Mon Oct 24, 2011 7:50 pm
Has thanked: 11 times
Been thanked: 3 times
Contact:

Can't Change Individual Ports

Post 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!!!

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Can't Change Individual Ports

Post 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.

rocket200
Posts: 68
Joined: Mon Oct 24, 2011 7:50 pm
Has thanked: 11 times
Been thanked: 3 times
Contact:

Re: Can't Change Individual Ports

Post 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


User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Can't Change Individual Ports

Post 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.

rocket200
Posts: 68
Joined: Mon Oct 24, 2011 7:50 pm
Has thanked: 11 times
Been thanked: 3 times
Contact:

Re: Can't Change Individual Ports

Post 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!

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Can't Change Individual Ports

Post 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);

rocket200
Posts: 68
Joined: Mon Oct 24, 2011 7:50 pm
Has thanked: 11 times
Been thanked: 3 times
Contact:

Re: Can't Change Individual Ports

Post 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!

rocket200
Posts: 68
Joined: Mon Oct 24, 2011 7:50 pm
Has thanked: 11 times
Been thanked: 3 times
Contact:

Re: Can't Change Individual Ports

Post 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

Post Reply