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 CCS Technical Support

Why doesn't this simple program work?

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



Joined: 01 Nov 2011
Posts: 17

View user's profile Send private message

Why doesn't this simple program work?
PostPosted: Sun Feb 05, 2012 4:22 pm     Reply with quote

I'm trying to make a very simple program, so I can better understand using TIMER0, and I can't get it to work. Can someone please help me out? I'm using a pickit2 and using the pickit2 program to write the output .hex file to the chip

Here is the Main Program:
Code:
#include "C:\Users\Kevin\Documents\PIC Programming\PIC Program Development\DevBoard Mode Switcher\DevBoard Mode Switcher.h"
#int_TIMER0
void TIMER0_isr(void)
{
if    (input(PIN_A1))
     sb_status=1;

else if (input(PIN_A1==0))
    sb_status=0;
}




void main()
{

   setup_adc_ports(NO_ANALOGS|VSS_VDD);
   setup_adc(ADC_OFF);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC);
   setup_vref(FALSE);
   enable_interrupts(INT_TIMER0);
   enable_interrupts(GLOBAL);
   setup_oscillator(OSC_INTRC);

while(1)
{
if (sb_status==1);{
output_high(PIN_A0);
}
}
}


and here is the header file:
Code:
#include <12F683.h>
#device adc=8

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES INTRC                    //Internal RC Osc
#FUSES NOCPD                    //No EE protection
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NOMCLR                   //Master Clear pin used for I/O
#FUSES NOPUT                    //No Power Up Timer
#FUSES NOBROWNOUT               //No brownout reset
#FUSES FCMEN                    //Fail-safe clock monitor enabled

#use delay(clock=4000000)
int8 sb_status;


Thanks.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Feb 05, 2012 4:55 pm     Reply with quote

Look at item 2.6 in this list:
http://www.drpaulcarter.com/cs/common-c-errors.php
It's not exactly the same as your problem but it's similar.
Username



Joined: 01 Nov 2011
Posts: 17

View user's profile Send private message

PostPosted: Sun Feb 05, 2012 5:00 pm     Reply with quote

So where is it looping? The while(1) is there to stop the program from ending. I forgot to mention, the led that is connected to PIN_A0 is stuck on and doesn't respond to input changes on PIN_A1. Is it stuck in the timer?

Edit: So is the semicolon on the main if() causing it?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Feb 05, 2012 5:13 pm     Reply with quote

You need to decide this.

But what's your compiler version ? If you have Warnings enabled,
it should give you a strong warning on that line, indicating that there
is a problem. It does this in vs. 4.129. Check the Project Build options.
Make sure Warnings are enabled.

More discussion:
http://forums.whirlpool.net.au/archive/1301379
Username



Joined: 01 Nov 2011
Posts: 17

View user's profile Send private message

PostPosted: Sun Feb 05, 2012 5:43 pm     Reply with quote

It still doesn't work after i changed it. Could you please explain a little of what's going on? Thanks
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Feb 05, 2012 6:03 pm     Reply with quote

1. Post a description of all external circuits connected to the PIC.

2. Describe your procedure for testing the program.

3. Describe how you know it doesn't work. For example, if you are
looking at an LED, and it doesn't light up, and that means it doesn't work,
then tell us this. (And in section #1 above, describe the LED circuit in
detail - components, connections, voltages, etc.)

4. What is your CCS compiler version ?
This is what version numbers look like:
http://www.ccsinfo.com/devices.php?page=versioninfo
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Sun Feb 05, 2012 7:18 pm     Reply with quote

You tell pin A0 to go high, but I don't see anywhere you ever tell it to go low. Unless you tell it to go low it will stay high till the cows come home.
_________________
The search for better is endless. Instead simply find very good and get the job done.
Username



Joined: 01 Nov 2011
Posts: 17

View user's profile Send private message

PostPosted: Mon Feb 06, 2012 4:29 pm     Reply with quote

Okay, so I added an if() function to allow it to go low.

