|
|
View previous topic :: View next topic |
Author |
Message |
Eleengnrng
Joined: 21 Apr 2006 Posts: 6
|
RTC Issue with PIC16F877A |
Posted: Wed Sep 13, 2006 2:17 pm |
|
|
Ok here is the issue i am using a 20Mhz clock and i am using timer one on to count the number of clock cycles to equal 1 second. no problem there. once a complet minute happens it enter a function that decraments the a value and displays on a seven gegment display. the issue is that some where when i tell it to start to count down it hangs up when switching back and forth from some set conditions. BELOW IS A BUNCH OF CODE. HELP IS NEED FAST.
Code: |
#include <16F877A.h>
#device adc=8
#FUSES NOWDT, HS, PUT, NOPROTECT, NODEBUG, NOBROWNOUT, NOLVP, NOCPD, NOWRT
#use delay(clock=20000000)
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdlibm.h>
#define Timer_LOC 0x00 // Timer data Location 5
#define Temp_LOC 0x06 // Temp Data Location 10 EEprom
#define XTAL_FREQUENCY 20000000
#define TIMER1_FREQUENCY (XTAL_FREQUENCY / 4) // 1 clock tick = 1 instr. cycle = crystal frequency / 4
/**************************************************************************/
/* Global Variables */
/**************************************************************************/
int8 STSP=0,digit,sec=0,tempsetp=0,tempsetpm,acttemp=0;
int8 offsethigh,offsetlow,timera,timerb,timerval = 0;
int32 Ticker;
int8 Seconds=0, Choice=1, rawtemp;
int32 dispR = 458752, dispH = 1245184;
/**************************************************************************/
/* Functions & Function Delcarations */
/**************************************************************************/
void Initialize_RTC(void)
{
if (Choice == 1)
{
Ticker = TIMER1_FREQUENCY-dispR; // initialize clock counter to number of clocks per second
}
if (Choice == 2)
{
Ticker = TIMER1_FREQUENCY-dispH; // initialize clock counter to number of clocks per second
}
setup_timer_1( T1_INTERNAL | T1_DIV_BY_1 ); // initialize 16-bit Timer1 to interrupt
// exactly every 65536 clock cycles
// (about 76 times per second)
enable_interrupts( INT_TIMER1 ); // Start RTC
}
void Scroll_MSG(); // look at end of the code beyond Main Program
void Ready_MSG(); // look at end of the code beyond Main Program
//void digman(int8 value, int8 dvalue); //look at end of code beyond main program
/**************************************************************************/
/* END Functions & Function Delcarations */
/**************************************************************************/
/**************************************************************************/
/**************************************************************************/
/* Interupts Section Begins */
/**************************************************************************/
#int_RB
void RB_isr(){
//MONITORS SOME BUTTONS
}//Port b on change int routine for temp setting
/*****************************************************************************/
/* External interupt */
/*****************************************************************************/
#int_EXT
Void Ext_isr(){
disable_interrupts(INT_EXT);
delay_ms(100);
digit=timerval;//input timer data
delay_ms(10);
If ((STSP==0)&&(input(PIN_B0)==0)){//if equal to 0 then its in start mode
if ((STSP == 0)&&(input(PIN_B0)==0)){
stsp=stsp+1;//increment stsp
digman(digit,0);//load dig manipulation function with timer data
//output_low(Pin_A4);//turn timer buzzer off
Initialize_RTC();
lcd_goto(2,16);
printf(write_LCD,"%c",0x78);
}
}else if ((STSP!=0)&&(input(PIN_B0)==0)){//if equal to 1 then its in stop mode
digman(digit,0);//load dig manipulation function with timer data
stsp=0;//set STSP back to start position
disable_interrupts( INT_TIMER1 ); // disable RTC
timerval = digit;
lcd_goto(2,16);
printf(write_LCD,"%c",0x20);
output_C(timerval);
output_low(Pin_A4);//turn off buzzer
}
enable_interrupts(INT_EXT);
}//External int routine for Timer
/*****************************************************************************/
/* Timer 1 Interupt */
/*****************************************************************************/
#int_TIMER1
void TImer1_isr()
{
disable_interrupts(INT_TIMER1);
Ticker -= 65536; // Decrement ticker by clocks per interrupt
if ( Ticker < 65536 ) // If second has expired
{ Ticker += TIMER1_FREQUENCY; // Increment ticker by clocks per second
seconds++; // Increment number of seconds
Initialize_RTC();
}
if (Choice == 1){
if((Seconds == 60)) {digman(0,1); Seconds=0;}
}
if (Choice == 2){
if((Seconds == 30)) {digman(0,1); Seconds=0;}
}
enable_interrupts(INT_TIMER1);
} // End of timer 1 interup
#int_TIMER2
Void TIMER2_ISR(){
disable_interrupts(INT_TIMER2);
rawtemp = tempact();
acttemp = rawtemp;
/* if (rawtemp<=70){
acttemp = rawtemp-10;
}else if (rawtemp>70){
acttemp = rawtemp;
}*/
/*rawtemp = tempact();
if (rawtemp > acttemp+3){
acttemp = tempact();
}else if ((rawtemp < acttemp-3) && (acttemp != 0)){
acttemp = tempact();
}**/
enable_interrupts(INT_TIMER2);
}
/***************************************************************************/
/* End Of Interupt Section */
//*************************************************************************//
//*************************************************************************//
//*************************************************************************//
//*************************************************************************//
/* Begin Main Program */
/***************************************************************************/
void main()
{
SET_TRIS_A (0x01); //set reg a for temp sensor input
SET_TRIS_B (0xf1); //Misc Data Pins
SET_TRIS_C (0x00); //Set Port C to Outputs
SET_TRIS_D (0x00); //LCD Data Port
SET_TRIS_E (0x00); //LCD Control Port
port_b_pullups(TRUE);
setup_adc_ports(RA0_ANALOG);
setup_adc(ADC_CLOCK_DIV_32);
setup_psp(PSP_DISABLED);
setup_spi(FALSE);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
//setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);
setup_timer_2(T2_DIV_BY_16,240,13);
ext_int_edge(H_TO_L);//activate on high to low
enable_interrupts(INT_RB);
enable_interrupts(INT_EXT);
enable_interrupts(INT_TIMER1);
enable_interrupts(INT_TIMER2);
enable_interrupts(GLOBAL);
LCD_init();//Intilize the LCD Display
set_adc_channel(0);//set A-D converter channel to 0
output_low(Pin_A4);//turn buzzer off
if (READ_EEPROM(Timer_LOC) != 0xff)
timerval = READ_EEPROM(Timer_LOC);
if (READ_EEPROM(Temp_LOC) != 0xff)
tempsetp = READ_EEPROM(Temp_LOC);
set_tris_c(0x00);//set port c for all outputs
output_c(timerval);
delay_ms(10);
While(True){
lcd_goto(1,1);
printf(write_LCD,"%3u",tempsetp);//Display the temperature set point
lcd_goto(1,4);
printf(write_lcd,"%c",0x0df); //degree symbol
lcd_goto(1,5);
printf(write_lcd,"%c",0x43); //Celcius
offsethigh=tempsetp+3;
if (tempsetp != 0)
{offsetlow=tempsetp-6;}
lcd_goto(1,12);
printf(write_LCD,"%3u",acttemp);//Display the temperature
lcd_goto(1,15);
printf(write_lcd,"%c",0x0df); //degree symbol
lcd_goto(1,16);
printf(write_lcd,"%c",0x43); //Celcius
if (STSP==0) {output_c(timerval);} //output Timer Val if timer not active
if (acttemp>=tempsetp&&acttemp<offsethigh){
output_low(Pin_A5);
Choice = 1;
ready_msg();
}else if (tempsetp == 0){
output_low(Pin_A5);
Choice = 1;
ready_msg();
}else if ((acttemp==tempsetp)||(acttemp>offsetlow)){
output_low(Pin_A5);
Choice = 1;
ready_msg();
}else if (acttemp<=offsetlow){
while (acttemp<=offsethigh){
output_high(Pin_A5);
Choice = 2;
scroll_msg();
lcd_goto(1,1);
printf(write_LCD,"%3u",tempsetp);//Display the temperature set point
lcd_goto(1,4);
printf(write_lcd,"%c",0x0df); //degree symbol
lcd_goto(1,5);
printf(write_lcd,"%c",0x43); //Celcius
offsethigh=tempsetp+3;
if (tempsetp != 0)
{offsetlow=tempsetp-6;}
lcd_goto(1,12);
printf(write_LCD,"%3u",acttemp);//Display the temperature
lcd_goto(1,15);
printf(write_lcd,"%c",0x0df); //degree symbol
lcd_goto(1,16);
printf(write_lcd,"%c",0x43); //Celcius
}
}
/* if ((decval == 0)&&(STSP == 1)){
output_high(PIN_A4);
delay_ms(8000);
}*/
if (read_eeprom(Timer_LOC) != timerval)
{write_eeprom(Timer_LOC,timerval);}
if (read_eeprom(Temp_LOC) != tempsetp)
{write_eeprom(Temp_LOC,tempsetp);}
}
} // End Of Main ***************************************************************
/***************************************************************************/
/* End Of Main */
//*************************************************************************//
//*************************************************************************//
//*************************************************************************//
//*************************************************************************//
/* Begin LCD Functions */
/***************************************************************************/
void Scroll_MSG(){
//DISPLAYS HEATING MESSAGE
}//eng of scroll function
void Ready_MSG(){
// DISPLAYS READY MESSAGE
}// End of Ready Function
|
I left out chunks of code becasue i think that it not part of my problem and i only want.
Last edited by Eleengnrng on Wed Sep 13, 2006 3:11 pm; edited 1 time in total |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
Eleengnrng
Joined: 21 Apr 2006 Posts: 6
|
|
Posted: Wed Sep 13, 2006 2:46 pm |
|
|
yes i have seen this code and modled my code after it. it works great the problem is that it seems to hang up when the main part of the function which i sum how cut out switchs betuween ready and scroling msg.
here is the missing Main Code.
Code: |
//*************************************************************************//
/* Begin Main Program */
/***************************************************************************/
void main()
{
SET_TRIS_A (0x01); //set reg a for temp sensor input
SET_TRIS_B (0xf1); //Misc Data Pins
SET_TRIS_C (0x00); //Set Port C to Outputs
SET_TRIS_D (0x00); //LCD Data Port
SET_TRIS_E (0x00); //LCD Control Port
port_b_pullups(TRUE);
setup_adc_ports(RA0_ANALOG);
setup_adc(ADC_CLOCK_DIV_32);
setup_psp(PSP_DISABLED);
setup_spi(FALSE);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
//setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);
setup_timer_2(T2_DIV_BY_16,240,13);
ext_int_edge(H_TO_L);//activate on high to low
enable_interrupts(INT_RB);
enable_interrupts(INT_EXT);
enable_interrupts(INT_TIMER1);
enable_interrupts(INT_TIMER2);
enable_interrupts(GLOBAL);
LCD_init();//Intilize the LCD Display
set_adc_channel(0);//set A-D converter channel to 0
output_low(Pin_A4);//turn buzzer off
if (READ_EEPROM(Timer_LOC) != 0xff)
timerval = READ_EEPROM(Timer_LOC);
if (READ_EEPROM(Temp_LOC) != 0xff)
tempsetp = READ_EEPROM(Temp_LOC);
set_tris_c(0x00);//set port c for all outputs
output_c(timerval);
delay_ms(10);
While(True){
lcd_goto(1,1);
printf(write_LCD,"%3u",tempsetp);//Display the temperature set point
lcd_goto(1,4);
printf(write_lcd,"%c",0x0df); //degree symbol
lcd_goto(1,5);
printf(write_lcd,"%c",0x43); //Celcius
offsethigh=tempsetp+3;
if (tempsetp != 0)
{offsetlow=tempsetp-6;}
lcd_goto(1,12);
printf(write_LCD,"%3u",acttemp);//Display the temperature
lcd_goto(1,15);
printf(write_lcd,"%c",0x0df); //degree symbol
lcd_goto(1,16);
printf(write_lcd,"%c",0x43); //Celcius
if (STSP==0) {output_c(timerval);} //output Timer Val if timer not active
if (acttemp>=tempsetp&&acttemp<offsethigh>offsetlow)){
output_low(Pin_A5);
Choice = 1;
ready_msg();
}else if (acttemp<=offsetlow){
while (acttemp<=offsethigh){
output_high(Pin_A5);
Choice = 2;
scroll_msg();
lcd_goto(1,1);
printf(write_LCD,"%3u",tempsetp);//Display the temperature set point
lcd_goto(1,4);
printf(write_lcd,"%c",0x0df); //degree symbol
lcd_goto(1,5);
printf(write_lcd,"%c",0x43); //Celcius
offsethigh=tempsetp+3;
if (tempsetp != 0)
{offsetlow=tempsetp-6;}
lcd_goto(1,12);
printf(write_LCD,"%3u",acttemp);//Display the temperature
lcd_goto(1,15);
printf(write_lcd,"%c",0x0df); //degree symbol
lcd_goto(1,16);
printf(write_lcd,"%c",0x43); //Celcius
}
}
/* if ((decval == 0)&&(STSP == 1)){
output_high(PIN_A4);
delay_ms(8000);
}*/
if (read_eeprom(Timer_LOC) != timerval)
{write_eeprom(Timer_LOC,timerval);}
if (read_eeprom(Temp_LOC) != tempsetp)
{write_eeprom(Temp_LOC,tempsetp);}
}
} // End Of Main ***************************************************************
/***************************************************************************/
/* End Of Main */
//*************************************************************************//
//*************************************************************************//
//*************************************************************************//
//*************************************************************************//
/* Begin LCD Functions */
/***************************************************************************/
|
Sorry for the length in code but this problem has been aggravating and persistant every time i think that i have fixed it it appears again. so there is something that i am missing. I am figureing that a new pair of eys will help the problem out. thanks for any assistance. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Sep 13, 2006 2:57 pm |
|
|
Can you edit your first post, and re-post the code, but this time click
on the box below the posting that says:
Code: | X Disable HTML in this post |
The reason is that if you leave HTML turned on, then all lines of code
that have "greater than", and "less than", etc., get messed up.
For example, the "TImer1_isr()" code is screwed up.
Also, after you've done that, can you post a short comment on
exactly what line of code does it hang up on. |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Wed Sep 13, 2006 3:28 pm |
|
|
You didn't post the code of your interrupt handlers. I see you enable 4 interrupts, please make sure you implemented functions for handling these interrupts. The compiler will not complain about a missing interrupt function, but when the interrupt occurs (accidently) it will not get cleared and loop forever; the PIC appears to stall. |
|
|
|
|
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
|