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

PIC16F727 Timer1 Interrupt

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



Joined: 25 Jul 2009
Posts: 3

View user's profile Send private message

PIC16F727 Timer1 Interrupt
PostPosted: Sat Jul 25, 2009 11:12 pm     Reply with quote

I am having problem with PIC16F727 Timer 1 Interrupt.
Here is the scenario:
IDE = MPLabv8.30
PIC = PIC16F727
Compiler = CCS PCW
Target Board has power supply(3.3V) a PIC only with interface to LEDs and pads for capacitive sensing.
I have attached the project file with this email for your convenience.

Here is my problem:
I was planning to use capacitive sensing module on PIC16F727 for my project which detects button press on 5 different pads. I was using Timer 1 and Timer 0 but I was unable to generate an interrupt and jump into ISR.

After failing several attempts over couple of days, I have decided just to generate an interrupt by overflowing Timer 1. Still I was unable to make the program jump into the ISR.

In the end, I have decided to debug what happens when the Timer 1 is running on its own and if it ever overflows, just to make sure if I am doing it correctly.

Here is what I observed:
Using MPLAB SIM, to debug the code, i have noticed that Timer1 is incrementing correctly and when it overflows it does jump into the ISR and start executing code inside the ISR. But when I program the target device with it, it never goes into the ISR.

Could you please have a look at my project file and advise me on what I am doing wrong and what I need to do.

Regards,

TS


Code:


#include "WAGv10_TMR1_INT.h"
/**********************************************************************/
/******************************UTILITY*********************************/
/**********************************************************************/
//convert nibble to chars
static char BCDNyb2Chr (unsigned char BCDNyb){
    return (BCDNyb + ((BCDNyb < 10) ? '0' : '7'));
}

//converts hex to hex ascii
//for example i = 0x1234 => i == (0x31,0x32,0x33,0x34) ascii values
char* h2ha(unsigned long data, char *buffer, unsigned char len){
   
   char *ptr;

   ptr = buffer + len;
   *ptr-- = '\0';
   while (len--){
      *ptr-- = BCDNyb2Chr (data & 0x0f);
      data >>= 4;
   }

   return (buffer);
}


//converts hex to decimal ascii to send over rs232,
//for example: i = 0x1234 => i = d4660 => i == (0x34,0x36,0x36,0x30) decimal ascii
char* h2da(unsigned long data, char *buffer, unsigned char len){
   char *ptr;
   
   ptr = buffer + len;
   *ptr--='\0';
   while (len--){
      *ptr-- = (0x30 + data % 10)   ;
      data /= 10;
   }
   
   return (buffer);
}

/**********************************************************************/
/******************************COMMUNICATIONS**************************/
/**********************************************************************/
static unsigned char UART_RXINT_ENABLE = 0x10;
static unsigned char UART_RXINT_DISABLE = 0xEF;
static unsigned char UART_RXFLAG_CLEAR = 0xEF;

static unsigned char GLOBAL_INTERRUPTS_ENABLE = 0x80;
static unsigned char GLOBAL_INTERRUPTS_DISABLE = 0x7F;
static unsigned char PERIPHERAL_INTERRUPTS_ENABLE = 0x40;
static unsigned char PERIPHERAL_INTERRUPTS_DISABLE = 0xBF;


//initialize uart
//enable interrupt if uart_int = 1
void enable_uart(unsigned char uart_int){
   
   TXSTA = 0b00100100; //8 bit tx, Asyc mode, high speed
   RCSTA = 0b10000000;   //serial port enabled, 8 bit rx
   SPBRG = 25;         //19200 @ 8MHz

   if(uart_int){
      PIR1 &= UART_RXFLAG_CLEAR;
      PIE1 |= UART_RXINT_ENABLE;
      INTCON |= PERIPHERAL_INTERRUPTS_ENABLE;
      INTCON |= GLOBAL_INTERRUPTS_ENABLE;
   }
}
//disable UART and its interrupt
void disable_uart(void){

   PIR1 &= UART_RXFLAG_CLEAR;
   PIE1 &= UART_RXINT_DISABLE;
   INTCON &= PERIPHERAL_INTERRUPTS_DISABLE;
   INTCON &= GLOBAL_INTERRUPTS_DISABLE;
   
   TXSTA = 0x00;
   RCSTA = 0x00;

}

