UNIT
PROGRAMMING IN C FOR PIC
: PART-A
SHORT QUESTIONS WITH SOLUTIONS
Q1. What is a data type? List the various data types in C that are used in PIC18 microcontroller,
Ans: Model
Data Type
A data type is used to indicate the type of data value stored in a variable.’The data type of a variable is specifi
time of its declaration and is attached till the end of execution of the program.
Data Types in PIC18 Microcontroller
The various data types of C that are used in PICI8 microcontroller are as follows,
char
unsigned char
int
pre
4. unsigned int 5
5. short
6. unsigned short
7. tong, é
8. unsigned long
9. short long
10.__unsigned short long.
2. Write @ short note on bit-addressable /O programming,
Ans:
ible and bit-addressable. The ke
The /O ports of PICI8 are byte accessib ss
bits of VO poris and the format used is, PORT * bits.xy, where ‘x’ indicates t
port.
'yword sbit is used to access the Si
he ports 4, B, Cot D and ‘y* indi
“The single bit addresses of a PIC18 are shown in the below table,
— ~ 128 to 127
ye ees Owr25
int 32,768 t0 + 32,767
unsigned int 0 to 65,535
a + 32,768 to 32,767
‘ unsigaed sett 0 t065,535
short long, ~ 8,388,608 t0 + 8,388,607
i unsigned short long 010 1677215
~PN47485,648 10 +2,147.483,649
‘unsigned Jong Lg 42
SPECTRUM ALL-IN-ONE JOURNAL FOR ENGINEERING STUDENTSMICROPROCESSORS AND MICROCONTROLLERS [JNTU KAKINADA]
out logical operators in G.
is referred as logical operator. These operators
The operator which is used
to combine or relate two or more expressio
sf are tabulated below,
ae useful in decision making and in testing more than one condition. These are three types. which
Operation
Logical Operator
&& Logical AND
Logical OR
Logical NOT
Q4, Write a short note on,
(i) Bitwise operator
)_Bitwise shift operator
Ans:
ise Operator ;
Bitwise operators are similar to that of logical operators except that they work on binary bits. When bitwise operators
are used with Variables, they are internally converted to binary numbers and then bitwise operators are applied on
jaaividal bite, Bitwise operators do manipulation on bts stored in memory. These operators work with ‘char’ and “int”
“
data types.
(ii) Bitwise Shift Operator
bitwise logical operators, there are two more operators available in C and are referred as bitwise shift
In addition 10
operators. They ate,
(a) Shiftright -
(b)_ Shift left ‘
05. Write the SatamneEmEpere ec ASCH format
Ans: i 11 Format
rm:
steps to Convert a Packed BED #9 ASC! :
paceman aso ate a low,
The steps to vépuckd BCD.
1. Convert the packed BED '®
. ackes BoD with 0! 1000030H),
2. Now, Tag Unt
Example
(0x32, 0x39
00110010, 00111001
Sy Oe. non te
es ee 8M, Tm cos ac mon
af orerta, The checksU byte is calculated by adding the bytes t,,-<47 °? Yt Which is
Surge during operation : resultant sum gives the checksum byte. When ‘ogether and negl.
assigned at the end of Lot ey uncorupted Ifthe um isnot zero, one or mons jum ofall he bytes, ncleeal
Correspon carry ore bytes o re cha
Siting ee ae
corrupted data. _UNIT-6 Programming in C for PIC
Q7. Write a.C18 program to turn bit § of Port B on and off 50,000 times.
Ans:
include
define MYBIT PORTBbits.RBS
void main(void)
‘unsigned inti;
TRISBbits.TRISBS = 0;
Making PORTB.$ as an output
for{i-0;i
‘define getbit PORTBbits RBO
+#define sendbit PORTCbits. RCT
void main(void)
{
TRISBbits.TRISBO = 1; //making PORTB.0 an input
TRISCbits.TRISC7=0; _//making PORTC.7 an output
“get a bit from RBO
; H6.4
MICROPROCESSORS AND MICROCONTROLLERS [JNTU-KAKINADA]
* 7
TIONS WITH SOLUTIONS
SRT ws
09, Give a brief int F Carob ha)
program in C2 ‘0M PIC18 programming in C. What are the different reasons to use PIC18 |
Ans:
Model Papers, Q7(a)
PICI8 Programming in ©
on-chi > RONG nroller programing, the compiler generates large size hex file but the microcontrollers aré limited
ns Space for coding is limited to 2M only. Therefore, compilers and assemblers are used to convert
oer aaatuage oF high-level language codes to a compressed machine code that willbe later on stored in the
aeemans memory on the basis o device ype, The program memory may be permanent in ROM ora erasable read-
Reasons to use PICI8 Program in C
There are various feasons t0 use PICI8 program in C and they are as follows,
C level programming allows the implementation of large and critical programs in less time.
% The compiler and development tools in C are very efficient.
C provides different data types, arrays, structures, variables which makes the program simpler and understandable.
+ During compilation process, the compiler detects the errors and it can be rectified (in meanwhile). Therefore, it
reduces errors while running the program. |
+ C makes use of different statements such as.
execution of a program in a sequence,
Once the code is written in G, it can be used in other microcontrolicr. Since, C compiler automatically switches to the
corresponding microcontroller’s pre-directives, ie. the intial commands given to the compiler before starting the
program execution: =”
if else, for, while, do-while, switch for easy flow of program ie.
Example
es which consists of various functions. So, the user can directly
% provides standard Hbavits ead of declaring them again which reduces complexity.
by just giving the im
‘use them in their program
Example
math.h, stdio.hy ¢
the program can be divided into sub-programs called module. Th
aby bet modules of the same programs or differen programe consists
cr ie asembly language code can be we along with © language code
ler are controlled easily withthe use of C programming.
‘Mode! Papert, a7)
‘data value stored in a variable. The data
execution of the program.
‘pe of a variable is specified atthe
a smaller hex files. For this
Tevaiow din YpesofC tatareuedot res
C that are used of Pic ne Y2tious dataUNIT-6 Programming in C for PIC ;
[Data Type Size
| char 8-bit
I -vadllgned Char 8-bit
) int 16-bit 168 tors
| unsigned int 16-bit 0 to 65,535
short 16-bit 32,768 to 32,767
) ‘unsigned short 16-bit 0 to 65,535
short long 24-bit ~ 8,388,608 to + 8,388,607
| unsigned short tong “| 24-bit 010 16,777,215
| long 32-bit 2,147,483,648 to + 2,147,483,647
|__unsigned tong __|__32-bit__ __010 4,294,967,295
1. Unsigned char
“Unsigned char’ data type is a frequently used data type in PICI8 programming. It is a 8-bits (I-b
't accepts the values in the range 0-255 (j.c. 00-FFH), It is used in many cases like setting a counter value and
‘whe the signed data i not required (In uch cases, user must use the unsigned char type). Note tha, ifthe chara
mentioned as unsigned then, the C compiler will automatically consider it asa signed type.
Since, the number of registers and data RAM locations are limited in PIC18 microcontroller. The usage
hhex file. User should be careful while declaring the variables. Use the unsigned char data type
Example 1
toa lar;
PICI8 program to send the values 00-FF to PORTB
include _//declaration of TRISB and PORTS
void main(void)
unsigned char x;
TRISB = 0; Jimake Port B an output
for(x=0;x<=255;x++)
PORTB
while (1); {Required when a program running in hardware
When the above program is executed on simulator, the PORT B displays the values 00
‘The unsigned char data type is also used for ast
FFH in binary
ASCII char
of ASCII characters and extended
Example 2
/P1C18 program to send the hex vals forte ASCH characters FN, 1K, 410, 9, 1g
include
void main(void)
unsigned char Str{ J= “NIKHIL0916”; /fTbe data is stored in RAM
unsigned char x;
me ‘make Port B an output
for(x-0:x2103x4+4) i
PORTB = Stix: : :
while(1)5 J)Required when program is running in hardware
} ‘ -
a ENGINEERING STUDENTS :
SPECTRUM ALLIN-ONE JOURNAL FOR ENN ae
sinh ¢ a6.6
above pro LLERS [JNTU-KAKINADA]
‘When the > MICROPROCESSORS AND MICROCONTRO
B displays the val Seccutedon simular
Sieh aS eR NAIK Ate ee
91,6) ‘Values for
Example 3
ICIS prog <
stam t0 toggle all the bits of PORT
continuously
Hinchade
void main(void)
TRISB = 0;
for(;;)
PORTB = 0x55;
PORTB = OxAA;
)
When the above program is executed, the simulator will
display how the PORT B toggles continuously.
2. Signed char
“Signed char’ is a 8-bit data type and it accepts the values
in the range— 128 to + 127. Itmakes use of most significant bit
i.c., D7 of D7-D0 to represent the negative and positive values.
113s used inthe place of where ve and +ve values are required
to be declared like temperature.
Notice that, the
unsigned char will be tt
Example
C18 program
1/53 to PORT B
Unsigned int
“Unsigned int’ is a 16-bit data type and it accepts the
values in the range from 0 to 65,535 (ie, 0000 ~ FFFFH). It
Yan also set the counter values greater than 256. Since, PIC18
is a8-bit microcontroller the ‘int’ type takes 2-bytes of memory
(RAM), Because ofthe large size, misuse of int data types leads
toa larger hex file. For some PCS with 512MB memory, 32-bit
pentium’s registers and memory accesses, and a bus speed of
133MHZ. The misuse of int data type is negligable, Use the
unsigned data type when there is no need of declaring signed
data, Note that, the C compiler will take the int as signed int
unless it is declared as unsigned.
Example
C18 program to toggle all bits of PORT C 50,000 times
include
void main(void)
{
‘unsigned int ch;
TRISC = 0; //Naking PORT Cas an output
for(ch=0;ch<=50000;ch++)
{
PORTC = 0355;
PORTCC= 0xAA; ;
}
while(1); ‘/Repeat loop continuously
}
4. Signed in
‘Signed int'is a 16-bit datatype and it accepts the values
C18 program to toggle all bits of PORT C25,000 times
include
yoid main(void)
af x
int ch;
TRISC =0; aera
for(ch=0;ch<=25000;ch++)
t
PORTC = 0x55;
Cas an outputUNIT-6 Programming inc for PIC
Other Data Types
JC other than unsigned int, signed int, unsigned char, signed chat some other datatypes are also used,
the values greater than 16-bit, ic. short long is a 24-bit type and long is a 32-bit data type.
Example
/C18 program to toggle all bits of PORT € 100,000 times
#include
void main(void)
unsigned short long ch;
unsigned int p;
TRISC=0; //Making PORT C as an output
for(ch= }00000;ch++)
PORTC=0x55;
PORTC=0xAA;
while(1); ‘/Repeat loop continuously
)
6.2 0 PROGRAMMING é a
Q11. Explain input/output programming in C with examples.
Ans:
1/0 Programming
VO programming specifies the way in which different 1/ operations are carried out
ming in C can be done in two different ways. They are,
by a computer.
1. Bytesize YO
2. Bitaddressable 1/0.
1. Byte Size /0
In Byte size I/O programming the ports PORT A, PORT B, PORT C, POR’
labels of the ports defined in the C18 header file.
-addressable 1/0
T Dare byte accessible,
For answer refer Unit-V1, QI2.
Example v
‘The 1/0 program in to display the count fom 000 FFH onthe LEDs which are connected to
# include
# define LED PORTD / Defining P2 as LED
void main(void) :
{ , 4
TRISC = 0; 1/ Making PORTC as output
TRISD = 0; 1/ Making PORTD as output
PORTC = 00; Clear PORTC 4
SPECTRUM ALL-IN-ONE JOURNAL FOR ENGINEERING STUDENTSa
i KINADAI
ES MICROPROCESSORS AND MICROCONTROLLERS [JNTU-KAI
LED
: 1 Clear PORTD
; Repeat loop continuously
‘oerement PORTC
‘/nerement PORTD
a Write brief about bit-addressable 1/0 programming with example.
ns
The VO ports of PICI
vidual
are byte accessible and bit-addressable. The keyword sbit is used to access the Single/indivi
bis of VO pons and the format used is, PORT x bitsy, where x indieats the ports A,B, Cor D and ‘y" indicate bits 0-7 of a
or
The single bit addresses of a PICIS are shown in the below table,
PORTA | PORTB | PORTC | PORTD | PORTE | Port's Bit
RAO RBO RCO RDO REO DO
RAL RBI RCI RDI REL DI
RAZ RB2 RCD RD2 RE2 D2
RA3 ‘RBS RC} RD3 D3,
RAG ‘RBS RC4 RD4 D4
RAS RBS RCS RDS DS
RBG ROG RD6 D6
RB7 RCT RD? D7
: Table: Single bit Addresses of PICT8F548/4580 Ports
Port Bits Structure f
‘The structure of a PORT B bit as provided by C18 C complier is given as below,
extern volatile near unsigned char PORT!
extern volatile near union
{ . ;
struct :
( :
unsigned RBO:1;
unsigned REI;
unsigned RB2:I;
unsigned RB3:15UNIT-6 Programming in C for PIC S following C18 program:
a 13. Write the
unsigned CANTX:1; the byte of da
to access
unsigned CANRX:1; (Prog ree, wait for 500 ms(1/2 Seco
unsigned :1; then send it to part B-
unsigned PGM:1; 1m to get a byte of data
ii) Prograt
unsigned PGC:1; (1) iritis less than 500, send itto
unsigned PGD:1; Mode!
igned PGD:1 fis
% i byte of data
i) _//C 18 program to access the
ee ait Fr s00ms (1/2 seconds) and then send
Example 1
= #include 4
‘C18 program to toggle only RB7 bit continuously
‘without disturbing the rest of the bits of PORT B void Delay(unsigned int);
#include //Passing the p
define bit! PORTBbits RB7 //declare single bit ‘/unsigned int
void main(void)
id main(void) 5 void main( )
{
‘TRISBbits.TRISB7=0; making RB7 an output t
while (1) unsigned char byte; // Declaring byte
{ : TRISC=0xFF; —_ // Making PORT Cani
= cer eS ‘TRISD = 0; // Making PORT Di
bitt ‘turning off RB7 i
i While(1), ‘repeat loop
) ‘
Se byte=PORTC; —_// Access byte fi
{/C18 program to monitor bit RC4. If itis HIGH, send
//5SH to Port B; otherwise, send AAH to Port D Delay (500); Jhwait for 12 s
#include PORTD=byte; _//send byte to PORE
#define bit! PORTCbits:RC4 ‘Declaration of ‘
Isingle-bit
void main(void) }
t ‘oid Delay (unsigned int count)
TRISCbits TRISC4 (/Making RC4 an input : a
‘TRISD=0; /Making PORT D
an output unsigned inti: Decaring i)
while A
; @ fori = 0; i
and a buzzer
program to monitor the door sensor, and when
it opens, sound the buzzer. You can sound ‘define LCDMessage PORTB
the buzzer by sending a square wave of a few I The declaration LCDMessage
hundred Hz frequencies foe #define Enable_pin PORTCbits.RC2
‘The Enable pin
| Ans:
include
void MSDelay(unsigned int);
void main(void)
{
define Door_sensor PORTBbIts RB} unsigned char msg{] ="The Earth is but One Country”;
‘define bzr PORTChits.RC7 unsigned char ch;
void main(void) TRB //Making PORT B as an output
, TRISChits TRISC2=0; //Making PORTC.2.as an output
RIsBbits TRISBI = 15 5a for(ch=0;ch28;ch++) //sending all the 28 character
/yMaking PORTB.1 as an input | ( "
RISCbits.TRISCT = % LCDMessage = msafch];
Making PORTC.7 #8 4" Ces Enable_pin=1; :
while(Door_sensor—= 1) Enable_pin-0;
{ }
bar=0; while(1);
‘Repeat loop continuouslyare.
Ans:
ai7.
Ans:
Progr:
UNIT-6 Programming in C for PIC
ontinuou:
Write a C18 program to toggie all he bits of Port B, Port, and Port D
#include
Void MSDelay(unsigned int); Z
Void main(void)
TRISB = 0; ,
TRISC = 0;
TRISD = 0;
while(1)
PORTB = 0x55;
PORTC = oxs5;
PORTD = 0x55;
MSDelay(250);,
PORTB = OxAA;
PORTC = OxAA;
PORTD = 0xAA;
MSDelay(250);
}
,
void MSDelay(unsigned int count)
t
unsigned int i 7
unsigned char jz é
for(i=0;i
#define getbit PORTBbits.RBO
define sendbit PORTCits. RCT
void main(void)
{ 4 : ~
‘/making PORTB.O an input
‘/making PORTC.7 an output6.12
-KAKINADAI
dt MICROPROCESSORS AND MICROCONTROLLERS [JNTU-KA\
sendbit = get
* Hestabit from RBO and send tio RCT
Disassembly :
: ‘include
a ‘define getbit PORTBbits. RBO
3: ‘define sendbit PORTCbits.RC7
4 void main(void)
5 t
6: __ TRISBbits.TRISBO (imaking PORTB.0 an input
00002’ 8093” BSF ox193, 0, ACCESS
7: TRISCbits.TRISC7 = 0; /Imaking PORTC.7 an output
0000E4 _9E94 BSF Oxf94, 0x7, ACCESS
8 + while)
0000F2 D7F9 BRA Oxe6
9: { .
10: sendbit = getbit; i ‘/Iget a bit from RBO and send it to RC7
0000E6 5081 MOVF —_Oxf®1, W, ACCESS
0000E8 OBOL_— ANDLW Oxi
0000EA £002. BZ Oxf
Q000EC 8E82._BSF_OxfR2, 0x7, ACCESS
0000EE DOOL_-BRA* Oxf2
000f0. 9E82 BCE Oxf82, 0x7, ACCESS
oa ‘Model Paper, a7(b)
inst ; : fo gmbine or relat two or more expressions i referred as logical operator. These operators
she opeitr 8 types, tabulated as follows,
are useful in decision ™ |UNIT-6 Programming in C for PIC
@ Logical AND
Logical AND operator provides true value only when both the relational expressions are true
Synta:
xp 1 && exp 2
(i) Logical OR
The result of logical OR expression will be false only when both the relational expressions are false.
Syntax: exp 1 |j exp 2
Logical NOT
The result of this expression will be true, if the predefined expression is false and vice versa.
Symtax: exp != exp2
The functions of the above three logical operators can be easily understood by the below table, *
Tmputs | 4 pleat AND Logical NOT
als] A&B YI=B
o}o 0 0 1 i
of}. ° 1 1 °
1}o 0 1 0 1
a et 1 1 0 0
Table (2): Truth Table for Logical Operators
Q19. Explain the following logical operators of C with an example,
(i) Bitwise Operator
(i) Bitwi
shift operator.
Ans:
@ _ Bitwise Operator
Bitwise operators are similar to that of logical operators except that they work
are used with variables, they are internally converted to binary mimbers and then by
bits.
rk on binary bits. When bit
itwise operators are applied:
Bitwise operators perform manipulation on bits that are stored in memory. These
data types.
‘These operators and their operations are listed in table below,
operators work withPICIS program to illustrate the bitwise logical operators
# include
void main (void)
TRISB = 0;
TRISC = 0;
TRISD
TRISE
PORTB = 0X25 & OX2B ; // ANDing
PORTC = 0X05 | 0X20; // ORing
PORTD = 0X30" 0X65; /XORing
PORTE
0x79; “inverting
while (I);
(ii) Bitwise Shift Operator
In addition to bitwise logical operators, there are two more operators available’ in C called as bitwise shift operators,
They are,
@ Shift right
(©) Shift eft
“Table (2k Bitwise Shift Operators
Example : the bitwise shift operators
/PICI8 program 10 ;
# include < PISF458.47
void main (void)
{
TRISB =0;
TRISC=05UNIT-6 Programming in C for PIC
Q20. Write the following C18 programs,
i)
@
‘SPECTRUM ALL-IN-ONE JOU!
Program to toggle all the bits of PORT B
and PORT C continuously with a 250 ms
delay using inverting operator.
Porgram to toggle all the bits of PORT B,
PORT C and PORT D continuously with a
250 ms delay by using XOR operator.
‘C18 program to toggle all the bits of PORT B and PORT
C continuously with a 2SOms delay using inverting
‘operator
#include
‘Void MSDelay(unsigned int);
void main(void)
‘TRISB = 0; ‘making PORT B as output
TRISC=0; —_//making PORT C as output
PORTB = 0x35;
PORTC = 0x35
while(1)
PORTB = ~PORTB;
PORTC = ~PORTC;
MSDelay(250);
)
void MSDelay(unsigned int i_time)
{
unsigned int p;
unsigned char g;
for(p=0; p
void MSDelay(unsigned int);
void main(void)
{
TRISB = 0;
TRISC = 0;
TRISD = 0;
‘making PORT B as output
Jimaking PORT C as output
making PORT D as output
21.
PORTB = 0x35;
PORTC = 0x35;
PORTD = 0x35;
while(1)
PORTB = PORTBOxFFS
PORTC = PORTC’OxFF;
PORTD = PORTD*0xFF;
MsDelay(250);
) 4
} b
void MSDelay(unsigned int i_time)
{
unsigned int p;
unsigned char q;
for(p-0; psi
for (q=0; q<165 ; a+) ;
ime; p++)
Write a C18 program to read the
RBI bits and issue an ASCII cl
according to the following table.
0 0 | Send“0"to PORTD
° 1 | Send 1” to PORTD
1 0 Send ‘2° to PORTD.
1 1__| Send $3” to PORT.
‘include
void main(void)
{
unsigned char p;
TRISB=OxFF; _//make Port B an:
TRISD = 0; ‘make Port D an.
while (1) tepele
{
P=PORTB; —_//read PORTB
P=P&0x3; //mask the unused
switeh(p)
%
case(0): 3“ KINADA]
MICROPROCESSORS AND MICROCONTROLLERS [JNTU-KAI
YOR
Tissue ASCO
break;
case(|)
PORTD “Fissue ASCH 1
break;
case(2)
Hissue ASCH 2
Ufissue ASCU3
RE 6 vee
022. Explain in detail about data conversi _ ‘
Ans:
Microcontrollers have
applications and to maintain the ti
ve convedil to ASCH format to display. The
Mode! Papers, 7()
‘Real-Time Clock (RTC) where itis used to provide precise time and date in various real time
Mime reference even if power goes off. The data will be provided in packed BCD and it should
"ASCII code for digits 0-9 are as given below,
11 0001
11 0010 (0000 0010
O11 0011 0000 0011
011 0100 0000 0100
11 o101 0000 0101
on 0110 0000 01110
ououi (0000 0111
: ‘011 1000 0000 1000
39 011 1001
L 1001
“Table: ASCII Codes for Digits 0-9
“The steps 10 cOmveHE TA packed BCD.UNIT-6 Programming in C for PIC
Example
‘ASCH
Packed BCD Unpacked BCD eae
0x29) (x02, 0x09 Hi
00111001
9101001 | 00000010, 00001001 _| 00110010.
‘on PORT B and PORT C.
C18 program to convert packed BCD 0x29 to ASCII and display the bytes on P
#include
‘oid main(void)
{
unsigned’char a, b, ¢;
unsigned char bytel = 0x29;
TRISB = 0; (/Making PORT B as an output
TRISC = 0; ‘Making PORT C as an output
a= bytel & Ox0F; {Mask upper 4 bits
PORTB = a| 0x30; ‘Making Port B ASCIL
b=bytel & OxFO; INiask lower 4 bits
b=b>>4; “Uishift it to lower 4 bits
PORTC = b | 0x30; Making PORT C ASCIL
ASCII to Packed BCD Conversion
The steps to convert the ASCII to packed BCD are as follows,
Convert ASCII to unpacked BCD.
2. Then, combine it to obtain the packed BCD.
Example ;
‘Key ASCIL Unpacked BCD Packed BCD
4 34 ‘00000100
1 BI 00000111 01000111 (or) 47H
1C18 program to convert ASCII digits of 4” and ‘7 to packed BCD and display it on PORT B,
#include
void main(void)
s
unsigned char bedbyte;
unsigned char a
‘unsigned char b = ‘7"
TRISB = 0; //Making Port B as an output
a=a& 0x0F; //Mask 3 *
{/Shift left to make upper BCD di
‘mask 3
‘veombine to make packed BCD‘AKINADA]
oe MICROPRocEssons AND MICROCONTROLLERS [JNTU
023. Explain the concept of
PES *f checksum byte in ROM,
ond Process ours due to current
ity ne of detecting any erors in the contents of ROM. The errors of ROM oselt h Nc
“Stelaeodoagetie Seam mpc ana ec Cs en
‘of data, The checksim byt is calculated by adding the ns rt ate
theresuant sum gives the checksum byte, When the sum of The ane
‘tid to be uncorrupted. Ifthe sum is not zero, one oF more byte ee
corrupted data,
Example
To find checksum byte, the values must be added i,
32h + 45h+ 67h + 12h + 11h +32 h
So dropping the carry generates 33h,
Now 2's. complement of 33 h=CDh
So CDh is the checksum byte
‘Now, to ensure data integrity perform the below operation.
32h + 45h-+ 67h-+ 12h-+ 11h+32h+DCh=200h
=133h
So dropping the carry generates O0h ensuring thatthe data is not corrupted.
If any other value except 00h is obtained then it méans thatthe data in the ROM is corrupted.
Example
‘(C48 program to ealeulate the checksum byte forthe data given in above example
include PISF4S8A>UNIT-6 Programmi :
ing in C for PIC jre data
to ensul rwis
G24. Write a C16 program to perform the checksum [email protected]'to pORTD: Othe"
3FH, 52H, OxE8. If the data is good, send ASC!! chi
Ans:
include
void main(void)
tunsigned char data] = {0x25,0x62,0x3F,0x52,08E8}
unsigned char checksum = 0;
unsigned char i
TRISD = 0; ‘make Port D an output
for(i-O;i
void main(void)
ch, binbyte, dec, dee2, dee3;
//Making Port B as an output
Making Port C as an output
unsigned char-KAKINADA]
3g MICROPROCESSORS AND MICROCONTROLLERS (JNTU
ee ‘Mbinary (hex) byte
ch = binbyte / 10; Ws
ne (Minding remainder (LSD)
oa “middle digit
dec3 = x/ 10; Tonost-signiticant digit (MSD)
PORTS = dee;
PORTC = deed;
PORTD = dee3;