jhunt315
Junior Member level 1
I found some code online to set up a digital thermometer. I am using an LM34 and PIC16F917 hooked up to a seven segment display where it will read the temperature and display it on the seven segment display. I was wondering if anyone can tell me if i need the 20MHz crystal oscillator at all (or if i can do it without it) and in what line (if any) does the temperature actually get read?
Code:
#include <htc.h>
#define _XTAL_FREQ 20000000UL
typedef unsigned char UINT8;
typedef signed char INT8;
typedef unsigned int UINT16;
typedef signed int INT16;
//Connection of Seven segment display
#define SEVEN_SEGMENT_LAT PORTB
#define SEVEN_SEGMENT_TRIS TRISB
#define MUX_PORT PORTB
#define MUX_START_POS 1
#define MUX_DISP_COUNT 4
//Global Varriable
UINT8 DisplayArray[MUX_DISP_COUNT];//Holds 'data' for each disp
void SevenSegmentWrite(UINT16 n)
{
/*
This function breaks apart a given integer into separete digits
and writes them to the display array i.e. digits[]
*/
UINT8 i=0;
UINT8 j;
while
{
DisplayArray=n%10;
i++;
if(i==MUX_DISP_COUNT)
break;
n=n/10;
}
//Fill Unused area with 0
for(j=i;j<MUX_DISP_COUNT;j++) DisplayArray[j]=0;
}
void WriteSegment(UINT8 num)
{
switch (num)
{
case 0:
//-GFEDCBA
SEVEN_SEGMENT_LAT = 0B01000000;
break;
case 1:
//-GFEDCBA
SEVEN_SEGMENT_LAT = 0B01111001;
break;
case 2:
//-GFEDCBA
SEVEN_SEGMENT_LAT = 0B00100100;
break;
case 3:
//-GFEDCBA
SEVEN_SEGMENT_LAT = 0B00110000;
break;
case 4:
//-GFEDCBA
SEVEN_SEGMENT_LAT = 0B00011001;
break;
case 5:
//-GFEDCBA
SEVEN_SEGMENT_LAT = 0B00010010;
break;
case 6:
//-GFEDCBA
SEVEN_SEGMENT_LAT = 0B00000010;
break;
case 7:
//-GFEDCBA
SEVEN_SEGMENT_LAT = 0B01111000;
break;
case 8:
//-GFEDCBA
SEVEN_SEGMENT_LAT = 0B00000000;
break;
case 9:
//-GFEDCBA
SEVEN_SEGMENT_LAT = 0B00010000;
break;
}
}
void Wait()
{
UINT8 i;
for(i=0;i<1;i++)
__delay_ms(10);
}
void main()
{
//Set Up I/O Ports
TRISB0=0;
SEVEN_SEGMENT_TRIS = 0B10000000;//Disp is connect from 0-6 last bit 7 is unused
//MUX select lines
TRISB = 0B11100001;
//Init TIMER0
T0CS=0; //Timer Clock source is intruction clock that is 5MHz
PSA=0; //Prescaler is assigned to TIMER0
PS2=1;
PS1=1;
PS0=0; //Prescaler = FCPU/128
TMR0=1; //Enable Timer0 Interrupt
PEIE=1; //Enable Peripheral Interrup
//Enable Interrupts Globally
ei();
while(1)
{
for (UINT16 i=0;i<10000;i++)
{
SevenSegmentWrite(i);
Wait();
}
}
}
//Our Interrupt Service Routine
void interrupt ISR()
{
if(T0IE && T0IF)
{
//TIMER0 Overflow Interrupt
//First Clear the INT flag
T0IF=0;
//Value of "static varriable" is retained between calls
static int i;
//Switch On Display Pointed by 'i'
MUX_PORT = (~(1<<(i+MUX_START_POS)));
//Latch digit on it
WriteSegment(DisplayArray);
//On each time out select next display
i++;
//If on last display then goto first
if(i==MUX_DISP_COUNT)
i=0;
}
}
Also the code is attached. View attachment Code.txt
Code:
#include <htc.h>
#define _XTAL_FREQ 20000000UL
typedef unsigned char UINT8;
typedef signed char INT8;
typedef unsigned int UINT16;
typedef signed int INT16;
//Connection of Seven segment display
#define SEVEN_SEGMENT_LAT PORTB
#define SEVEN_SEGMENT_TRIS TRISB
#define MUX_PORT PORTB
#define MUX_START_POS 1
#define MUX_DISP_COUNT 4
//Global Varriable
UINT8 DisplayArray[MUX_DISP_COUNT];//Holds 'data' for each disp
void SevenSegmentWrite(UINT16 n)
{
/*
This function breaks apart a given integer into separete digits
and writes them to the display array i.e. digits[]
*/
UINT8 i=0;
UINT8 j;
while
{
DisplayArray=n%10;
i++;
if(i==MUX_DISP_COUNT)
break;
n=n/10;
}
//Fill Unused area with 0
for(j=i;j<MUX_DISP_COUNT;j++) DisplayArray[j]=0;
}
void WriteSegment(UINT8 num)
{
switch (num)
{
case 0:
//-GFEDCBA
SEVEN_SEGMENT_LAT = 0B01000000;
break;
case 1:
//-GFEDCBA
SEVEN_SEGMENT_LAT = 0B01111001;
break;
case 2:
//-GFEDCBA
SEVEN_SEGMENT_LAT = 0B00100100;
break;
case 3:
//-GFEDCBA
SEVEN_SEGMENT_LAT = 0B00110000;
break;
case 4:
//-GFEDCBA
SEVEN_SEGMENT_LAT = 0B00011001;
break;
case 5:
//-GFEDCBA
SEVEN_SEGMENT_LAT = 0B00010010;
break;
case 6:
//-GFEDCBA
SEVEN_SEGMENT_LAT = 0B00000010;
break;
case 7:
//-GFEDCBA
SEVEN_SEGMENT_LAT = 0B01111000;
break;
case 8:
//-GFEDCBA
SEVEN_SEGMENT_LAT = 0B00000000;
break;
case 9:
//-GFEDCBA
SEVEN_SEGMENT_LAT = 0B00010000;
break;
}
}
void Wait()
{
UINT8 i;
for(i=0;i<1;i++)
__delay_ms(10);
}
void main()
{
//Set Up I/O Ports
TRISB0=0;
SEVEN_SEGMENT_TRIS = 0B10000000;//Disp is connect from 0-6 last bit 7 is unused
//MUX select lines
TRISB = 0B11100001;
//Init TIMER0
T0CS=0; //Timer Clock source is intruction clock that is 5MHz
PSA=0; //Prescaler is assigned to TIMER0
PS2=1;
PS1=1;
PS0=0; //Prescaler = FCPU/128
TMR0=1; //Enable Timer0 Interrupt
PEIE=1; //Enable Peripheral Interrup
//Enable Interrupts Globally
ei();
while(1)
{
for (UINT16 i=0;i<10000;i++)
{
SevenSegmentWrite(i);
Wait();
}
}
}
//Our Interrupt Service Routine
void interrupt ISR()
{
if(T0IE && T0IF)
{
//TIMER0 Overflow Interrupt
//First Clear the INT flag
T0IF=0;
//Value of "static varriable" is retained between calls
static int i;
//Switch On Display Pointed by 'i'
MUX_PORT = (~(1<<(i+MUX_START_POS)));
//Latch digit on it
WriteSegment(DisplayArray);
//On each time out select next display
i++;
//If on last display then goto first
if(i==MUX_DISP_COUNT)
i=0;
}
}
Also the code is attached. View attachment Code.txt