//transmit a character over UART
unsigned char coms_tx (unsigned char i){
    while (!TRMT);
    putc (i);
    return (i);
}
//transmit at string of character over UART
void coms_tx_str (char *str){
   while (*str)
      coms_tx(*str++);
}


/**********************************************************************/
/****************************CAP SENSE*********************************/
/**********************************************************************/
unsigned char   index;
unsigned int   reading[NUM_BTTNS];
unsigned int   average[NUM_BTTNS];

//Initialise capacitive sensing module and the timer module
void init_cap(void)
 {

   // Set up variables
   for (index=0; index<5; index++) {
      average[index] = 0;
      reading[index] = 0;
    }

   // Initialize for Timer0 time base
   OPTION_REG = 0b11000100;            // Timer0 init
   T0IF = 0;                     // enable tmr0 intpt
   T0IE = 1;   
   T1CON  = 0b01000101;            // Timer1 enable, system clock, 1:1 prescale,
   T1GCON = 0b11100001;            // Timer1 gate init   

   
   // Inititialize for Timer2 time base
   // Tmr2 module setup
//   T2CON = 0b00000100;               // T2ON, prescale & postscale = 1:1
//   T2CON |=0b00000001;               // adjust prescalar   
//   T1CON  = 0b01000101;            // Timer1 enable, system clock, 1:1 prescale,
//   T1GCON = 0b11100001;            // Timer1 gate init
//   T1GCON &=0b11111100;            // clr T1GSS bits
//   T1GCON |=0b00000010;            // set T1GSS for Timer2 match PR2
//   PR2 = 0xB4;                     // pr2 for use w/timer2 ..  w/pres.1:4, 0xB4 sets 500us scan rate.
//   TMR2IF = 0;
//   TMR2IE = 1;


   // Cap Sense Module
   CPSCON0 = 0b10001100;            // control settings
   CPSCON1 = 0x00;                  // init to channel select = 0 (4 LSb's)
   index = CPSCON1;               // Start program ready to scan first channel
           
   // Gate Setup
   TMR1GIF   = 0;                  // clear gate intpt flag
   TMR1GIE   = 1;                  // enable gate intpt
   PEIE      = 1;                  // enable peripheral intpts

   GIE = 1;
   

}


/**********************************************************************/
/******************************MAIN************************************/
/**********************************************************************/
unsigned char j;   
unsigned long k;     

//Initialize the device to its default state
void initialize_pic (void){
//SETUP INTERNAL OSCILLATOR
   SETUP_OSCILLATOR(OSC_8MHz); //internal 8MHz Osc

   OSCTUNE = 0x00;
   while (ICSL && ICSS);   //wait till OSC is stable, only for internal OSC

//SETUP TIMERS   
   SETUP_TIMER_0(RTCC_INTERNAL);
   SETUP_TIMER_1(T1_DISABLED);
   SETUP_TIMER_2(T2_DISABLED,0,1);

//SETUP PORTS
   APFCON=0x00;

//SETUP PERIPHERALS   
   SETUP_ADC(ADC_OFF);
   FVRCON = 0x00;
   CPSCON0 = 0x00;
   SETUP_CCP1(CCP_OFF);
   SETUP_CCP2(CCP_OFF);
   SETUP_UART(FALSE);
   SETUP_SPI(SPI_SS_DISABLED);   

//SETUP INTERRUPTS
   CLEAR_INTERRUPT(GLOBAL|INT_RTCC|INT_RB|INT_EXT|INT_AD|INT_TBE|INT_RDA|INT_TIMER1|INT_TIMER2|INT_CCP1|INT_CCP2|INT_SSP|INT_TIMER0|INT_RB0|INT_RB1|INT_RB2|INT_RB3|INT_RB4|INT_RB5|INT_RB6|INT_RB7);
   DISABLE_INTERRUPTS(GLOBAL|INT_RTCC|INT_RB|INT_EXT|INT_AD|INT_TBE|INT_RDA|INT_TIMER1|INT_TIMER2|INT_CCP1|INT_CCP2|INT_SSP|INT_TIMER0|INT_RB0|INT_RB1|INT_RB2|INT_RB3|INT_RB4|INT_RB5|INT_RB6|INT_RB7);
}

