PWM using PCA in AT89C51CC01
Thu Aug 19 2010, 05:31 pm
hello ajay,
Thanks for ur reply.
I write the program as follows.
In this program my analog i/p pin is p1_7 [pot-R12] and o/p port is P0.
I can give analog i/o through P1_7 pin but no o/p given by port P0.
I don´t know why?
I have clearly write
value_converted = (ADDH << 2)+(ADDL);
#define value_converted P0 // ADC o/p
then why this program not give o/p?
Please check this program.
Tell me ur idea or sugestion for this problem.
Thanks in advance.
Thanks for ur reply.
I write the program as follows.
#include <stdio.h> #include "reg_c51.h" // standard conversion // The variable "channel" contains the channel to convert // The variable "value_converted" is an unsigned int #define channel P1_7 // ADC i/p #define value_converted P0 // ADC o/p void main() { ADCF = 0x80; // configure channel P1.7 for ADC ADCON = 0x20; // Enable the ADC ADCON &= 0xF8; // Clear the field SCH[2:0] ADCON |= channel; // Select channel ADCON |= 0x08; // Start conversion in standard mode // Wait flag End of conversion while((ADCON & 0x01)!= 0x01) { ADCON &= 0xEF;// Clear the End of conversion flag // read the value value_converted = (ADDH << 2)+(ADDL); } while (1) {} }
In this program my analog i/p pin is p1_7 [pot-R12] and o/p port is P0.
I can give analog i/o through P1_7 pin but no o/p given by port P0.
I don´t know why?
I have clearly write
value_converted = (ADDH << 2)+(ADDL);
#define value_converted P0 // ADC o/p
then why this program not give o/p?
Please check this program.
Tell me ur idea or sugestion for this problem.
Thanks in advance.
Fri Aug 20 2010, 12:45 am
wrong statement..
can you explain how you are reading this value? P0 is an 8bit port and you are moving a value higher than 8-bit. so be careful. ok?
// Wait flag End of conversion while((ADCON & 0x01)!= 0x01){} //wait here ADCON &= 0xEF;// Clear the End of conversion flag // read the value value_converted = (ADDH << 2)+(ADDL);
can you explain how you are reading this value? P0 is an 8bit port and you are moving a value higher than 8-bit. so be careful. ok?
shaik like this.
Fri Aug 20 2010, 12:52 pm
hello ajay,
thanks for ur reply.
As u say that there is no problem in program upto first 5 statements.
I had again read the ADC section in datasheet. In that for getting ADC o/p byte ADDL register contains 2 bits of Low byte and ADDH register contains High byte, so for copy ADDL value into ADDH i have to write like this for shifting 2 bits.
and after shifting i have to concate both register into ADDH and give this value to Port P0.
like
Am i right upto this?
if yes then is there problem in flag loop or clearing flag statement?
please check the following code.
please give me ur suggetion for this problem.
thanks in advance.
thanks for ur reply.
As u say that there is no problem in program upto first 5 statements.
I had again read the ADC section in datasheet. In that for getting ADC o/p byte ADDL register contains 2 bits of Low byte and ADDH register contains High byte, so for copy ADDL value into ADDH i have to write like this for shifting 2 bits.
ADDH << 2; ADDL << 2; ADDH << 2; ADDL << 2;
and after shifting i have to concate both register into ADDH and give this value to Port P0.
like
P0 (value_converted) = ADDH + ADDL;
Am i right upto this?
if yes then is there problem in flag loop or clearing flag statement?
please check the following code.
while((ADCON & 0x01)!= 0x01) { // Clear the End of conversion flag ADCON &= 0xEF; // read the value //value_converted = (ADDH << 2)+(ADDL); value_converted = ADDL; value_converted += (ADDH << 8); }
please give me ur suggetion for this problem.
thanks in advance.
[ Edited Fri Aug 20 2010, 07:04 pm ]
Sat Aug 21 2010, 12:38 am
i have already posted the correct flag statement.. please read comments too when i post code.
value_converted should be declared as unsigned int as your result is more than 8 bits so you need a bigger variable. unsigned int is 16-bit so it can store result easily..
value_converted should be declared as unsigned int as your result is more than 8 bits so you need a bigger variable. unsigned int is 16-bit so it can store result easily..
unsigned int value_converted; // Wait flag End of conversion while((ADCON & 0x01)!= 0x01){} //wait here ADCON &= 0xEF;// Clear the End of conversion flag // read the value value_converted = ADDH; value_converted = (value_converted << 2)+(ADDL); //now do whatever you want to do with value_converted //another way to debug your code is to print ADC value on serial port.
shaik like this.
Mon Aug 23 2010, 02:26 pm
hello ajay,
thanks for ur reply.
i had change in my program and now the code is as follows
I had one daut that when i write unsigned int value_converted; // ADC o/p then this variable contains 4 bytes = 16 bits, but for o/p i have only 8 bits in P0 pin P0 = value_converted;
In C none datatype contains 3 bytes for variable.
so is it possible to give 4 byte i/p for 8 bits o/p?
I set P0 for ADC o/p.I had run this program first in "Logic Analyser", but i not get any o/p in that, like some pins are 0 and some are 1.
Then i try to print the ADC decimal value on "Terminal", but it´s also not give any o/p value.
Is this statment is right if i want to assign ADC o/p value on P0 port P0 = value_converted;
I don´t understand wht is the problem?
please give me ur sugestion for this problem.
Thanks in advance.
thanks for ur reply.
i had change in my program and now the code is as follows
#include <stdio.h> #include "reg_c51.h" #define channel P1_7 // ADC i/p void main() { unsigned int value_converted; // ADC o/p ADCF = 0x80; // configure channel P1.7 for ADC ADCON = 0x20; // Enable the ADC ADCON &= 0xF8; // Clear the field SCH[2:0] ADCON |= channel; // Select channel ADCON |= 0x08; // Start conversion in standard mode while((ADCON & 0x01)!= 0x01) {} // Wait ADCON &= 0xEF; // Clear the End of conversion flag value_converted = (ADDH<<2 )+(ADDL); // read the value printf("%d",value_converted); P0 = value_converted; while (1) {} }
I had one daut that when i write unsigned int value_converted; // ADC o/p then this variable contains 4 bytes = 16 bits, but for o/p i have only 8 bits in P0 pin P0 = value_converted;
In C none datatype contains 3 bytes for variable.
so is it possible to give 4 byte i/p for 8 bits o/p?
I set P0 for ADC o/p.I had run this program first in "Logic Analyser", but i not get any o/p in that, like some pins are 0 and some are 1.
Then i try to print the ADC decimal value on "Terminal", but it´s also not give any o/p value.
Is this statment is right if i want to assign ADC o/p value on P0 port P0 = value_converted;
I don´t understand wht is the problem?
please give me ur sugestion for this problem.
Thanks in advance.
Mon Aug 23 2010, 04:56 pm
4 bytes = 16 bits <<<< errr... wrong
16bits is 2 byte
correct thats the reason i said not to use port for checking o/p better use uart.
you forgot to initialize your serial port before sending data to it. unitize uart then use printf.
16bits is 2 byte
but for o/p i have only 8 bits in P0 pin P0 = value_converted;
correct thats the reason i said not to use port for checking o/p better use uart.
Then i try to print the ADC decimal value on "Terminal", but it´s also not give any o/p value.
Is this statment is right if i want to assign ADC o/p value on P0 port P0 = value_converted;
I don´t understand wht is the problem?
please give me ur suggestion for this problem.
Thanks in advance.
you forgot to initialize your serial port before sending data to it. unitize uart then use printf.
shaik like this.
Mon Aug 23 2010, 06:35 pm
hey ajay,
oh..sorry for my misunderstanding.
1 byte = 8 bits.
Is there any another option for getting perfect o/p for 8 bits without UART?
Bcoz i don´t know much abt UART.
How can i write program using UART?
I know only full form of UART (Universal Asynchronious Receiver and Transmitter)
how can i initialize serial Port?
please give ur sugetion for this problem.
Thanks in advance.
oh..sorry for my misunderstanding.
1 byte = 8 bits.
Is there any another option for getting perfect o/p for 8 bits without UART?
Bcoz i don´t know much abt UART.
How can i write program using UART?
I know only full form of UART (Universal Asynchronious Receiver and Transmitter)
how can i initialize serial Port?
please give ur sugetion for this problem.
Thanks in advance.
Powered by e107 Forum System