|
|
View previous topic :: View next topic |
Author |
Message |
KU5D
Joined: 10 Feb 2008 Posts: 46 Location: Asheville, North Carolina
|
Port E problem? 18F4525 |
Posted: Mon May 12, 2008 9:11 am |
|
|
Guys,
This seems strange to me, or I'm missing something simple. I have a device that among many other things uses onewire to read an iButton. Initially, I was using pin_C4 as the one-wire pin, and the thing worked flawlessly. However, due to other hardware considerations I moved from C4 to E2, which is of course one of the analog input pins. I'm wondering if I have to specifically set the direction for this pin when writing to and reading from the one wire. On the C port, I didn't have to do this.
Now, along with the onewire, I have 7 analog inputs that I need to read, so here's how I set up my analog inputs:
Code: |
#define ADC_CLOCK_INTERNAL 0X07
#define AN0_TO_AN6 0x08
//initialize adc input channel and set up ports
inputChannel = 0;
setup_adc_ports (AN0_TO_AN6); //set AN0 thru AN6 analog
setup_adc (ADC_CLOCK_INTERNAL);
|
This should exclude E2 and I think it should be now a digital input.
This is what the .lst file shows:
Code: |
.................... setup_adc_ports (AN0_TO_AN6); //set AN0 thru AN6 analog
7D04: MOVF FC1,W
7D06: ANDLW C0
7D08: IORLW 08
7D0A: MOVWF FC1
|
I'm wondering two things. 1. is the compiler setting this up correctly, or 2. do I have to explicitly set the data direction on port E when using it as an input or output?
Ah yes, my compiler version is 3.249. _________________ Confidence is the feeling you have right before you fully understand the situation... |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon May 12, 2008 10:21 am |
|
|
Are you using standard i/o mode (the default mode), and are you
using CCS pin i/o functions ?
If so, the compiler should set the correct TRIS automatically. |
|
|
KU5D
Joined: 10 Feb 2008 Posts: 46 Location: Asheville, North Carolina
|
|
Posted: Mon May 12, 2008 10:29 am |
|
|
Can I do that and still use analog ins 0 thru 6, and have what was AN7 (pin E2) as a digital I/O for the onewire? Right now, I am not doing #USE STANDARD IO. I am using standard CCS pin funtions such as output_bit... _________________ Confidence is the feeling you have right before you fully understand the situation... |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon May 12, 2008 10:53 am |
|
|
It's the default mode. You don't have to specify it. It's automatically
in effect unless you override it by using #use fast_io or #use fixed_io.
In the test program shown below, the following code is generated for
the output_bit() function. It does set the TRIS. So I think your problem
is somewhere else. I don't think it's the TRIS, unless you're doing
something else besides using output_bit().
Code: |
... output_bit(PIN_E2, 1);
0014: BSF LATE.2
0016: BCF TRISE.2
|
Code: |
#include <18F452.h>
#fuses XT,NOWDT,PUT,BROWNOUT,NOLVP
#use delay(clock=4000000)
//=================================
void main()
{
output_bit(PIN_E2, 1);
while(1);
} |
|
|
|
KU5D
Joined: 10 Feb 2008 Posts: 46 Location: Asheville, North Carolina
|
|
Posted: Mon May 12, 2008 11:14 am |
|
|
OK, the output seems to be working, at least it doesn't look any different on a 'scope than it does on Port C. However, this is a onewire device that I read, so I suspect that once I send the command from Pin_E2, I need to turn it around and make it an input. Can I do this by pin, or do I have to set the direction for the entire port? I still need to keep my analog inputs AN0 thru AN6.
I didn't include #fuses in the previous post. Here they are (don't think this is the issue):
Code: |
#device ADC=8
#device HIGH_INTS=TRUE
#fuses HS,NOWDT,NOPROTECT,NOLVP,PUT
#use delay(clock=10000000)
|
_________________ Confidence is the feeling you have right before you fully understand the situation... |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon May 12, 2008 11:17 am |
|
|
The CCS pin i/o function that changes a pin to an input is the
output_float() function. Here is the code from the test program
shown below:
Code: |
... output_bit(PIN_E2, 1);
0014: BSF LATE.2
0016: BCF TRISE.2
...
... output_float(PIN_E2);
0018: BSF TRISE.2
|
Code: | #include <18F452.h>
#fuses XT,NOWDT,PUT,BROWNOUT,NOLVP
#use delay(clock=4000000)
//=================================
void main()
{
output_bit(PIN_E2, 1);
output_float(PIN_E2);
while(1);
} |
|
|
|
KU5D
Joined: 10 Feb 2008 Posts: 46 Location: Asheville, North Carolina
|
|
Posted: Mon May 12, 2008 11:24 am |
|
|
OK, here's my onewire code. There's a function at the bottom that is called from main() when a timer runs out and sets the getOneWire flag. It's called "DoOnewire". As I said earlier, this works fine if I define the OneWire as pin C4. It breaks if I use E2. The array called AnalogIns[] may be a bit misleading...it isn't the ADC, it's a buffer used to ship this stuff over a radio link.
Code: |
void onewire_reset() // OK if just using a single permanently connected device
{
output_low (OneWire); // pull 1-wire low for reset pulse
delay_us(500);
output_float(OneWire); // float 1-wire high
delay_us(500);
output_float(OneWire);
}
//================== onewire_write() =========================
void onewire_write(int data)
{
int count;
for (count=0; count<8; ++count)
{
output_low(OneWire);
delay_us( 2 ); // pull 1-wire low to initiate write time-slot.
output_bit(OneWire, shift_right(&data,1,0)); // set output bit on 1-wire
delay_us(60);
output_float(OneWire); // set 1-wire high again,
delay_us( 2 ); // for more than 1us minimum.
}
}
//====================== read1wire() ===================================
//This function reads the 8 -bit data via the 1-wire sensor. */
//
//Parameters:
//Returns: 8-bit (1-byte) data from sensor
//======================================================================
int8 onewire_read()
{
int8 count, data;
for (count=0; count<8; ++count)
{
output_low(OneWire);
delay_us( 2 ); // pull 1-wire low to initiate read time-slot.
output_float(OneWire); // now let 1-wire float high,
delay_us( 8 ); // let device state stabilise,
shift_right(&data,1,input(OneWire)); // and load result.
delay_us(120);
}
return( data );
}
//==============================================================================
// DoOneWire()
DoOneWire()
{
int8 oneWireCnt;
int8 busy = 0;
disable_interrupts(GLOBAL);
onewire_reset();
onewire_write(0x33);
while (busy == 0)
busy = onewire_read();
onewire_reset();
onewire_write(0x33);
for(oneWireCnt = 8; oneWireCnt >0; oneWireCnt--)
{
AnalogIns[oneWireCnt + 7] = onewire_read();
}
getOneWire = FALSE; //clear the get me flag
if(inDebugMode)
{
fprintf(PC,"iButton: %X %X %X %X %X %X %X %X \r",AnalogIns[8],AnalogIns[9],AnalogIns[10],AnalogIns[11],
AnalogIns[12],AnalogIns[13],AnalogIns[14],AnalogIns[15]);
}
enable_interrupts(GLOBAL);
}
|
_________________ Confidence is the feeling you have right before you fully understand the situation... |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon May 12, 2008 11:46 am |
|
|
Do you know for sure that Pin E2 is free ? What if it's connected to a
pull-down resistor or some other circuit ?
I suggest trying a different pin than E2. See if another pin will work. |
|
|
KU5D
Joined: 10 Feb 2008 Posts: 46 Location: Asheville, North Carolina
|
|
Posted: Mon May 12, 2008 12:23 pm |
|
|
PCM,
Try this on for infuriating:
2 identical iButton sockets; 1 wired for the old board using C4 and the other wired for the new board using E2. All made up nice and neat with heatshrink, and good connectors.
Problem: ground wire separated inside of heatshrink on socket for new board. I've chased this for hours and wasted not only my time, but yours as well.
My apologies,
many thanx _________________ Confidence is the feeling you have right before you fully understand the situation... |
|
|
|
|
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
|