void main(){
   unsigned char buf[5] = {0};

   initialize_pic();
   output_high(PWRON);
   enable_uart(0);


   TMR1ON = 0;
   TMR1H = TMR1L = 0;
   T1CON = 0x00;
   T1GCON = 0x00;   
   TMR1IF = 0;
   TMR1IE = 1;
   PEIE = 1;                  // enable peripheral intpts
   GIE = 1;

   TMR1ON = 1;

   while(1){

      PORTB = 0x00;
      delay_ms(100);
      PORTB = 0xFF;
      
      k = TMR1H;
      k <<= 8;
      k += TMR1L;
      
      enable_uart(0);
      coms_tx(10);
      
      INTCON = 0x90;
      
      putc('0');
      putc('x');
      h2ha(k, buf, 4);   
      coms_tx_str(buf);
      putc('-');
      k = INTCON;
      putc('0');
      putc('x');
      h2ha(k, buf, 2);   
      coms_tx_str(buf);
      putc('-');
      k = PIE1;
      putc('0');
      putc('x');
      h2ha(k, buf, 2);   
      coms_tx_str(buf);
      putc('-');
      k = PIR1;
      putc('0');
      putc('x');
      h2ha(k, buf, 2);   
      coms_tx_str(buf);
      
      
      //      TMR1L = TMR1H = 0;
      
      coms_tx(10);
      coms_tx(10);
      
      
      disable_uart();         // turn off uart
      
      
      if(TMR1IF){
         TMR1IF = 0;
         TMR1ON = 0;
         
         
         enable_uart(0);
         coms_tx(10);
         
         k = PIR1;
         putc('0');
         putc('x');
         h2ha(k, buf, 2);   
         coms_tx_str(buf);
         
         
         TMR1L = TMR1H = 0;
         
         
         coms_tx(10);
         coms_tx(10);
         
         
         disable_uart();         // turn off uart
         TMR1ON = 1;
      
      }


   }
}


/**********************************************************************/
/****************************INTERRUPTS********************************/
/**********************************************************************/

#int_timer1
void timer1_isr(){

   disable_interrupts(GLOBAL);
   clear_interrupt(INT_TIMER1);

   for(j = 0; j < 10; j++){

      output_high(SPEED);
      delay_ms(10);
      output_low(SPEED);
      delay_ms(10);

   }

   TMR1IF = 0;
   TMR1ON = 0;
   TMR1H = TMR1L = 0;
   TMR1ON = 1;

   GIE = 1;

}



The HEADER FILE
Code:

#include <16F727.h>

#device adc=8
#device *=16
#device ICD=TRUE
#use delay(clock=8000000)
#use rs232(baud=19200, xmit=PIN_C6, rcv=PIN_C7)

#FUSES INTRC, NOWDT, PUT, MCLR, NOPROTECT, NOBROWNOUT, BORV25, PLLEN, NODEBUG, VCAP_A5

