Helps me with this question Toggling data in PORTB (12%) The book had shown how
ID: 3789675 • Letter: H
Question
Helps me with this question
Toggling data in PORTB (12%)
The book had shown how PORTB data can be toggled between 0x55 and 0xAA (which is one’s complement of 0x55).
Show in C language three ways of toggling data in PORTD between decimal 80 and its one’s complement with 300 ms delay.
What is the value of that one’s complement?
How should you set TRISD in such code? (hint: look at examples of chapter 7. You do not have to include whole program such as #include <P18F458.h>, void main (void) etc.). You can assume code of MSDelay as follows without rewriting that.
void MSDelay(unsigned int itime)
{
unsigned int i;
unsigned char j;
for(i=0;i<itime;i++)
for(j=0;j<165;j++);
}
Explanation / Answer
First Method:
void MSDelay(unsigned int itime)
{
unsigned int i;
unsigned char j;
long binary;
for(i=0;i<itime;i++)
for(j=0;j<165;j++);
binary = decimalToBinary(80);
first(binary);
}
long decimalToBinary(long n) {
int remainder;
long binary = 0, i = 1;
while(n != 0) {
remainder = n%2;
n = n/2;
binary= binary + (remainder*i);
i = i*10;
}
return binary;
}
void first(long binary)
{
int i;
char *c = &binary[0];
char *c1 = &binary1[0];
do
{
if(*c=='1')
*c1='0';
else
*c1='1';
c++;
c1++;
}while( *c !='' );
printf("n 1st Complement is =>> %s n",binary1);
}
Second Method:
void MSDelay(unsigned int itime)
{
unsigned int i;
unsigned char j;
for(i=0;i<itime;i++)
for(j=0;j<165;j++);
}
Third Method:
void MSDelay(unsigned int itime)
{
unsigned int i;
unsigned char j;
long binary;
for(i=0;i<itime;i++)
for(j=0;j<165;j++);
binary = decimalToBinary(80);
first(binary);
}
long decimalToBinary(long n) {
int remainder;
long binary = 0, i = 1;
while(n != 0) {
remainder = n%2;
n = n/2;
binary= binary + (remainder*i);
i = i*10;
}
return binary;
}
void first(long binary)
{
char onesComplement[100];
int counter, error=0, digitCount;
digitCount = strlen(binary);
for(counter=0; counter < digitCount; counter++) {
if(binary[counter]=='1') {
onesComplement[counter] = '0';
} else if(binary[counter]=='0') {
onesComplement[counter] = '1';
} else {
printf("Error :( ");
return 1;
}
}
onesComplement[digitCount] = '';
printf("Ones Complement : %s", onesComplement);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.