I have a problem about reading mcp3204 serial adc with atmel at89s8252 microcontroller. I am reading digital signal but from 800 to BFF. it must be from 000 to FFF. I written the program below...
#include<at898252.h> #include<stdio.h>
sbit ADC_CS = P3^4; sbit ADC_CLK = P1^7; sbit ADC_DO = P1^6; sbit ADC_DI = P1^5;
void InitSerial(void); void write_adc_byte(char data_byte); unsigned int ReadADC(unsigned char channel); void DelayMs(unsigned int count);
//--------------------------------------- // Main program //--------------------------------------- void main(void) { InitSerial(); // Initialize serial port while(1) { putchar(0x0C); // clear Hyper terminal printf("Ch 0 : %03X\n\r",ReadADC(0)); printf("Ch 1 : %03X\n\r",ReadADC(1)); DelayMs(1000); // Delay about 100 mS } }
//--------------------------------------- // Initialize serial port //--------------------------------------- void InitSerial(void) { SCON = 0x52; // setup serial port control TMOD = 0x20; // hardware (9600 BAUD @11.05592MHZ) TH1 = 0xFD; // TH1 TR1 = 1; // Timer 1 on }
//--------------------------------------- // read analog from ADC // Single end mode(2 channel) //--------------------------------------- unsigned int ReadADC(unsigned char channel) { unsigned char i,k; unsigned int AdcResult; // 12 bit
ADC_CS=0; // Active chip select k++; // Delay about 1 uS ADC_CLK=0; // make clock low first k++;k++; channel = channel? 0xF0 : 0xE0; k++;k++; // delay about 2 uS //--- write command 4 bit ---------- for(i=0; i< 4;i++) { ADC_DI = (channel & 0x80) != 0; channel<<=1; ADC_CLK=1; k++;k++; // delay about 2 uS ADC_CLK=0; }
k++;k++; // delay about 2 uS ADC_CLK=1; k++;k++; // delay about 2 uS ADC_CLK=0; k++;k++; // delay about 2 uS
//--- read ADC result 12 bit -------- AdcResult=0; for(i=0;i<12;i++) { ADC_CLK=1; k++;k++; // delay about 2 uS AdcResult<<=1; AdcResult=AdcResult | (ADC_DO & 0x01); ADC_CLK=0; k++;k++; // delay about 2 uS } ADC_CS=1; return(AdcResult); }
//--------------------------------------- // Delay mS function //--------------------------------------- void DelayMs(unsigned int count) { // mSec Delay 11.0592 Mhz unsigned int i; // Keil v7.5a while(count) { i = 115; while(i>0) i--; count--; } }