//Register Definitions - Bank 0
#byte TMR0 = 0x01
#byte PORTA = 0x05
#byte PORTB = 0x06
#byte PORTC = 0x07
#byte PORTD = 0x08
#byte PORTE = 0x09
#byte INTCON = 0x0B
#bit GIE = 0x0B.7
#bit PEIE = 0x0B.6
#bit T0IE = 0x0B.5
#bit INTE = 0x0B.4
#bit RBIE = 0x0B.3
#bit T0IF = 0x0B.2
#bit INTF = 0x0B.1
#bit RBIF = 0x0B.0
#byte PIR1 = 0x0C
#bit TMR1GIF = 0x0C.7
#bit ADIF = 0x0C.6
#bit RCIF = 0x0C.5
#bit TXIF = 0x0C.4
#bit SSPIF = 0x0C.3
#bit CCP1IF = 0x0C.2
#bit TMR2IF = 0x0C.1
#bit TMR1IF = 0x0C.0
#byte PIR2 = 0x0D
#bit CCP2IF = 0x0D.0
#byte TMR1L = 0x0E
#byte TMR1H = 0x0F
#byte T1CON = 0x10
#bit TMR1ON = 0x10.0
#byte TMR2 = 0x11
#byte T2CON = 0x12
#byte SSPBUF = 0x13
#bit BF = 0x13.0
#byte SSPCON = 0x14
#bit SSPEN = 0x14.5
#bit CKP = 0x14.4
#byte CCPR1L = 0x15
#byte CCPR1H = 0x16
#byte CCP1CON = 0x17
#byte RCSTA = 0x18
#byte TXREG = 0x19
#byte RCREG = 0x1A
#byte CCPR2L = 0x1B
#byte CCPR2H = 0x1C
#byte CCP2CON = 0x1D
#byte ADRESH = 0x1E
#byte ADCON0 = 0x1F

//Register Definitions - Bank 1
#byte OPTION_REG = 0x81
#byte TRISA = 0x85
#byte TRISB = 0x86
#byte TRISC = 0x87
#byte TRISD = 0x88
#byte TRISE = 0x89
#byte PIE1 = 0x8C
#bit TMR1GIE = 0x8C.7
#bit ADIE = 0x8C.6
#bit RCIE = 0x8C.5
#bit TXIE = 0x8C.4
#bit SSPIE = 0x8C.3
#bit CCP1IE = 0x8C.2
#bit TMR2IE = 0x8C.1
#bit TMR1IE = 0x8C.0
#byte PIE2 = 0x8D
#bit CCP2IE =0x8D.0
#byte PCON = 0x8E
#byte T1GCON = 0x8F
#byte OSCCON = 0x90
#bit ICSL = 0x90.3
#bit ICSS = 0x90.2
#byte OSCTUNE = 0x91
#byte PR2 = 0x92
#byte SSPADD = 0x93
#byte SSPSTAT = 0x94
#bit SMP = 0x94.7
#bit CKE = 0x94.6
#byte WPUB = 0x95
#byte IOCB = 0x96
#byte TXSTA = 0x98
#bit TRMT = 0x98.1
#byte SPBRG = 0x99
#byte APFCON = 0x9C
#byte FVRCON = 0x9D
#byte ADCON1 = 0x9F

//Register Definitions - Bank 2
#byte CPSCON0 = 0x108
#byte CPSCON1 = 0x109
#byte PMDATL = 0x10C
#byte PMADRL = 0x10D
#byte PMDATH = 0x10E
#byte PMADRH = 0x10F

//Register Definitions - Bank 3
#byte ANSELA = 0x185
#byte ANSELB = 0x186
#byte ANSELD = 0x188
#byte ANSELE = 0x189
#byte PMCON1 = 0x18C

//PORT Definitions - PORTA
#define ZOUT      PIN_A0
#define YOUT      PIN_A1
#define XOUT      PIN_A2
#define TESTPIN1   PIN_A3
#define SELFTEST      PIN_A4
#define VCAP      PIN_A5
#define OSC2      PIN_A6
#define OSC1      PIN_A7

