CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

help with setup_comparator + setup_vref

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
random guy



Joined: 02 Jun 2007
Posts: 11

View user's profile Send private message

help with setup_comparator + setup_vref
PostPosted: Sat Jun 02, 2007 7:09 am     Reply with quote

hello i am working on my first pic program. i am using the pic16f690. my board has 4 leds at c0,c1,c2,c3 and a button on a3.

just to get my feet wet i wanted to test out turning on the leds in order with a short delay and having an inturrupt when the button is pressed which will light the two central leds till let go.

good thing is that ccs generated some stuff for me (correctly i hope) when i used the project wizard. though this line was left open ended (and thus gives an error)

setup_comparator( setup_vref(FALSE

the manual gave an example that setup_vref(type|value) should work. but cant seem to get a correct value with false. (2 close paranthesis doesnt work)

when i comment out this line i can complile but the device it only flashes the first led and doesnt go onward. the button stops the shinning of the single led but doesnt shine the central leds as it should.

here is my code.

Code:
#include "C:\Program Files\PICC\Projects\16f690.h"
#int_RA
#use delay(clock=4000000)

RA_isr()
{
   while (input(PIN_A3)==1)
   {
   output_high(PIN_C1);
   output_high(PIN_C2);
   }
   return 1;
}

void main()
{

   setup_adc_ports(NO_ANALOGS|VSS_VDD);
   setup_adc(ADC_OFF);
   setup_spi(FALSE);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(   setup_vref(FALSE
   enable_interrupts(INT_RA);
   enable_interrupts(GLOBAL);
   setup_oscillator(False);

   // TODO: USER CODE!!
while(1)
{
   output_high(PIN_C0);
   delay_ms(200);
   output_low(PIN_C0);

   output_high(PIN_C1);
   delay_ms(200);
   output_low(PIN_C1);

   output_high(PIN_C2);
   delay_ms(200);
   output_low(PIN_C2);

   output_high(PIN_C3);
   delay_ms(200);
   output_low(PIN_C3);

   output_high(PIN_C2);
   delay_ms(200);
   output_low(PIN_C2);

   output_high(PIN_C1);
   delay_ms(200);
   output_low(PIN_C1);
}

}
random guy



Joined: 02 Jun 2007
Posts: 11

View user's profile Send private message

PostPosted: Sat Jun 02, 2007 7:12 am     Reply with quote

here is a picture of the kit i have

http://www.arrowne.com/innov/in83/pics/c9i18_microchip_image1.jpg

thanks for the help.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Jun 02, 2007 11:42 am     Reply with quote

What's your compiler version ? You can find it at the top of the .LST
file for your project. The .LST file will be in your project directory.
The version will be a 4-digit number, such as 3.191, 4.013, or 4.039, etc.

Did the CCS compiler come with the kit ? Is it a full version of the
compiler, or is it a limited version made just for this kit ? CCS ought
to upgrade you to a version that has doesn't have that Wizard bug.
You need to contact CCS support to ask for this.


For your first test program, I would not try to use interrupts.
Just blink the LEDs.

This board has jumper options for the LEDs. Each LED has a separate
jumper which must be installed to enable the LED. Do you have all the
LED jumpers installed ?

I have to go away for the rest of the day. If you need more help today
someone else will have to reply.
random guy



Joined: 02 Jun 2007
Posts: 11

View user's profile Send private message

PostPosted: Sat Jun 02, 2007 4:32 pm     Reply with quote

Version 4.013, so i guess that isnt the most recent.

Quote:
For your first test program, I would not try to use interrupts.
Just blink the LEDs.


quote thanks for the tip, will play around more with that but i guess i will need interrupts eventually. it seems like just one little bit is missing, would anyone happen to know what should go there?

Quote:
This board has jumper options for the LEDs. Each LED has a separate
jumper which must be installed to enable the LED. Do you have all the
LED jumpers installed ?


umm i dont know, i tried doing the same thing with the open source sdcc compiler at home and i could turn on and off leds by just changing it to an output port. i dont know anything about jumpers.

thanks for help so far you and anyone else who replies.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Jun 03, 2007 1:21 am     Reply with quote

I don't have PCM vs. 4.013. I do have vs. 4.014, and I compiled a test
program and looked at the .LST file. There's a bug in the CCS startup
code. It enables Comparator #2. This will cause problems with some
of the pins on Port C if you try to use them as normal i/o pins.

You can fix this by adding the line shown in bold below. This will
turn off both comparators.
Quote:
void main()
{
setup_comparator(NC_NC_NC_NC);
random guy



Joined: 02 Jun 2007
Posts: 11

View user's profile Send private message

PostPosted: Sun Jun 03, 2007 5:13 pm     Reply with quote

hmmm ok getting closer i think. i tried replaceing my setupcompare statement with yours and made the setup_vref statement its own line (as in the following).

the inturrupt is sorta working in that the code in its function is being executed but the inturupt code is being executed when the program is run without ever doing what is in main(). so the program is getting to isr() but under wrong conditions.

when the button is pressed while in the isr() statement the leds dont shine at all.

here is my code

Code:
#include "C:\Program Files\PICC\Projects\16f690.h"
#int_RA
RA_isr()
{

       output_high(PIN_C1);
       delay_ms(200);
       output_low(PIN_C1);
       delay_ms(50);
       output_high(PIN_C2);
       delay_ms(200);
       output_low(PIN_C2);
       delay_ms(50);
}



void main()
{

   setup_adc_ports(NO_ANALOGS|VSS_VDD);
   setup_adc(ADC_OFF);
   setup_spi(FALSE);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);
   enable_interrupts(INT_RA);
   enable_interrupts(GLOBAL);
   setup_oscillator(False);

   // TODO: USER CODE!!
   while (1)
   {
       output_high(PIN_C0);
       delay_ms(20);
       output_low(PIN_C0);

delay_ms(10);

       output_high(PIN_C1);
       delay_ms(20);
       output_low(PIN_C1);

delay_ms(10);

       output_high(PIN_C2);
       delay_ms(20);
       output_low(PIN_C2);

delay_ms(10);

       output_high(PIN_C3);
       delay_ms(20);
       output_low(PIN_C3);

delay_ms(10);

       output_high(PIN_C2);
       delay_ms(20);
       output_low(PIN_C2);

delay_ms(10);

       output_high(PIN_C1);
       delay_ms(20);
       output_low(PIN_C1);
delay_ms(10);
   }
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Jun 03, 2007 5:53 pm     Reply with quote

You need to post the missing lines at the start of your program.
These will be the #fuses statement and the #use delay() statement, etc.

Here's a link to the schematic for the PicKit2 board.
http://ww1.microchip.com/downloads/en/DeviceDoc/LPC%20Demo%20Board%20Schematic.pdf
It only shows one switch is available on the whole board, and it's
normally intended to be used as a MCLR reset switch. However,
it could be used as a switch for i/o pin A3 if the NOMCLR fuse was
used. That's why I want to see your #fuses statement and the other
CCS compiler directive statements that go at the start of the program.
random guy



Joined: 02 Jun 2007
Posts: 11

View user's profile Send private message

PostPosted: Sun Jun 03, 2007 8:05 pm     Reply with quote

thanks i think it is getting closer to working. i used that fuse along with some i saw in an example file somewhere. the problem with the button halting everything is fixed now but the problem where it only executes isr() and not main() is still there. i tried adding return 0; to the end of isr() but that didnt do anything but remove the compile warning about having no return value.

i dont understand where in the inturrupt code that i could specify it to look for a change in a3 (either a change in it or when it is equal to 1)

hopefully if that is only thing in the way of me having a working function with a working interrupt. here is my code (labled some things as new to show changes)

Code:
#include "C:\Program Files\PICC\Projects\16f690.h"
#int_RA
//***new***
#fuses INTRC_IO,NOPROTECT, NOWDT, NOMCLR
#use delay(clock=4000000)

RA_isr()
{
       output_high(PIN_C1);
       delay_ms(20);
       output_low(PIN_C1);
       delay_ms(5);
       output_high(PIN_C2);
       delay_ms(20);
       output_low(PIN_C2);
       delay_ms(5);
      //***new***
       return 0;
}



void main()
{

   setup_adc_ports(NO_ANALOGS|VSS_VDD);
   setup_adc(ADC_OFF);
   setup_spi(FALSE);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);
   enable_interrupts(INT_RA);
   enable_interrupts(GLOBAL);
   setup_oscillator(False);

   // TODO: USER CODE!!
   while (1)
   {
       output_high(PIN_C0);
       delay_ms(20);
       output_low(PIN_C0);

delay_ms(5);

       output_high(PIN_C1);
       delay_ms(20);
       output_low(PIN_C1);

delay_ms(5);

       output_high(PIN_C2);
       delay_ms(20);
       output_low(PIN_C2);

delay_ms(5);

       output_high(PIN_C3);
       delay_ms(20);
       output_low(PIN_C3);

delay_ms(5);

       output_high(PIN_C2);
       delay_ms(20);
       output_low(PIN_C2);

delay_ms(5);

       output_high(PIN_C1);
       delay_ms(20);
       output_low(PIN_C1);

   }
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Jun 03, 2007 8:47 pm     Reply with quote

Quote:
i tried adding return 0;

Interrupt routines don't return anything. Remove the return() statement.

Quote:

#int_RA
//***new***
#fuses INTRC_IO,NOPROTECT, NOWDT, NOMCLR
#use delay(clock=4000000)

RA_isr()
{

The #int_RA directive should be placed on the line just before the
RA_isr() routine. See any CCS example file that uses interrupts.


Quote:

but the problem where it only executes isr() and not main() is still there

You must clear the "mismatch" condition on Port A when you exit the isr.
This is done by reading port A into a variable, as shown below.
This requirement is given in the 16F690 data sheet in Section 4.2.2.
See the example shown below:
Code:

#int_ra
RA_isr()
{
int8 c;
.
.
.

c = input_a();
}


Quote:

I dont understand where in the inturrupt code that i could specify it
to look for a change in a3.

enable_interrupts(INT_RA);
enable_interrupts(GLOBAL);

Look in the 16F690.H file, at the end of the file. It lists all of the
constants that you can use with enable_interrupts(). You will see
that there is a specific one for just pin A3. Use it instead of the one
that you have above.

Also, just before the line that enables interrupts, you should put a line
of code to read Port A (to clear the mismatch condition), and another
line to clear the INT_RA interrupt, by calling the clear_interrupt() function.
random guy



Joined: 02 Jun 2007
Posts: 11

View user's profile Send private message

thanks a lot
PostPosted: Mon Jun 04, 2007 9:40 pm     Reply with quote

yay i followed your guidelines and it worked, thanks a lot. you have taught me a lot, thank you very much for everything.

i am now off to continue to improve the code and see if i can get it to do something useful.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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