|
|
View previous topic :: View next topic |
Author |
Message |
dani
Joined: 02 Jun 2004 Posts: 16 Location: Pakistan
|
PIC16f676 Internal Oscillator problem. |
Posted: Mon Jun 09, 2008 5:37 am |
|
|
Hello all,
I have written a code to generate 50 Hz flip flop pulses through PIC16F676. I have used internal oscillator for this. The results are not coming correct in terms of pulse timings(width). it seems that some pic after programming work well and on another it doesn't. Is this due to internal oscillator or am i doing some thing wrong some where . Plz Help.
Regards
Dani |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Jun 09, 2008 1:45 pm |
|
|
Post a short but complete (and compilable) program that shows the
problem. Also post your compiler version. |
|
|
dani
Joined: 02 Jun 2004 Posts: 16 Location: Pakistan
|
|
Posted: Tue Jun 10, 2008 6:55 am |
|
|
Code: |
#include "D:\CURRENT_PROJECTS\UPS\pp676.h"
#define BttlowInd PIN_C5
#define RelayBttChrg PIN_C2
#define UPSLED PIN_C3
#define LEdWapda PIN_A4
#define PUSH PIN_C4
#define PULL PIN_C1
#define WapdaIn PIN_A5
#define Buzzer PIN_A3
#define FAN PIN_C0
#define BttCh 1
#define MainCh 0
int1 Flg,WapdaFlg=0,FlgChrg,FlgUps=0,chrgFlg=0,tflg = 0,jOflg = 0,shutFlg = 0,sflg=0,BFLG1=0,BFLG2=0,bFlg3=0;
int16 BttVolt,DelayTime,DTT=2400,Ltime=0,dt,chrgCounter=0,d,sCounter = 0,shutcounter=0,bcounter=0,blCounter=0;
INT8 BUZZFLG = 0;
void SetBttVolt(void);
void WapdaCheck(void);
void doWapdaAbsent(void);
void doWapdaPresent(void);
void SetDelayTime(void);
void doShutDown(void);
#int_TIMER1
void TIMER1_isr(void)
{
scounter++;
d++;
if((WapdaFlg==0) && (FLgUps==1)){ // WAPDA IS ABSENT BUT UPS IS WORKING DUE TO BATT VOLTAGE ABOVE 2.0
// printf("%ld,%ld\r",ltime,dtt);
if (LTime==0)
LTime = 1;
dt = dTT - 3000;
if(dTT >= 3000){
output_high(PUSH);
delay_us(dt);
}
output_low(PUSH);
delay_us(Ltime);
delay_us(100); //dead gap at end of c2
output_low(PULL);
delay_us(Ltime);
delay_us(100); //dead gap at end of c2
output_high(PULL);
delay_us(2000); // fix 5ms high time
delay_us(dT);
output_low(PULL);
delay_us(Ltime);
delay_us(100); //dead gap at end of c2
delay_us(Ltime);
delay_us(100); //dead gap at end of c2
output_high(PUSH);
}
else{
output_low(PUSH);
output_low(PULL);
}
set_timer1(63533); // fix 3ms delay
IF(BUZZFLG>0)
{
BUZZFLG--;
OUTPUT_HIGH(BUZZER);
bflg1 = 1;
bflg2 =1;
bflg3 = 1;
}ELSE{
OUTPUT_LOW(BUZZER);
bcounter++;
//printf("%ld\r",bcounter);
}
}
void main()
{
int16 i=0;
Flg = 0;
FlgUps=0;
WapdaFlg = 0;
set_tris_a(0b100011);
output_c(0);
output_low(pin_a2);
output_low(pin_a3);
output_low(pin_a4);
setup_adc_ports(sAN0|sAN1|VSS_VDD);
setup_adc(ADC_CLOCK_DIV_32);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);
setup_comparator(NC_NC);
setup_vref(FALSE);
output_high(LEDWAPDA);
// output_high(UPSled);
//output_low(pin_a4);
If(!input(WAPDAIN)){
wapdaFlg = 1;
Flgups = 0;
enable_interrupts(INT_TIMER1);
enable_interrupts(GLOBAL);
}else{
wapdaFlg = 0;
Flgups = 1;
SetDelayTime();
enable_interrupts(INT_TIMER1);
enable_interrupts(GLOBAL);
while(d<100){
SetDelayTime();
}
}
/* for(i=10;i>1;i--){
SetBttVolt();
WapdaCheck();
if (WapdaFlg){
doWapdaPresent();
}else{//if waPDA IS ABSENT
doWapdaAbsent();
}
delay_ms(1);
Ltime = 2400;
dTT = 5000;
}*/
while (1){
SetBttVolt();
WapdaCheck();
if(shutFlg == 0){
if (WapdaFlg){
doWapdaPresent();
bflg1=0;
//bflg3=0;
}else{//if waPDA IS ABSENT
doWapdaAbsent();
bflg2=0;
}
}else
{
doShutDown();
}
SetDelayTime();
}//while
}
void SetDelayTime(){
int16 t;
INT16 Tempf;
set_adc_channel(MAINCH);
delay_us(20);
t = read_adc();
If (t <= 327){ // 1.0v for 9.8ms pulse
DelayTime = 9800;
}Else if (t >= 491){ // 2.0v for 3.0ms pulse
DelayTime = 3000;
}Else{
t = 491 - t;
TempF = t * 41; // conversion for 245 to 6800ms
DelayTime = 3000 + TempF;
}
DTT = DELAYTIME;
LTIME = (9800 - DTT)/2;
// printf("%ld\r",dtt);
}
void WapdaCheck(){
If(!input(WAPDAIN)){ // 'Wapda present
while( d < 100 && !input(WAPDAIN)){
// printf("%ld\r",d);
}
if(d>=100){
WapdaFlg = 1;
flg = 0;
output_low(PUSH);
output_low(PULL);
output_Low(BttLowInd);
output_LOW(LedWapda);
shutFlg = 0;
d=101;
// printf("-----------0\r");
}
}Else{
WapdaFlg = 0;
d=0;
output_Low(RelayBttChrg);
output_HIGH(LedWapda);
output_low(FAN);
// printf("-----------1\r");
}
}
void SetBttVolt(){
set_adc_channel( BTTCH );
delay_us(20);
BttVolt = read_adc();
}
void doWapdaAbsent(){
if(bflg1==0)
{
buzzflg = 500;
}
jOflg = 0;
chrgcounter=0;
output_Low(RelayBttChrg);
output_LOW(UPSLED);
output_high(FAN);
chrgflg = 0;
chrgCounter=0;
FlgUps = 1;
// printf("-----------b\r");
If (BttVolt < 368){ // 'Battery is too low. At 10v (on ADC 1.8) shutdown UPS
shutcounter++;
if(shutcounter>100){
shutcounter=0;
output_HIGH(UPSLED);
output_low(buzzer);
shutFlg = 1;
}
}else if(BttVolt < 409 ){ // '21v (on ADC 2.1v) Low battery indication
blcounter++;
if(blcounter>100){
blcounter=0;
output_high(BttLowInd);
shutcounter=0;
if(bflg3==0)
{
buzzflg = 500;
bcounter = 0;
}else if(bcounter > 1000)
{
bcounter=0;
bflg3=0;
}
}
}else{
shutcounter=0;
blcounter=0;
}
}
void doWapdaPresent(){
if(bflg2==0)
{
buzzflg = 250;
}
output_High(UPSLED);
// printf("-----------c,%ld\r",bttvolt);
If (BttVolt > 572){ // 'Voltage exceeds 27v (on ADC 2.7v) then cut charging
output_Low(RelayBttChrg);
output_low(FAN);
chrgflg==0;
}
Else If (BttVolt < 491 && joFlg ==1 ){ // 'Voltage 12v when charging starts
output_High(RelayBttChrg);
output_high(FAN);
output_low(BttLowInd);
chrgflg=1;
}Else If (BttVolt < 572 && joFlg ==0 ){ // 'Voltage 26v when charging starts
chrgCounter++;
// printf("**chc = %ld\r",chrgCounter);
if(chrgflg==0 && chrgCounter>10000){
output_High(RelayBttChrg);
output_high(FAN);
output_low(BttLowInd);
chrgFlg=1;
jOflg=1;
chrgcounter=10001;
}
}
}
void doShutDown(){
FLgUps=0;
jOflg = 0;
chrgcounter=0;
shutcounter=0;
if(sflg == 0 && scounter > 100)
{
output_high(bttLowInd);
scounter = 0;
sflg = 1;
//printf("-----------\r");
}else if(sflg == 1 && scounter > 100){
output_low(bttLowInd);
scounter = 0;
sflg = 0;
// printf("***************\r");
}
}
|
The above is the complete code for a ups opreation.
But my questin was bit general in a sense that may be
1 - I dont know how to use internall oscilator or How to calibrate it.
2 - If i use Timer1 interrupt to generate fixed interval interrupt for generating 50Hz flip folp pulses. Is it possible in 16f676 with internall oscilator or not.
Because
3- I have used the same code in 16f877a with external crystal oscialator and results are seme with evey chip, But this is not the case while is use 16f676 and internall oscillator. So where i am doing wrong.[/quote] |
|
|
mskala
Joined: 06 Mar 2007 Posts: 100 Location: Massachusetts, USA
|
|
Posted: Tue Jun 10, 2008 7:23 am |
|
|
Without looking at your code, which also is incomplete since there are no fuse assignments, my comment is really what is your expectation? I don't know what you mean by every chip is different. The internal oscillator is only calibrated to +/- 1%, which is hundreds of times worse than you can do with external. Are you seeing 2% difference in frequency? There is a calibration register that you can use if necessary, but I still doubt it can match external, especially over temperature. |
|
|
|
|
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
|