0% found this document useful (0 votes)
13 views12 pages

Lab - MP Reports 8-9 Microprocessor

The document outlines Lab 8 and Lab 9 activities focused on programming PIC and AVR microcontrollers in C. It includes tasks such as binary to decimal conversion, BCD to ASCII conversion, and toggling pins on microcontrollers, along with detailed procedures for setting up the environment, writing code, and debugging in Proteus. The aim is to enhance understanding of C programming for embedded systems using specific microcontroller architectures.

Uploaded by

Mustafa Aasim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views12 pages

Lab - MP Reports 8-9 Microprocessor

The document outlines Lab 8 and Lab 9 activities focused on programming PIC and AVR microcontrollers in C. It includes tasks such as binary to decimal conversion, BCD to ASCII conversion, and toggling pins on microcontrollers, along with detailed procedures for setting up the environment, writing code, and debugging in Proteus. The aim is to enhance understanding of C programming for embedded systems using specific microcontroller architectures.

Uploaded by

Mustafa Aasim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Lab 8: Pic Programming in C

Conducted on:17-04-2024 Submitted on:02-05-2024

Instructor: DR. USMAN ALI


Lab Engineer: MAM ROZEENA

1) M. AYYAN ARSHAD 2) M. UMAR FAROOQ

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:

Step 1: Set Up Proteus Environment


 Open Proteus and Create a New Project: Choose a suitable workspace size.
 Place the PIC18F458 Microcontroller: Search for PIC18F458 in the component
library and place it on your schematic.
 Configure the Oscillator and Power Connections: Connect a 4 MHz crystal
with 22 pF capacitors to the OSC1 and OSC2 pins. Also, connect VDD to 5V
and VSS to GND. Make sure MCLR is connected to VDD through a pull-up
resistor (typically 10 kΩ).
Step 2: Write C Code
#include <xc.h>
unsigned char packed_bcd = 0x74;
// Function to convert packed BCD to ASCII
void bcd_to_ascii(unsigned char bcd) {
unsigned char tens = (bcd >> 4) + '0'; // Convert the tens digit to ASCII
unsigned char ones = (bcd & 0x0F) + '0'; // Convert the ones digit to ASCII
PORTB = tens; // Display tens digit on PORTB
PORTC = ones; // Display ones digit on PORTC
}

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:

Step 1: Set Up Proteus Environment


 Open Proteus and Create a New Project: Choose a suitable workspace size.
 Place the PIC18F458 Microcontroller: Search for PIC18F458 in the component
library and place it on your schematic.
 Configure the Oscillator and Power Connections: Connect a 4 MHz crystal
with 22 pF capacitors to the OSC1 and OSC2 pins. Also, connect VDD to 5V
and VSS to GND. Make sure MCLR is connected to VDD through a pull-up
resistor (typically 10 kΩ).
Step 2: Write C Code
// Function to initialize ADC
void ADC_Init() {
ADCON1bits.PCFG3 = 1; // Set AN0 (PIN0) as analog input
ADCON1bits.PCFG2 = 0;
ADCON1bits.PCFG1 = 0;
ADCON1bits.PCFG0 = 0;
ADCON2bits.ADFM = 1; // Right-justify the result
ADCON2bits.ACQT2 = 1; // Set acquisition time to 16 TAD
ADCON2bits.ACQT1 = 0;
ADCON2bits.ACQT0 = 0;
ADCON2bits.ADCS2 = 1; // Set ADC conversion clock to FOSC/16
ADCON2bits.ADCS1 = 0;
ADCON2bits.ADCS0 = 0;
ADCON0bits.ADON = 1; // Enable ADC module
}

// Function to read ADC value


unsigned char ADC_Read() {
ADCON0bits.GO = 1; // Start ADC conversion
while (ADCON0bits.GO); // Wait for conversion to complete
return ADRESH; // Return the 8 most significant bits of the result
}

// Function to send a byte over UART


void UART_Write(unsigned char data) {
while (!TXIF); // Wait until the transmitter is ready
TXREG = data; // Send the data
}

void main(void) {
unsigned char data;
unsigned char adcValue;
unsigned char sendFlag = 0;

// Initialize UART with a baud rate of 9600


TXSTA = 0b00100100; // Enable transmission, 8-bit data, asynchronous mode
RCSTA = 0b10010000; // Enable reception, 8-bit data, continuous receive
SPBRG = 25; // Set baud rate to 9600 (Fosc = 8MHz)

// Initialize ADC
ADC_Init();

while (1) {
if (sendFlag == 0) {
adcValue = ADC_Read(); // Read ADC value

// Convert 10-bit ADC result to 8-bit


data = adcValue >> 2;

UART_Write(data); // Send the data over UART


sendFlag = 1; // Set the send flag
}

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

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:
Lab 9: AVR Programming in C

Conducted on:17-04-2024 Submitted on:02-05-2024

Instructor: DR. USMAN ALI


Lab Engineer: MAM ROZEENA

1) M. AYYAN ARSHAD 2) M. UMAR FAROOQ

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.

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.
1. C is easier to modify and update.
2. You can use code available in function libraries.
3. C code is portable to other microcontrollers with little or no modification.

Task1
Write an AVR C program to toggle all bits of Port B 50,000 times.
Procedure:

Step 1: Set Up Proteus Environment


 Open Proteus and Create a New Project: Choose a suitable workspace size.
 Place the ATmega32 Microcontroller: Search for ATmega32 in the component
library and place it on your schematic.
Step 2: Write C Code
#include <avr/io.h>
#include <util/delay.h>

int main(void) {

DDRB = 0xFF;

while (1) {

PORTB ^= 0xFF;

_delay_ms(1);

for (int i = 0; i < 50000; i++) {

asm volatile ("nop");


}
}

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

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 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:

Step 1: Set Up Proteus Environment


 Open Proteus and Create a New Project: Choose a suitable workspace size.
 Place the ATmega32 Microcontroller: Search for ATmega32 in the component
library and place it on your schematic.
Step 2: Write C Code
#include <avr/io.h>
#include <util/delay.h>

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

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:

You might also like