|
|
View previous topic :: View next topic |
Author |
Message |
coderchick Guest
|
Is it software? is it hardware? is it startup power issue? |
Posted: Mon May 05, 2008 1:17 pm |
|
|
Hey all,
I'm using an 18F4525 that is supposed to interact with a Hexadecimal rotary switch. On the switch there are 4 set locations for 4 different devices that my code can work with with slight differences for each device. What I want to do is, upon startup, read in the HEX switch from port C (temp) and set the device_type.
Code: |
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_psp(PSP_DISABLED);
setup_spi(FALSE);
//****5MHz
setup_timer_0(RTCC_INTERNAL);
setup_timer_1(T1_INTERNAL|T1_DIV_BY_2);
setup_timer_2(T2_DIV_BY_4,175,2);
//******
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
enable_interrupts(INT_EXT); //turn on EXT to find start bit
ext_int_edge( H_TO_L ); // Sets up EXT
disable_interrupts(INT_TIMER2); //turn off timer 2
enable_interrupts(GLOBAL);
initialize(&que); //sets up parameters for que
temp = (0x0f & input_c()); //read lowest portc bits
device_type = 0x0f ^ temp; //invert portc bits, see Device table for reference
while (TRUE) //continuous loop through the different mode cases, read buttons, etc.
{
switch (mode)
{
|
Now I only want this to occur once at start up. Read the port, set the device and then continue. However, I can't seem to make it work this way. At first I thought it was a hardware issue, but when it's hooked up to the debugger, everything works fine, and whatever I have the switch set to, it reads correctly evertime I run the debugger. The only way I've been able to make things work without the debugger is by rearranging the code to look like this...
Code: |
initialize(&que); //sets up parameters for que
while (TRUE) //continuous loop through the different mode cases, read buttons, etc.
{
temp = (0x0f & input_c()); //read lowest portc bits
device_type = 0x0f ^ temp; //invert portc bits, see Device table for reference
switch (mode)
{
|
Which causes the code to read in port C everytime it loops around, which isn't what I need or want. I've tried adding a delay at the beginning right before initialize(&que); to see if it was a power issue on startup, but that didn't make any difference. If anyone has any ideas for me, I'm more than willing to listen, this thing has been driving me crazy for almost a week now.
Kmoe |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon May 05, 2008 1:36 pm |
|
|
Make a very simple test program that only tests reading the switches.
Get rid of all other code. Example:
Code: |
#include <18F4525.h>
#fuses XT,NOWDT,PUT,BROWNOUT,NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
//====================================
void main()
{
int8 value;
while(1)
{
value = input_c();
value &= 0x0F;
printf("%x \n\r", value);
delay_ms(500);
}
while(1);
} |
This program will read the switches and display the value every 1/2
second on a terminal window. Try it. Change the oscillator fuse
and #use delay() statement as required to match your hardware. |
|
|
coderchick Guest
|
|
Posted: Mon May 05, 2008 5:51 pm |
|
|
thanks PCM, but I figured it out. It turned out to be a case of the compiler Wizard being too helpful. Once I removed the line setup_spi(FALSE); everything worked the way I wanted it to. Thanks for your time!
Kmoe |
|
|
|
|
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
|