- Dapatkan link
- X
- Aplikasi Lainnya
Membuat Display 4 baris 7 segment menggunakan MAX7221
JUN 3
pada contoh ini menggunakan 7 segment kecil < 1 inch dgn tegangan 5v.jika Anda ingin menggunakan 7 segmen dgn tegangan lebih dari 5 v ( 7 segment ukuran yg besar i inch,2 inch 3 inch dst) maka tinggalditambahkan penguat daya misal dgn transistor atau ic ULN2083 atau lainnya.
koneksi

Kode program C dengan codevision
/*****************************************************
CodeWizardAVR V1.24.
Chip type : ATmega16
Clock frequency : 11.059200 MHz
*****************************************************/
// Standard Input/Output functions
#include <stdio.h>
#include <delay.h>
#include <spi.h>
#include <mega16.h>
//=========================
CodeWizardAVR V1.24.
Chip type : ATmega16
Clock frequency : 11.059200 MHz
*****************************************************/
// Standard Input/Output functions
#include <stdio.h>
#include <delay.h>
#include <spi.h>
#include <mega16.h>
//=========================
// SPI
#define PIN_SCK PORTB.7
#define PIN_MOSI PORTB.5
#define PIN_SS PORTB.4
#define PIN_SCK PORTB.7
#define PIN_MOSI PORTB.5
#define PIN_SS PORTB.4
#define ON 1
#define OFF 0
#define OFF 0
#define MAX7219_LOADa1 PORTB.4=1 //chip enable 1
#define MAX7219_LOADa0 PORTB.4=0 //chip enable 0
#define MAX7219_LOADb1 PORTB.3=1 //chip enable 1
#define MAX7219_LOADb0 PORTB.3=0 //chip enable 0
#define MAX7219_LOADc1 PORTB.2=1 //chip enable 1
#define MAX7219_LOADc0 PORTB.2=0 //chip enable 0
#define MAX7219_LOADd1 PORTB.1=1 //chip enable 1
#define MAX7219_LOADd0 PORTB.1=0 //chip enable 0
#define MAX7219_LOADa0 PORTB.4=0 //chip enable 0
#define MAX7219_LOADb1 PORTB.3=1 //chip enable 1
#define MAX7219_LOADb0 PORTB.3=0 //chip enable 0
#define MAX7219_LOADc1 PORTB.2=1 //chip enable 1
#define MAX7219_LOADc0 PORTB.2=0 //chip enable 0
#define MAX7219_LOADd1 PORTB.1=1 //chip enable 1
#define MAX7219_LOADd0 PORTB.1=0 //chip enable 0
#define MAX7219_MODE_DECODE 0x09
#define MAX7219_MODE_INTENSITY 0x0A
#define MAX7219_MODE_SCAN_LIMIT 0x0B
#define MAX7219_MODE_POWER 0x0C
#define MAX7219_MODE_TEST 0x0F
#define MAX7219_MODE_NOOP 0x00
#define MAX7219_MODE_INTENSITY 0x0A
#define MAX7219_MODE_SCAN_LIMIT 0x0B
#define MAX7219_MODE_POWER 0x0C
#define MAX7219_MODE_TEST 0x0F
#define MAX7219_MODE_NOOP 0x00
#define MAX7219_DIGIT0 0x01
#define MAX7219_DIGIT1 0x02
#define MAX7219_DIGIT2 0x03
#define MAX7219_DIGIT3 0x04
#define MAX7219_DIGIT4 0x05
#define MAX7219_CHAR_BLANK 0xF
#define MAX7219_CHAR_NEGATIVE 0xA
//================================
#define RXB8 1
#define TXB8 0
#define UPE 2
#define OVR 3
#define FE 4
#define UDRE 5
#define RXC 7
#define MAX7219_DIGIT1 0x02
#define MAX7219_DIGIT2 0x03
#define MAX7219_DIGIT3 0x04
#define MAX7219_DIGIT4 0x05
#define MAX7219_CHAR_BLANK 0xF
#define MAX7219_CHAR_NEGATIVE 0xA
//================================
#define RXB8 1
#define TXB8 0
#define UPE 2
#define OVR 3
#define FE 4
#define UDRE 5
#define RXC 7
#define FRAMING_ERROR (1<<FE)
#define PARITY_ERROR (1<<UPE)
#define DATA_OVERRUN (1<<OVR)
#define DATA_REGISTER_EMPTY (1<<UDRE)
#define RX_COMPLETE (1<<RXC)
#define PARITY_ERROR (1<<UPE)
#define DATA_OVERRUN (1<<OVR)
#define DATA_REGISTER_EMPTY (1<<UDRE)
#define RX_COMPLETE (1<<RXC)
//void MAX7219_displayNumber(volatile long number);
void MAX7219_displayNumber(long number,char baris);
void MAX7219_clearDisplay(char baris);
//void MAX7219_writeData(char data_register, char data);
void MAX7219_writeData(char data_register, char data, char baris);
void spiSendByte (char databyte);
void MAX7219_displayNumber(long number,char baris);
void MAX7219_clearDisplay(char baris);
//void MAX7219_writeData(char data_register, char data);
void MAX7219_writeData(char data_register, char data, char baris);
void spiSendByte (char databyte);
// USART Receiver buffer
#define RX_BUFFER_SIZE 8
char rx_buffer[RX_BUFFER_SIZE];
#define RX_BUFFER_SIZE 8
char rx_buffer[RX_BUFFER_SIZE];
#if RX_BUFFER_SIZE<256
unsigned char rx_wr_index,rx_rd_index,rx_counter;
#else
unsigned int rx_wr_index,rx_rd_index,rx_counter;
#endif
unsigned char rx_wr_index,rx_rd_index,rx_counter;
#else
unsigned int rx_wr_index,rx_rd_index,rx_counter;
#endif
// This flag is set on USART Receiver buffer overflow
bit rx_buffer_overflow;
bit rx_buffer_overflow;
// USART Receiver interrupt service routine
interrupt [USART_RXC] void usart_rx_isr(void)
{
char status,data;
status=UCSRA;
data=UDR;
if ((status & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))==0)
{
rx_buffer[rx_wr_index]=data;
if (++rx_wr_index == RX_BUFFER_SIZE) rx_wr_index=0;
if (++rx_counter == RX_BUFFER_SIZE)
{
rx_counter=0;
rx_buffer_overflow=1;
};
};
}
interrupt [USART_RXC] void usart_rx_isr(void)
{
char status,data;
status=UCSRA;
data=UDR;
if ((status & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))==0)
{
rx_buffer[rx_wr_index]=data;
if (++rx_wr_index == RX_BUFFER_SIZE) rx_wr_index=0;
if (++rx_counter == RX_BUFFER_SIZE)
{
rx_counter=0;
rx_buffer_overflow=1;
};
};
}
#ifndef _DEBUG_TERMINAL_IO_
// Get a character from the USART Receiver buffer
#define _ALTERNATE_GETCHAR_
#pragma used+
char getchar(void)
{
char data;
while (rx_counter==0);
data=rx_buffer[rx_rd_index];
if (++rx_rd_index == RX_BUFFER_SIZE) rx_rd_index=0;
#asm(“cli”)
–rx_counter;
#asm(“sei”)
return data;
}
#pragma used-
#endif
// Get a character from the USART Receiver buffer
#define _ALTERNATE_GETCHAR_
#pragma used+
char getchar(void)
{
char data;
while (rx_counter==0);
data=rx_buffer[rx_rd_index];
if (++rx_rd_index == RX_BUFFER_SIZE) rx_rd_index=0;
#asm(“cli”)
–rx_counter;
#asm(“sei”)
return data;
}
#pragma used-
#endif
// Declare your global variables here
char digitsInUse = 5;
void main(void)
{
// Declare your local variables here
char digitsInUse = 5;
void main(void)
{
// Declare your local variables here
int a,b,c,d,i;
// Input/Output Ports initialization
// Port A initialization
PORTA=0x00;
DDRA=0x00;
// Input/Output Ports initialization
// Port A initialization
PORTA=0x00;
DDRA=0x00;
// Port B initialization
PORTB=0x00;
DDRB=0xff; // SCK MOSI CS/LOAD/SS
PORTB=0x00;
DDRB=0xff; // SCK MOSI CS/LOAD/SS
// Port C initialization
PORTC=0x00;
DDRC=0x00;
PORTC=0x00;
DDRC=0x00;
// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTD=0x00;
DDRD=0x00;
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTD=0x00;
DDRD=0x00;
// USART initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: On
// USART Transmitter: Off
// USART Mode: Asynchronous
// USART Baud rate: 9600
UCSRA=0x00;
UCSRB=0x90;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x47;
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: On
// USART Transmitter: Off
// USART Mode: Asynchronous
// USART Baud rate: 9600
UCSRA=0x00;
UCSRB=0x90;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x47;
// SPI initialization
// SPI Type: Master
// SPI Clock Rate: 86.400 kHz
// SPI Clock Phase: Cycle Half
// SPI Clock Polarity: Low
// SPI Data Order: MSB First
//SPCR=0x53;
//SPSR=0x00;
// SPI Enable, Master mode
SPCR =0x53;
for(i=1;i<5;i++)
{
// Decode mode to “Font Code-B”
MAX7219_writeData(MAX7219_MODE_DECODE, 0xFF,i);
// Scan limit runs from 0.
MAX7219_writeData(MAX7219_MODE_SCAN_LIMIT, digitsInUse – 1,i);
MAX7219_writeData(MAX7219_MODE_INTENSITY, 8,i);
MAX7219_writeData(MAX7219_MODE_POWER, ON,i);
delay_ms(50);
}
//
// SPI Type: Master
// SPI Clock Rate: 86.400 kHz
// SPI Clock Phase: Cycle Half
// SPI Clock Polarity: Low
// SPI Data Order: MSB First
//SPCR=0x53;
//SPSR=0x00;
// SPI Enable, Master mode
SPCR =0x53;
for(i=1;i<5;i++)
{
// Decode mode to “Font Code-B”
MAX7219_writeData(MAX7219_MODE_DECODE, 0xFF,i);
// Scan limit runs from 0.
MAX7219_writeData(MAX7219_MODE_SCAN_LIMIT, digitsInUse – 1,i);
MAX7219_writeData(MAX7219_MODE_INTENSITY, 8,i);
MAX7219_writeData(MAX7219_MODE_POWER, ON,i);
delay_ms(50);
}
//
// Global enable interrupts
#asm(“sei”)
#asm(“sei”)
a=12305;
b=11300;
c=11612;
d=b-c;
MAX7219_displayNumber(a,1);
MAX7219_displayNumber(b,2);
MAX7219_displayNumber(c,3);
MAX7219_displayNumber(d,4);
b=11300;
c=11612;
d=b-c;
MAX7219_displayNumber(a,1);
MAX7219_displayNumber(b,2);
MAX7219_displayNumber(c,3);
MAX7219_displayNumber(d,4);
while (1)
{
{
i=i+1;
if(i==999)i=0;
};
};
}
//===============================================================================
//=========================
void spiSendByte (char databyte)
{
SPDR = databyte;
// Wait until transfer is complete
while (!(SPSR & (1 << 7)));
}
//=========================
void spiSendByte (char databyte)
{
SPDR = databyte;
// Wait until transfer is complete
while (!(SPSR & (1 << 7)));
}
void MAX7219_writeData(char data_register, char data, char baris)
{
if(baris==1)
{
MAX7219_LOADa0;
// Send the register where the data will be stored
spiSendByte(data_register);
// Send the data to be stored
spiSendByte(data);
MAX7219_LOADa1;
}
if(baris==2)
{
MAX7219_LOADb0;
// Send the register where the data will be stored
spiSendByte(data_register);
// Send the data to be stored
spiSendByte(data);
MAX7219_LOADb1;
}
if(baris==3)
{
MAX7219_LOADc0;
// Send the register where the data will be stored
spiSendByte(data_register);
// Send the data to be stored
spiSendByte(data);
MAX7219_LOADc1;
}
if(baris==4)
{
MAX7219_LOADd0;
// Send the register where the data will be stored
spiSendByte(data_register);
// Send the data to be stored
spiSendByte(data);
MAX7219_LOADd1;
}
{
if(baris==1)
{
MAX7219_LOADa0;
// Send the register where the data will be stored
spiSendByte(data_register);
// Send the data to be stored
spiSendByte(data);
MAX7219_LOADa1;
}
if(baris==2)
{
MAX7219_LOADb0;
// Send the register where the data will be stored
spiSendByte(data_register);
// Send the data to be stored
spiSendByte(data);
MAX7219_LOADb1;
}
if(baris==3)
{
MAX7219_LOADc0;
// Send the register where the data will be stored
spiSendByte(data_register);
// Send the data to be stored
spiSendByte(data);
MAX7219_LOADc1;
}
if(baris==4)
{
MAX7219_LOADd0;
// Send the register where the data will be stored
spiSendByte(data_register);
// Send the data to be stored
spiSendByte(data);
MAX7219_LOADd1;
}
}
void MAX7219_clearDisplay(char baris)
{
char i;
i = digitsInUse;
// Loop until 0, but don’t run for zero
do {
// Set each display in use to blank
MAX7219_writeData(i, MAX7219_CHAR_BLANK,baris);
} while (–i);
{
char i;
i = digitsInUse;
// Loop until 0, but don’t run for zero
do {
// Set each display in use to blank
MAX7219_writeData(i, MAX7219_CHAR_BLANK,baris);
} while (–i);
}
void MAX7219_displayNumber(long number,char baris)
{
char negative = 0;
char i = 0;
if (number < 0) {
negative = 1;
number =number * -1; //rubah ke +
}
MAX7219_clearDisplay(baris);
// If number = 0, only show one zero then exit
if (number == 0) {
MAX7219_writeData(1, 0,baris);
return;
}
// Initialization to 0 required in this case,
// does not work without it. Not sure why.
// Loop until number is 0.
do {
MAX7219_writeData(++i, number % 10,baris);
// Actually divide by 10 now.
number /= 10;
} while (number);
{
char negative = 0;
char i = 0;
if (number < 0) {
negative = 1;
number =number * -1; //rubah ke +
}
MAX7219_clearDisplay(baris);
// If number = 0, only show one zero then exit
if (number == 0) {
MAX7219_writeData(1, 0,baris);
return;
}
// Initialization to 0 required in this case,
// does not work without it. Not sure why.
// Loop until number is 0.
do {
MAX7219_writeData(++i, number % 10,baris);
// Actually divide by 10 now.
number /= 10;
} while (number);
// display the sign.
if (negative) {
MAX7219_writeData(i+1, MAX7219_CHAR_NEGATIVE,baris);
}
}
http://www.limaportal.blogspot.com
if (negative) {
MAX7219_writeData(i+1, MAX7219_CHAR_NEGATIVE,baris);
}
}
http://www.limaportal.blogspot.com

Komentar
Posting Komentar