We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
Hi Friends,
I have used AT89C51ED2 for computing serial ADC MCP 3202 using SPI Protocol, the code is working fine for the volatages above 1V, as the voltage decresses below 1 Volts the data retrived is indeterminstic i.e., the value below 1000 counts are represented as aribitary.
I am attaching the code with mail., If there is any valid solution for this, Plz let me know
#include <REG51xD2.H> #include<stdio.h> #include<stdlib.h> #include"display.h" sbit CS = P1^0; #define CLEAR_TI (SPSTA =SPSTA & 0x7F); unsigned int call_adc(unsigned char Ch); unsigned int call_adc(unsigned char Ch) { unsigned int Data1=0x00,Data2=0x00; unsigned int Value=0x0000; unsigned char Channel; if(Ch==0) Channel=0x00; /* Channel Select 0 */ else Channel=0xE0; /* Channel Select 1 */ CS=0; /* Chip Select for ADC Active State */ SPDAT=0x01; /* Channel i/p for SPI */ while(!(SPSTA & 0x80)); CLEAR_TI; SPDAT=Channel; /* Channel i/p for SPI */ while(!(SPSTA & 0x80)); CLEAR_TI; Data1=SPDAT; /* Higher 8bit of ADC value */ SPDAT=0xFF; while(!(SPSTA & 0x80)); CLEAR_TI; Data2=SPDAT; /* Lower 8bit of ADC value */ Value=Value|Data1; /* OR First Byte */ Value=Value<<8; Value=Value|Data2; /* Or 2nd Byte*/ Value=Value & 0x0fff ; /* Masking Higher 4 bits (Nibble) */ CS=1; /* Chip Select for ADC off State*/ return Value; /* 12 Bit ADC Data */ } void main() { unsigned int Adc_Data=0x0000; unsigned char Buffer[6]; // P1_1=1; SPCON |= 0x10; /* Master mode */ SPCON |= 0x20; /* Disable Slave Select */ SPCON |= 0x82; /* Fclk Periph/128 */ SPCON &= ~0x08; /* CPOL=0; transmit mode example */ SPCON |= 0x04; /* CPHA=1; transmit mode example */ SPCON |= 0x40; /* run spi */ SPSTA &= 0x0F; /* Clear Trasmit Flag */ Initlcd(); while(1) { Adc_Data=call_adc(0x00); sprintf(Buffer,"%d",(unsigned int)Adc_Data); display(Buffer,0x80); } }