View previous topic :: View next topic |
Author |
Message |
towpew
Joined: 25 Sep 2015 Posts: 24 Location: sweden
|
Writing to two different I/O at the same time |
Posted: Thu Oct 08, 2015 9:41 am |
|
|
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: 19513
|
|
Posted: Thu Oct 08, 2015 10:02 am |
|
|
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
|
Writing to two different I/O at the same time |
Posted: Thu Oct 08, 2015 11:28 am |
|
|
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: 19513
|
|
Posted: Fri Oct 09, 2015 12:09 am |
|
|
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
|
|
Posted: Fri Oct 09, 2015 7:07 am |
|
|
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: 1634 Location: Perth, Australia
|
Re: Writing to two different I/O at the same time |
Posted: Fri Oct 09, 2015 7:26 pm |
|
|
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: 9226 Location: Greensville,Ontario
|
|
Posted: Sat Oct 10, 2015 5:32 am |
|
|
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
|
|
Posted: Sat Oct 10, 2015 2:02 pm |
|
|
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: 19513
|
|
Posted: Sun Oct 11, 2015 2:04 am |
|
|
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?. |
|
|
|