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

Atmega128 push button example

I recently bought a development board for an ATMEGA128, sadly there was no examples and no schematic supplied but after some trial and error I managed to get this up and running. The board came with 8 LEDs connected to PORTC and 8 switches connected to PORTB. So as a basic example I decided to write an example which when a button was pressed would light up all the LEDs and then they would all switch off.

To use a push button switch with a microcontroller, first you should configure the corresponding pin as an input. After this is done you can read the state of that pin and perform an action based on this.

In the schematic below I have shown the switch I used connected up and the LEDs, obviously you would need to power the device and also you may wish to fit a suitable crystal.

 

Schematic

atmega128 leds and button

atmega128 leds and button

Code

The code was written in AtmelStudio and the board had a 10 pin ISP connector which I connected up and used a USBASP cable with a GUI to Avrdude called AVRPal.

To explain some of the terminology used below

DDR is the Data Direction Register which determines whether a pin is input or output, the PORT register is the output register which is used to write the output to pins and the PIN register is the PORT Input Register which is used to read data from the input pins

 

#define F_CPU 8000000UL
 
#include <avr/io.h>
#include <util/delay.h>
 
int main(void)
{
DDRC = 0xFF; //Makes all pind of PORTC as Output
 
DDRB &= ~(1<<PB0);//Makes first pin of PORTD as Input
 
while(1) //infinite loop
{
if(PINB & (1<<PB0) == 1) //If switch is pressed
{
PORTC = 0xFF; //Turns ON LEDs
_delay_ms(1000); //1 second delay
PORTC = 0x00; //Turns OFF LEDs
}
}
}

 

Links
5PCS ATMEGA128A

Atmega128 avr development board

AVR development board ATMEGA128 development board minimum system + USB cable