Lab - MP Reports 8-9 Microprocessor
Lab - MP Reports 8-9 Microprocessor
Aim
Understanding writing codes in C for PIC 18F microcontroller
Theory
1) Chapter 7 of PIC Microcontroller and embedded systems by MAZIDI.
2) Data sheet of PIC microcontroller
Equipment
PC with MPLABX and XC8 compiler installed.
Tasks Marks
2) Binary to decimal conversion 4
3) BCD to ASCII conversion 4
Reason behind C programming of pic
The following are some of the major reasons for writing programs in C instead
of Assembly. I. It is easier and less time consuming to write in C than in
Assembly.
2. C is easier to modify and update.
3. You can use code available in function libraries.
4. C code is portable to other microcontrollers with little or no modification.
Example
Following C code toggles all pins of PORTC and PORTD continuously with an approximate delay
of 250ms
#include <P18F458.h>
voidMSDelay(unsigned int); void
main(vd)
{
TRISC = 0;
TRISD = 0;
while(1)
{
PORTC = 0x55;
PORTD = 0x55;
MSDelay(250);
PORTC = 0xAA;
PORTD = 0xAA;
MSDelay(250);
}
}
voidMSDelay(unsigned intitime)
{
unsignedinti; unsigned char j;
for(i=0;i<itime;i++)
for(j=0;j<165;j++);
}
Task1
Write a Cl8 program to convert packed BCD to ASCII and display the bytes on
PORTB and PORTC.
Procedure:
void main() {
TRISB = 0x00; // Set PORTB as output
PORTB = 0x00; // Initialize PORTB with 0
TRISC = 0x00; // Set PORTC as output
PORTC = 0x00; // Initialize PORTC with 0
bcd_to_ascii(packed_bcd);
while (1) {
// Your application logic here
}
}
Step 3: Compile and Load the Program
Compile the assembly code in MPLAB X IDE to generate a hex file.
Load this hex file into the PIC18F458 in your Proteus simulation
Step 4: Run and Debug in Proteus
Add virtual LEDs or a digital monitor in Proteus to observe the memory
addresses and verify the operation.
Start the simulation in Proteus, and check the LEDs or memory monitor to
ensure that the correct values are stored and calculated at the defined
addresses.
Proteus:
Task2
Write a C18 program to convert FD hex to decimal and display the digits on
PORTB, PORTC, and PORTD
Procedure:
void main(void) {
unsigned char data;
unsigned char adcValue;
unsigned char sendFlag = 0;
// Initialize ADC
ADC_Init();
while (1) {
if (sendFlag == 0) {
adcValue = ADC_Read(); // Read ADC value
if (TXSTAbits.TRMT) {
sendFlag = 0; // Clear the send flag if the transmission is complete
}
}
}
Step 3: Compile and Load the Program
Compile the assembly code in MPLAB X IDE to generate a hex file.
Load this hex file into the PIC18F458 in your Proteus simulation
Aim
Understanding writing codes in C for AVR microcontroller
Theory
1) Chapter 7 of PIC Microcontroller and embedded systems by MAZIDI.
2) Data sheet of PIC microcontroller
Equipment
PC with MPLABX and XC8 compiler installed.
Task1
Write an AVR C program to toggle all bits of Port B 50,000 times.
Procedure:
int main(void) {
DDRB = 0xFF;
while (1) {
PORTB ^= 0xFF;
_delay_ms(1);
return 0;
}
Step 3: Compile and Load the Program
Compile the assembly code in MPLAB X IDE to generate a hex file.
Load this hex file into the ATmega32 in your Proteus simulation
Task2
Write an AVR C program to toggle all the pins of Port C continuously with a 10 ms
delay. Use a predefined delay function in library “delay.h”.
Procedure:
int main(void) {
DDRB = 0xFF;
while (1) {
PORTB ^= 0xFF;
_delay_ms(10);
}
return 0;
}
Step 3: Compile and Load the Program
Compile the assembly code in MPLAB X IDE to generate a hex file.
Load this hex file into the ATmega32 in your Proteus simulation