|
|
View previous topic :: View next topic |
Author |
Message |
JimB
Joined: 25 Aug 2005 Posts: 65 Location: Huntington Beach, CA
|
Serial data out....what am I doing incorrect? |
Posted: Wed Apr 12, 2006 1:47 am |
|
|
I am trying shift data out using the shift_left() command. I get the clock and chip select to function, but no data. When I compile and run the IDE, I see a burst of correct data before actually running the program, but not after starting the program. I have tried this on the B and D ports.
I also found that I needed pullup resistors on these lines. I thought that the tris_x statement enabled an active output?
Also why is (<=) required in the if() statement rather than just an (=). When replaced with an = it runs continuously, not in 16 cycle bursts. Seems like it should stop after 16 clock cycles then repeat because of the while(1).
[/code]
#include <16F877A.h>
#device ICD=TRUE
#fuses HS,NOWDT,NOPROTECT
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
//#use rs232(debugger)
#define maxsel PIN_C1 // Low activates the D/A
#define maxclk PIN_C2 // D/A clock
#define maxdat PIN_C3 // D/A data
main()
{
long offset = 422; // 01A6 hex
int j=0;
int i=0;
set_tris_C(0x0F); // sets C0,C1,C2,C3 as outputs
output_high(maxsel); // D/A chip select disabled
while(1)
{
output_low(maxsel); // D/A chip select enabled
for (i=0; i<=15; i++) // write the reference word
{
output_bit(maxdat,shift_left(&offset,2,0));
output_high(maxclk);
output_low(maxclk);
}
output_high(maxsel); // write the value shifted out
j++;
printf(" run number = %u\n\r",j);
}
} |
|
|
Ttelmah Guest
|
|
Posted: Wed Apr 12, 2006 2:39 am |
|
|
Second question first.
'=', is the assignment operator, _not_ the 'test'. In C, the code to test equality, is '=='.
Now the first question.
You are using a shift. When you shift a value (as opposed to rotating it), '0' bits are shifted in, as the value is shifted out the bottom. Hence the loop works _once_ (shifting the value out), and then there is nothing left in the variable to output. You need to 'reload' the variable in the loop.
Your TRIS is wrong. '0x0F', sets C0...C3 as _inputs_.
However the compiler will be overriding this, and setting the pins as outputs when they are used (otherwise you would see nothing). You are probably seeing the 'RMW' problem of the PIC. With the PIC, when you output a bit, the whole port is physically 'read', the requested bit is changed, and then the value is written back (read, modify, write). Now if (for instance), you do:
output_low(PIN_C1);
output_high(PIN_C1);
output_low(PIN_C2);
There can be a problem if there is significant capacitance/load on the pins. The second operation, sets the output drive 'high' for pin C1, in the last clock cycle of the instruction. The next line, reads the port, as the first clock cycles of the next instruction. There is only one twenty millionth of a second, for the signal on C1, to have actually got 'high', before it is being read. If it has not reached the high state, then it is read as a 'low', and writen back as a low, and the output is 'lost'.
There are two ways round this:
First, maintain a 'shadow' copy of what you want to output, set/clear the corresponding bits in this, and output the whole byte.
Second, allow enough time between operations, for the pins to settle (delay_us(1) for example).
Best Wishes |
|
|
JimB
Joined: 25 Aug 2005 Posts: 65 Location: Huntington Beach, CA
|
About the pullups? |
Posted: Wed Apr 12, 2006 10:28 am |
|
|
Thanks for the clear answer. The tris statement was a desparation move since nothing else seemed to work. I usually don't use it because all of the literature advises to try to do without it.
However, I still am not clear on why pullup resistors were needed, even for the slower signals? I have also run this with a 4 MHz clock with the same results. |
|
|
|
|
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
|