ATmega16 and MAX7219 8 x 8 matrix

This is a picture of the 8 x 8 LED matrix display I used which I connected to my ATmega16

Here are the connections

CS -> PORTD 2
DIN -> PORTD 1
CLK -> PORTD 0

MAX7219

MAX7219

Schematic

 

Code

 

#define CS_Pin PORTD.F2
#define DIN_Pin PORTD.F1
#define CLK_Pin PORTD.F0
 
unsigned const short Alphabet[156]={
0x7f, 0x88, 0x88, 0x88, 0x88, 0x7f, // A
0x6e, 0x91, 0x91, 0x91, 0x91, 0xff, // B
0x42, 0x81, 0x81, 0x81, 0x81, 0x7E, // C
0x7e, 0x81, 0x81, 0x81, 0x81, 0xff, // D
0x91, 0x91, 0x91, 0x91, 0xff, 0x81, // E
0x80, 0x90, 0x90, 0x91, 0xff, 0x81, // F
0x4e, 0x89, 0x89, 0x81, 0x81, 0x7e, // G
0xff, 0x10, 0x10, 0x10, 0x10, 0xff, // H
0x00, 0x81, 0xff, 0xff, 0x81, 0x00, // I
0x00, 0x80, 0xfe, 0x81, 0x01, 0x06, // J
0x81, 0xc3, 0x24, 0x99, 0xff, 0x81, // K
0x03, 0x01, 0x01, 0x81, 0xff, 0x81, // L
0xff, 0x60, 0x18, 0x18, 0x60, 0xff, // M
0xff, 0x06, 0x08, 0x10, 0x60, 0xff, // N
0x7e, 0x81, 0x81, 0x81, 0x81, 0x7e, // O
0x70, 0x88, 0x88, 0x89, 0xff, 0x81, // P
0x7e, 0x87, 0x89, 0x85, 0x81, 0x7e, // Q
0x61, 0x93, 0x94, 0x98, 0x98, 0xff, // R
0x4e, 0x91, 0x91, 0x91, 0x91, 0x62, // S
0xc0, 0x81, 0xff, 0xff, 0x81, 0xc0, // T
0xfe, 0x01, 0x01, 0x01, 0x01, 0xfe, // U
0xfc, 0x02, 0x01, 0x01, 0x02, 0xfc, // V
0xff, 0x02, 0x04, 0x04, 0x02, 0xff, // W
0xc3, 0x24, 0x18, 0x18, 0x24, 0xc3, // X
0xc0, 0x20, 0x1f, 0x1f, 0x20, 0xc0, // Y
0xc3, 0xa1, 0x91, 0x89, 0x85, 0xc3, // Z
};
 
void SPI_Write_Byte(unsigned short num)
{
unsigned short t, Mask, Flag;
CLK_Pin = 0;
Mask = 128;
for (t=0; t<8; t++) { Flag = num & Mask; if(Flag == 0) { DIN_Pin = 0; } else { DIN_Pin = 1; } CLK_Pin = 1; CLK_Pin = 0; Mask = Mask >> 1;
}
}
 
void MAX7219_INIT() {
 
// Set BCD decode mode
CS_Pin = 0; // CS pin is pulled LOW
SPI_Write_Byte(0x09); // Select Decode Mode register
SPI_Write_Byte(0x00); // Select BCD mode for digits DIG0-DIG7
CS_Pin = 1; // CS pin is pulled HIGH
 
// Set display brighness
CS_Pin = 0; // CS pin is pulled LOW
SPI_Write_Byte(0x0A); // Select Intensity register
SPI_Write_Byte(0x05); // Set brightness
CS_Pin = 1; // CS pin is pulled HIGH
 
// Set display refresh
CS_Pin = 0; // CS pin is pulled LOW
SPI_Write_Byte(0x0B); // Select Scan-Limit register
SPI_Write_Byte(0x07); // Select digits DIG0-DIG3
CS_Pin = 1; // CS pin is pulled HIGH
 
// Turn on the display
CS_Pin = 0; // CS pin is pulled LOW
SPI_Write_Byte(0x0C);
SPI_Write_Byte(0x01);
CS_Pin = 1; // CS pin is pulled HIGH
 
// Disable Display-Test
CS_Pin = 0; // CS pin is pulled LOW
SPI_Write_Byte(0x0F); // Select Display-Test register
SPI_Write_Byte(0x00); // Disable Display-Test
CS_Pin = 1; // CS pin is pulled HIGH
 
}
 
void Write_Byte(unsigned short myColumn, unsigned short myValue)
{
CS_Pin = 0; // select max7219.
SPI_Write_Byte(myColumn); // send myColumn value to max7219 (digit place).
SPI_Write_Byte(myValue); // send myValue value to max7219 (digit place).
CS_Pin = 1; // deselect max7219.
}
 
