mariuszoll
Member level 5
- Joined
- Aug 28, 2012
- Messages
- 82
- Helped
- 1
- Reputation
- 2
- Reaction score
- 0
- Trophy points
- 1,286
- Activity points
- 2,147
Hi,
I have to implement a thermometer. I use on my project a PIC18F2620, a LM35, and the temperature is displayed on 3 seven segment display. In order to have a stable display, I use the Timer0, and I read the value of temperature at each 1 sec. The problem is the fact that there is nothing displayed.
Please find attached my C code:
#include <p18f2620.h>
#include <delays.h>
#include <adc.h>
/*****Configuration of internal registers*****/
#pragma config WDT = OFF // watch dog disabled
#pragma config OSC = INTIO67
#pragma config IESO = OFF //Oscillator Switchover mode disabled
#pragma config XINST = OFF
#pragma config BOREN = OFF //Brown-out Reset disabled in hardware and software
#pragma config PBADEN=OFF //PORTB<4:0> pins are configured as digital I/O on Reset
#pragma config STVREN=OFF //Stack full/underflow will not cause Reset
#pragma config LVP=OFF //Single-Supply ICSP disabled
/******definition of variables*****/
unsigned int i=0,n,u=0,d=0,f=0,k=0;
unsigned int dig[3];
float quanta=0.0,val=0.0, val1=0.0,tempinC=0.0, j=0.0;
/*****Local Functions*****/
void init(void)
{
// outputs
TRISC=0; //port C configured as output
TRISBbits.TRISB0=0;
TRISBbits.TRISB1=0;
TRISBbits.TRISB2=0;
TRISBbits.TRISB3=0;
TRISBbits.TRISB4=0;
TRISBbits.TRISB5=0;
TRISAbits.TRISA1=0;
//inputs
TRISAbits.TRISA0=1; // input from senzor
// ADC setings
// Config the required ADC pins as analog pins (AN0)
ADCON1bits.PCFG3=1;
ADCON1bits.PCFG2=1;
ADCON1bits.PCFG1=1;
ADCON1bits.PCFG0=0;
// Set the voltage reference
ADCON1bits.VCFG1=0; // Vref- = Vss
ADCON1bits.VCFG0=1; // Vref+ = AN3
// Select ADC input channel (AN0)
ADCON0bits.CHS3=0;
ADCON0bits.CHS2=0;
ADCON0bits.CHS1=0;
ADCON0bits.CHS0=0;
// Select A/D aquisition time (2Tad)
ADCON2bits.ACQT2=0;
ADCON2bits.ACQT1=0;
ADCON2bits.ACQT0=1;
// Select A/D conversion clock ( Fosc/4)
ADCON2bits.ADCS2=1;
ADCON2bits.ADCS1=0;
ADCON2bits.ADCS0=0;
//Select result format right justified
ADCON2bits.ADFM = 1;
//turn on A/D module
ADCON0bits.ADON=1;
// Oscillator config
OSCCON=0b01100111;
// Timer0 configuration
T0CONbits.T0PS0=1; // TMR0 prescaler=256
T0CONbits.T0PS1=1;
T0CONbits.T0PS2=1;
T0CONbits.PSA=0; // TMR0 assignement
T0CONbits.T0CS=0; // Fosc/4
T0CONbits.T08BIT=1; // 8 bit
INTCONbits.TMR0IE=1; // Enable TMR0 interrupt
INTCONbits.PEIE=1; // Enable peripheral interrupt
INTCONbits.GIE=1; // Enable INTs globally
T0CONbits.TMR0ON=1;
// Port Initialisation
LATC=0xFF;
LATBbits.LATB0=1;
LATBbits.LATB1=1;
LATBbits.LATB2=1;
LATBbits.LATB3=1;
LATBbits.LATB4=1;
LATBbits.LATB5=1;
}
//Functions
//7 Segment Display Function
void Seven_Segment (unsigned int n)
{
switch
{
case 0:
LATCbits.LATC0=0;
LATCbits.LATC1=0;
LATCbits.LATC2=0;
LATCbits.LATC3=0;
LATCbits.LATC4=0;
LATCbits.LATC5=0;
LATCbits.LATC6=1;
LATCbits.LATC7=1;
break;
case 1:
LATCbits.LATC0=1;
LATCbits.LATC1=0;
LATCbits.LATC2=0;
LATCbits.LATC3=1;
LATCbits.LATC4=1;
LATCbits.LATC5=1;
LATCbits.LATC6=1;
LATCbits.LATC7=1;
break;
case 2:
LATCbits.LATC0=0;
LATCbits.LATC1=0;
LATCbits.LATC2=1;
LATCbits.LATC3=0;
LATCbits.LATC4=0;
LATCbits.LATC5=1;
LATCbits.LATC6=0;
LATCbits.LATC7=1;
break;
case 3:
LATCbits.LATC0=0;
LATCbits.LATC1=0;
LATCbits.LATC2=0;
LATCbits.LATC3=0;
LATCbits.LATC4=1;
LATCbits.LATC5=1;
LATCbits.LATC6=0;
LATCbits.LATC7=1;
break;
case 4:
LATCbits.LATC0=1;
LATCbits.LATC1=0;
LATCbits.LATC2=0;
LATCbits.LATC3=1;
LATCbits.LATC4=1;
LATCbits.LATC5=0;
LATCbits.LATC6=0;
LATCbits.LATC7=1;
break;
case 5:
LATCbits.LATC0=0;
LATCbits.LATC1=1;
LATCbits.LATC2=0;
LATCbits.LATC3=0;
LATCbits.LATC4=1;
LATCbits.LATC5=0;
LATCbits.LATC6=0;
LATCbits.LATC7=1;
break;
case 6:
LATCbits.LATC0=0;
LATCbits.LATC1=1;
LATCbits.LATC2=0;
LATCbits.LATC3=0;
LATCbits.LATC4=0;
LATCbits.LATC5=0;
LATCbits.LATC6=0;
LATCbits.LATC7=1;
break;
case 7:
LATCbits.LATC0=0;
LATCbits.LATC1=0;
LATCbits.LATC2=0;
LATCbits.LATC3=1;
LATCbits.LATC4=1;
LATCbits.LATC5=1;
LATCbits.LATC6=1;
LATCbits.LATC7=1;
break;
case 8:
LATCbits.LATC0=0;
LATCbits.LATC1=0;
LATCbits.LATC2=0;
LATCbits.LATC3=0;
LATCbits.LATC4=0;
LATCbits.LATC5=0;
LATCbits.LATC6=0;
LATCbits.LATC7=1;
break;
case 9:
LATCbits.LATC0=0;
LATCbits.LATC1=0;
LATCbits.LATC2=0;
LATCbits.LATC3=0;
LATCbits.LATC4=1;
LATCbits.LATC5=0;
LATCbits.LATC6=0;
LATCbits.LATC7=1;
break;
}
}
void DisplayTemp(void)
{
//decimals
LATCbits.LATC7=1;
LATBbits.LATB3=0;
LATBbits.LATB4=1;
LATBbits.LATB5=1;
Seven_Segment(d);
Delay100TCYx(70);
//units
LATCbits.LATC7=0;
LATBbits.LATB3=1;
LATBbits.LATB4=0;
LATBbits.LATB5=1;
Seven_Segment(u);
Delay10TCYx(5);
//fractions
LATCbits.LATC7=1;
Delay1KTCYx(10);
LATBbits.LATB3=1;
LATBbits.LATB4=1;
LATBbits.LATB5=0;
Seven_Segment(f);
Delay10TCYx(5);
}
/***** main ******/
void main(void)
{
init();
LATBbits.LATB0=0; //C
LATBbits.LATB1=0; //C
LATBbits.LATB2=0; //C
k=0;
while(1)
{
val=0.0;
/* for(i=0; i<10; i++)
{
ConvertADC();
while(BusyADC());
val=val+ReadADC();
}
val1=val/10;
quanta=1023/1.22;
tempinC=(val1*100/quanta)-10-2;
n=(int)tempinC;
u=n%10;
n=n/10;
d=n;
j=(tempinC-n)*10;
f=(int)j;
DisplayTemp(); */
if(INTCONbits.TMR0IE && INTCONbits.TMR0IF)
{
k++;
if(k==15) // temperature dispalyed at each 1sec
{
for(i=0; i<10; i++)
{
ConvertADC();
while(BusyADC());
val=val+ReadADC();
}
val1=val/10;
quanta=1023/1.22;
tempinC=(val1*100/quanta)-10;
n=(int)tempinC;
u=n%10;
n=n/10;
d=n;
j=(tempinC-n)*10;
f=(int)j;
DisplayTemp();
// k=0;
}
INTCONbits.TMR0IF=0;
k=0;
}
}
}
Thank you in advance.
I have to implement a thermometer. I use on my project a PIC18F2620, a LM35, and the temperature is displayed on 3 seven segment display. In order to have a stable display, I use the Timer0, and I read the value of temperature at each 1 sec. The problem is the fact that there is nothing displayed.
Please find attached my C code:
#include <p18f2620.h>
#include <delays.h>
#include <adc.h>
/*****Configuration of internal registers*****/
#pragma config WDT = OFF // watch dog disabled
#pragma config OSC = INTIO67
#pragma config IESO = OFF //Oscillator Switchover mode disabled
#pragma config XINST = OFF
#pragma config BOREN = OFF //Brown-out Reset disabled in hardware and software
#pragma config PBADEN=OFF //PORTB<4:0> pins are configured as digital I/O on Reset
#pragma config STVREN=OFF //Stack full/underflow will not cause Reset
#pragma config LVP=OFF //Single-Supply ICSP disabled
/******definition of variables*****/
unsigned int i=0,n,u=0,d=0,f=0,k=0;
unsigned int dig[3];
float quanta=0.0,val=0.0, val1=0.0,tempinC=0.0, j=0.0;
/*****Local Functions*****/
void init(void)
{
// outputs
TRISC=0; //port C configured as output
TRISBbits.TRISB0=0;
TRISBbits.TRISB1=0;
TRISBbits.TRISB2=0;
TRISBbits.TRISB3=0;
TRISBbits.TRISB4=0;
TRISBbits.TRISB5=0;
TRISAbits.TRISA1=0;
//inputs
TRISAbits.TRISA0=1; // input from senzor
// ADC setings
// Config the required ADC pins as analog pins (AN0)
ADCON1bits.PCFG3=1;
ADCON1bits.PCFG2=1;
ADCON1bits.PCFG1=1;
ADCON1bits.PCFG0=0;
// Set the voltage reference
ADCON1bits.VCFG1=0; // Vref- = Vss
ADCON1bits.VCFG0=1; // Vref+ = AN3
// Select ADC input channel (AN0)
ADCON0bits.CHS3=0;
ADCON0bits.CHS2=0;
ADCON0bits.CHS1=0;
ADCON0bits.CHS0=0;
// Select A/D aquisition time (2Tad)
ADCON2bits.ACQT2=0;
ADCON2bits.ACQT1=0;
ADCON2bits.ACQT0=1;
// Select A/D conversion clock ( Fosc/4)
ADCON2bits.ADCS2=1;
ADCON2bits.ADCS1=0;
ADCON2bits.ADCS0=0;
//Select result format right justified
ADCON2bits.ADFM = 1;
//turn on A/D module
ADCON0bits.ADON=1;
// Oscillator config
OSCCON=0b01100111;
// Timer0 configuration
T0CONbits.T0PS0=1; // TMR0 prescaler=256
T0CONbits.T0PS1=1;
T0CONbits.T0PS2=1;
T0CONbits.PSA=0; // TMR0 assignement
T0CONbits.T0CS=0; // Fosc/4
T0CONbits.T08BIT=1; // 8 bit
INTCONbits.TMR0IE=1; // Enable TMR0 interrupt
INTCONbits.PEIE=1; // Enable peripheral interrupt
INTCONbits.GIE=1; // Enable INTs globally
T0CONbits.TMR0ON=1;
// Port Initialisation
LATC=0xFF;
LATBbits.LATB0=1;
LATBbits.LATB1=1;
LATBbits.LATB2=1;
LATBbits.LATB3=1;
LATBbits.LATB4=1;
LATBbits.LATB5=1;
}
//Functions
//7 Segment Display Function
void Seven_Segment (unsigned int n)
{
switch
{
case 0:
LATCbits.LATC0=0;
LATCbits.LATC1=0;
LATCbits.LATC2=0;
LATCbits.LATC3=0;
LATCbits.LATC4=0;
LATCbits.LATC5=0;
LATCbits.LATC6=1;
LATCbits.LATC7=1;
break;
case 1:
LATCbits.LATC0=1;
LATCbits.LATC1=0;
LATCbits.LATC2=0;
LATCbits.LATC3=1;
LATCbits.LATC4=1;
LATCbits.LATC5=1;
LATCbits.LATC6=1;
LATCbits.LATC7=1;
break;
case 2:
LATCbits.LATC0=0;
LATCbits.LATC1=0;
LATCbits.LATC2=1;
LATCbits.LATC3=0;
LATCbits.LATC4=0;
LATCbits.LATC5=1;
LATCbits.LATC6=0;
LATCbits.LATC7=1;
break;
case 3:
LATCbits.LATC0=0;
LATCbits.LATC1=0;
LATCbits.LATC2=0;
LATCbits.LATC3=0;
LATCbits.LATC4=1;
LATCbits.LATC5=1;
LATCbits.LATC6=0;
LATCbits.LATC7=1;
break;
case 4:
LATCbits.LATC0=1;
LATCbits.LATC1=0;
LATCbits.LATC2=0;
LATCbits.LATC3=1;
LATCbits.LATC4=1;
LATCbits.LATC5=0;
LATCbits.LATC6=0;
LATCbits.LATC7=1;
break;
case 5:
LATCbits.LATC0=0;
LATCbits.LATC1=1;
LATCbits.LATC2=0;
LATCbits.LATC3=0;
LATCbits.LATC4=1;
LATCbits.LATC5=0;
LATCbits.LATC6=0;
LATCbits.LATC7=1;
break;
case 6:
LATCbits.LATC0=0;
LATCbits.LATC1=1;
LATCbits.LATC2=0;
LATCbits.LATC3=0;
LATCbits.LATC4=0;
LATCbits.LATC5=0;
LATCbits.LATC6=0;
LATCbits.LATC7=1;
break;
case 7:
LATCbits.LATC0=0;
LATCbits.LATC1=0;
LATCbits.LATC2=0;
LATCbits.LATC3=1;
LATCbits.LATC4=1;
LATCbits.LATC5=1;
LATCbits.LATC6=1;
LATCbits.LATC7=1;
break;
case 8:
LATCbits.LATC0=0;
LATCbits.LATC1=0;
LATCbits.LATC2=0;
LATCbits.LATC3=0;
LATCbits.LATC4=0;
LATCbits.LATC5=0;
LATCbits.LATC6=0;
LATCbits.LATC7=1;
break;
case 9:
LATCbits.LATC0=0;
LATCbits.LATC1=0;
LATCbits.LATC2=0;
LATCbits.LATC3=0;
LATCbits.LATC4=1;
LATCbits.LATC5=0;
LATCbits.LATC6=0;
LATCbits.LATC7=1;
break;
}
}
void DisplayTemp(void)
{
//decimals
LATCbits.LATC7=1;
LATBbits.LATB3=0;
LATBbits.LATB4=1;
LATBbits.LATB5=1;
Seven_Segment(d);
Delay100TCYx(70);
//units
LATCbits.LATC7=0;
LATBbits.LATB3=1;
LATBbits.LATB4=0;
LATBbits.LATB5=1;
Seven_Segment(u);
Delay10TCYx(5);
//fractions
LATCbits.LATC7=1;
Delay1KTCYx(10);
LATBbits.LATB3=1;
LATBbits.LATB4=1;
LATBbits.LATB5=0;
Seven_Segment(f);
Delay10TCYx(5);
}
/***** main ******/
void main(void)
{
init();
LATBbits.LATB0=0; //C
LATBbits.LATB1=0; //C
LATBbits.LATB2=0; //C
k=0;
while(1)
{
val=0.0;
/* for(i=0; i<10; i++)
{
ConvertADC();
while(BusyADC());
val=val+ReadADC();
}
val1=val/10;
quanta=1023/1.22;
tempinC=(val1*100/quanta)-10-2;
n=(int)tempinC;
u=n%10;
n=n/10;
d=n;
j=(tempinC-n)*10;
f=(int)j;
DisplayTemp(); */
if(INTCONbits.TMR0IE && INTCONbits.TMR0IF)
{
k++;
if(k==15) // temperature dispalyed at each 1sec
{
for(i=0; i<10; i++)
{
ConvertADC();
while(BusyADC());
val=val+ReadADC();
}
val1=val/10;
quanta=1023/1.22;
tempinC=(val1*100/quanta)-10;
n=(int)tempinC;
u=n%10;
n=n/10;
d=n;
j=(tempinC-n)*10;
f=(int)j;
DisplayTemp();
// k=0;
}
INTCONbits.TMR0IF=0;
k=0;
}
}
}
Thank you in advance.