//PORT Definitions - PORTB
#define IRQ         PIN_B0
#define CPT         PIN_B1
#define CPI         PIN_B2
#define CPM         PIN_B3
#define CPR            PIN_B4
#define CPB         PIN_B5
#define PGC         PIN_B6
#define PGD         PIN_B7

//PORT Definitions - PORTC
#define PWRON      PIN_C0
#define SPEED      PIN_C1
#define CPTLED      PIN_C2
#define SCK         PIN_C3
#define SDI         PIN_C4
#define SDO         PIN_C5
#define TXD         PIN_C6
#define RXD         PIN_C7

//PORT Definitions - PORTD
#define CSN         PIN_D0
#define CE         PIN_D1
#define READY      PIN_D2
#define INVALID      PIN_D3
#define CPILED      PIN_D4
#define CPMLED      PIN_D5
#define CPRLED      PIN_D6
#define CPBLED      PIN_D7

//PORT Definitions - PORTE
#define GSEL      PIN_E0
#define 0gDET      PIN_E1
#define SLEEP      PIN_E2
#define MCLR      PIN_E3


//define execution mode
#define debug   1 //development mode ON
#define sens_mode 1 //sensing mode
               //0 = receive only
               //1 = capacitance
               //2 = acceleration


#ifndef bool
#define bool unsigned char
#endif
#ifndef false
#define false 0
#endif
#ifndef true
#define true !false
#endif

//FUNCTION DEFINITIONS OF UTILITY ROUTINES///////////////////////
static char BCDNyb2Chr (unsigned char BCDNyb);
static char BCDChr2Nyb (unsigned char BCDChr);
char* h2ha(unsigned long data, char *buffer, unsigned char len);
static char* h2da(unsigned long data, char *buffer, unsigned char len);


//FUNCTION DEFINITIONS OF COMMUNICATIONS ROUTINES////////////////
void enable_uart(unsigned char uart_int);
void disable_uart(void);

unsigned char coms_tx (unsigned char i);
void coms_tx_str (char *str);

//VARIABLE DEFINES USED IN CAP SENS ROUTINES/////////////////////////
#define NUM_BTTNS      5

//FUNCTION DEFINITIONS OF CAP SENS ROUTINES/////////////////////////
void init_cap(void);

//FUNCTION DEFINITIONS OF MAIN ROUTINES/////////////////////////////
void initialize_pic(void);
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Jul 26, 2009 12:42 am     Reply with quote

You have a large number of things that are wrong with your program.

You're using direct register access unnecessarily, when you should be
using CCS functions, or CCS library code invoked with a #use statement.
You're using illegal function parameters for some CCS functions.
You're enabling Global interrupts inside an isr.
There is way too much code in your program that is unnecessary.

Here is an example of a program that uses Timer1 interrupts to blink
an LED on Pin B0 at approximately a 1 Hz rate.
Code:

#include <16F877.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)

#int_timer1
void timer1_isr(void)
{
output_toggle(PIN_B0);
}

//======================================
void main()
{
output_low(PIN_B0);

setup_timer_1(T1_INTERNAL | T1_DIV_BY_8);
set_timer1(0);

clear_interrupt(INT_TIMER1);
enable_interrupts(INT_TIMER1);
enable_interrupts(GLOBAL);


while(1);
}


Here is an example of a "Hello World" program.
Code:
#include <16F877.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

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

printf("Hello World\n\r");

while(1);
}


Note how simple and easy it is when you use CCS functions and library code.
taniyyus



Joined: 25 Jul 2009
Posts: 3

View user's profile Send private message

PostPosted: Sun Jul 26, 2009 10:55 am     Reply with quote

Thank you for your reply.
I am actually used to using C18 and HiTech, recently moved to CCS.

Could you be kind enough to point out, illegal function parameters for some CCS functions.

What you saw of my in only a small portion, it has multiple other routines to handle RS232 and wireless radio communication, spi to control radio, capacitive sensing, etc.

Could you also explain why is it not recommended to enable global interrupts inside the isr?

Many thanks,

