Click here to Skip to main content
15,881,424 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i have
PortB(pins RB0 through RB7)of the pic16 defined as an input with a dip switch connected to it
PortD(pins RD0 through RD7)as output each connected to a LED,also each pin of PORT B corresponds to one from PORT D

how can i configure that this port is the input and output in the first place and it's supposed to be configured in the pic code? or in the c# application? i think it 's supposed to be in the pic program. but i'm just wondering

when reading from the microcontroller i want to read the status of the 8 output LEDS once or i can collect them, how is that possible?

and i want to be able to control the status of these leds using my c# program but i have an issue on how to receive their status and set it later .

i tried reading but it was on port A not on the output(port d)
i'm using proteus to stimulate the circuit and a comport virtual emulator and to write the code of the pic mikroc (i'm new to it most of the codes i got from the internet and i kinda understood)
i just want to know how to read the status of port D like (101011) pr something like that and how to set each pin of portD if that's possible

i'm still new to all of this i'm just trying my best ,
excuse my lack of knowledge ,afterall i'm still learning .
please, and Thanks !:)


my c# functions code to read and write
SerialPort port ;
void Pread()
{
    port = new SerialPort("COM2", 9600, Parity.None, 8, StopBits.One);
   // port.ReadTimeout = 500;
    port.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
    port.Open();


}

private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{
    SerialPort port= (SerialPort)sender;
    string w = port.ReadByte().ToString();
    if (w != String.Empty)
    {
        Invoke(new Action(() => richTextBox1.AppendText(w))); //it will read and append the reading to a richtextbox
    }
}

void Pwrite(string num)
{
    port.WriteTimeout = 500;
    port.Open();
    port.WriteLine(num.ToString());
    port.Close();
}

private void button1_Click(object sender, EventArgs e)
{
    Pread();
}
Posted
Comments
gggustafson 24-Apr-14 13:42pm    
Which PIC16 microchip are you using?
Duaa7 24-Apr-14 13:46pm    
pic16f877a sorry for not mentioning it!
gggustafson 24-Apr-14 16:06pm    
Paraphrasing the documentation Microchip PIC16F87XA Data Sheet (http://ww1.microchip.com/downloads/en/DeviceDoc/39582C.pdf), PORTB is an 8-bit wide bidirectional port. Data direction is defined by data direction register TRISB. Setting a TRISB bit (= 1) will make the corresponding PORTB pin an input. Clearing a TRISB bit (= 0) will make the corresponding PORTB pin an output pin.

Four of the PORTB pins, RB7:RB4, have an interrupton-change feature. Only pins configured as inputs can cause this interrupt to occur (i.e., any RB7:RB4 pin configured as an output is excluded from the interrupton-change comparison). The input pins (of RB7:RB4) are compared with the old value latched on the last read of PORTB. The “mismatch” outputs of RB7:RB4 are OR’ed together to generate the RB port change interrupt with flag bit RBIF (INTCON<0>).

The interrupt-on-change feature is recommended for wake-up on key depression operation and operations where PORTB is only used for the interrupt-on-change feature. Polling of PORTB is not recommended while using the interrupt-on-change feature.

So, from what I read, you can be notified of a change in 4 of the 8 DIP switches using interrupts. But if you need to detect the changes in all 8, you must use polling. And if you use polling, you may not interrupt-on-change feature. And you appear to be right: the setup must be made using PIC16 code.

PORTD is an 8-bit port. Each pin is individually configurable as an input or output.

Here I am a little confused by your question. I was assuming that the DIP switches on PORTB would control the LED segments on PORTD. Maybe you could supply a little more information on how the DIP switches and LED segments are wired and how the PC interacts with the PIC16 (through serial port?).
Duaa7 25-Apr-14 17:08pm    
//drive.google.com/file/d/0B6NPKZsgNLOCVHE1RWF4dzNOWnM/edit?usp=sharing
that's the circuit i built using proteus I want my c # program to be able to change the status of a pin in portD (but i'm wondering portD is receiving the 1 bit from portB or from the serial port now) so if its receiving it from portb its like i'm changing the value of the pin on port b to be able to change the value on portD,the pc will be interacting with the pic with through the c# code it's not totally done i'm just trying to receive the values of port D and send the values from the pc to portD now and when this is all good i'll try to improve the way it works by adding a thread that will continuously be reading the values



GUI will be such as showing the full map of the floor with red/green symbols in each room representing light and Air conditioning ( AC: heater/cooler) . Green means ON that and RED means OFF (one symbol for lights and one symbol for AC in each room).
The status of these symbols will be later on acquired from the microcontroller ( real field) which will keep sending this info to the computer through com port serial interface

On another hand the computer software will be :
1) Calculating the total energy in all the floor and take action if the total Current is above a certain threshold.
(The action would be to switch off AC and/or Light in one on more room.)

2) Developing a time schedule that will also dictate time range of switching On and OFF the AC or Lights.

After processing the first 2 above conditions, if any change in the status is required The computer software will change the status and send the command to the microcontroller.

i still haven't done any of them and i'm still in the first step
https://drive.google.com/file/d/0B6NPKZsgNLOCNVRaaHY3QmNpZVU/edit?usp=sharing
https://drive.google.com/file/d/0B6NPKZsgNLOCRjRfMzF4bE4wSUU/edit?usp=sharing
that's the c# program and the points will be shown according to the status of the portd pins
i hope everything is clear
gggustafson 25-Apr-14 17:44pm    
I can access the drawing in the first link. However the last two links are protected. I've applied for permission to look at them. We will have to wait.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900