Nimms
Newbie level 4
Hi All,
Iam very new to Pic,iam designing a 4 channel thermistor temperaturse sensor using pic16f1937.
i wanted to read 4 thermistors inputs and get the ADC values convert it to temprature and send it to serial port.iam getting different adc values in the hyper terminal,is this the correct code.how can i convert the adc values to temperature ands send it to serial port ..could anyone help me
#include <stdio.h>
#include <stdlib.h>
#include <htc.h>
#define _XTAL_FREQ 4000000
int advalue=0;
void ADCInit()
{
ANSELA = 0xF;
ANSELB = 0;
ANSELE = 0;
ADCON1 = 0xA0;
}
void usart_init(void)
{
SYNC = 0; // Transmisson is asynchronous
TRISC7 = 1;
TRISC6 = 1;
BRGH = 1;
SPBRG = 25; // 4mhz xtal 9600 BAUD
TXEN = 1;
CREN = 1;
SPEN = 1; // SPEN and CREN
RCIF = 0; // Clear USART receive interrupt flag
TXIF = 0; // Clear USART transmit interrupt flag
}
void usart_write_data(unsigned char data) //Reads the character and move to TXREG register
{
while(!TXIF);
TXREG = data;
}
void USART_putstring(char* StringPtr)
{
while(*StringPtr != 0x00)
{
usart_write_data(*StringPtr);
StringPtr++;
}
}
unsigned int ADCread(unsigned char ch)
{
if (ch>13)
return 0;
ADCON0 = 0;
ADCON0 = ch<<3; // select ADCchannel
ADCON0 = 1; // swich on adc module
_nop();
_nop();
GO_nDONE=1; //START CONVERSION
while(GO_nDONE); //waiting conversion to finish
ADCON0=0;
return (int)ADRESH *256 + ADRESL;
}
void main(void)
{
OSCCON = SYS_OSC;
_nop();
_nop();
void ADCInit();//Initializing register
unsigned int ADCread(unsigned char ch);
void usart_init(void);
void usart_write_data(unsigned char data) ;
void USART_putstring(char* StringPtr);
//double getTemp();
//double convert_ADC2Temp( int adc_value );
//double temp_value;
//double y2;
char buffer[5];
int adc_value;
//int i=0;
sysinit () ;
ADCInit();
usart_init();
for(;
{
USART_putstring("Reading channel ");
USART_putstring(" : ");
adc_value= ADCread(0);
itoa( buffer,adc_value, 10);
USART_putstring(buffer);
USART_putstring(" ");
usart_write_data('\r');
usart_write_data('\n');
}
}
Iam very new to Pic,iam designing a 4 channel thermistor temperaturse sensor using pic16f1937.
i wanted to read 4 thermistors inputs and get the ADC values convert it to temprature and send it to serial port.iam getting different adc values in the hyper terminal,is this the correct code.how can i convert the adc values to temperature ands send it to serial port ..could anyone help me
#include <stdio.h>
#include <stdlib.h>
#include <htc.h>
#define _XTAL_FREQ 4000000
int advalue=0;
void ADCInit()
{
ANSELA = 0xF;
ANSELB = 0;
ANSELE = 0;
ADCON1 = 0xA0;
}
void usart_init(void)
{
SYNC = 0; // Transmisson is asynchronous
TRISC7 = 1;
TRISC6 = 1;
BRGH = 1;
SPBRG = 25; // 4mhz xtal 9600 BAUD
TXEN = 1;
CREN = 1;
SPEN = 1; // SPEN and CREN
RCIF = 0; // Clear USART receive interrupt flag
TXIF = 0; // Clear USART transmit interrupt flag
}
void usart_write_data(unsigned char data) //Reads the character and move to TXREG register
{
while(!TXIF);
TXREG = data;
}
void USART_putstring(char* StringPtr)
{
while(*StringPtr != 0x00)
{
usart_write_data(*StringPtr);
StringPtr++;
}
}
unsigned int ADCread(unsigned char ch)
{
if (ch>13)
return 0;
ADCON0 = 0;
ADCON0 = ch<<3; // select ADCchannel
ADCON0 = 1; // swich on adc module
_nop();
_nop();
GO_nDONE=1; //START CONVERSION
while(GO_nDONE); //waiting conversion to finish
ADCON0=0;
return (int)ADRESH *256 + ADRESL;
}
void main(void)
{
OSCCON = SYS_OSC;
_nop();
_nop();
void ADCInit();//Initializing register
unsigned int ADCread(unsigned char ch);
void usart_init(void);
void usart_write_data(unsigned char data) ;
void USART_putstring(char* StringPtr);
//double getTemp();
//double convert_ADC2Temp( int adc_value );
//double temp_value;
//double y2;
char buffer[5];
int adc_value;
//int i=0;
sysinit () ;
ADCInit();
usart_init();
for(;
{
USART_putstring("Reading channel ");
USART_putstring(" : ");
adc_value= ADCread(0);
itoa( buffer,adc_value, 10);
USART_putstring(buffer);
USART_putstring(" ");
usart_write_data('\r');
usart_write_data('\n');
}
}