Taniyyu
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Jul 26, 2009 11:14 am     Reply with quote

Quote:

Could you be kind enough to point out, illegal function parameters for
some CCS functions.

These lines use illegal parameters. The parameters are not allowed to be
OR'ed together for these functions. Only a single parameter is allowed.
Quote:
CLEAR_INTERRUPT(GLOBAL|INT_RTCC|INT_RB|INT_EXT|INT_AD|INT_TBE|INT_RDA|INT_TIMER1|INT_TIMER2|INT_CCP1|INT_CCP2|INT_SSP|INT_TIMER0|INT_RB0|INT_RB1|INT_RB2|INT_RB3|INT_RB4|INT_RB5|INT_RB6|INT_RB7);
DISABLE_INTERRUPTS(GLOBAL|INT_RTCC|INT_RB|INT_EXT|INT_AD|INT_TBE|INT_RDA|INT_TIMER1|INT_TIMER2|INT_CCP1|INT_CCP2|INT_SSP|INT_TIMER0|INT_RB0|INT_RB1|INT_RB2|INT_RB3|INT_RB4|INT_RB5|INT_RB6|INT_RB7);



Quote:
Could you also explain why is it not recommended to enable global interrupts inside the isr?

Because nested interrupts are not supported. Also, the PIC will
automatically re-enable Global interrupts at the end of the CCS
interrupt handler code. It executes a RETFIE instruction which
does this.
Guest








PostPosted: Sun Jul 26, 2009 12:57 pm     Reply with quote

Thank you for your suggestion. It certainly behaves better with your suggestions. So I have decided to convert the whole project to use CCS functions. Now, please bear with me because I am recent convert, some of the question I ask might be irrelevant and illogical.

Now I have a few questions:
I need to the control over most of the peripherals so it is essential that I can turn these on/off and also have access to the interrupt flags/enable bits.

So my question are,
1.what is the difference in using #use SPI (.....) or #use RS232 (....) and setup_SPI(...) or setup_uart(....) respectively.

2. how do i
a)disable SPI
b) enable/disable tramit/receive interrupt (TXIE,TXIF.RCIE,RCIF) on UART
c) change/add defines to the 16F727.h or any other devices' header files.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Jul 26, 2009 1:35 pm     Reply with quote

So my question are,
Quote:

1.what is the difference in using #use SPI (.....) or #use RS232 (....) and
setup_SPI(...) or setup_uart(....) respectively.

#rs232() invokes the CCS "library" code to use the hardware UART
or a software UART (done by "bit-banging").
setup_uart() is used to change the baudrate of the hardware UART
at runtime. This is in the CCS manual.

#use spi() invokes the CCS library routines for SPI. However, this
method is new. It may still have some bugs, depending upon your
compiler version. The default settings of the #use spi() library code
are not always the normal expected settings. To ensure the correct
settings are used, you must specify more parameters than perhaps
you expected to. But, #use spi() has the ability to do software SPI
in addition to hardware SPI. The spi_xfer() function is used with
the #use spi() library, to transmit and receive data.

setup_spi() is the older, more reliable method of using the hardware
SPI module. spi_write() and spi_read() are used to transfer data
with this method.


Quote:

2. how do i

a)disable SPI

This line will clear the SSPCON1 register, disabling hardware SPI:
Quote:
setup_spi(FALSE);

However the TRIS settings for the SDO and SCLK pins will be left as
output pins. In other words, setup_spi() with the correct parameters
will configure the TRIS. But setup_spi(FALSE) does not restore all SPI
pins to input mode.


Quote:
b) enable/disable tramit/receive interrupt (TXIE,TXIF.RCIE,RCIF) on UART

Use these lines:
Code:
enable_interrupts(INT_TBE);
disable_interrupts(INT_TBE);
enable_interrupts(INT_RDA);
disable_interrupts(INT_RDA);

This is in the manual. The list of constants used to enable/disable
interrupts is at the end of the .h file for your PIC.


