View previous topic :: View next topic |
Author |
Message |
Youngpup
Joined: 01 Oct 2007 Posts: 11
|
inputing bits on port D of a PIC18F4680 |
Posted: Mon Oct 15, 2007 8:02 am |
|
|
I hope someone can help me. I am trying to read in a 7 bit value through port D. I can only get bits 6,5,4 to read correctly but bits 3,2,1,0 are always low. I can't seem to see anything wrong with the code. The only thing I haven't tried is replacing the PIC(Last resort). Here is the code.
#include "C:\Documents and Settings\tfaulkner.OTTAWA\My Documents\Motor Controller\Software\Motor Controller V1.0.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <can-18xxx8.c>
static int step[8]; // this is an array of steps
static unsigned int8 CAN_BUS_ID; // this is the address for this antenna
static int8 CAN_ALL_ID;// This is for all calibration
static int8 MASTER_ID = 0x00;// this is the ID for the FPGA Card
static int8 buffer[8];// this data storage
Steps(); //
Calibration();
TX_CANBUS();
RX_CANBUS();
void main()
{
RX_CANBUS();
}
RX_CANBUS()
{
struct rx_stat rxstat;
int32 rx_id;
int8 rx_len;
boolean RX;
can_init();
CAN_ALL_ID = 0x7E;
set_tris_d(0xFF);
CAN_BUS_ID = input_d();
rx_id = 0;
step[0] = 0x01;
step[1] = 0x03;
step[2] = 0x02;
step[3] = 0x06;
step[4] = 0x04;
step[5] = 0x0c;
step[6] = 0x08;
step[7] = 0x09;
while(RX = TRUE)
{
if(can_kbhit()) // Message available ?
{
// If so, get the message.
if(can_getd(rx_id, buffer, rx_len, rxstat))
{
if(rx_id == CAN_BUS_ID) // Is it for this board ?
{
Steps();
TX_CANBUS();
}
if(rx_id == CAN_ALL_ID) // Is it for this board ?
{
Calibration();
TX_CANBUS();
}
}
}
}
}
Calibration()
{
int i=0;
while(LIMIT_SW_REAR == 1)
{
output_a(step[7]);//1001
delay_us(1900);
output_a(step[5]);//1100
delay_us(1900);
output_a(step[3]);//0110
delay_us(1900);
output_a(step[1]);//0011
delay_us(1900);
}
while((i<5000) && (LIMIT_SW_FRONT == 1))
{
output_a(step[1]);//1001
delay_us(2000);
output_a(step[3]);//1100
delay_us(2000);
output_a(step[5]);//0110
delay_us(2000);
output_a(step[7]);//0011
delay_us(2000);
i++;
}
}
Steps()
{
int direction;
int num_steps[7];
int i, x;
x=0;
direction = buffer[0];
/* for(i=1; i<8; i++)
{
num_steps[x]=buffer[i];
x++;
}
*/
num_steps[1] = buffer[1];
i=0;
if(direction == 1)
{
while((i<num_steps[1]))// && (LIMIT_SW_FRONT == 1))
{
output_a(step[1]);//1001
delay_us(2000);
output_a(step[3]);//1100
delay_us(2000);
output_a(step[5]);//0110
delay_us(2000);
output_a(step[7]);//0011
delay_us(2000);
i++;
}
}
else
{
while((i<num_steps[1]))// && (LIMIT_SW_REAR == 1))
{
output_a(step[7]);//1001
delay_us(2000);
output_a(step[5]);//1100
delay_us(2000);
output_a(step[3]);//0110
delay_us(2000);
output_a(step[1]);//0011
delay_us(2000);
i++;
}
}
output_a(0b0000);
// for(i=0; i<8; i++)
//{
// buffer[i]=0;
//}
}
TX_CANBUS()
{
int notify;
notify = CAN_BUS_ID;
rotate_left(¬ify, 1);
can_putd(MASTER_ID, notify, 1, 1, 1, 0);
}
Thanks. |
|
![](templates/subSilver/images/spacer.gif) |
dyeatman
Joined: 06 Sep 2003 Posts: 1924 Location: Norman, OK
|
|
Posted: Mon Oct 15, 2007 8:15 am |
|
|
One thought...
Look at page 139 of the data sheet (Port D) and see what else is enabled on those pins that might OVERRIDE the digital inputs. |
|
![](templates/subSilver/images/spacer.gif) |
Youngpup
Joined: 01 Oct 2007 Posts: 11
|
|
Posted: Mon Oct 15, 2007 9:30 am |
|
|
I looked on Page 139. I have commented out PSP part of the 18F4680.h and the Comparator part as well. This still did not help me. Any other suggestions? |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Oct 15, 2007 10:00 am |
|
|
1. Post your compiler version.
2. Your code has some other bugs.
Quote: | Calibration()
{
int i = 0;
while(( i < 5000 ) && (LIMIT_SW_FRONT == 1))
|
In CCS, an 'int' is an unsigned 8-bit integer. It can only hold 0 to 255.
The code above will not work as expected.
Quote: | void main()
{
RX_CANBUS();
while(1);
} |
I don't know if the RX_CANBUS() function ever returns, but if it does,
you need to place a continuous loop at the end of main(). This is to
prevent the PIC from executing the hidden sleep instruction that CCS
places there, which will put the PIC into Sleep mode. |
|
![](templates/subSilver/images/spacer.gif) |
Youngpup
Joined: 01 Oct 2007 Posts: 11
|
|
Posted: Mon Oct 15, 2007 10:25 am |
|
|
Thanks for those catches. I had not tested that part of the code yet. I have been stuck on a few other issues like the one at hand. My compiler version is 3.213. |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Oct 15, 2007 10:43 am |
|
|
I don't have that version so I can't test it. I think it's likely that the
comparator is turned on. It could be that your version of the compiler
doesn't turn them off. Try adding the following code, which will turn
them off by writing directly to the CMCON register.
Code: | #byte CMCON = 0xFB4
void main()
{
CMCON = 0x07; // Turn off the comparators
while(1);
} |
|
|
![](templates/subSilver/images/spacer.gif) |
Youngpup
Joined: 01 Oct 2007 Posts: 11
|
|
Posted: Mon Oct 15, 2007 10:49 am |
|
|
Thank you, Thank you soooo much. It worked. You are the best. |
|
![](templates/subSilver/images/spacer.gif) |
frequentguest Guest
|
|
Posted: Mon Oct 15, 2007 3:08 pm |
|
|
Quote: | I have commented out PSP part of the 18F4680.h and the Comparator part as well. |
That was part of the problem. Commenting out part of the device header file probably won't help too much. |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah Guest
|
|
Posted: Mon Oct 15, 2007 3:35 pm |
|
|
Youngpup wrote: | I looked on Page 139. I have commented out PSP part of the 18F4680.h and the Comparator part as well. This still did not help me. Any other suggestions? |
Big hint here. The include file itself does nothing. Commenting out the stuff here, does not turn off these components, all it does, is remove the definitions _needed in the code_, to control the stuff.
The sequence to turn off the comparator, is:
setup_comparators(NC_NC_NC_NC);
Which needs the definition for 'NC_NC_NC_NC' from the include file.
PCM_programmers solution, is another way of doing exactly the same thing, for compiler versions, where the default routines do not work, but the reason it did not work for you, was that you were not loading the code to turn the parts off at all....
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Oct 15, 2007 3:49 pm |
|
|
Normally the compiler inserts start-up code which turns off the
comparators. For example, here's the start-up code for the 18F4680
for vs. 4.058:
Code: | ... void main()
... {
00004: CLRF FF8
00006: BCF FD0.7
00008: CLRF FEA
0000A: CLRF FE9
// Set all analog pins to be digitial i/o pins.
0000C: MOVF FC1,W // ADCON1
0000E: ANDLW C0
00010: IORLW 0F
00012: MOVWF FC1
// Turn off the Comparators.
00014: MOVLW 07
00016: MOVWF FB4 // CMCON
...
... while(1);
00018: BRA 0018 |
My suspicion is that vs. 3.213 may not do this correctly. The power-on
reset condition for the comparators is that they are turned on. I don't
have vs. 3.213, so I can't be sure that the setup_comparators() function
works correctly either. That's why I proposed turning them off by
writing directly to the CMCON register. |
|
![](templates/subSilver/images/spacer.gif) |
|