You are on page 1of 3

Chapter #7

AVR Programming in C

Data Conversion
DDRB =0xFF; //Port B =O/P
1. ASCII to BCD, BCD to ASCII w&= 0x0F; //Mask upper nibble
µcontrollers have a real time clock (RTC) where w<<=4; // Make upper nibble
time & date are kept even when power is OFF. z&= 0xF0; //Mask lower nibble
The format is usually "Packed BCD". To display bcdbyte=w|z; //(w,z)=(upper,lower)=47
them, we convert into ASCII. PORTB=bcdbyte; //Display pack_BCD

return 0;
}

2. Calculate Checksum Byte


Checksum byte is an extra byte tagged to the
end of a series of bytes of data. Its use is to
maintain Data Integrity.
To calculate checksum byte,
For BCD->ASCII, convert into unpack_BCD, then 1. Add the bytes, drop the carries.
to pack_BCD. For ASCII->BCD, convert into 2. Take 2’s complement of sum.
unpack_BCD, then combine to get pack_BCD.

/*WAP to convert Pack_BCD 0x29 to ASCII &


display the bytes on Port B,C */
#include <avr/io.h>
int main (void)
{ /*WAP to calculate checksum byte for data */
unsigned char x,y; #include <avr/io.h>
unsigned char mybyte=0x29; int main (void)
{
DDRB=DDRC =0xFF; //Port B,C=O/P unsigned char x,chksum,sum=0;
x=mybyte&0x0F; //Mask upper nibble unsigned char mydata={0x25, 0x62, 0x3F, 0x52};
PORTB=x|0x30; //Display ASCII
y=mybyte&0xF0; //Mask lower nibble DDRA=DDRB=DDRC=0xFF; //Port A,B,C=O/P
y>>=4; // Make lower nibble for (x=0;x<4;x++)
PORTB=y|0x30; //Display ASCII {
PORTA=mydata[x]; //Display each byte on PortA
return 0; sum+=mydata[x];
} PORTB=sum; //Display sum byte on PortB
}
/*WAP to convert ASCII of 4,7 to Pack_BCD & chksum=~sum+1;
display the byte on Port B */ PORTC=chksum; //Display checksum byte on PortB
#include <avr/io.h>
int main (void) return 0;
{ }
unsigned char w=’4’,y=’7’; /*WAP to check data integrity. Send ‘G’ or ‘B’
unsigned char bcdbyte; to Port D if data is good or bad */
#include <avr/io.h> Data Serialization
int main (void) Sending a byte of data one bit at a time through
{ single pin of µcontroller.
unsigned char x,chksum=0; 1. Use Serial Port
unsigned char mydata={0x25, 0x62, 0x3F, 0x52,0xE8}; User has little control over sequence of
execution. Execution is based on standards such
DDRD=0xFF; //Port D=O/P as .
for (x=0;x<5;x++) 2. Control sequence of data & spaces in
chksum+=mydata[x]; between
chksum=~chksum+1; µcontrollers have a real time clock (RTC) where
if (chksum==0) time & date are kept even when power is OFF.
PORTD=’G’; The format is usually "Packed BCD". To display
else them, we convert into ASCII.
PORTD=’B’; /*WAP to send value 0x44 serially to PC3; LSB
first */
return 0; #include <avr/io.h>
} #define serPin3
int main (void)
3. Hex to Dec,BCD {
“printf” can convert data from hex to decimal & unsigned char x,conbyte=0x44,regALSB;
vice-versa. But it takes lot of memory space & regALSB=conbyte;
increases hex-file size. So, we need a conversion DDRC|=(1<<serPin); //Configure O/P
routine. for (x=0;x<8;x++)
/*WAP to convert 0xFD to decimal & display {
digits on Port B,C,D */ if (regALSB & 0x01)
#include <avr/io.h> PORTC|=(1<<serPin);
int main (void) else
{ PORTC&=~(1<<serPin);
unsigned char x,binbyte,d1,d2,d3; regALSB=regALSB>>1;
}
DDRB=DDRC=DDRD=0xFF; //Port D=O/P return 0;
x=binbyte/10; }
d1=binbyte%10; //Units Digit /*WAP to send value 0x44 serially to PC3; MSB
d2=x%10; // Tens Digit first */
d3=x/10; // Hundreds Digit #include <avr/io.h>
PORTB=d1;PORTC=d2;PORTD=d3; #define serPin3
//Display UTH digits on Port B,C,D int main (void)
return 0; {
} unsigned char x,conbyte=0x44,regALSB;
regALSB=conbyte;
Compilers have pre-defined functions to carry DDRC|=(1<<serPin); //Configure O/P
out data conversion. To do so, include “stdlib.h” for (x=0;x<8;x++)
in the code. {
if (regALSB & 0x80)
PORTC|=(1<<serPin);
else
PORTC&=~(1<<serPin);
regALSB=regALSB<<1;
} /*WAP to get value 0x44 serially from PC3;
return 0; MSB first */
} #include <avr/io.h>
/*WAP to get value 0x44 serially from PC3; #define serPin3
LSB first */ int main (void)
#include <avr/io.h> {
#define serPin3 unsigned char x, regA=0;
int main (void) DDRC&=~(1<<serPin); //Configure I/P
{ for (x=0;x<8;x++)
unsigned char x, regA=0; {
DDRC&=~(1<<serPin); //Configure I/P regA<<=1;
for (x=0;x<8;x++) regA|=(PINC&(1<<serPin))<<serPin;
{ //Copy serPin bit of PortC to LSB of regA
regA>>=1; }
regA|=(PINC&(1<<serPin))<<(7-serPin); return 0;
//Copy serPin bit of PortC to MSB of regA }
}
return 0; Memory Allocation
}

You might also like