Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

internal RC problem in PIC16F630

Status
Not open for further replies.

srevish

Member level 2
Joined
Apr 23, 2013
Messages
48
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,661
HI,

I am using PIC16F630 for my project.

I have programmed the PIC to operate the relay at 4min (240 seconds), but the problem is PIC is operating the relay in time deviation of 20 seconds ( some IC will operates in 260 seconds and some will operate in 230 seconds) , also some IC is operating exactly in 240 seconds, I am having the timing variations from one PIC to another.

For the same program , I am having different results in all different ICS. Can any one help me to solve this problem.
 

Why are you using the internal RC oscillator when there is a factory adjusted internal clock generator in these devices? If you use the 4MHz internal oscillator the timing will be much more accurate and you use fewer components as well !

Brian.
 

Why are you using the internal RC oscillator when there is a factory adjusted internal clock generator in these devices? If you use the 4MHz internal oscillator the timing will be much more accurate and you use fewer components as well !

Brian.

Dear Sir,

The system clock is 20MHz, I used the prescaler 1:1, so that clock speed will be 5MHz,

How to use the 4MHz internal oscillator. Please guide me since this is the first time I am working on this project.
 

If you are trying to use the RC oscillator at 20MHz I'm surprised you managed to get it working at all.

The RC oscillator is intended to be used where timing isn't critical and cost have to be as low as possible. If you need accurate timing you can use the internal clock or for very precise timing you use an external crystal. Can you show your schematic and code please, it sounds like you are doing things the hard way when there is an easier solution.

Brian.
 

If you are trying to use the RC oscillator at 20MHz I'm surprised you managed to get it working at all.

The RC oscillator is intended to be used where timing isn't critical and cost have to be as low as possible. If you need accurate timing you can use the internal clock or for very precise timing you use an external crystal. Can you show your schematic and code please, it sounds like you are doing things the hard way when there is an easier solution.

Brian.

Dear Sir,

I have given the code and attached the schematic in this please make a note of it.
Code:
 /* Buildmet project 27-12-06. Modified for Dryspell by KS Natarajan. Additions tower drying LED and timing change
   Suitable for PCB DSP Vr 1.11
   WHEN switch is open pin 13 is 5 volt. The dew point poor-->cycle not stretched-->the cycle will be continued
   WHEN switch is shorted pin 13 is 0 volt the dew point good-->cycle stretch -->the cycle will hold at the present state
// Progarm timings changed for UL controller model DS31-200. modification done by vignesh on 15/12/2010
*/
#include<htc.h>
#define		PB(p,b) 		((unsigned)&(p)*8+(b))
#define		gen_init()		PORTA=0,PORTC=0,TRISA=0x0c,TRISC=0,CMCON=0x07,OSCCAL=0xff,OPTION=0X80,\
							INTCON=0,IOCA=0,PIE1=0;T1CON=0x00,VRCON=0; 

bit 	dew_input 					@	PB(PORTA,2);	//
bit		switch1						@	PB(PORTA,3);	//
bit 	PIN2_led1 					@	PB(PORTA,5);	//
bit		PIN3_led2					@	PB(PORTA,4);	//
bit		PIN5_led6					@	PB(PORTC,5);
bit		PIN10_led9					@	PB(PORTC,0);
bit		Dewpoint_Switch					@	PB(PORTA,0); // This is actually pin 13
bit		PIN12_led10					@	PB(PORTA,1);	//
bit		PIN6_40_percent_led3		@	PB(PORTC,4);
bit		PIN7_60_percent_led4		@	PB(PORTC,3);	//
bit		PIN8_80_percent_led5		@	PB(PORTC,2);	//
bit		PIN9_100_percent_led7		@	PB(PORTC,1);	//
#define     preload_tmr1()  TMR1H=0x15,TMR1L=0x50; 	//0.5 sec delay
#define     init_tmr1()   	T1CON=0x00,PIE1=0x01,PEIE=1;
#define     enable_tmr1  	TMR1ON=1;
#define 	disable_tmr1 	TMR1ON=0;
unsigned char sw_count,count,data,tensec_count,sec_count;
unsigned int min_count,min_5_count;
bit sec_7_flag,dew_flag,init_flag,dec_flag;
void eeprom_write1(void)
{
    EECON1=0;
    EEIF=0;
    EEADR=0x05;
    EEDATA=sw_count;  //LS BYTES pulse_width DATA save
    WREN=1; 
    EECON2=0x55;
    EECON2=0xaa;
    WR=1;
    while(!EEIF);
    asm("nop");
}
void eeprom_read1(void)
{
  EEADR=0x05;
  EEDATA=0;
  EECON1=0;
  RD=1;
  while(RD);
  data=EEDATA;
}

