-
Notifications
You must be signed in to change notification settings - Fork 0
/
main9.c
166 lines (138 loc) · 5.11 KB
/
main9.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
* ATmega16_Master.c
* http://www.electronicwings.com
* https://www.electronicwings.com/avr-atmega/atmega1632-spi
*/
#define F_CPU 8000000UL /* Define CPU Frequency 8MHz */
#include <avr/io.h> /* Include AVR std. library file */
#include <util/delay.h> /* Include Delay header file */
#include <stdio.h> /* Include Std. i/p o/p file */
#include <string.h> /* Include String header file */
//#include "LCD_16x2_H_file.h" /* Include LCD header file */
//#include "SPI_Master_H_file.h" /* Include SPI master header file */
#include <avr/io.h>
#include <util/delay.h>
#define SS_Enable (PORTB |= (1<<PB4))
#define MOSI PB5
#define MISO PB6
#define SCK PB7
#define SS PB4
// Define LCD control pins and data port
#define LCD_RS 0 // Register select pin
#define LCD_RW 1 // Read/write pin (set to write mode)
#define LCD_EN 2 // Enable pin
#define LCD_D4 4 // Data bit 4
#define LCD_D5 5 // Data bit 5
#define LCD_D6 6 // Data bit 6
#define LCD_D7 7 // Data bit 7
#define LCD_PORT PORTB // Change to the appropriate port
// Function to send a command to the LCD
void LCD_Command(unsigned char cmd) {
LCD_PORT = (LCD_PORT & 0x0F) | (cmd & 0xF0); // Send high nibble
LCD_PORT &= ~(1 << LCD_RS); // RS = 0 for command
LCD_PORT |= (1 << LCD_EN); // Enable pulse
_delay_us(1); // Small delay
LCD_PORT &= ~(1 << LCD_EN); // Disable pulse
_delay_us(200); // Delay after command
LCD_PORT = (LCD_PORT & 0x0F) | ((cmd << 4) & 0xF0); // Send low nibble
LCD_PORT |= (1 << LCD_EN); // Enable pulse
_delay_us(1); // Small delay
LCD_PORT &= ~(1 << LCD_EN); // Disable pulse
_delay_ms(2); // Delay after command
}
// Function to send a data byte to the LCD
void LCD_Data(unsigned char data) {
LCD_PORT = (LCD_PORT & 0x0F) | (data & 0xF0); // Send high nibble
LCD_PORT |= (1 << LCD_RS); // RS = 1 for data
LCD_PORT |= (1 << LCD_EN); // Enable pulse
_delay_us(1); // Small delay
LCD_PORT &= ~(1 << LCD_EN); // Disable pulse
_delay_us(200); // Delay after data
LCD_PORT = (LCD_PORT & 0x0F) | ((data << 4) & 0xF0); // Send low nibble
LCD_PORT |= (1 << LCD_RS); // RS = 1 for data
LCD_PORT |= (1 << LCD_EN); // Enable pulse
_delay_us(1); // Small delay
LCD_PORT &= ~(1 << LCD_EN); // Disable pulse
_delay_us(100); // Delay after data
}
// Function to initialize the LCD
void LCD_Init() {
// Configure LCD control pins and data port as outputs
DDRB |= (1 << LCD_RS) | (1 << LCD_RW) | (1 << LCD_EN) | (1 << LCD_D4) | (1 << LCD_D5) | (1 << LCD_D6) | (1 << LCD_D7);
// Initialize LCD in 4-bit mode
_delay_ms(20); // Wait for LCD to power up
LCD_Command(0x02); // Set 4-bit mode
LCD_Command(0x28); // 2-line, 5x8 font size
LCD_Command(0x0C); // Display ON, cursor OFF
LCD_Command(0x06); // Entry mode: Increment cursor
LCD_Clear(); // Clear the screen
}
// Function to clear the LCD screen
void LCD_Clear() {
LCD_Command(0x01); // Clear display
_delay_ms(2); // Delay after clearing
}
// Function to move the cursor to a specific position (0-based)
void LCD_SetCursor(uint8_t row, uint8_t col) {
uint8_t position = row * 0x40 + col;
LCD_Command(0x80 | position); // Set DDRAM address
}
// Function to display a string at the current cursor position
void LCD_DisplayString(const char *str) {
while (*str) {
LCD_Data(*str++);
}
}
void LCD_String_xy(uint8_t row, uint8_t col, const char *str) {
// Calculate the DDRAM address based on row and column
uint8_t position = row * 0x40 + col;
LCD_Command(0x80 | position); // Set DDRAM address
// Display the string
while (*str) {
LCD_Data(*str++);
}
}
void SPI_Init() /* SPI Initialize function */
{
DDRB |= (1<<MOSI)|(1<<SCK)|(1<<SS); /* Make MOSI, SCK, SS
as Output pin */
DDRB &= ~(1<<MISO); /* Make MISO pin
as input pin */
PORTB |= (1<<SS); /* Make high on SS pin */
SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR0); /* Enable SPI in master mode
with /16 */
SPSR &= ~(1<<SPI2X); /* Disable speed doubler */
}
void SPI_Write(char data) /* SPI write data function */
{
char flush_buffer;
SPDR = data; /* Write data to SPI data register */
while(!(SPSR & (1<<SPIF))); /* Wait till transmission complete */
flush_buffer = SPDR; /* Flush received data */
/* Note: SPIF flag is cleared by first reading SPSR (with SPIF set) and then accessing SPDR hence flush buffer used here to access SPDR after SPSR read */
}
char SPI_Read() /* SPI read data function */
{
SPDR = 0xFF;
while(!(SPSR & (1<<SPIF))); /* Wait till reception complete */
return(SPDR); /* Return received data */
}
int main(void)
{
uint8_t count;
char buffer[5];
LCD_Init();
SPI_Init();
LCD_String_xy(1, 0, "Master Device");
LCD_String_xy(2, 0, "Sending Data: ");
SS_Enable;
count = 0;
while (1) /* Send Continuous count */
{
SPI_Write(count);
sprintf(buffer, "%d ", count);
LCD_String_xy(2, 13, buffer);
count++;
_delay_ms(500);
}
}