Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

efficient programming of ports partly in use

 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR
View previous topic :: View next topic  
Author Message
jwolf

Bascom Member



Joined: 18 Jan 2013
Posts: 24

PostPosted: Tue Oct 20, 2015 7:17 pm    Post subject: efficient programming of ports partly in use Reply with quote

Dear forum community,
I have the following problem:
I need for controlling a dot matrix LED display 4 bits of one port (e.g. PC0 .. PC3 of an Atmega Cool ,
whereas other bits of the port (PC4 and PC5) are already in use for I2C.
For the output to PC0 to PC3 I just need to output a number 0 .. 7.
So what I coded looks like:
Code:
I = Row And &B00000100                                   'filter out rowadr bit 2
   If I = 4 Then
      Rowadr2 = 1
      Else
      Rowadr2 = 0
   End If
   I = Row And &B00000010                                   'filter out rowadr bit 1
   If I = 2 Then
      Rowadr1 = 1
      Else
      Rowadr1 = 0
   End If
   I = Row And &B00000001                                   'filter out rowadr bit 0
   If I = 1 Then
      Rowadr0 = 1
      Else
      Rowadr0 = 0
   End If


1st question: is there any other and more effective way to set the port bits (Rowadr0 .. Rowadr2)?

2nd question: I tried to program the same as expressed in the If Then Else statement using logic AND operations only,
but I was very surprised that the execution took longer !? (measuring the time between the output of subsequent Row addresses with the scope).

Any ideas?

(BASCOM-AVR version : 2.0.7.8 )
Back to top
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 2262

blank.gif
PostPosted: Tue Oct 20, 2015 7:41 pm    Post subject: Re: efficient programming of ports partly in use Reply with quote

jwolf wrote:
For the output to PC0 to PC3 I just need to output a number 0 .. 7.

I do not see use of the 4th bit, 0-7 needs only 3 bits.
Also it is not clear from this chunk of code, where Rowadr0 starts, is it PC0?
If yes and in case "Row" will only contain values 0-7, your whole code can be replaced by:
Code:
I = PORTC AND &h11111000
PORTC = I OR Row
Back to top
View user's profile
jwolf

Bascom Member



Joined: 18 Jan 2013
Posts: 24

PostPosted: Tue Oct 20, 2015 8:20 pm    Post subject: Re: efficient programming of ports partly in use Reply with quote

Thank you for the reply,

sorry, I mixed up 1 .. 3 with 0 .. 2 - so of course 3 bits are sufficient to code 0 .. 7.
Your understanding is fully correct, Rowadr0 refers to port bit PC0.
I am aware of the possibility to write the entire port, but my fear was what would happen if I write to port bits that are used for other purposes and e.g. used as input?
So far I thought writing a 1 to a port bit that is configured as input would switch on the pull-up resistor?
Or is it only if I write the 1 to the port bit and when writing the entire port nothing would happen to the ports bits configured as input?

Best regards,
Johannes
Back to top
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 2262

blank.gif
PostPosted: Tue Oct 20, 2015 8:31 pm    Post subject: Re: efficient programming of ports partly in use Reply with quote

jwolf wrote:
Your understanding is fully correct, Rowadr0 refers to port bit PC0.

Then my code fits.
Quote:
So far I thought writing a 1 to a port bit that is configured as input would switch on the pull-up resistor?

An AND of &h11111000 with, for example bit 7 (input, no pullup, so content 0), still gives 0, so no modifications done.
Reading a bit with 0 and writing it to 0 doesn't change anything.
Quote:
Or is it only if I write the 1 to the port bit and when writing the entire port nothing would happen to the ports bits configured as input?

There's one risk: if interrupts write to the same port, then my short code needs to be made atomar, which means encapsulated by a Disable/Enable Interrupts.
Back to top
View user's profile
olhexy

Bascom Member



Joined: 03 Apr 2011
Posts: 192
Location: Tilburg, Netherlands

netherlands.gif
PostPosted: Tue Oct 20, 2015 9:08 pm    Post subject: Re: efficient programming of ports partly in use Reply with quote

jwolf wrote:
Dear forum community,
I have the following problem:
I need for controlling a dot matrix LED display 4 bits of one port (e.g. PC0 .. PC3 of an Atmega Cool ,
whereas other bits of the port (PC4 and PC5) are already in use for I2C.

Any ideas?

(BASCOM-AVR version : 2.0.7.8 )

Code:
 'By specifying Wr the library lcd4busy_anypin (based on LUCIANO) will be used
Config Lcdpin = Pin , Db4 = Portc.4 , Db5 = Portc.5 , Db6 = Portc.6 , Db7 = Portc.7 , E = Portc.2 , Wr = Portc.1 , Rs = Portc.0


Did you know that you can connect the LCDpins to every Port.bit as you wish?
For example:
Code:
Config Lcdpin = Pin , Db4 = Porta.4 , Db5 = Portb.5 , Db6 = Portc.6 , Db7 = Portd.7 , E = Portc.1 , Wr = Portb.2 , Rs = Porta.7

I am not sure if this goes for all versions of Bascom LCDlibraries, but if you do specify Wr, then you get lcd4busy_anypin Very Happy
Wr is in many older Bascom examples connected to ground, not to a Port.

Edit: My comment is for a HD44780 LCDdisplay. May be that a LEDdot display is totally different.


Last edited by olhexy on Tue Oct 20, 2015 9:40 pm; edited 1 time in total
Back to top
View user's profile
jwolf

Bascom Member



Joined: 18 Jan 2013
Posts: 24

PostPosted: Tue Oct 20, 2015 9:40 pm    Post subject: Re: efficient programming of ports partly in use Reply with quote

Thank you very much again.
This should solve my problem.
I will re-write my program accordingly and test it.

Best regards,
Johannes
Back to top
View user's profile
jwolf

Bascom Member



Joined: 18 Jan 2013
Posts: 24

PostPosted: Tue Oct 20, 2015 9:50 pm    Post subject: Re: efficient programming of ports partly in use Reply with quote

Dear olhexy,

I am not using a LCD display and thus not using any Bascom LCD library,
what I have is a LED dotmatrix with some logic inside which I need to control on low level, selecting lines and writing character patterns digit per digit.
I was also using LCD displays and the related library from Bascom, but was not aware so far of the Wr.
How can you specify Wr then?

Cheers,
Johannes
Back to top
View user's profile
olhexy

Bascom Member



Joined: 03 Apr 2011
Posts: 192
Location: Tilburg, Netherlands

netherlands.gif
PostPosted: Tue Oct 20, 2015 10:20 pm    Post subject: Re: efficient programming of ports partly in use Reply with quote

jwolf wrote:

How can you specify Wr then?

You can just not mention Wr, or comment it out in de config
Code:
'Wr = ....
, then it is not specified.
(But then Wr must be connected to GND, or you can also connect to a Port.bit that you set low. With this setting you can only write, reading is not possible. No big problem, because reading LCD is rare. But it (Wr = 1) is needed for reading the Busyflag. In LCDdocs Wr is the same as R/W; it is usually pin 5 at LCDconnection)



If you bring it up in the Config, like
Code:
Wr = Portb.1
then it is specified. Simple!
Back to top
View user's profile
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
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
You cannot attach files in this forum
You cannot download files in this forum