|
|
View previous topic :: View next topic |
Author |
Message |
O_R_E Guest
|
Convert Volts of LM35 with PIC16F877 and MAX187 |
Posted: Thu Apr 06, 2006 3:11 pm |
|
|
Hello, I am using a PIC16F877 and I make the conversion to 12 bits by means of a MAX187. By means of a LM35, receipt the tension in mV, later to show it by display. The problem this in which to the entrance of the ADC I have the correct value, but in display this value oscillates much, and it does not give a correct value. This it is my code:
#include "C:\Program Files C\20060403_spi.h"
#include <max187.c>
#include <lcd.c>
#use fast_io (A)
#use fast_io (B)
#use fast_io (C)
#use fast_io (D)
#use fast_io (E)
#byte port_a=0X05
#byte port_b=0X06
#byte port_c=0X07
#byte port_d=0X08
#byte port_e=0X09
#ifndef MAX4534_EN
#define MAX4534_A0 PIN_A0
#define MAX4534_A1 PIN_A1
#define MAX4534_EN PIN_A2
#endif
void main()
{
float temperatura;
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_psp(PSP_DISABLED);
setup_spi(FALSE);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
set_tris_a(0x00); //Port A as exit
set_tris_b(0x00); //Port B as exit
set_tris_c(0b11010111); //Port C as coming and goings
set_tris_d(0xFF); //RD0...RD7 exit
set_tris_e(0x00); //RE0...RE2 gate
port_b=0;
/*Init mux 4:1 MAX4534 */
port_a=0;
delay_ms(5);
output_high(MAX4534_EN);
output_high(MAX4534_A1);
output_low(MAX4534_A0);
/*Init A/D MAX187*/
init_ext_adc();
lcd_init(); //Init LCD
lcd_putc(" Digital\n"); //Extract text
lcd_putc(" Termometer"); //Extract text
delay_ms(3000);
lcd_putc("\f"); //Clean screen
lcd_putc("Temperatura\n"); //Extract text
lcd_putc("actual"); //Extract text
delay_ms(1000);
lcd_gotoxy(14,2); //Arranged cursor LCD
lcd_putc("oC");
while(1)
{
init_ext_adc();
lcd_gotoxy(8,2); //Arranged cursor LCD
lcd_putc(" "); //Clean this sector of screen
lcd_gotoxy(8,2); //Arranged cursor LCD
temperatura = read_ext_adc();
printf(lcd_putc,"%03.1f",temperatura); //xxx.x �C
delay_ms (1000);
temperatura=0;
}
}
And the code off <max187.c>, it's the same off drivers ADS8320.c CCS Compiler
//////////////// Driver for MAX187 A/D Converter ///////////////////////
//// ////
//// init_ext_adc() Call after power up ////
//// ////
//// value = read_ext_adc() Converts to digital number ////
//// and sends to MCU ////
//// ////
/////////////////////////////////////////////////////////////////////////
//// (C) Copyright 1996,2003 Custom Computer Services ////
//// This source code may only be used by licensed users of the CCS ////
//// C compiler. This source code may only be distributed to other ////
//// licensed users of the CCS C compiler. No other use, ////
//// reproduction or distribution is permitted without written ////
//// permission. Derivative programs created using this software ////
//// in object code form are not restricted in any way. ////
/////////////////////////////////////////////////////////////////////////
#ifndef MAX187_CS
#define MAX187_DOUT PIN_C4
#define MAX187_SCLK PIN_C3
#define MAX187_CS PIN_C2
#endif
void init_ext_adc() {
output_high(MAX187_CS);
output_high(MAX187_SCLK);
}
long read_ext_adc() {
int i;
long data;
data=0;
output_low(MAX187_CS);
for(i=0;i<=5;i++) // take sample and send start bit
{
output_low(MAX187_SCLK);
delay_us(1);
output_high(MAX187_SCLK);
delay_us(1);
}
for(i=0;i<8;++i) { // send sample over spi
output_low(MAX187_SCLK);
delay_us(1);
shift_left(&data,2,input(MAX187_DOUT));
output_high(MAX187_SCLK);
delay_us(1);
}
output_high(MAX187_CS);
return(data);
}
float convert_to_volts(long data) {
return ((float)data*2.5/0xffff);
}
Sorry for my basic english
Thank you,
Regards |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Apr 06, 2006 3:47 pm |
|
|
What you have done is to take the CCS driver for the ADS8320
and then edit it to make it look as if CCS has a MAX187 driver
and then post it. That's not going to work. The two chips are
not compatible. You have to make a real driver for the MAX187.
--------
Just a notice to anyone reading this thread from Google.
The code posted above is not a valid driver for the MAX187.
It's made to look as if CCS has a driver for it. They do not.
Do not use the code posted above. It will not work. |
|
|
O_R_E Guest
|
MAX187 |
Posted: Wed Apr 12, 2006 9:47 am |
|
|
Hello,
I have modified driver of the MAX187, but what I obtain is the value multiplied by two, that is to say, the double. As I can solve it? Thanks This is the code of driver MAX187:
#ifndef MAX187_CS
#define MAX187_DOUT PIN_C4
#define MAX187_SCLK PIN_C3
#define MAX187_CS PIN_C2
#endif
void init_ext_adc() {
output_low(MAX187_CS);
output_low(MAX187_SCLK);
}
long read_ext_adc() {
int i;
long data;
data=0;
delay_us(86); //Delay 86 us, to init conversion (tCONV)
output_low(MAX187_CS);
for(i=0;i<13;++i) { // send sample over spi
output_low(MAX187_SCLK);
delay_us(1);
shift_left(&data,2,input(MAX187_DOUT));
output_high(MAX187_SCLK);
delay_us(1);
}
delay_us(10);
output_high(MAX187_CS);
return(data);
}
float convert_to_volts(long data) {
return ((float)data*5.0/0xFFFF);
}
And this is the main code:
#include "C:\Program Files C\20060405_spi.h"
#include <max187.c>
#include <lcd.c>
#use fast_io (A)
#use fast_io (B)
#use fast_io (C)
#use fast_io (D)
#use fast_io (E)
#byte port_a=0X05
#byte port_b=0X06
#byte port_c=0X07
#byte port_d=0X08
#byte port_e=0X09
#ifndef MAX4534_EN
#define MAX4534_A0 PIN_A0
#define MAX4534_A1 PIN_A1
#define MAX4534_EN PIN_A2
#endif
void main()
{
float value;
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_psp(PSP_DISABLED);
setup_spi(FALSE);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
set_tris_a(0x00);
set_tris_b(0x00);
set_tris_c(0b11010111);
set_tris_d(0xFF);
set_tris_e(0x00);
port_b=0;
port_a=0;
/*Init LCD*/
lcd_init();
lcd_putc(" Digital\n");
lcd_putc(" Reference");
delay_ms(2000);
lcd_putc("\f");
lcd_putc("Value\n");
delay_ms(2000);
lcd_gotoxy(14,2);
lcd_putc("V");
while (1)
{
output_high(MAX4534_EN); //Init multiplexor MAX4534
output_low(MAX4534_A1);
output_high(MAX4534_A0);
init_ext_adc(); //Init MAX187
value = read_ext_adc();
lcd_gotoxy(8,2);
lcd_putc(" ");
lcd_gotoxy(8,2);
printf(lcd_putc,"%03.2f",value); //Tension in [Volts]
delay_ms (2000);
value=0;
}
}
Thank you |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
O_R_E Guest
|
Conversion AD con MAX187 y PIC16F877 |
Posted: Thu Apr 13, 2006 4:26 am |
|
|
Hello Jdm programer,
This driver off MAX187, it's ok:
//////////////// Driver for MAX187 A/D Converter ///////////////////////
//// ////
//// init_ext_adc() Call after power up ////
//// ////
//// value = read_ext_adc() Converts to digital number ////
//// and sends to MCU ////
//// ////
/////////////////////////////////////////////////////////////////////////
//// (C) Copyright 1996,2003 Custom Computer Services ////
//// This source code may only be used by licensed users of the CCS ////
//// C compiler. This source code may only be distributed to other ////
//// licensed users of the CCS C compiler. No other use, ////
//// reproduction or distribution is permitted without written ////
//// permission. Derivative programs created using this software ////
//// in object code form are not restricted in any way. ////
/////////////////////////////////////////////////////////////////////////
#ifndef MAX187_CS
#define MAX187_DOUT PIN_C4
#define MAX187_SCLK PIN_C3
#define MAX187_CS PIN_C2
#endif
void init_ext_adc() {
output_low(MAX187_CS);
output_low(MAX187_SCLK);
}
long read_ext_adc() {
int i;
long data;
data=0;
delay_us(86); //Espero 86 us, para iniciar la conversion (tCONV)
output_low(MAX187_CS);
for(i=0;i<12;++i) { // send sample over spi
output_low(MAX187_SCLK);
delay_us(1);
shift_left(&data,2,input(MAX187_DOUT));
output_high(MAX187_SCLK);
delay_us(1);
}
delay_us(10);
output_high(MAX187_CS);
return(data);
}
float convert_to_volts(long data) {
return ((float)data*2.5/0xFFFF);
}
The problem was in the curl, that is of 13 clocks. The paragraph of datasheet had confused me that says in page 15:
1. Use a general-purpose I/O line on the CPU to pull CS
low. Keep SCLK low.
2. Wait the for the maximum conversion time specified
before activating SCLK. Alternatively, look for a
DOUT rising edge to determine the end of
conversion.
3. Activate SCLK for a minimum of 13 clock.
Thanks |
|
|
|
|
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
|