void led_set1(void)
{
  PIN3_led2=0;
  PIN2_led1=0;
  PIN5_led6=0;
  PIN10_led9=0;     
  Dewpoint_Switch=1;
  PIN12_led10=0;
}
void led_set2(void)
 {
  PIN3_led2=0;
  PIN2_led1=1;
  PIN5_led6=0;
  PIN10_led9=1;    //PIN10(INLET VLAVE) changed from 0 to 1.on 15/12/2010 by vignesh due to programm for UL controller model DS31-200
  Dewpoint_Switch=0;
  PIN12_led10=1;
 }
void led_set3(void)
{
 PIN3_led2=1;
 PIN2_led1=0;
 PIN5_led6=0;
 PIN10_led9=1;    //PIN10(INLET VLAVE) changed from 0 to 1.on 28/12/2010 by vignesh due to programm for UL controller model DS31-200
 Dewpoint_Switch=0;
 PIN12_led10=1;
}
void led_set4(void)
{
  PIN3_led2=0;
  PIN2_led1=0;
  PIN5_led6=1;
  PIN10_led9=0;
  Dewpoint_Switch=0;
  PIN12_led10=0;
}
void led_set5(void)
 {
  PIN3_led2=0;
  PIN2_led1=0;
  PIN5_led6=0;
  PIN10_led9=0;     //PIN10(INLET VLAVE) changed from 1 to 0.on 28/12/2010 by vignesh due to programm for UL controller model DS31-200
  Dewpoint_Switch=1;
  PIN12_led10=0;
 }

