(حل و پاسخ تمارین آز ریزپردازنده)
Practice 1 #
Use 8 LED and light them up in simulator
Devices :
- ATMEGA32
- LED-RED
CODE #
/*
* Flasher.c
*
* Created: 9/29/2024 11:13:44 AM
* Author: AMIR-MOHAMMAD-SAFARI
*/
#include <delay.h>
#include <mega32.h>
int i;
void main(void) {
DDRD = 0xFF; // Set PORTD as output
while (1) {
// First loop to shift bits from 1 to 128
for (i = 1; i <= 128; i = i * 2) {
PORTD = i;
delay_ms(100);
}
// Second loop to shift bits from 64 back to 1
for (i = 64; i >= 1; i = i / 2) {
PORTD = i;
delay_ms(100);
}
}
}
Schematic #
Practice 2 #
Light up the LED Counter in simulator
- 0 to 9
- 9 to 0
- Speed of counting
- Count 0 to 5
Devices :
- ATMEGA32
- 7SEGMENT
- SW-SPDT
- 7447 Decoder
CODE 0 to 9 and 9 to 0 #
/*
* CounterLED.c
*
* Created: 10/6/2024 9:43:52 AM
* Author: AMIR-MOHAMMAD-SAFARI
*/
#include <mega32.h>
#include <stdio.h>
#include <delay.h>
void main (void) {
DDRC.0 = 0;
DDRA = 0xFF;
while(1) {
if (PINC.0 == 1) {
int i = 0;
while(i < 10) {
PORTA = i;
i++;
delay_ms(50); // Speed 50 MS
}
}
else {
int i = 9;
while(i >= 0) {
PORTA = i;
i--;
delay_ms(50); // Speed 50 MS
}
}
}
}
Changes that count 0 to 5
while(i < 5) {
PORTA = i;
i++;
delay_ms(50);
}
else {
int i = 5;
while(i >= 0) {
PORTA = i;
i--;
delay_ms(50);
}
}
Schematic #
Practice 3 #
Algorithm that show Sum and Multiply of numbers and show on LED
We have 4 SW_SPDT
- A0 and A1
- B0 and B1
Current project is on ATMEGA32
Devices :
- ATMEGA32
- SW-SPDT
- 7447 Decoder
- 7SEGMENT
CODE #
/*
* LED_StarPlus.c
*
* Created: 10/13/2024 8:23:08 AM
* Author: JARVIS
*/
#include <mega32.h>
#include <stdio.h>
#include <delay.h>
void main (void) {
DDRD = 0xFF;
DDRC.0 = 0;
DDRA = 0x00;
DDRB = 0x00;
while (1) {
if (PINC.0 == 1) {
PORTD = PINA + PINB;
}
else {
PORTD = PINA * PINB;
}
}
}
Schematic #
Practice 4 #
Showing something on LCD, which is LM016L. We can even clear screen or change position of things on LCD.
Current project is on ATMEGA32
Devices :
- LM016L
- ATMEGA32
CODE #
/*
* LCD_16Bit_Code_C.c
*
* Created: 10/20/2024 8:13:19 AM
* Author: JARVIS
*/
#include <mega32.h>
#include <stdio.h>
#include <delay.h>
#asm
.equ __lcd_port = 0x18;
#endasm
#include <lcd.h>
void main(void) {
PORTB = 0x00;
DDRB = 0x00;
lcd_init(16);
lcd_clear();
lcd_gotoxy(0,0);
lcd_putsf("Amir Mohammad");
lcd_gotoxy(9,1);
lcd_putsf("Safari");
}
Schematic #
Scroll Practice 4 - 1 #
Now the data shown in LCD will be scroll horizontally
CODE #
/*
* LCD_16Bit_Code_C_Move.c
*
* Created: 10/20/2024 10:37:27 AM
* Author: JARVIS
*/
#include <mega32.h>
#include <stdio.h>
#include <delay.h>
#asm
.equ __lcd_port = 0x18;
#endasm
#include <lcd.h>
void main(void) {
char name[] = "Amir Mohammad Safari ";
int len = 23;
int i;
PORTB = 0x00;
DDRB = 0x00;
lcd_init(32);
while (1) {
for (i = 0; i < len - 1; i++) {
lcd_clear();
lcd_gotoxy(0, 0);
lcd_puts(&name[i]);
delay_ms(20);
}
}
}
Schematic #
Practice 5 #
Devices :
- LM032L
- KEYPAD MATRIX
- RES
- ATMEGA32
CODE #
/*
* KeyPad.c
*
* Created: 10/30/2024 1:57:05 AM
* Author: JARVIS
*/
#include <mega32.h>
#include <stdio.h>
#include <delay.h>
#asm
.equ __lcd_port = 0x1B;
#endasm
#include <lcd.h>
unsigned char scan_key(void);
unsigned char code[4][4] = {{7,4,1,10},{8,5,2,0},{9,6,3,11},{12,13,14,15}};
char lcd_buffer[10];
void main (void) {
unsigned char key;
PORTC = 0x00;
DDRC = 0xF0;
lcd_init(16);
lcd_clear();
while(1) {
key = scan_key();
if (key != 0xFF) {
lcd_clear();
lcd_gotoxy(0,0);
sprintf(lcd_buffer, "Button = %d", key);
lcd_puts(lcd_buffer);
}
}
}
unsigned char scan_key(void) {
unsigned char i, data, num_key = 0xFF, temp = 0x70;
for (i = 0; i < 4; i++) {
PORTC = temp;
delay_ms(5);
data = PINC & 0x0F;
if (data == 0x07)
num_key = code[0][i];
if (data == 0x0B)
num_key = code[1][i];
if (data == 0x0D)
num_key = code[2][i];
if (data == 0x0E)
num_key = code[3][i];
temp = ((temp >>= 1) | 0x80) & 0xF0;
}
return num_key;
}
Schematic #
Practice 6 #
2 Mode
- Cathode
- Anode
[CATHODE] #
Devices :
- ATMEGA32
- 7SEGMENT-CATHODE
- GROUND
Table #
+-------+----+---+---+---+---+---+---+---+-----+
| CHAR | DP | G | F | E | D | C | B | A | HEX |
+-------+----+---+---+---+---+---+---+---+-----+
| 0 | 0 | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 3F |
| 1 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 0 | 06 |
| 2 | 0 | 1 | 0 | 1 | 1 | 0 | 1 | 1 | 5B |
| 3 | 0 | 1 | 0 | 0 | 1 | 1 | 1 | 1 | 4F |
| 4 | 0 | 1 | 1 | 0 | 0 | 1 | 1 | 0 | 66 |
| 5 | 0 | 1 | 1 | 0 | 1 | 1 | 0 | 1 | 6D |
| 6 | 0 | 1 | 1 | 1 | 1 | 1 | 0 | 1 | 7D |
| 7 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 1 | 07 |
| 8 | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 7F |
| 9 | 0 | 1 | 1 | 0 | 1 | 1 | 1 | 1 | 6F |
+-------+----+---+---+---+---+---+---+---+-----+
CODE #
/*
* 7SEG-CATHODE-COUNTER.c
*
* Created: 11/6/2024 1:39:03 AM
* Author: JARVIS
*/
#include <mega32.h>
#include <delay.h>
void main (void) {
int i = 0;
unsigned char seg[10] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F};
DDRC = 0xFF;
PORTC = 0x00;
while(1) {
if (i > 9)
i = 0;
PORTC = seg[i];
delay_ms(100);
i++;
}
}
Schematic #
[ANODE] #
Devices :
- ATMEGA32
- 7SEGMENT-ANODE
- VCC (5V)
Table #
+-------+----+---+---+---+---+---+---+---+-----+
| CHAR | DP | G | F | E | D | C | B | A | HEX |
+-------+----+---+---+---+---+---+---+---+-----+
| 0 | 1 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | C0 |
| 1 | 1 | 1 | 1 | 1 | 1 | 0 | 0 | 1 | F9 |
| 2 | 1 | 0 | 1 | 0 | 0 | 1 | 0 | 0 | A4 |
| 3 | 1 | 0 | 1 | 1 | 0 | 0 | 0 | 0 | B0 |
| 4 | 1 | 0 | 0 | 1 | 1 | 0 | 0 | 1 | 99 |
| 5 | 1 | 0 | 0 | 1 | 0 | 0 | 1 | 0 | 92 |
| 6 | 1 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 82 |
| 7 | 1 | 1 | 1 | 1 | 1 | 0 | 0 | 0 | F8 |
| 8 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 80 |
| 9 | 1 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 90 |
+-------+----+---+---+---+---+---+---+---+-----+
CODE #
/*
* 7SEG-ANODE-COUNTER.c
*
* Created: 11/6/2024 1:40:49 AM
* Author: JARVIS
*/
#include <mega32.h>
#include <delay.h>
void main (void) {
int i = 0;
unsigned char seg[10] = {0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8, 0x80, 0x90};
DDRC = 0xFF;
PORTC = 0x00;
while(1) {
if (i > 9)
i = 0;
PORTC = seg[i];
delay_ms(100);
i++;
}
}
Schematic #
Practice 7 #
Show 0-255 on 7SEG
We have 9 Switch
Devices :
- ATMEGA32
- SP-SPST (Switch)
- LCD (LM016L)
CODE #
/*
* 0-255-LCD-SWITCH.c
*
* Created: 11/17/2024 5:33:14 AM
* Author: JARVIS
*/
#include <mega32.h>
#include <delay.h>
#include <stdio.h>
#include <lcd.h>
#asm
.equ __lcd_port = 0x1B;
#endasm
void main(void) {
char buffer [20];
unsigned char num;
PORTB = 0xFF;
DDRB = 0x00;
PORTC = 0xFF;
DDRC = 0x00;
lcd_init(20);
lcd_clear();
lcd_putsf("Set Keys");
while (1) {
if (PINC.0 != 0) {
lcd_clear();
lcd_gotoxy(0,0);
sprintf(buffer, "Hi, please start");
lcd_puts(buffer);
num = 0x00;
}
else {
num =~ PINB;
}
if (num != 0x00) {
lcd_clear();
lcd_gotoxy(0,0);
sprintf(buffer, "Number = %d", num);
lcd_puts(buffer);
delay_ms(100);
}
else if (PINC.0 == 0) {
lcd_clear();
lcd_putsf("Number = 0");
delay_ms(100);
}
}
}
Schematic #
Practice 8 #
We have 8 resistors connected to atmega32
Devices :
- ATMEGA32
- Resistor 330 OHM
- 7SEG-MPX4-CA
CODE #
/*
* 7-SEG-9999.c
*
* Created: 11/17/2024 8:00:24 PM
* Author: JARVIS
*/
#include <mega32.h>
#include <delay.h>
void main(void) {
unsigned char seg[10] = {0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8, 0x80, 0x90};
int i = 0, ones, tens, hundreds, thousands;
DDRC = 0xFF;
PORTC = 0x00;
DDRD = 0xFF;
PORTD = 0x00;
while (1) {
thousands = i / 1000;
hundreds = (i % 1000) / 100;
tens = (i % 100) / 10;
ones = i % 10;
PORTD = 0b00001000; // Display ones
PORTC = seg[ones];
delay_ms(1); // Reduce delay for multiplexing
PORTD = 0b00000100; // Display tens
PORTC = seg[tens];
delay_ms(1);
PORTD = 0b00000010; // Display hundreds
PORTC = seg[hundreds];
delay_ms(1);
PORTD = 0b00000001; // Display thousands
PORTC = seg[thousands];
delay_ms(1);
i++;
if (i > 9999) i = 0;
}
}
Schematic #
Credit #
- Mr. Hamed Sarbazi (Teacher)
- Mr. Amir Mohammad Safari (TA)