I taking the PIC out of the breadboard jumper setup and just putting it into the pickit2 for programming, so it shouldn't be programmed wrong because of other peripherals being connected to it during programming. I'm writing the .hex file to the chip through the Pickit2 program that came with the programmer.

I'm pushing a momentary push button that has power on one side and the jumper to the PIC pin A1 on the other side. I even thought it might be the button so I connected the jumper from the pin to power directly and it still stayed the same. Pin A0 is connected to an LED. With this program though, the LED is on from the start and doesn't react to any change.

I have tried supply voltages 2.5 to up to 5v and the result is the same.

I have tried compiler version 4.073 and 4.120 (what I'm currently using).

Here is what the code looks like now:
Code:

#include "C:\Users\Kevin\Documents\PIC Programming\PIC Program Development\DevBoard Mode Switcher\DevBoard Mode Switcher.h"
#int_TIMER0
void TIMER0_isr(void)
{
if    (input(PIN_A1))
     sb_status=1;

else if (input(PIN_A1==0))
    sb_status=0;
}


void main()
{
   setup_adc_ports(NO_ANALOGS|VSS_VDD);
   setup_adc(ADC_OFF);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC);
   setup_vref(FALSE);
   enable_interrupts(INT_TIMER0);
   enable_interrupts(GLOBAL);
   setup_oscillator(OSC_INTRC);

while(1)
{
if (sb_status==1)
   output_high(PIN_A0);

else if (sb_status==0)
   output_low(PIN_A0);

}
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Feb 06, 2012 4:53 pm     Reply with quote

Quote:
I'm pushing a momentary push button that has power on one side and the
jumper to the PIC pin A1 on the other side.

So it can only output a high level state to the PIC pin ? Then when it's
not pressed, the PIC pin is left floating ? That's not good.

Change your switch circuit so it looks like this:
Code:

           +5v
            |
            <
            > 4.7K       
            <         ___  Switch 
To          |        _|_|_
PIC -----------------o   o------
pin                            |             
                              --- GND
                               -   

This is the industry standard way to connect a switch. The pull-up resistor
holds the input pin at a high logic level as the idle state. When the button
is pressed, it puts a low logic level (0v) on the PIC pin.

Then change your code so it looks for a logic 0 as the active state (button
pressed).
Username



Joined: 01 Nov 2011
Posts: 17

View user's profile Send private message

PostPosted: Wed Feb 08, 2012 5:06 pm     Reply with quote

I know that pull-ups and pull-downs are a good way to reduce stray current, but i'm not sure that is causing the problem. Even when I take the button out of the circuit and jumper the pin directly to power or ground there is no response. Could it be something is set wrong in the FUSES?

Thanks
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Feb 08, 2012 6:55 pm     Reply with quote

Before you try using interrupts, try this simple program and see if it
works. When you press down the button, the LED should go on.
When release the button, the LED should go off. I tested this code in
hardware, with vs. 4.120, and it works.

The button circuit is the same one as I posted earlier in this thread.
The LED circuit looks like this:
Code:

pin      470 ohms      LED       
A0  -----/\/\/\/------->|----
                            |
                            |
                          -----  Ground 
                           ---
                            -

Code:

#include <12F683.H>
#fuses INTRC_IO, NOWDT, BROWNOUT, PUT
#use delay(clock=4M)
 
#define BUTTON_PIN  PIN_A1
#define LED_PIN  PIN_A0

//======================================
void main()
{

while(1)
  {
   if(input(BUTTON_PIN) == 0)
      output_high(LED_PIN);
   else
      output_low(LED_PIN);
  }

}
Douglas Kennedy



Joined: 07 Sep 2003
Posts: 755
Location: Florida

View user's profile Send private message AIM Address

PostPosted: Thu Feb 09, 2012 4:18 am     Reply with quote

I haven't used this chip 12F683. Most start with reading the data sheet and noting the capabilities of each pin (Ex. does it default to analog is it open drain etc) On small packages the programming pins maybe needed to perform other functions after programming so attention needs to be paid to disconnecting the programmer when running the target and disconnecting the target circuit on the programming pins while programming. After reading the data sheet heed PCM programmer's advice of starting simple.
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