Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Modify only Selected Pins!

Status
Not open for further replies.

PICMICRO

New Member
Hi there,
I am using Hi-Tech C compiler
Code:
struct myvar{
unsigned data: 6;
};
void main(){
.............
myvar driver;
driver.data = 0xFF; // equivalent to driver.data = 0b111111, because 2 MSBs are ignored
.............

When I write datas to the driver.data, I want it to appear on the first 6 pins of PORTB without altereing the rest 2 pins. How can I do that?
 
You need to OR the value of the port with the value you want to write (0x00111111). Check your compiler manual for the OR binary operator...I don't use Hi-Tech.
 
Last edited:
That's not quite right, if the new data contains both 0 and 1 bits you need to clear the 6 bits to zeros first, then load them with the 6 bit data;

PORTB = (PORTB & 0b11000000); // clear lower 6 bits of PORTB
PORTB = (PORTB | driver.data); // copy bits from driver.data to PORTB

That way makes the PORTB pins zero before they are set, which you may not want.

This way only writes to PORTB once;
driver.data = (driver.data & 0b00111111); // safe! forces top 2 bits to zero
if(PORTB.F7) driver.data |= 0b10000000; // set top bit to match PORTB
if(PORTB.F6) driver.data |= 0b01000000; // set next bit to match PORTB
PORTB = driver.data; // one single write to PORTB
 
Last edited:
Hi RB,
Thanks for reply.
The second method was what I wanted.
But doesn't the process appear too lengthy for the task we want to do which is just -Updatae PORTB,B0:B6 with a data.

Anyways, that will do my job.
BTW, I had made driver.data is just 6 bit wide (hoping this could allow me to update only PORTB,B0:B6). So, I should make it 8-bits to go with your solution. Even better, I don't need even a structure, just a simple
Code:
char driver_data
will work.
 
It is a bit easier to see like this.

Code:
char keepBits, newBits;

// use & to select the bits we need from PORTB and myData
keepBits = PORTB & 0xC0;    // 0b11000000 is 0xC0
newBits = myData & 0x3F;    // 0b00111111 is 0x3F

// combine keepBits and newBits and assign to PORTB
PORTB = keepBits | newBits;
Now that you understand how it works you can write it in one line
Code:
PORTB = (PORTB & 0xC0) | (myData & 0x3F);
Your method of using a 6 bit variable may work too. It does in C18 but I do not know how to write the structure in Hi Tech.
 
Last edited:
That's a nice refinement down to one line 3v0. :)

Personally I prefer to keep PIC C code as vertical as possible, for a few benefits;
1. allows commenting of each small stage (good for beginners that might read my code)
2. comments on each stage show in the asm output generated by the compiler
3. so its easier to convert to asm (or basic etc)
4. keeping code vertical gives greater control over compiling output and RAM usage (can make faster code)
5. additions and changes to any stage are less messy, or some stages can be ripped out and made into a function etc

I know we have very different C styles and I respect your C expertise, but I still think for C code for tiny microcontrollers with limited speed and RAM that a more vertical style pays off quite well. :)
 
Thanks RB.

My code tends to be more vertical then most. I posted both versions to satisfy both camps if you get my meaning. People tend to look at me as simplistic when I post vertical code. Can't win ;)

I liked you list of 4 reasons to go vertical.

I think this specific line of code once debugged will give equal results and performance with the longer version. If I were to use the long version I would set it up as a inline'ed function esp if it was used more then once.

Many of our micro controllers are not so little anymore. I was looking at the FEZ Panda Ian was talking about. 300K prior to any application code..

I am seeing increasing fragmentation in the uC world. Not just by vendor or language but more so by processor power. It is getting to where the 8 bit and 32 bit people are in different worlds. Perhaps the EE an CS types too. I try to live in both of these.
 
3v0! I agree it is getting difficult... I have four projects on the go with the Panda and C# is a brilliant tool...but (you knew there was a but ) its very similar to windows programing... The micro.net framework is hidden and not accessible, which really naffs me off especially trying to get a serial driver going...

Anyway I'm not going to hijack this thread.

Ian
 
Last edited:
Do you fancy giving .netmf a go then? I like C# its a good mixture of VB6 and C++ (its actually Java, shoosh Sun systems may hear). I have a Cobra linked up to an SVGA full touch screen, .net has WPF (windows) you can have a full system up and running in minutes. There is a guy called Thomas (Skewworks) developed "Pyxis" a complete operating system for it.

The biggest drawback is the netmf is managed code (interpreted) so it doesn't run in real time..However the FEZ range have a thing called RLP, runtime loadable procedures (DLL's) , so applications can run in managed code but have assembled functions that run directly,making them very fast indeed.

All the development software is free and C# .net comes with an emulator, so you don't need to buy any hardware until you're ready..

Give it a go.

Ian

P.S. I'm not selling this, its my opinion.. (before Eric and Nigel tell me off)
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top