// This is clear matrix function.
void Clear_Matrix(void)
{
unsigned short x;
 
for(x=1;x<9;x++)
{
Write_Byte(x,0x00);
}
}
 
void Write_Char(char myChar)
{
unsigned short Column, Start_Byte;
 
// Clear the display first.
Clear_Matrix();
// The next line defines our byte, from which to start the array.
Start_Byte = (myChar - 65) * 6; // 65 represents the letter "A" in ASCII code.
// We are using only columns from 2 through 7 for displaying the character.
for(Column=2;Column<8;Column++)
{
Write_Byte(Column, Alphabet[Start_Byte++]);
}
}
 
void main()
{
unsigned int x;
DDRD=0xFF;
 
MAX7219_INIT(); // initialize max7219
do{
for(x=65;x<=90;x++) // Increment with 1, from 65 until 90.
{
Write_Char(x);
Delay_ms(1000); // This is our delay, between two consecutive character.
}
}while(1);
}

 

 

Links

MAX7219 Dot Matrix Module Display DIY kit

MAX7219 Dot matrix module display module

Atmega16 and a 7 segment display

In this example we will show you how to connect a 7 segment display to our Atmega16. You can think of a 7 segment display as 7 individual LEDs in a configuration like the picture below.

Image 1 shows the layout and image 2 shows how the segments are arranged

7 seg Blank

7 seg Blank

So by lighting certain segments you can display numbers, so for example to display the number 1 you would light segments B and C. Here are more examples

7 segment

7 segment

In this example we connect a HDSP-C3Y3 common anode display to our ATmega16, you can see the connections in the schematic below. We basically connect up segment A to PD0, segment B to PD1 and so on.

Schematic

atmega16 and 7segment

atmega16 and 7segment

Code

The code was written in mikroC pro for AVR

 

int main(void)
{
DDRD = 0xFF; // Configure port D as output
while(1)
{
 
/*
 
1 1 1 1 1 1 1 1
DP G F E D C B A
 
*/
 
PORTD = 0xC0; // Display Number 0
delay_ms(1000); // Wait for 1s
PORTD = 0xF9; // Display Number 1
delay_ms(1000); // Wait for 1s
PORTD = 0xA4; // Display Number 2
delay_ms(1000); // Wait for 1s
PORTD = 0xB0; // Display Number 3
delay_ms(1000); // Wait for 1s
PORTD = 0x99; // Display Letter 4
delay_ms(1000); // Wait for 1s
PORTD = 0x92; // Display Letter 5
delay_ms(1000); // Wait for 1s
PORTD = 0x82; // Display Letter 6
delay_ms(1000); // Wait for 1s
PORTD = 0xF8; // Display Letter 7
delay_ms(1000); // Wait for 1s
PORTD = 0x80; // Display Letter 8
delay_ms(1000); // Wait for 1s
PORTD = 0x90; // Display Letter 9
delay_ms(1000); // Wait for 1s
}
}

 

Links

 

wholesale 50pcs/lot 1bit Common Cathode/Common Anode Digital Tube 0.56″ 0.56in Red LED Digit 7 Segment

20 PCS LD-3161BG 1 Digit 0.36″ GREEN 7 SEGMENT LED DISPLAY COMMON ANODE

Atmega16 and TIL311

til311

til311

Datasheet

Til 311 datasheet

Connection

Here are the TIL311 pin

PINS 1 and 14 LED supply/Logic Supply 5v
PIN 2 Latch Data Input B PORT D pin 1
PIN 3 Latch Data Input A PORT D pin 0
PIN 4 Left DP Cathode N/C
PIN 5 Latch Strobe Input Ground
PIN 7 Ground Ground
PIN 8 Blanking Input
PIN 10 Right DP Cathode N/C
PIN 12 Latch Data Input D PORT D pin 3
PIN 13 Latch Data Input C PORT D pin 2
til311

til311

Code

 

int i;
 
void main()
{
DDRD=0xFF;
PORTD=0xFF;
 
while(1)
{
for(i = 1; i<=15; i++)
{
PORTD = i;
delay_ms(200);
}
}
}

 

Links

5 PCS Promotion TIL311 HEXADECIMAL DISPLAY WITH LOGIC

Atmega16 and 74HC595 shift register example

Sometimes in your projects you simply do not have enough I/O lines available, take for example a lot of the multiple LED examples, these use 8 outputs to control 8 LEDs via your PIC, that can restrict the amount of outputs you would have available to drive other devices. Instead of this we can use a shift register, in this case a 74HC595 and using 3 I/O pins we can control 8 LED’s, thats a saving of 5 I/O pins for other uses.

