0

I'm using PIC 89v51 microcontroller, and i have to generate voltages from port pins of a particular port [i have used R2R ladder ckt in hardware] but the thing is i need to generate it from the microcontroller only i.e. i have taken four port pins say P2.0=V1, P2.1=V2, P2.2=V3, P2.0=V4;

So if V1=0; V2=0; V3=0; V4=1 it should display the voltage say 1v and similarly for all binary values from 0010 to 1111 it should display 2v to 15v,kindly help me in resolving this..

MEANWHILE I'll make clear that I have constructed R2R ladder circuit, but while checking the output i'm not using this circuit... I'm testing from microcontroller only..

Anindo Ghosh
  • 50,446
  • 8
  • 105
  • 201
angiey
  • 1
  • 1
  • 1
    Please share a link to the PIC 89v51 you mention, unable to find any such part. Also, if you are looking for help figuring out why your code doesn't work, putting the code in the question helps. If you are looking for someone to write the code for you, "YourHomeworkDoneForYou.com" is not this site. – Anindo Ghosh Apr 19 '13 at 07:05
  • I wrote it wrong it's p89v51 from nxp i could not write in detail...and i want to tell that i have already tried using DAC_table as below but it didn't worked :unsigned char DAC_table[16] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F}; – angiey Apr 19 '13 at 07:13
  • 3
    That table is rather pointless since the values are the same as the index... – Chris Stratton Apr 19 '13 at 20:20

3 Answers3

2

I'll leave this as pseudo-code as I don't know the compiler your're using but one approach is something like the following. It does a bitwise AND on each bit of the value and shifts it into the least significant bit, some compilers won't require that but there's no harm including it if unsure.

void set_output_v(uint8_t v)
{
    P2.0 = v & 0x01;
    P2.1 = (v & 0x02) >> 1;
    P2.2 = (v & 0x04) >> 2;
    P2.3 = (v & 0x08) >> 3;
}

On many compilers there will also be a way to set a whole port at a time. Try the above code first but you may also be able to do something like the following to keep the high nybble of P2 and shift the value v into the lower nybble.

P2 = (P2 & 0xF0) | v;
PeterJ
  • 17,201
  • 37
  • 57
  • 91
  • Does OP want to read the pins or Set the Pins?? I am confused with your answer! I was assuming he wants to read and Pins – Swanand Apr 19 '13 at 07:07
  • @Swanand, the question mentions an R2R network, setting an output voltage and checking outputs at the moment so I'm assuming it's only a write required. It is a bit unclear though. – PeterJ Apr 19 '13 at 07:10
  • @Peterj Thank you sir..as i'm using 'C' [keil]..i'll try to check if it works.. – angiey Apr 19 '13 at 07:26
  • @camil yes!!i had used DAC table for it.. unsigned char DAC_table[16] = { 0x00, 0x01, 0x02, 0x03,........ 0x0F};i had done mistake during declaration and initialisation of table so it went wrong.. – angiey Apr 19 '13 at 07:34
0

As already suggested, you could read the whole port value to variable

short PIN1 = 0, PIN2 = 1, PIN3 = 4, PIN4 = 7;
short value = (P2 & (1<<PIN1))>>PIN1;
value += (P2 & (1<<PIN2))>>(PIN2-1);
value += (P2 & (1<<PIN3))>>(PIN3-2);
value += (P2 & (1<<PIN4))>>(PIN4-3);

That will give you the value of 0-15 for any variant of input pins. Not only p2.0,p2.1, p2.2, p2.3.

EDIT: Lets say that P2 is input port with 8 input bits. You have connected inputs on pin0, pin1, pin4 and pin7. (common situation when working with uC, as most of the pins have multiple functions and sometimes you end up with free pins that are not 0,1,2,3 but rather scattered...) PIN1-4 represents the position bits in input port. This is how you could read those 4 bits and put them in 4bit value output.

Gossamer
  • 663
  • 1
  • 6
  • 14
  • See @PeterJ's answer for how to do this. This is just complete nonsense. If PIN1-4 are constants they have the wrong values, and if they are supposed to be inputs they can only be 0 or 1. What the code is supposed to be doing is anybody's guess. – user207421 Apr 19 '13 at 23:38
  • @EJP did you even try the code? Pin1-4 are positions of input pins in P2 byte. Reading from P2 input byte is done by shifting the inputs in 4 bit value. – Gossamer Apr 20 '13 at 06:53
  • I don't have the hardware. Do you? I don't know what 'positions of input pins' means. I would have expected PIN{1,2,3,4}={1,2,4,8} as in @PeterJ's answer, or else 8 PINs corresponding to the bit positions 0..7. Your code doesn't make any sense to me either. The first two shifts don't do anything at all; neither does the fourth; and I don't understand the significance of e.g. PIN4-3. Good code should be self-explanatory. As yours isn't, perhaps you could explain it? – user207421 Apr 20 '13 at 08:32
  • 1
    @EJP actually the first two shifts are not without effect, except in the special case where a pin has a bit position of 0. This seems to be trying to handle a case of non-sequential port pin assignments, and to do so generically, such that only the assignments line needs to be changed if the signals are moved. – Chris Stratton Apr 20 '13 at 18:35
  • @ChrisStratton PIN0 has a value of zero in this code. Ergo the first two shifts do nothing. Ditto the fourth, as PIN1 has a value of 1 in this code. The expressions (PIN2-1), (PIN3-2), (PIN4-3) are only correct for the given values of PIN{1,2,3}, and similarly the absence of a subtraction in the right-shift by PIN0 is only correct if PIN0 is zero. Ergo this code is not general purpose at all: it relies on those values. The code appears to assume that the data is in bits 0,1,4,7 and maps it to bits 0,1,2,3. I don't see anything in the question that justifies that assumption. – user207421 Apr 21 '13 at 01:54
  • You are mistaken as you do not yet understand what the code is doing. Try it with different values for the pins. The answer is an attempt to be somewhat generic, though there are limitations to it. – Chris Stratton Apr 21 '13 at 14:04
  • @ChrisStratton I understand the code. I don't understand what it is trying to do. Take the last line. It shifts PIN4 down three bits. What's generic about that? And when is the author, or you, going to explain it to us? – user207421 Apr 21 '13 at 21:49
  • No, that is not what the last line does. I suggest you actually try running the code and do so for different pin assignments in addition to the example set given. It's not perfectly universal as there is a major limitation, but it's a lot more universal than you think it is. – Chris Stratton Apr 22 '13 at 04:37
  • Wow, we are still on this.. @EJP, try this code in closed environment and try to set value of P2 to any combination of bits and you will see that you will get output that reflects the state of bits on pins (PIN1-4) in 4 bit output. (thats what shifts do, they maps P2.0 to Pin1, P2.1 to pin2, P2.4 to Pin3 and P2.7 to Pin4, resulting in 4 bit output). _ChrisStratton, what do you see as limitation, except pins should fulfil Pin1<Pin2<Pin3<Pin4 ? – Gossamer Apr 22 '13 at 06:04
0

You're never going to get 15V using just a microcontroller and an R2R ladder. The highest voltage you can get is whatever your logic level voltage of the uC is. Typically either 5V, or 3.3V

jwygralak67
  • 709
  • 4
  • 9