|
|
View previous topic :: View next topic |
Author |
Message |
Guard
Joined: 20 Jan 2005 Posts: 43
|
PIC16c781 AN4 little problem |
Posted: Wed Feb 02, 2005 3:25 am |
|
|
pcwh 3.217, Mplab 6.30
HI,
The An4 seems dosn't work.
This is part of my project:
If I load in the Dac NTC3(AN4) value for debug , on the dac pin I read 0,5V.On the NTC3 the voltage is 4.4V!
Do you know where is my error?
Thank!
main () {
TRISA=255;
TRISB=37;
WPUB=0;
REFCON=0; // VR reference is disabled
setup_adc_ports(sAN4|VSS_VDD);
setup_adc(ADC_CLOCK_DIV_32);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_128); setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DIV_BY_1, 64, 1);
disable_interrupts(INT_RB);
disable_interrupts(INT_RTCC);
disable_interrupts(GLOBAL);
setup_dac();
setup_comparators ();
setup_psmc();
do
{
Camp = calibrate_opamp_internal(); // Calibrazione opAMP
}
while ( !Camp);
Dac =51; // 1v REFERENCE
SMCON =1 ; // PSMC ON
Ntc3 = Read(4);
do {
if (Ntc3 < C70) // C70 = 2,5V
{
overload();
}
} while (1);
}
void setup_dac()
{
DARSL=0;
DARSH=0;
DAOE =1;
DAC =0;
DAON =1;
}
void setup_comparators (void)
{
// comparator 1
C1CHH =1; // AN6
C1CHL =0;
C1R =0;
C1POL=0;
C1SP =1;
C1OE =1;
C1ON =1;
// comparator 2
C2CHH =1; // AN6
C2CHL =0;
C2R =0;
C2SP =1;
C2POL=0;
C2OE =0;
C2ON =1; //
}
void setup_psmc(void)
{
SMCON =0 ;
DCH = 0; // non usato PSM
DCL = 0; // non usato
MAXDCH = 0; //50% D.C.
MAXDCL = 0;
MINDCH = 0; // 0% D.C.
MINDCL = 0;
SMCCLH = 0;
SMCCLL = 0;
SMCCS =1 ;
PWMPSM =1 ; // PWM MODE
SMCOM =0 ; // SINGLE OUTPUT
SCEN =0;
S1APOL =0;
S1BPOL =0;
}
boolean calibrate_opamp_internal(void)
{
CMPEN =0;
OPAON =1; // OP AMP
GBWP =0; // 70kHz CALREF=0;
delay_ms(20);
CAL=1; // Start calibration
while( bit_test(CALCON, 7) );
if (bit_test(CALCON, 6)) // check for error
return(false);
else
return(true);
} |
|
|
Ttelmah Guest
|
|
Posted: Wed Feb 02, 2005 9:07 am |
|
|
You are presumably aware that 'AN4', is not on the pin A4, but on A5?...
Best Wishes |
|
|
Guard
Joined: 20 Jan 2005 Posts: 43
|
|
Posted: Wed Feb 02, 2005 10:49 am |
|
|
Hi,
In the code << setup_adc_ports(sAN4|VSS_VDD) >>,
Does sAN4 relative to pin 9 ?
Why AN5? |
|
|
Ttelmah Guest
|
|
Posted: Wed Feb 02, 2005 11:45 am |
|
|
Guard wrote: | Hi,
In the code << setup_adc_ports(sAN4|VSS_VDD) >>,
Does sAN4 relative to pin 9 ?
Why AN5? |
Sorry, I was thinking of the other chips of this type, where the pins go A0=RA0, A1=RA1, A2=RA2, A3=RA3, A5=RA4. They 'skip a pin', when going up portA. In your case, RA4, goes onto port B, which implies you have to set this tris bit as an input (which you seem to do, though I cannot verify this, since you don't show the defintions for 'TRISA', and 'TRISB' - why don't you use the CCS instructions set_tris_A, and set_tris_B?). Also 'beware', that since you don't se the #use_fast_io directives, CCS will 'override' your TRIS settings, if you touch any I/O fnction that deals with the port. Though I can't see one in the code you post, it could be happening.
The behaviour you are seeing, is that for some reason, B0, is being set as an output. You need to work out why...
Best Wishes |
|
|
Guest
|
|
Posted: Thu Feb 03, 2005 4:47 am |
|
|
Hi,
is it a Bug?
In the my circuit Ntc3 =4V -->204 steps then Fan=0 ( PIN_b7 =0):
if (Ntc3 < 115) Fan =1; // pin_RB7 high
I have trouble in #define directive, observe the code:
In the "READING" function, there are two way for reading AN4,
#include <16C781.h>
#use delay(clock=10000000)
#fuses NOWDT,HS, PUT,MCLR, BROWNOUT, BORV42,
#define Select Set_adc_Channel
#define read Read_adc();
#define wait delay_us(15);
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// MCU : PIC16C781
// PCM 3.218 - Mplab 6.30
//////////////////////////////////
////////////////////////////////////////////////////////////////Dichiarazioni
static int Ntc3;
///////////////////////////////////////////////////////////// Definizioni di servizio
#define NTC 4
//////////////////////////////////////////////////////////////////////////////////////////
// Livelli di temperatura da discriminare
// A/D su piedino 9 U1
// Rapporto linearit� tra (35-90) gradi.XLS (4)
#define Cn30 251 //4,936V
#define Cn05 5 // 0,1036V
#define C25 209 // 4,1V
#define C30 198 // 3,9V
#define C2 21 // 0,4144V
#define C60 136 //2,68V
#define C45 170 //3,34V
#define C50 159 //3,12V
#define C70 115 //2,257V
#define C85 85 //1,676V
#define C90 77 //1,514V
#define C95 69 //1,361V
#define C100 62 //1,221V
#define C105 56 //1,095V
#define C110 50 //0,981V
#define C150 21 //0,416V
////////////////////////////////////////// Porte
#byte port_A=0x05
#byte Port_b = 0x06
#byte TRISB =0x86
#byte TRISA =0x85
#byte wpub =0x95
#byte Ansel =0x9d
#bit Fan = Port_B.7
#bit Gate = Port_B.6
#bit pin_datain = Port_B.5
#bit pin_dataout = Port_B.4
///////////////////////////////////////////// A/D
#byte REFCON=0X9B
#byte ADCON0 =0X1F
#bit ADCSL= ADCON0.6
#bit ADCSH= ADCON0.7
#bit CHSH= ADCON0.5
#bit CHSM= ADCON0.4
#bit CHSL= ADCON0.3
#bit GODONE= ADCON0.2
#bit CHS3= ADCON0.1
#bit ADON= ADCON0.0
#byte ADCON1=0X9F
#bit VCFGH= ADCON1.5
#bit VCFGL= ADCON1.4
//////////////////////////////////////// Comparatori interrupt
#byte PIE1 =0x8c
#byte PIR1 =0x0c
#bit C2IE= PIE1.5 // ENABLE INT COMP2
#bit C2IF= PIR1.5 // FLAG INT COMP2
//////////////////////////////////////// PSMC
// Registro di controllo 0
#byte PSMCCON0 = 0x111
#bit SMCCLH = 0x111.7
#bit SMCCLL = 0x111.6
#bit MINDCH = 0x111.5
#bit MINDCL = 0x111.4
#bit MAXDCH = 0x111.3
#bit MAXDCL = 0x111.2
#bit DCH = 0x111.1
#bit DCL = 0x111.0
/////////////////////////////////////// PSMC
// Registro di controllo 1
#byte PSMCCON1 = 0x112
#bit SMCON = 0x112.7
#bit S1APOL = 0x112.6
#bit S1BPOL = 0x112.5
#bit NOTUSED = 0x112.4
#bit SCEN = 0x112.3
#bit SMCOM = 0x112.2
#bit PWMPSM = 0x112.1
#bit SMCCS = 0x112.0
//////////////////////////////////////// Amplificatore
#byte OPACON = 0x11C
#byte CALCON = 0x110 //
#bit CMPEN=OPACON. 6
#bit OPAON=OPACON. 7
#bit GBWP =OPACON. 0
#bit CAL = CALCON.7
#bit CALREF= CALCON.5
/////////////////////////////////////// Registri del comparatore 1
#byte CM1CON0 = 0x119
#bit C1ON = 0X119.7
#bit C1OUT = 0X119.6
#bit C1OE = 0X119.5
#bit C1POL = 0X119.4
#bit C1SP = 0X119.3
#bit C1R = 0X119.2
#bit C1CHH = 0X119.1
#bit C1CHL = 0X119.0
/////////////////////////////////////// Registri del comparatore 2
#byte CM2CON0 = 0x11A
#bit C2ON = 0X11A.7
#bit C2OUT = 0X11A.6
#bit C2OE = 0X11A.5
#bit C2POL = 0X11A.4
#bit C2SP = 0X11A.3
#bit C2R = 0X11A.2
#bit C2CHH = 0X11A.1
#bit C2CHL = 0X11A.0
//////////////////////////////////////Registri del DAC
#byte DAC = 0x11E
#byte DACON0 = 0x11F
#byte ANSEL = 0x9D
#bit DAON= DACON0.7
#bit DAOE= DACON0.6
#bit DARSH= DACON0.1
#bit DARSL= DACON0.0
///////////////////////////////////////////////// Prototipi
short int calibrate_opamp_internal(void) ;
short int _Datain(void);
int Reading(int channel);
void setup_comparators (void);
void setup_psmc(void);
void setup_dac(void);
void config_mcu(void);
/////////////////////////////////////////////////InterruptA
#int_RTCC
void RTCC_isr()
{
}
////////////////////////////////////////// INT per msgerr
#int_RB
void RB_isr(void)
{
}
//////////////////////////////////// P R I N C I P A L E
void main()
{
Config_mcu(); // Inizializza le periferiche
Fan =0
do
{
Ntc3 =Reading(NTC);
if (Ntc3 < 115) Fan =1; // pin_RB7 high
}while(1);
}
////////////////////////////////////// LEGGI
int Reading(int channel)
{
////////////// FUNCTION !!!
int result;
set_adc_channel(4);
delay_us(15);
result=read_adc();
return result;
//////////// NOT FUNCTION !!!
/*
int result;
Select(channel);
wait
result=read;
return result;
*/
}
void Config_mcu(void)
{
TRISA=255;
TRISB=37;
WPUB=0;
setup_adc_ports( sAN4|VSS_VDD); // AN4 � il solo ingresso analogico
setup_adc(ADC_CLOCK_DIV_32);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_128); // overflow a 13.1mS
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DIV_BY_1, 64, 1);
disable_interrupts(INT_RB);
disable_interrupts(INT_RTCC);
C2IE =0; // Interrupt Comparatore 2 disabilitato
disable_interrupts(GLOBAL);
/*
set_tris_A(255);
set_tris_b(37);
port_b_pullups(FALSE);
*/
/*
setup_dac();
setup_comparators ();
setup_psmc();
*/
}
void setup_dac()
{
DARSL=0;
DARSH=0;
DAOE =1;
DAC =0;
DAON =1;
}
void setup_comparators (void)
{
// Configura comparatore 1
C1CHH =1; // AN6
C1CHL =0;
C1R =0;
C1POL=0;
C1SP =1;
C1OE =1;
C1ON =1;
// Configura comparatore 2
C2CHH =1; // AN6
C2CHL =0;
C2R =0;
C2SP =1;
C2POL=0;
C2OE =0;
C2ON =1; //
}
void setup_psmc(void)
{
SMCON =0 ;
DCH = 0; // non usato PSM
DCL = 0; // non usato
MAXDCH = 0; //50% D.C.
MAXDCL = 0;
MINDCH = 0; // 0% D.C.
MINDCL = 0;
SMCCLH = 0;
SMCCLL = 0;
SMCCS =1 ;
PWMPSM =1 ; // PWM MODE
SMCOM =0 ; // SINGOLA USCITA
SCEN =0;
S1APOL =0;
S1BPOL =0;
}
boolean calibrate_opamp_internal(void)
{
CMPEN =0; // Amplificatore operazionale abilitato
OPAON =1; // Scelto OP AMP
GBWP =0; // 70kHz di banda
CALREF=0;
delay_ms(10); //ritardo
CAL=1; // Start calibrazione
while( bit_test(CALCON, 7) );
if (bit_test(CALCON, 6)) // Controlla un eventuale errore dopo la calibrazione
return(false);
else
return(true);
}
///////////////////////////////////////////// |
|
|
Guest
|
|
Posted: Thu Feb 03, 2005 4:48 am |
|
|
Hi,
is it a Bug?
In the my circuit Ntc3 =4V -->204 steps then Fan=0 ( PIN_b7 =0):
if (Ntc3 < 115) Fan =1; // pin_RB7 high
I have trouble in #define directive, observe the code:
In the "READING" function, there are two way for reading AN4,
#include <16C781.h>
#use delay(clock=10000000)
#fuses NOWDT,HS, PUT,MCLR, BROWNOUT, BORV42,
#define Select Set_adc_Channel
#define read Read_adc();
#define wait delay_us(15);
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// MCU : PIC16C781
// PCM 3.218 - Mplab 6.30
//////////////////////////////////
////////////////////////////////////////////////////////////////Dichiarazioni
static int Ntc3;
///////////////////////////////////////////////////////////// Definizioni di servizio
#define NTC 4
//////////////////////////////////////////////////////////////////////////////////////////
// Livelli di temperatura da discriminare
// A/D su piedino 9 U1
// Rapporto linearit� tra (35-90) gradi.XLS (4)
#define Cn30 251 //4,936V
#define Cn05 5 // 0,1036V
#define C25 209 // 4,1V
#define C30 198 // 3,9V
#define C2 21 // 0,4144V
#define C60 136 //2,68V
#define C45 170 //3,34V
#define C50 159 //3,12V
#define C70 115 //2,257V
#define C85 85 //1,676V
#define C90 77 //1,514V
#define C95 69 //1,361V
#define C100 62 //1,221V
#define C105 56 //1,095V
#define C110 50 //0,981V
#define C150 21 //0,416V
////////////////////////////////////////// Porte
#byte port_A=0x05
#byte Port_b = 0x06
#byte TRISB =0x86
#byte TRISA =0x85
#byte wpub =0x95
#byte Ansel =0x9d
#bit Fan = Port_B.7
#bit Gate = Port_B.6
#bit pin_datain = Port_B.5
#bit pin_dataout = Port_B.4
///////////////////////////////////////////// A/D
#byte REFCON=0X9B
#byte ADCON0 =0X1F
#bit ADCSL= ADCON0.6
#bit ADCSH= ADCON0.7
#bit CHSH= ADCON0.5
#bit CHSM= ADCON0.4
#bit CHSL= ADCON0.3
#bit GODONE= ADCON0.2
#bit CHS3= ADCON0.1
#bit ADON= ADCON0.0
#byte ADCON1=0X9F
#bit VCFGH= ADCON1.5
#bit VCFGL= ADCON1.4
//////////////////////////////////////// Comparatori interrupt
#byte PIE1 =0x8c
#byte PIR1 =0x0c
#bit C2IE= PIE1.5 // ENABLE INT COMP2
#bit C2IF= PIR1.5 // FLAG INT COMP2
//////////////////////////////////////// PSMC
// Registro di controllo 0
#byte PSMCCON0 = 0x111
#bit SMCCLH = 0x111.7
#bit SMCCLL = 0x111.6
#bit MINDCH = 0x111.5
#bit MINDCL = 0x111.4
#bit MAXDCH = 0x111.3
#bit MAXDCL = 0x111.2
#bit DCH = 0x111.1
#bit DCL = 0x111.0
/////////////////////////////////////// PSMC
// Registro di controllo 1
#byte PSMCCON1 = 0x112
#bit SMCON = 0x112.7
#bit S1APOL = 0x112.6
#bit S1BPOL = 0x112.5
#bit NOTUSED = 0x112.4
#bit SCEN = 0x112.3
#bit SMCOM = 0x112.2
#bit PWMPSM = 0x112.1
#bit SMCCS = 0x112.0
//////////////////////////////////////// Amplificatore
#byte OPACON = 0x11C
#byte CALCON = 0x110 //
#bit CMPEN=OPACON. 6
#bit OPAON=OPACON. 7
#bit GBWP =OPACON. 0
#bit CAL = CALCON.7
#bit CALREF= CALCON.5
/////////////////////////////////////// Registri del comparatore 1
#byte CM1CON0 = 0x119
#bit C1ON = 0X119.7
#bit C1OUT = 0X119.6
#bit C1OE = 0X119.5
#bit C1POL = 0X119.4
#bit C1SP = 0X119.3
#bit C1R = 0X119.2
#bit C1CHH = 0X119.1
#bit C1CHL = 0X119.0
/////////////////////////////////////// Registri del comparatore 2
#byte CM2CON0 = 0x11A
#bit C2ON = 0X11A.7
#bit C2OUT = 0X11A.6
#bit C2OE = 0X11A.5
#bit C2POL = 0X11A.4
#bit C2SP = 0X11A.3
#bit C2R = 0X11A.2
#bit C2CHH = 0X11A.1
#bit C2CHL = 0X11A.0
//////////////////////////////////////Registri del DAC
#byte DAC = 0x11E
#byte DACON0 = 0x11F
#byte ANSEL = 0x9D
#bit DAON= DACON0.7
#bit DAOE= DACON0.6
#bit DARSH= DACON0.1
#bit DARSL= DACON0.0
///////////////////////////////////////////////// Prototipi
short int calibrate_opamp_internal(void) ;
short int _Datain(void);
int Reading(int channel);
void setup_comparators (void);
void setup_psmc(void);
void setup_dac(void);
void config_mcu(void);
/////////////////////////////////////////////////InterruptA
#int_RTCC
void RTCC_isr()
{
}
////////////////////////////////////////// INT per msgerr
#int_RB
void RB_isr(void)
{
}
//////////////////////////////////// P R I N C I P A L E
void main()
{
Config_mcu(); // Inizializza le periferiche
Fan =0
do
{
Ntc3 =Reading(NTC);
if (Ntc3 < 115) Fan =1; // pin_RB7 high
}while(1);
}
////////////////////////////////////// LEGGI
int Reading(int channel)
{
////////////// FUNCTION !!!
int result;
set_adc_channel(4);
delay_us(15);
result=read_adc();
return result;
//////////// NOT FUNCTION !!!
/*
int result;
Select(channel);
wait
result=read;
return result;
*/
}
void Config_mcu(void)
{
TRISA=255;
TRISB=37;
WPUB=0;
setup_adc_ports( sAN4|VSS_VDD); // AN4 � il solo ingresso analogico
setup_adc(ADC_CLOCK_DIV_32);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_128); // overflow a 13.1mS
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DIV_BY_1, 64, 1);
disable_interrupts(INT_RB);
disable_interrupts(INT_RTCC);
C2IE =0; // Interrupt Comparatore 2 disabilitato
disable_interrupts(GLOBAL);
/*
set_tris_A(255);
set_tris_b(37);
port_b_pullups(FALSE);
*/
/*
setup_dac();
setup_comparators ();
setup_psmc();
*/
}
void setup_dac()
{
DARSL=0;
DARSH=0;
DAOE =1;
DAC =0;
DAON =1;
}
void setup_comparators (void)
{
// Configura comparatore 1
C1CHH =1; // AN6
C1CHL =0;
C1R =0;
C1POL=0;
C1SP =1;
C1OE =1;
C1ON =1;
// Configura comparatore 2
C2CHH =1; // AN6
C2CHL =0;
C2R =0;
C2SP =1;
C2POL=0;
C2OE =0;
C2ON =1; //
}
void setup_psmc(void)
{
SMCON =0 ;
DCH = 0; // non usato PSM
DCL = 0; // non usato
MAXDCH = 0; //50% D.C.
MAXDCL = 0;
MINDCH = 0; // 0% D.C.
MINDCL = 0;
SMCCLH = 0;
SMCCLL = 0;
SMCCS =1 ;
PWMPSM =1 ; // PWM MODE
SMCOM =0 ; // SINGOLA USCITA
SCEN =0;
S1APOL =0;
S1BPOL =0;
}
boolean calibrate_opamp_internal(void)
{
CMPEN =0; // Amplificatore operazionale abilitato
OPAON =1; // Scelto OP AMP
GBWP =0; // 70kHz di banda
CALREF=0;
delay_ms(10); //ritardo
CAL=1; // Start calibrazione
while( bit_test(CALCON, 7) );
if (bit_test(CALCON, 6)) // Controlla un eventuale errore dopo la calibrazione
return(false);
else
return(true);
}
///////////////////////////////////////////// |
|
|
Guard
Joined: 20 Jan 2005 Posts: 43
|
|
Posted: Thu Feb 03, 2005 4:50 am |
|
|
Ops!
Post caos!
I am Guest |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Thu Feb 03, 2005 7:48 am |
|
|
If you were registered then you could delete the other one
Also, I usually don't bother looking at code if the person doesn't bother to use the "code" button when posting. I am sure others feel the same. |
|
|
Guard
Joined: 20 Jan 2005 Posts: 43
|
|
Posted: Fri Feb 04, 2005 2:43 am |
|
|
I moved the post in "PIC16c781 bug ?" topic
and I used code button
Thank |
|
|
|
|
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
|