|
|
View previous topic :: View next topic |
Author |
Message |
oyhan
Joined: 03 Jun 2005 Posts: 21 Location: TURKEY
|
MCP3201 (microchip) is 12 bit adc |
Posted: Fri Aug 11, 2006 1:25 am |
|
|
Dear Friends..
MCP3201 is 12 bit adc on SPI communication. Below is sample program that is not working. Can some one guide me where is mistake. Thanks..
Code: |
#include <16F88.h>
#fuses INTRC_IO, MCLR, NOWDT, NOPROTECT, NOBROWNOUT, PUT, NOLVP
#use delay(clock=8000000)
#include "flex_lcd.c"
#define Dout PIN_B0
#define CLK PIN_B1
#define CS PIN_A0
// Define the register addresses for ANSEL and CMCON.
#byte ANSEL = 0x9B
#byte CMCON = 0x9C
BYTE read_adc_byte(BYTE number_of_bits) {
BYTE i,data;
data=0;
for(i=0;i<number_of_bits;++i) {
output_low(CLK);
delay_us(50);
shift_left(&data,1,input(Dout));
output_high(CLK);
delay_us(50);
}
return(data);
}
void main()
{ int data_l;
long int data_h, value;
float result;
data_h=0;
data_l=0;
set_tris_a(0b11100000); set_tris_b(0b00000001);
ANSEL = 0; // Set all A/D pins to digital
CMCON = 7; // Disable comparators
setup_adc(ADC_OFF); output_high(PIN_A1); output_high(PIN_A2); output_high(PIN_A3); output_high(PIN_A4);
lcd_init(); delay_ms(500); lcd_putc("Value:");
while(1) {
output_high(CS);
delay_us(1);
output_low(CS);
data_h = read_adc_byte(9);
data_l = read_adc_byte(4);
output_high(CS);
value = (data_h << 4) | data_l;
lcd_gotoxy(7,1);
printf(lcd_putc,"%0Lu",value);
delay_ms(250); delay_ms(250);
}
}
|
Last edited by oyhan on Sun Aug 13, 2006 5:18 am; edited 1 time in total |
|
|
oyhan
Joined: 03 Jun 2005 Posts: 21 Location: TURKEY
|
|
Posted: Fri Aug 11, 2006 3:46 pm |
|
|
Can some one guide me where is mistake. Thanks.. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Aug 11, 2006 3:49 pm |
|
|
CCS has many examples of how to do software SPI read operations.
They use the shift_left() function. Look at the MCP3204 driver for
an example. It's in this folder: c:\Program Files\Picc\Drivers |
|
|
akokyaw
Joined: 11 Feb 2005 Posts: 24
|
MCP3202 |
Posted: Sun Aug 13, 2006 1:08 am |
|
|
Hi,
Try this code,
Code: | #include <16F877.h>
#fuses XT,NOWDT,NOPROTECT,NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7) // Jumpers: 8 to 11, 7 to 12
#define CLK PIN_B0
#define Dout PIN_B1
#define Din PIN_B2
#define CS PIN_B3
void ShiftOut (int data, int no_of_bit)
{
int i;
for(i=0 ; i<no_of_bit>>= 1;
output_high(CLK);
delay_us(50);
}
}
BYTE read_adc_byte(BYTE number_of_bits) {
BYTE i,data;
data=0;
for(i=0;i<number_of_bits;++i) {
output_low(CLK);
delay_us(50);
shift_left(&data,1,input(Dout));
output_high(CLK);
delay_us(50);
}
return(data);
}
void main()
{
int data_l;
long int data_h, value;
float result;
data_h=0;
data_l=0;
printf("\r**** Initializing of MCP3202 12 Bit A/D Converter ****\n ");
while(1) {
output_high(CS);
delay_us(1);
output_low(CS);
ShiftOut(0b00001100, 4);
data_h = read_adc_byte(9);
data_l = read_adc_byte(4);
output_high(CS);
value = (data_h << 4) | data_l;
result = value * 0.001226;
printf("\rOutput Voltage = %f V", result);
delay_ms(250); delay_ms(250);
}
} |
hope it will help you,
regards,
ako |
|
|
oyhan
Joined: 03 Jun 2005 Posts: 21 Location: TURKEY
|
|
Posted: Sun Aug 13, 2006 5:29 am |
|
|
Thanks akokyaw But I want to use mcp3201 . Only 3 Data port and only Dout . It Does not use DIn. I thing I convert your code for mcp 3201 but It did not run. (New code is in my first message) I saw some problem. For example (for any voltage between 0-5V); Always.. First time Value = 03071 and second time is Value=1024 totaly is 4095. I think it musn't change this value but I do not understand why?? I have to see only one Value on the lcd why not?? Help me please..
Best Regards.. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Aug 13, 2006 2:44 pm |
|
|
I didn't analyze your code in detail, but I can see one problem.
You're calling the "read_adc_byte()" function, and requesting it to
get a 9-bit value. Example: read_adc_byte(9);
But the function puts the data into a byte (8-bits), and it returns a byte.
It can't get 9 bits. You need to create a "read_adc_word()" function
that can get up to 16 bits. |
|
|
|
|
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
|