void interrupt isr(void)
{
 if(TMR1IF)
  {
  TMR1IF=0;
  preload_tmr1();
  if(sec_count++>21)		//20
   {
   sec_count=0;
   if(!dew_flag) //If dew_flag is true it means dew point is good and timing will not be incremented. stretch
    min_count++;  // Dew point poor and timing incremented
   if(min_count>=240)
	min_count=0;
   }
  if(!switch1)
   count++;
  if(switch1)
   count=0;
  tensec_count--;
  if(tensec_count==0)
   {
	sec_7_flag=0;
	count=0;
   }
  if(count>=70)
   {
	sec_7_flag=1;
	tensec_count=200;
	count=0;
   }
  switch(sw_count)
   {
   case 1:
		 PIN6_40_percent_led3=1;
		 PIN9_100_percent_led7=0;
		 if(min_count<=2)
		 led_set5();
		 else if(min_count>2 && min_count<84)
		 led_set1();
		 else if(min_count==84)
		   {led_set1();
		     if(dew_input) {dew_flag=0;}
			else {dew_flag=1;}
		   }
		 else if(min_count>=85 && min_count<=120)
		  	led_set2();
		 else if(min_count>121 && min_count<204)
		  	led_set3();
		 else if(min_count==204)
		    {
			led_set3();
		     if(dew_input) {dew_flag=0;}
			else {dew_flag=1;}
		    }
		 else if(min_count>205 && min_count<=240 )
		 led_set4();         
		 break;	
	 
  case 2:
		 PIN7_60_percent_led4=1;
		 PIN6_40_percent_led3=0;
	if(min_count<=2)
		 led_set5();
		 else if(min_count>2 && min_count<66)
		 led_set1();
		 else if(min_count==66)
			 {
			led_set1();
		     if(dew_input) {dew_flag=0;}
			else {dew_flag=1;}
		   }
		 else if(min_count>=67 && min_count<=120)
		  	led_set2();
		 else if(min_count>121 && min_count<186)
		  	led_set3();
		 else if(min_count==186)
		     {
			led_set3();
		     if(dew_input) {dew_flag=0;}
			else {dew_flag=1;}
		     }
		 else if(min_count>187 && min_count<=240 )
		 led_set4();       
		 break;
		 
  case 3:
		 PIN8_80_percent_led5=1;
		 PIN7_60_percent_led4=0;
	     if(min_count<=2)
		 led_set5();
		 else if(min_count>2 && min_count<48)
		 led_set1();
		 else if(min_count==48)
		    {
			led_set1();
		     if(dew_input) {dew_flag=0;}
			else {dew_flag=1;}
		   }
		 else if(min_count>=49 && min_count<=120)
		  	led_set2();
		 else if(min_count>121 && min_count<168)
		  	led_set3();
		 else if(min_count==168)
		     {
			led_set3();
		     if(dew_input) {dew_flag=0;}
			else {dew_flag=1;}
		    }
		 else if(min_count>169 && min_count<=240 )
		 led_set4();    
		break;	
	 
  case 4:
		 PIN9_100_percent_led7=1;
		 PIN8_80_percent_led5=0;
		if(min_count<=2)
		 led_set5();
		 else if(min_count>2 && min_count<30)
		 led_set1();
		 else if(min_count==30)
		    {
			led_set1();
		     if(dew_input) {dew_flag=0;}
			else {dew_flag=1;}
		   }
		 else if(min_count>=31 && min_count<=120)
		  	led_set2();
		 else if(min_count>121 && min_count<150)
		  	led_set3();
		 else if(min_count==150)
		     {
			led_set3();
		     if(dew_input) {dew_flag=0;}
			else {dew_flag=1;}
		    }
		 else if(min_count>151 && min_count<=240 )
		 led_set4();
		 		 
		 break;
       }   		//end of switch
   }
}

void main(void)
{
 gen_init();
 init_tmr1();
 GIE=1;
 PIN3_led2=0;
 PIN2_led1=0;
 PIN5_led6=0;
 dew_flag=0;
 sec_7_flag=0;
 PIN10_led9=0;
 //Dewpoint_Switch=0;
 PIN12_led10=0;
 PIN6_40_percent_led3=0;
 PIN7_60_percent_led4=0;
 PIN8_80_percent_led5=0;
 PIN9_100_percent_led7=0;
 preload_tmr1();
 min_5_count=0;
 sw_count=4;
 init_flag=1;
 min_5_count=0;
 eeprom_read1();
 if(data==0xff)
  sw_count=4;
 else
  sw_count=data;
 enable_tmr1;
 
 while(1)
 {
 if(sec_7_flag||init_flag)
 	{
  while(tensec_count||init_flag)
  	  {
   	  init_flag=0;
   	  if(!switch1)
   		{
		while(!switch1);
	 	sw_count++;
		if(sw_count>=5)
     		sw_count=1;
	 	min_count=0;
	 	eeprom_write1();
	 	tensec_count=200;
	  	}
   	}
  	}
 }
}

Dry spell ul sch.png
 
Last edited by a moderator:

Well, to run it at 20MHz you have to drive an external oscillator into pin 2 -or- connect a quartz crystal or resonator between pins 2 and 3 and the schematic does not show either of these methods so I will assume you are actually using the 4MHZ internal clock and are not running at 20MHz at all. The config information is not present in the program listing so I cannot confirm this.

What you need to do is documented in the data sheet "9.2.5.1 Calibrating the Internal Oscillator" to ensure accuracy and I do not see this in the program. Take care to read and understand the warning about erasing the calibration number!

Brian.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top