You can use it to control 8 outputs at a time while only taking up a few pins on your microcontroller. You can link multiple registers together to extend your output even more. The 74HC595 has an 8 bit storage register and an 8 bit shift register. Data is written to the shift register serially, then latched onto the storage register. The storage register then controls 8 output lines

Lets look at the 74HC595 in more detail

595 pin diagram

595 pin diagram

Pins

PINS 1-7, 15 Q0 to Q7 Output Pins
PIN 8 GND Ground, Vss
PIN 9 Q7″ Serial Out
PIN 10 MR Master Reclear, active low
PIN 11 SH_CP Shift register clock pin
PIN 12 ST_CP Storage register clock pin (latch pin)
PIN 13 OE Output enable, active low
PIN 14 DS Serial data input
PIN 16 Vcc Positive supply voltage usually 5v

This is a link to the 74HC595 datasheet

Schematic

 

atmega16 and 74hc595

atmega16 and 74hc595

Code

 

#define SHIFT_CLOCK PORTD.F2
#define SHIFT_LATCH PORTD.F1
#define SHIFT_DATA PORTD.F0
 
void shiftdata595(unsigned char _shiftdata)
{
unsigned int i;
unsigned char temp;
temp = _shiftdata;
i=8;
while (i>0)
{
if (temp.F7==0)
{
SHIFT_DATA = 0;
}
else
{
SHIFT_DATA = 1;
}
temp = temp<<1;
SHIFT_CLOCK = 1;
Delay_us(1);
SHIFT_CLOCK = 0;
i--;
}
}
 
void latch595()
{
SHIFT_LATCH = 1;
Delay_us(1);
SHIFT_LATCH = 0;
}
 
void main()
{
DDRD=0xFF;
PORTD=0xFF;
 
while(1)
{
shiftdata595(1);
latch595();
delay_ms(200);
 
shiftdata595(0x02);
latch595();
delay_ms(200);
 
shiftdata595(0b00000100);
latch595();
delay_ms(200);
}
}

 

Links

10PCS SN74HC595N

30pcs SN74HC595N 8 Bit Shift Register

ATMEGA16 and LCD example

In this example we have the hello world of LCD examples, we display that text on the LCD in question. Connection is straightforward and you can see it at the top of the code, we use Port B in this example

LCD_RS -> PORT B4
LCD_EN -> PORT B5
LCD_D4 -> PORT B0
LCD_D5 -> PORT B1
LCD_D6 -> PORT B2
LCD_D7 -> PORT B3

The LCD we used is similar to the one in the picture below, we connect this up and use it in 4 Bit mode, this saves on I/O pins.

16x2_Character_LCD_Display_Module
LCD Pin Description

Pin No Symbol I/O Description
1 Vss Ground
2 Vcc +5V
3 Vee Contrast Control
4 RS Input Command/Data Register
5 R/W Input Read/Write Register
6 E Input/Output Enable
7 DB0 Input/Output Not Used in 4-Bit Mode
8 DB1 Input/Output Not Used in 4-Bit Mode
9 DB2 Input/Output Not Used in 4-Bit Mode
10 DB3 Input/Output Not Used in 4-Bit Mode
11 DB4 Input/Output Data Bus in 4-Bit Mode
12 DB5 Input/Output Data Bus in 4-Bit Mode
13 DB6 Input/Output Data Bus in 4-Bit Mode
14 DB7 Input/Output Data Bus in 4-Bit Mode
15 Vcc For LCD Back Light
16 Vss For LCD Back Light

Schematic

Basic schematic showing LCD connections, assumption is you will already have power, reset and possible external clock circuitry as well

atmega16 and lcd

atmega16 and lcd

Code

Written in mikroC pro for AVR, this particular example uses the built in LCD libraries.

 

 

sbit LCD_RS at PORTB4_bit;
sbit LCD_EN at PORTB5_bit;
sbit LCD_D4 at PORTB0_bit;
sbit LCD_D5 at PORTB1_bit;
sbit LCD_D6 at PORTB2_bit;
sbit LCD_D7 at PORTB3_bit;
 
sbit LCD_RS_Direction at DDB4_bit;
sbit LCD_EN_Direction at DDB5_bit;
sbit LCD_D4_Direction at DDB0_bit;
sbit LCD_D5_Direction at DDB1_bit;
sbit LCD_D6_Direction at DDB2_bit;
sbit LCD_D7_Direction at DDB3_bit;
 
void main()
{
 
PORTB = 0;
Lcd_Init(); // Initialize LCD
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
Lcd_Out(1,1,"Hello world"); //Write the word mikroC on the LCD
Delay_ms(300);
} // end main

 

 

 

Links
ATMEGA16/ATmega32 AVR Minimum System Board + USB ISP USBasp Programmer

ATMEGA16 AVR microcontroller

ATMEGA16 Minimum System Board