float temp; //Current Temp
int adc_result; //store read_adc() in 8 bits mode
temp=5*100*adc_result/255.0; //LM35 output
// use PIC16F676
#include <16F676.h>
// ADC resolution is 10 bits
#device adc=10
// Define fuses !!
#fuses INTRC_IO,NOWDT,PUT,NOPROTECT,BROWNOUT,NOMCLR
// Define clock used for this MCU
#use delay (clock=4000000) // 4MHz clock
// No need for that any way !!
#rom 0x3ff={0x3444}
// Define PIC Ports
#byte PORTA = 0x05
#byte PORTC = 0x07
#byte TRISA = 0x85
#byte TRISC = 0x87
// I don't see it necessary to redifne ports in another name
#define SPORTA PORTA
#define SPORTC PORTC
// Define some constants for interuupts
#define TICKS_BETWEEN_INTERRUPTS 5000 //5000
#define INTERRUPT_OVERHEAD 35
#define TMR1RESET (0xFFFF-(TICKS_BETWEEN_INTERRUPTS-INTERRUPT_OVERHEAD))
// Here the 7 segment decoder values reside
const char SegCode[11] = {0x40,0x57,0x22,0x06,0x15,0x0C,0x08,0x56,0x00,0x04,0xFF};
// 0 1 2 3 4 5 6 7 8 9
// Used to output manipulate digits, one at a time (MUX)
const char Column[3] = {0x02,0x01,0x04};
static char Segment[3] = {0x7f,0x7f,0x7f};
static unsigned char ColCount=0x00;
// PIC CPU intialization function protoype
void CPU_SETUP(void);
// Display function protoype
void Display(void);
// ADC result manipulation function protoype
void HTO7S(unsigned int32 Num);
byte i;
unsigned int32 result;
// Interrupt handler routine, use timer 1 interrupt
#INT_TIMER1
void Timer1(void)
{
// Set timer 1 to use its interrupt for display muxing
set_timer1(TMR1RESET);
// Display the result on 7 seg digits
Display();
}
void main()
{
unsigned char i;
Initialize PIC configuration, nice way to do that in deed
CPU_SETUP();
while(true)
{
result=0;
// Read adc result 20 times using this for loop
for (i=0;i<20;i++)
{
set_adc_channel(3);
delay_ms(1);
// finally, the reslt will contain 20 ADC readings
result=result+read_adc();
}
//result = 0x3fe;
// Take the average of the 20 ADC results readings, and
// use it to manipulate this to be able to display it on 7 digits
HTO7S(result/20);
delay_ms(200);
}
}
void CPU_SETUP()
{
// not use comparator module
setup_comparator(NC_NC_NC_NC);
// A3 as analog input, refrences are Vss, and Vdd
setup_adc_ports( sAN3 | VSS_VDD);
//Divide the internal clock for use ADC on 64 = 1000000/64
setup_adc(ADC_CLOCK_DIV_64);
TRISA=0b00011000;
PORTA=0x27;
TRISC=0b00000000;
PORTC=0x37;
// Use internal clock, prescalar ratio is 1
setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);
// Reset timer 1 now
set_timer1(TMR1RESET);
// Set the global interrupt bit in options register
enable_interrupts(GLOBAL);
// enable timer 1 interrupt now
enable_interrupts(INT_TIMER1);
}
//-------------------------------------
// Display routine
//-------------------------------------
void Display()
{
// off all digits column and Segment G
PORTA = 0b00100111;
// off segment a-f
PORTC = 0b00111111;
delay_cycles(2);
// for loop could be used here, but it will cause a stack overflow !!
if (ColCount>=3)
ColCount=0;
// Display corresponding digit
SPORTC = Segment[ColCount];
// maniplate digit value in order to put the bits on portc and porta.5
// XOR it with 7, solve this your self, its easy
SPORTA = ((Segment[ColCount] & 0b01000000)>>1) | (Column[ColCount]^0x07);
ColCount++;
}
//--------------------------------------
// Convet HEX 2 byte to 7-Segment code
//--------------------------------------
void HTO7S(unsigned int32 Num)
{
unsigned int32 res;
Segment[0]=SegCode[30*Num/10230];
// If first digit is 0, turn it off
if (Segment[0]==0x40)
Segment[0]=0xFF;
// manipulate the result in order to display it on digits, maths trick, but smart one.
// needs another post in deed for me to explain
res = 30*Num%10230;
Segment[1]=SegCode[10*res/10230];
res=10*res%10230;
Segment[2]=SegCode[10*res/10230];
}
Is Result decimal number of temperature, why it's divided by 10.24?Result=(int)Result/10.24;
xmli1976 said:Is Result decimal number of temperature, why it's divided by 10.24?
pvinbox@yahoo.com said:the code don't have any comments sorry
Code Warrior said:Can you please explain little bit
pvinbox@yahoo.com said:still if u have any doubt
no problem send me a pm
// ADC resolution is 10 bits
#device adc=10
A3 as analog input, refrences are Vss, and Vdd
#include <16f877a.h>
//#device *=16
#device adc=8
#FUSES NOWDT //No Watch Dog Timer
#FUSES XT //Crystal osc <= 4mhz
#FUSES PUT //Power Up Timer
#FUSES NOPROTECT //Code not protected from reading
#FUSES NODEBUG //No Debug mode for ICD
#FUSES BROWNOUT //Reset when brownout detected
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD //No EE protection
#FUSES NOWRT //Program memory not write protected
#use delay(clock=4000000,RESTART_WDT)
#define TICKS_BETWEEN_INTERRUPTS 5000 //5000
#define INTERRUPT_OVERHEAD 35
#define TMR1RESET (0xFFFF-(TICKS_BETWEEN_INTERRUPTS-INTERRUPT_OVERHEAD))
#byte PORTB = 0x06 //it have conneted all 7 segment displays
#byte PORTC = 0x07 //it have connected all BC557 display driver
#byte TRISB = 0x86
#byte TRISC = 0x87
//// CA display
//// segment activates on LOW state of pins..
const char SegCode[11] = {0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10,0xFF};
// 0 1 2 3 4 5 6 7 8 9 OFF
const char Column [3] = {0x06,0x05,0x03}; //0x06 MS digit
static char Segment[3] = {0x7f,0x7f,0x7f}; //Start value for all segmenta, OFF
static unsigned char ColCount=0x00;
void CPU_SETUP(void);
void Display(void);
void HTO7S(unsigned int32 Num);
byte i;
unsigned int32 result;
#INT_TIMER1
void Timer1(void)
{
set_timer1(TMR1RESET);
Display();
}
void CPU_SETUP()
{
setup_spi(FALSE);
setup_psp(PSP_DISABLED);
setup_vref(FALSE);
setup_adc_ports(ALL_ANALOG);
setup_adc(ADC_CLOCK_DIV_64);
setup_comparator(NC_NC_NC_NC); // not use comparator module
TRISC=0x00; //PortC as OUTPUT
PORTC=0x07; //All BC557in OFF. LOW is active
TRISB=0x00; //PortB as OUTPUT
PORTB=0xFF; //All segment in OFF state
setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);
set_timer1(TMR1RESET);
enable_interrupts(GLOBAL);
enable_interrupts(INT_TIMER1);
}
//-------------------------------------
// Display routine
//-------------------------------------
void Display()
{
PORTC=0x07; //BC557 OFF.
PORTB=0xFF; //All segment OFF
delay_cycles(2);
if (ColCount>=3)
ColCount=0;
PORTB = Segment[ColCount];
PORTC = Column[ColCount];
ColCount++;
}
//--------------------------------------
// Convet HEX 2 byte to 7-Segment code
//--------------------------------------
void HTO7S(unsigned int32 Num)
{
unsigned int32 res;
Segment[0]=SegCode[500*Num/2550]; // V=(Vref*read_adc())/(255*10)
if (Segment[0]==0x40)
Segment[0]=0xFF;
res = 500*Num%2550; // V=(Vref*read_adc())%(255*10)
Segment[1]=SegCode[10*res/2550]; // res/255.
res=10*res%2550; // res%255.
Segment[2]=SegCode[10*res/2550]; // res/255
}
void main()
{
unsigned char i;
CPU_SETUP();
while(true) // reading LM35DZ output
{
result=0;
for (i=0;i<100;i++)
{
set_adc_channel(0); //LM35DZ connected to AN0
delay_ms(1);
result=result+read_adc();
}
//divide by 100 as average value
//result = 0x3fe; //
HTO7S(result/100); //Convert result/100 to 7 segments
delay_ms(200);
}
}
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?