Quote:
c) change/add defines to the 16F727.h or any other devices' header files.

Don't do that. Don't change any #defines in that file. If you do, the
CCS functions may work correctly anymore. You will be sabotaging
your program.

Download the CCS manual and study the functions in it.
http://www.ccsinfo.com/downloads/ccs_c_manual.pdf
taniyyus



Joined: 25 Jul 2009
Posts: 3

View user's profile Send private message

PostPosted: Tue Jul 28, 2009 9:51 pm     Reply with quote

I am trying to convert my project to use only CCS functions now.
Could you help me with the following:

1. How do I use the the following to check if my OSC is stable:
Code:

setup_oscillator(OSC_STATE_LOCKED);
setup_oscillator(OSC_STATE_STABLE 8);

are the above lines correct?

2. How do I configure APFCON register?
3. How do I configure PortB Interrupt on Change (IOCB)?
4. How do I configure ANSELx?

5. I usually poll TRMT (TXSTA,0) to see if the UART is ready to transmit again, how can I achieve this with CCS functions.

I understand that I have many questions. But with a little help, I can get used to the compiler and understand it better.

I also have questions regarding the Capacitive Sensing Module but I will leave that for later on a new thread.

Many thanks in advance.

Regards,

Taniyyus
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Jul 29, 2009 1:18 pm     Reply with quote

Quote:

1. How do I use the the following to check if my OSC is stable:

setup_oscillator(OSC_STATE_LOCKED);
setup_oscillator(OSC_STATE_STABLE 8);

are the above lines correct?

No. The 16F727.h file says in oscillator section:
Quote:
// Constants used in setup_oscillator() are:
.
.
.
#define OSC_8MHZ 0x120
// Result may be (ignore all other bits)
#define OSC_STATE_LOCKED 4
#define OSC_STATE_STABLE 8

"Result" means the return value of the function, not the parameter.
"Ignore all other bits" means you must use those constants to mask
down the return value. Then test if those bits are set. Example:
Code:
#include <16F727.H>
#fuses INTRC_IO, NOWDT, PLLEN
#use delay(clock=8000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

//====================================
void main()
{
int8 i;

// Allow up to 100 ms for internal oscillator to lock and stabilize.
for(i = 0; i < 100; i++)
   {
    if(setup_oscillator(OSC_8MHZ) & (OSC_STATE_LOCKED | OSC_STATE_STABLE))
       break;
    delay_ms(1);
   }

while(1);
}



Quote:
2. How do I configure APFCON register?

Look up the register address in this section of the 16F727 data sheet:
Quote:
FIGURE 2-6: PIC16F726/LF726 AND PIC16F727/LF727 SPECIAL FUNCTION REGISTERS

Declare the address of the APFCON register with a #byte statement in
a line of code above main(). Then within main() or in another function,
set the APFCON register to whatever 8-bit value you desire, with one line
of code.

Quote:
3. How do I configure PortB Interrupt on Change (IOCB)?

Same as above.

Quote:
4. How do I configure ANSELx?

Use the setup_adc_ports() function. Look in the 16F727.h file for the
constants that are used as parameters for that function. Individual
"sANx" constants may be bitwise-OR'ed together to configure multiple
i/o pins as analog pins.

Quote:

5. I usually poll TRMT (TXSTA,0) to see if the UART is ready to transmit
again, how can I achieve this with CCS functions.

You would have to define the bit address of the TRMT bit with a #bit
statement, and then test TRMT with a line of code. But, the CCS putc()
function tests the TXIF bit in a loop before it transmits data, so you don't
need to test TRMT. Here's the putc() code:
Quote:
.......... putc(0x55);
0021: MOVLW 55
0022: BTFSS PIR1.TXIF
0023: GOTO 022
0024: MOVWF TXREG

You can study what the compiler does by compiling a short test program
and then examine the .LST file. That's where the code above came from.
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