View previous topic :: View next topic |
Author |
Message |
hayee
Joined: 05 Sep 2007 Posts: 252
|
Writing code in CCS |
Posted: Mon Sep 06, 2010 1:04 am |
|
|
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
|
|
Posted: Mon Sep 06, 2010 7:41 am |
|
|
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: 1611 Location: Central Illinois, USA
|
|
Posted: Mon Sep 06, 2010 10:07 pm |
|
|
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
|
|
Posted: Tue Sep 07, 2010 12:07 am |
|
|
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
|
|
Posted: Tue Sep 07, 2010 1:06 am |
|
|
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. |
|
|
|