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

problem with timers

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



Joined: 12 Oct 2008
Posts: 7

View user's profile Send private message

problem with timers
PostPosted: Tue Oct 21, 2008 2:18 am     Reply with quote

Hi .
I am a new C language user and work with CCS compiler. I have an
project that use Timer1 of PIC16F877A and can't use timer. Please help
me to solve my program's problem.
My project:
When program start if pin B4 have input, it will go to s2. In s2 y (is a
Counter) increment and Timer1 start to timing, and save in timer
variable when y=10, the answer of formula s=(10.0/timer); and send
to RS232 port.
But the timer don't start or give random numbers and the answer of formula get wrong.
Thanks for helping me.

my program :
Code:

#include <16F877A.h>
#device adc=8

#bit sync = 0.0
#bit BRGH = 1.1
#bit spen = 1.2
#bit txen = 1.3
#bit T1CON = 0.
#bit TMR1ON = 0x0000.0
#bit TMR1CS = 0x0000.1

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES HS                       //High speed Osc (> 4mhz)
#FUSES NOPUT                    //No Power Up Timer
#FUSES NOPROTECT                //Code not protected from reading
#FUSES BROWNOUT                 //Reset when brownout detected
#FUSES LVP                      //Low Voltage Programming on B3(PIC16) or B5(PIC18)
#FUSES NOCPD                    //No EE protection
#FUSES NOWRT                    //Program memory not write protected
#FUSES NODEBUG                  //No Debug mode for ICD

#use delay(clock=11059200)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,stream=Host PC,enable=PIN_B0)
#include <string.h>
#include <stdarg.h>
 
long int x,a,z,d;
Long int y;
float s;
int32 timer;

void main()
{
   setup_adc_ports(NO_ANALOGS);
   setup_adc(ADC_OFF);
   setup_psp(PSP_DISABLED);
   setup_spi(FALSE);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_INTERNAL|T1_DIV_BY_8);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);
   SET_TRIS_B(0x04);


s:{
    if ( input(PIN_B4) )
        goto s2;
    else
        goto main;
}

s2:{
       do {
       if ( input(PIN_B4) )
           y=y+1;
       if (y==1)
           set_timer1(0);
       timer==get_timer1();
       if (y==10)
           s=(10.0/timer);
       printf(" s :   %f    m/s \n\r" ,s);
       if (y==11)
           y=0;
       if (y==0)
           timer = 0;
       goto main;
       } while (true);
       
   }
}


}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Oct 21, 2008 12:11 pm     Reply with quote

Nobody wants to look at code that has goto's in it. You need to re-write
your code in the normal C style, without goto's and labels.
ECACE



Joined: 24 Jul 2006
Posts: 94

View user's profile Send private message

PostPosted: Tue Oct 21, 2008 12:43 pm     Reply with quote

rezaf,

I'm not sure exactly what is supposed to be happening with y. Like PCM said, you need to learn to put it in normal C style. You have written in more like BASIC. I had a break just now, so I reformatted your code to something closer to normal C style. Now, I will be the first to admit, I am by no means an expert at C, and there are a ton of people here that are 1000% better at C than I am, but it does get you closer to what you want.

I would suggest you pick up Nigel Gardner's PICmicro MCU C 'An Introduction to Programming the Microchip PIC in CCS C'.
Here is your code reformatted. It still needs work with what happens with Y. I just did not fully understand what you wanted to do there.
I have also commented out some things that you shouldn't have set.
One big thing is turning off that LVP...unless your design really calls for it.

Again, I am by no means an expert programmer, in fact I would put myself in a beginner/intermediate level. I also may not be able to answer too many questions, I just had a break right now and had the time. I don't get too many breaks like this...sorry.

Good Luck

Code:
#include <16F877A.h>
#device adc=8

//#bit sync = 0.0          //Not sure why these were being set,
//#bit BRGH = 1.1          //The #use rs232 will take care of all this
//#bit spen = 1.2
//#bit txen = 1.3
//#bit T1CON = 0.          //You don't need to set these either,
//#bit TMR1ON = 0x0000.0    //Setup Timer1 will take care of all this
//#bit TMR1CS = 0x0000.1

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES HS                       //High speed Osc (> 4mhz)
#FUSES NOPUT                    //No Power Up Timer
#FUSES NOPROTECT                //Code not protected from reading
#FUSES BROWNOUT                 //Reset when brownout detected
//#FUSES LVP                      //Low Voltage Programming on B3(PIC16) or B5(PIC18)
#FUSES NOCPD                    //No EE protection
#FUSES NOWRT                    //Program memory not write protected
#FUSES NODEBUG                  //No Debug mode for ICD

#use delay(clock=11059200)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,stream=Host PC,enable=PIN_B0)
#include <string.h>
#include <stdarg.h>


//Variable deffinitions

long int x,a,z,d;
long int y;
float s;
int32 timer;



//Function prototypes of the functions at the end.  We can define them here
//so it will compile main(), then actually define the functions after main()

void Setup_Perifs();
void s2();



//Main of program will consist of first setting up the periferals,
//then sitting in a loop at the bottom, checking B4.  If B4 goes
//high, then it will execute s2, otherwise it just keeps checking B4

void main()
{
   Setup_Perifs();

   While(TRUE)
   {
      if(input(PIN_B4))   //If Pin B4 goes high, then
      s2();         //Execute s2, otherwise
   }               //Keep checking Pin B4.
}



//Functions that are get used in main()
//We can now define them and add some meat to them
//With them down here, they are not locked into main,
//and can be more easily called/modified/coppied.

void s2()
{                  //We only got here if pin B4 went high
   y++;               //So increment Y
   set_timer1(0);         //Clear timer1
   
   while (!input(PIN_B4))
   {
      ;            //Just sit here till pin B4 is released
   }

                  //Only way out of the above loop is if B4 is released
   timer==get_timer1();   //Now copy the value inside of timer1

       if (y==10)          //If y equeals 10, then...
       {
          s=(10.0/timer);   //s will equal 10 divided my what was in timer 1
          printf(" s :   %f    m/s \n\r" ,s); //print out S
       }
       
       if (y==11)          //If y is equal to 11, then make y equal to 0
           y=0;          //
}          //Now that this routine is done, it will go back to main()


//Setup Periferals of processor
void setup_Perifs()
{
   setup_adc_ports(NO_ANALOGS);
   setup_adc(ADC_OFF);
   setup_psp(PSP_DISABLED);
   setup_spi(FALSE);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_INTERNAL|T1_DIV_BY_8);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);
   SET_TRIS_B(0x04);
}

_________________
A HW Engineer 'trying' to do SW !!! Run!!!
John P



Joined: 17 Sep 2003
Posts: 331

View user's profile Send private message

PostPosted: Wed Oct 22, 2008 2:01 am     Reply with quote

Surely
SET_TRIS_B(0x04);

and
input(PIN_B4)

are mismatched?

The SET_TRIS_B command is equivalent to SET_TRIS_B(0b00000100), which sets pin B2 as an input, and all others as outputs.

Beware Bit 4 being taken as the same as a byte with value 0x4!
rezaf



Joined: 12 Oct 2008
Posts: 7

View user's profile Send private message

Hi ECACE
PostPosted: Sat Oct 25, 2008 1:21 am     Reply with quote

Hi ECACE,
very thanks for your answer, but your code too don't work and the timer variable is always zero.I don't know where of code are wrong.
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