|
|
View previous topic :: View next topic |
Author |
Message |
MLE Guest
|
ccp1 interrupt still not working |
Posted: Thu Aug 07, 2003 9:39 am |
|
|
Hello,
I cannot get the ccp1 interrupt to work. I am trying to get an interrupt every 0.5sec. I have an external clock input of 4096Hz, so I want timer1 to create a match at 2048. This is what I've tried to set up, but the interrupt routine is never triggered.
Here is my code (i've included the rtc.h file at the end)
----------------------------------------------------------------
#include "mylcd.h"
#include "rtc.h"
#include <string.h>
#fuses XT,NOWDT,NOPUT,NOBROWNOUT,NOLVP
#byte ADCON1 = 0X9F //The adcon1 register
#byte PORTA = 0x05
#byte PORTB = 0x06
#byte PORTC = 0x07
#byte PORTD = 0x08
#byte PORTE = 0x09
#bit CCP1_FLAG = 0x0C.2 //the capture/compare flag
/*----------FUNCTIONS-----------*/
/***********CCP1_int***********************/
/*This function handles the capture and */
/*compare inturrupt routine. */
/******************************************/
#INT_CCP1
CCP1_int(){
//Variables
int RTCsec, //seconds of the real-time-clock
RTCmin, //minutes of the real-time-clock
RTChour; //hour of the real-time-clock
lcd_clear();
readTime(&RTCsec,&RTCmin,&RTChour);
displayTime(RTCsec,RTCmin,RTChour);
}
/***************Main Program Starts Here*************/
void main(){
char line1[25];
/*----------configure all the ports-------*/
Set_Tris_A(0x00); //set port A to outputs
Set_Tris_B(0x33); //make RB5, RB4(BUTTONS),
//RB1 (DQin) and RB0(pulse in) inputs
Set_Tris_C(0xD7); //RC7, RC6 (for Hserin/Hserout)
//RC4(RTC),RC2, RC1, RC0 inputs
Set_Tris_D(0x00); //set port D to outputs
Set_Tris_E(0x00); //set port E to outputs
PORTA = 0;
PORTB = 0;
PORTC = 0;
PORTD = 0;
PORTE = 0;
/*-----------set PIC registers------------*/
ADCON1=0b00001110;
//write to rtc
writeRTC(9, 56, 4, 24, 7,3); //starts the clock and sets
//sqw freq. to 4096Hz
SETUP_CCP1(CCP_COMPARE_RESET_TIMER);//set to reset TMR1
//set the compare reg. to generate
//a match at 2048 (every .5 sec)
//since the clock input is at 4096 Hz
SET_PWM1_DUTY(2048);
SETUP_TIMER_1(T1_EXTERNAL_SYNC|T1_DIV_BY_1); //external clock
SET_TIMER1(0); //start timer 1 counting at 0
CCP1_FLAG = 0; //clear the ccp flag
ENABLE_INTERRUPTS(INT_CCP1); //enable clock output interrupt
ENABLE_INTERRUPTS(GLOBAL);
delay_us(500);
lcd_init();
lcd_clear();
strcpy(line1, "Interrupt Test"); // copy string
lcd_puts(line1);
delay_ms(1000);
while(1);
}//end of main()
----------------------------------------------------------------
#define SCLpin PIN_C3
#define SDApin PIN_C4
#define SYNC PIN_C0
#define I2C1307W 0xD0 //the i2c control for writing to the rtc
#define I2C1307R 0xD1 //the i2c control for reading to the rtc
#define RTCCtrl 0x11 //this sets the clock output to 4096 per second
#use i2c(master,sda=SDApin, scl=SCLpin) //specific for the clock read and writes
/************convert2BCD*******************/
/*This function converts a two digit int */
/*to its BCD form. */
/* */
/*PARAMETERS: */
/* DEC - the integer to be */
/* converted */
/*RETURNS: */
/* BCD - the binary coded dec. */
/* format of DEC */
/******************************************/
int convert2BCD(int DEC){
int BCD;
BCD = DEC/10; //get the first digit
BCD = BCD<<4;
BCD = BCD + (DEC\%10); //add the second digit
return BCD;
}
/************convert2BIN*******************/
/*This function converts a bcd number to */
/*its binary form. */
/* */
/*PARAMETERS: */
/* BCD - the number to be */
/* converted */
/*RETURNS: */
/* BIN - the binary form of the */
/* number */
/******************************************/
int convert2BIN(int BCD){
int BIN;
BIN=((BCD>>4)&0x0f)*10+(BCD&0x0f);
return BIN;
}
/************writeRTC**********************/
/*This function writes the specified time */
/*and date to the rtc */
/* */
/*PARAMETERS: */
/* seconds - seconds of the rtc */
/* minutes - minutes of the rtc */
/* hours - hours of the rtc */
/* days - date of the rtc */
/* month - month of the rtc */
/* year - the year of the rtc */
/* (0=
/*RETURNS: */
/******************************************/
void writeRTC(int seconds, int minutes, int hours, int days, int month,
int year) {
//convert everything to BCD before writing to the clock
seconds = convert2BCD(seconds);
minutes = convert2BCD(minutes);
hours = convert2BCD(hours);
days = convert2BCD(days);
month = convert2BCD(month);
year = convert2BCD(year);
disable_interrupts(GLOBAL);
I2C_start();
I2C_write(I2C1307W);
I2C_write(0x00); //starting address (the seconds register)
I2C_write(seconds); //write the seconds
I2C_write(minutes); //write the minutes
I2C_write(hours); //write the hours
I2C_write(0x00); //this is the weekday, but we don't use it
I2C_write(days); //write the date
I2C_write(month); //write the month
I2C_write(year); //write the year
I2C_write(RTCctrl); //write the control register
I2C_stop();
enable_interrupts(GLOBAL);
}
/************readTime**********************/
/*This function reads the seconds, minutes*/
/*and hours from the rtc */
/* */
/*PARAMETERS: */
/* sec - seconds of the rtc */
/* min - minutes of the rtc */
/* hour - hours of the rtc */
/*RETURNS: */
/******************************************/
void readTime(int * sec, int * min, int * hour){
disable_interrupts(GLOBAL);
I2C_Start();
I2C_write(I2C1307W);
I2C_write(0x00); //starting address
I2C_Start();
I2C_write(I2C1307R);
* sec=I2C_read(); //read the seconds
* min=I2C_read(); //read the minutes
* hour=I2C_read(0); //read the hours
I2C_stop();
enable_interrupts(GLOBAL);
}
/************displayTime*******************/
/*This function displays the time on the */
/*lcd screen */
/* */
/*PARAMETERS: */
/* sec - seconds to be displayed */
/* min - minutes to be displayed */
/* hour - hours to be displayed */
/*RETURNS: */
/******************************************/
void displayTime(int sec, int min, int hour){
int RTCsec,RTCmin,RTChour;
char string[7], seconds[3], minutes[3], hours[3], colon[2];
//convert time to binary
RTCsec=convert2BIN(sec);
RTCmin=convert2BIN(min);
RTChour=convert2BIN(hour);
//convert numeric values to strings
sprintf(seconds,"\%d",RTCsec);
sprintf(minutes,"\%d",RTCmin);
sprintf(hours,"\%d",RTChour);
strcpy(string, "Time "); // copy string
strcpy(colon, ":");
//display on LCD
lcd_clear();
lcd_puts(string);
lcd_puts(hours);
lcd_puts(colon);
lcd_puts(minutes);
lcd_puts(colon);
lcd_puts(seconds);
}
---------------------------------------------------------------
Thank you,
Emily
___________________________
This message was ported from CCS's old forum
Original Post ID: 144516786 |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
Re: ccp1 interrupt still not working |
Posted: Fri Aug 08, 2003 2:19 pm |
|
|
:=Hello,
:=I cannot get the ccp1 interrupt to work. I am trying to get an interrupt every 0.5sec. I have an external clock input of 4096Hz, so I want timer1 to create a match at 2048. This is what I've tried to set up, but the interrupt routine is never triggered.
:=SETUP_CCP1(CCP_COMPARE_RESET_TIMER);//set to reset TMR1
:=
:=SET_PWM1_DUTY(2048);
-------------------------------------------------------------
I don't think you should use the SET_PWM1_DUTY(2048) function
to setup the period. It doesn't write to the CCPR1H register.
So do the following:
Put these two lines above main():
#byte CCPR1L = 0x15
#byte CCPR1H = 0x16
Then comment out the SET_PWM1_DUTY(2048) line, and substitute
these two lines instead:
CCPR1L = 0;
CCPR1H = 0x08;
//SET_PWM1_DUTY(2048);
This will set the 16-bit CCPR1 register to 0x0800, which is
2048. Now at least you will have the CCPR1 register set to
the correct value. I don't know if this will make your whole
program work, but you'll be closer to making it work.
___________________________
This message was ported from CCS's old forum
Original Post ID: 144516844 |
|
|
|
|
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
|