CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Writing to two different I/O at the same time

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
towpew



Joined: 25 Sep 2015
Posts: 24
Location: sweden

View user's profile Send private message

Writing to two different I/O at the same time
PostPosted: Thu Oct 08, 2015 9:41 am     Reply with quote

Hello
I have a C code question.
I have seen in the manual that it is possible to write to different bits at the same time using a struct like this.

Code:

//PIC 16F1937

#byte LATA = 0x10C
#bit Abit1 = LATA.1
#bit Abit4 = LATA.4

struct {
          int1 Abit1;
          int1 Abit4;
         } MyAbits;

#byte MyAbits = 0x10C



Now MyAbits pionts to the LATA regsiter and only bit 1 and 4.
This works OK.

But if i want to include I/O from another port say PORTB I cannot include them in the same struct that want work because I can only point MyAbits to 1 memory register.

To clarify, I want to built my own i/o MAP from different i/o's.
MyAbits should be a variable that writes to bit1 and 4 in PORTA and bit 2 and 5 in PORTB and bit 7 and 8 in PORTC.

Does anyone know a way to do this using CCS ?
Ttelmah



Joined: 11 Mar 2010
Posts: 19297

View user's profile Send private message

PostPosted: Thu Oct 08, 2015 10:02 am     Reply with quote

Nothing really to do with CCS. The _processor_ cannot write to two memory addresses at the same time.
towpew



Joined: 25 Sep 2015
Posts: 24
Location: sweden

View user's profile Send private message

Writing to two different I/O at the same time
PostPosted: Thu Oct 08, 2015 11:28 am     Reply with quote

Yea Ok i understand that.

But I was curious how the actual processor uses LCD segments on different ports, but it could may be done in the hardware.
Ttelmah



Joined: 11 Mar 2010
Posts: 19297

View user's profile Send private message

PostPosted: Fri Oct 09, 2015 12:09 am     Reply with quote

Totally hardware.

When you use a peripheral, it disables the normal port I/O functions. Things like the LCD, are done from larger latches, connected directly to the pins (except for the switches to disconnect them when normal port I/O is involved). Effectively the pin is no longer portx.y, but just a pin connected to the LCD controller.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Oct 09, 2015 7:07 am     Reply with quote

Quote:
But I was curious how the actual processor uses LCD segments on
different ports, but it could may be done in the hardware.

It uses BCF and BSF statements to clear or set bits in the LCDDATAx
registers, which then control the segment driver pins. Example:
Code:

....................       case 1:   // Digit on left side of LCD.     
....................         lcd_symbol(segments >> 8, DIGIT8_H);   
009C:  MOVFF  segments+1,@@19
00A0:  CLRF   @@1A
00A2:  MOVLB  F
00A4:  BTFSS  x58.4
00A6:  BRA    00A4
00A8:  BTFSS  @@19.7
00AA:  BCF    LCDDATA3.SEG28COM0
00AC:  BTFSC  @@19.7
00AE:  BSF    LCDDATA3.SEG28COM0
00B0:  BTFSS  @@19.6
00B2:  BCF    LCDDATA9.SEG28COM1
00B4:  BTFSC  @@19.6
00B6:  BSF    LCDDATA9.SEG28COM1
00B8:  BTFSS  @@19.5
00BA:  BCF    LCDDATA15.SEG28COM2
00BC:  BTFSC  @@19.5
00BE:  BSF    LCDDATA15.SEG28COM2

Code compiled from:
http://www.ccsinfo.com/forum/viewtopic.php?t=33377&start=11
asmallri



Joined: 12 Aug 2004
Posts: 1632
Location: Perth, Australia

View user's profile Send private message Send e-mail Visit poster's website

Re: Writing to two different I/O at the same time
PostPosted: Fri Oct 09, 2015 7:26 pm     Reply with quote

towpew wrote:
Hello
I have a C code question.
I have seen in the manual that it is possible to write to different bits at the same time using a struct like this.

Code:

//PIC 16F1937

#byte LATA = 0x10C
#bit Abit1 = LATA.1
#bit Abit4 = LATA.4

struct {
          int1 Abit1;
          int1 Abit4;
         } MyAbits;

#byte MyAbits = 0x10C



From a 'C' perspective, you are writing to the structure with a single write but from an assembler perspective, it is not what is happening. It is individually setting and clearing bits as required. If your application specifically required both bits change state simultaneously then it get much more complex on a processor that does not have a separate LAT register for the I/O port in question.
_________________
Regards, Andrew

http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!!
temtronic



Joined: 01 Jul 2010
Posts: 9150
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sat Oct 10, 2015 5:32 am     Reply with quote

There is a 'thread' somewhere here about writing to two ports so try using the 'search' feature. Also the 'flex_LCD' driver does this as well, it's located in the 'code library'.
Be aware that NO method will allow the PIC to write 'simultaneously' to 2 or more I/O ports. Can't be done due to the PIC having 8 bit 'wide' ports.Now depending on processor speed and what the data controls, your program controls it probably will be fine.
In the PC World it's called 'multitasking' where doing 2 or more things very fast makes it 'appear' that both actions occour at the same time( AKA simultaneously).

Jay
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Oct 10, 2015 2:02 pm     Reply with quote

This is the thread.
http://www.ccsinfo.com/forum/viewtopic.php?t=18949
That's a very old thread. Nowadays, you would use getenv() to get
the port register addresses, instead of entering them as numbers.
Example:
Code:
// Define the bit addresses of the spare pins.
// The addresses shown below are for the 16F877.
#byte PortA = getenv("SFR:PortA")
#byte PortB = getenv("SFR:PortB")
#byte PortC = getenv("SFR:PortC")

#bit RA0 = PortA.0
#bit RA1 = PortA.1
#bit RA4 = PortA.4
#bit RB2 = PortB.2
#bit RB5 = PortB.5
#bit RB7 = PortB.7
#bit RC0 = PortC.0
#bit RC5 = PortC.5
Ttelmah



Joined: 11 Mar 2010
Posts: 19297

View user's profile Send private message

PostPosted: Sun Oct 11, 2015 2:04 am     Reply with quote

One thing that has been ignored through this thread, is the simple question of 'why'?.

Now the systems being pointed to, allow you to 'hide' the separate nature of the pins, to some extent. Great, if this is just for simplicity. However if this is for a 'real' electronic reason (needing signals to switch together), then a deep breath has to be taken, and a big step back made. In such cases, hiding the separate nature can be very dangerous. If (for instance), these are pins controlling devices being switched on/off, then the sequencing may well need to be different, when going from one state to another. So if (perhaps) one low of pins turns on a particular driver, and others turn on another driver the other way, the sequences have to become 'turn off the drivers that are on, and only then turn on the other drivers'. If the PIN identities are hidden in a wrapper, the sequencing becomes that of the wrapper function, implying that the order may well be wrong in one direction, if not the other...
In such systems it becomes vital to control the timings yourself, and work out what order things have to happen to be safe, and ensure the code handles the changes in this sequence in both directions. Get the sequencing wrong when dealing with anything involving power, and nasty things can start to happen.

So big question. Why?.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
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


Powered by phpBB © 2001, 2005 phpBB Group