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--; } }
I see that you are still not preserving your source code formatting.
http://www.keil.com/forum/docs/thread9702.asp
Two lines up from the box where you pasted that mess are instructions regarding source code.
I written again the code....
sbit ADC_CS = P3^4; sbit ADC_CLK= P1^7; sbit ADC_DO = P1^6; sbit ADC_DI = P1^5;
//------ 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(9600BAUD@11.0592MHZ) 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; }
// Delay mS function void DelayMs(unsigned int count){ unsigned int i; while(count) { i = 115; while(i>0) i--; count--; } }
Write less and read more!
Then you would know how to post code to this forum - and probably also what is wrong with your code...
Whem you make a post, there are 4 bullet points immediately above the 'Message' box where you type - Read them, and follow their instructions!
There is also a link, "Tips for Posting Messages", that gives further details...
But those instructions - just as this text - requires reading...
I am SORRY.. You are right about tips for posting message.. My mind is confused about problem... Thanks for your attention.
Thanks for your attention..
#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(9600BAUD@11.0592MHZ) 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){ unsigned int i; while(count) { i = 115; while(i>0) i--; count--; } }