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

Writing code in CCS

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



Joined: 05 Sep 2007
Posts: 252

View user's profile Send private message

Writing code in CCS
PostPosted: Mon Sep 06, 2010 1:04 am     Reply with quote

Guys I want to write the following code in CCS. I am not getting some parameters. How to define them in CCS like
Code:

TMR1GE=0;         //no gate control
TMR1ON=1;         //tmr1 off
TMR1ON=0;         //stop tmr1
TMR1H=0, TMR1L=0;      //reset tmr1
TMR1ON=1;            //start tmr1
TMR1IF=0;            //reset tmr1 flag

How should I do it, any suggestions.
Code:

#include <htc.h>
#include "gpio.h"

__CONFIG(MCLRDIS & BORDIS & WDTDIS & PWRTEN & INTIO);

#define IOC_INT_PORT   GPIO
#define IOC_INT_DDR   TRISIO
#define IOC_INT      (1<<3)      //gpio3 is the ioc pin

#define TMR1_ERROR   0      //tmr1 error term

unsigned short pulse_width;      //pulse width counter
unsigned short tmr1_overflow_counter;   //how many times tmr1 has overflown

void mcu_init(void)
{
 ANSEL=0x00;         //all pins gpio
 CMCON=0x07;         //analog comparators off
}

void tmr1_init(void)
{         //initialize tmr1 as timer
 TMR1IF=0;         //reset tmr1 flag
 TMR1GE=0;         //no gate control
 T1CKPS1=0, T1CKPS0=0;      //prescaler 1:1
 TMR1CS=0;         //tmr1 source is internal clock
 TMR1ON=1;         //tmr1 off
}

void ioc_int_init(void)
{            //initialize external interrupt
 IO_IN(IOC_INT_DDR, IOC_INT);   //external interrupt pin as input
 IOCB |= IOC_INT;      //enable ioc on ioc_int pin
 GPIE=1;            //enable interrupt on change
 GIE=1;            //enable interrupt
}
   
void interrupt isr(void)
{      //interrupt service routine
 if (GPIE & GPIF)
 {      //a valid IOC interrupt has taken place
  //TMR1ON=0;         //stop tmr1
  IO_GET(IOC_INT_PORT, IOC_INT);   //dummy read on ioc_int
  GPIF=0;            //reset ioc interrupt flag
  pulse_width=(TMR1H << 8) | TMR1L;   //read tmr1
  //pulse_width+=TMR1_ERROR;   //correct the error term
  TMR1H=0, TMR1L=0;      //reset tmr1
  //TMR1ON=1;            //start tmr1
  TMR1IF=0;            //reset tmr1 flag
  tmr1_overflow_counter=0;   //reset tmr1 overflown counter
  //return;
 }
}
      
Void main(void)
{
 mcu_init();         //initialize the mcu
 tmr1_init();         //initialize tmr1
 ioc_int_init();         //initialize external interrupt

While (1)
 {
  //TODO Auto-generated main function
  if (TMR1IF)
  {           //tmr1 has overflown
   TMR1IF=0;      //reset tmr1
   tmr1_overflow_counter++;   //increment tmr1  overflown counter
  }
 }
}
tinley



Joined: 09 May 2006
Posts: 67

View user's profile Send private message Visit poster's website

PostPosted: Mon Sep 06, 2010 7:41 am     Reply with quote

CCS uses functions to set up the timer and other registers. Have a look in the manual under 'BUILT-IN-FUNCTIONS' and 'timer'
bkamen



Joined: 07 Jan 2004
Posts: 1615
Location: Central Illinois, USA

View user's profile Send private message

PostPosted: Mon Sep 06, 2010 10:07 pm     Reply with quote

Not to mention all your interrupt stuff.

CCS has helper functions for those as well.

I mean, you CAN write them yourself if you want to. However, this is the joy of CCS. A lot of this kind of stuff is done for you. That's why makes this compiler nice.

-Ben
_________________
Dazed and confused? I don't think so. Just "plain lost" will do. :D
hayee



Joined: 05 Sep 2007
Posts: 252

View user's profile Send private message

PostPosted: Tue Sep 07, 2010 12:07 am     Reply with quote

I have looked in the "BUILT-IN-FUNCTION" in the timer section and found nothing what I have required. That's why I asked here.
What I found is
Quote:

T1CKPS1=0 , T1CKPS0=0 //prescaler 1:1
TMR1CS=0 // tmr1 source is internal clock
can be replaced by
setup_timer_1(T1_INTERNAL | T1_DIV_BY_1)

Quote:

GPIE=1 //enable interrupt on change
can be replaced by
ext_int_edge(0,L_TO_H);

Quote:

GIE=1 // enable interrupt
can be replaced by
enable_interrupts(INT_EXT)

Quote:

pulse_width=(TMR1H <<8) | TMR1L;//read tmr1
can be replaced by
pulse_width=get_timer_1();

Quote:

TMR1H=0 ,TMR1L=0 //reset tmr1
can be replaced by
set_timer(0);


But what about the remaining functions? How I define in CCS
like
TMR1GE=0; //no gate control
TMR1ON=0; //tmr1 off
TMR1ON=1; //start tmr1
TMR1IF=0; //reset tmr1 flag
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Tue Sep 07, 2010 1:06 am     Reply with quote

Quote:
TMR1ON=0; //tmr1 off
can be replaced by:
setup_timer1(T1_DISABLED)

Quote:
TMR1IF=0; //reset tmr1 flag
can be replaced by
clear_interrupt(INT_TIMER1);

Quote:
TMR1ON=1; //start tmr1
There is no function for setting this bit only (an omission). you have to configure the whole timer again:
setup_timer_1(T1_INTERNAL | T1_DIV_BY_1)

Quote:
TMR1GE=0; //no gate control
This is a feature of the newer PIC processors. Check the header file of your processor if it is mentioned. This is the generic thing to do when in doubt about supported features.


The good thing about the CCS code is that the code is easier to read and it makes it very easy to switch to another PIC processor if ever needed. Often only changing the include file suffices.
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