Categories

Atmega16 and MAX7219 seven segment display

In this example we connect a MAX7219 seven segment display to our ATmega16, the connection was as follows

VCC → 5V
GND → GND
DIN → PB5
CS → PB0
CLK → PB7

 

MAX7219 seven segment display

MAX7219 seven segment display

Schematic

 

Code


sbit Chip_Select at PORTB0_bit;
sbit Chip_Select_Direction at DDB0_bit;

char i;

void max7219_init1() {
Chip_Select = 0;                // Select MAX7219
SPI1_Write(0x09);               // BCD mode for digit decoding
SPI1_Write(0xFF);
Chip_Select = 1;                // Deselect MAX7219

Chip_Select = 0;                // Select MAX7219
SPI1_Write(0x0A);
SPI1_Write(0x0F);               // Segment luminosity intensity
Chip_Select = 1;                // Deselect MAX7219

Chip_Select = 0;                // Select MAX7219
SPI1_Write(0x0B);               // Set scan-limit
SPI1_Write(0x07);               // Display all 8 digits
Chip_Select = 1;                // Deselect MAX7219

Chip_Select = 0;                // Select MAX7219
SPI1_Write(0x0C);               // Set Shutdown register
SPI1_Write(0x01);               // Normal operation
Chip_Select = 1;                // Deselect MAX7219

Chip_Select = 0;                // Select MAX7219
SPI1_Write(0xFF);               // No test
SPI1_Write(0x00);

Chip_Select = 1;                // Deselect MAX7219
}

void main() {

Chip_Select_Direction = 1;      // Set PB0 pin as output
SPI1_Init();                    // Initialize SPI module
max7219_init1();                // Initialize  MAX7219

for (i = 1; i<=8; i++) {
Chip_Select = 0;            // Select MAX7219
SPI1_Write(i);              // Send i to MAX7219 (digit place)
SPI1_Write(8-i);            // Send 8-i to MAX7219 (digit value)
Chip_Select = 1;            // Deselect MAX7219
}
// The result "01234567" is written on the 7-Seg displays
}

 

Links
MAX7219 CWG 8-Digital Tube Display Module Control Module

MAX7219 Dot Matrix Module Display

5pcs MAX7219 DIP-24 LED Display Driver IC

ATMega16 and Arduino LCD Keypad shield

Being interested in various microcontrollers means we purchase lots of modules, development boards and sensors. One of these is the ARduino of which we have purchased many different shields, as part of a basic exercise I decided to see how easy it would be to connect the LCD Keypad shield to an ATMEGA16

The first step is to create a breakout for the Arduino shields on veroboard, you then need solder a set of pins which you can connect to your ATmega16 port pins, some shields have a set of pins that can be used. Here is my setup you can see the shield at one end and the ATmega16 breakout board at the other and the interconnecting wire

atmega16 and lcd

atmega16 and lcd

 

LCD Shield

Here are the pin connections and details of the shield

lcd shield schematic

lcd shield schematic

Pin Function
Analog 0 Button (select, up, right, down and left)
Digital 4 DB4
Digital 5 DB5
Digital 6 DB6
Digital 7 DB7
Digital 8 RS (Data or Signal Display Selection)
Digital 9 Enable
Digital 10 Backlit Control

 

Code

The code was written in mikroC PRO for AVR


sbit LCD_RS at PORTD2_bit;
sbit LCD_EN at PORTD3_bit;
sbit LCD_D4 at PORTD4_bit;
sbit LCD_D5 at PORTD5_bit;
sbit LCD_D6 at PORTD6_bit;
sbit LCD_D7 at PORTD7_bit;

sbit LCD_RS_Direction at DDD2_bit;
sbit LCD_EN_Direction at DDD3_bit;
sbit LCD_D4_Direction at DDD4_bit;
sbit LCD_D5_Direction at DDD5_bit;
sbit LCD_D6_Direction at DDD6_bit;
sbit LCD_D7_Direction at DDD7_bit;

void main()
{

PORTD = 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
LCD Keypad Shield LCD1602 Module Display

ATMEGA16 AVR Minimum System Board

ATMEGA16 and LCD example

This example is 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

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

ATMEGA 16 and RGB LED examples

A couple of simple examples, we used an RGB LED breakout. This was a common anode type, so to switch an LED on you need to switch the appropriate port pin low.

 

Schematic

atmega and rgb led

atmega and rgb led

Code

These examples were written in mikroC PRO for AVR

Example 1 : Cycle through Red, green and blue colours


void main() {
DDRB = 0xFF;           // Set direction to be output

do {
PORTB = 0xFF;        // Turn OFF diodes on PORTB
Delay_ms(200);      // 1 second delay
PORTB = 0xFE;
Delay_ms(200);      // 1 second delay
PORTB = 0xFD;
Delay_ms(200);      // 1 second delay
PORTB = 0xFB;
Delay_ms(100);      // 1 second delay
} while(1);            // Endless loop
}

Example 2 : Cycle through all colours


void main()
{

DDRB = 0xFF;
PORTB = 0xFF; // Set RB0 to high 00000001
do // To set infinite loop
{

PORTB = PORTB - 0x01; //
Delay_ms(250);
if(PORTB == 0xF8) //11111000 = R,G,B on
{
PORTB = 0xFF; //LEDS off
Delay_ms(500);
}
}while(1); // To set infinite loop
}

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

ATMEGA16 AVR microcontroller

ATMEGA16 Minimum System Board