/***********************************************************************************
Project: max7219 driver
Prozessor: atmega128 family
Compiler: winAVR
Autor: a tao
***********************************************************************************/
#include <avr/io.h>
#include <stdlib.h>
#include "max7219.h"
//FOR max7219
#define MOS_7219_CLR PORTF &= ~(1 << PF0) /*MOS置低电平*/
#define MOS_7219_SET PORTF |= (1 << PF0) /*MOS置高电平*/
#define S0_7219_CLR PORTF &= ~(1 << PF1) /*S0置低电平*/
#define S0_7219_SET PORTF |= (1 << PF1) /*S0置高电平*/
#define SCK_7219_CLR PORTB &= ~(1 << PB6) /*SCK置低电平*/
#define SCK_7219_SET PORTB |= (1 << PB6) /*SCK置高电平*/
#define S1_7219_CLR PORTD &= ~(1 << PD7) /*S1置低电平*/
#define S1_7219_SET PORTD |= (1 << PD7) /*S1置高电平*/
//两行led,显示参数,row1显示第一行,row2显示第二行
#define row1 0
#define row2 1
uint8_t led_row1[16]={0x01,0x01,0x02,0x02,0x03,0x03,0x04,0x04,0x05,0x05,0x06,0x06,0x07,0x07,0x08,0x08};
uint8_t led_row2[16]={0x01,0x7f,0x02,0xaa,0x03,0x0b,0x04,0x04,0x05,0x7f,0x06,0x06,0x07,0x07,0x08,0x82};
void delay_us(uint16_t u) //延时2us左右,delay(240)=100us;
{
while(u)
{
u--;
}
}
// inti max7219
void init7219()
{
DDRF |= (1 << PF0) ; /*设置输出*/
DDRF |= (1 << PF1) ; /*设置输出*/
DDRB |= (1 << PB6) ; /*设置输出*/
DDRD |= (1 << PD7) ; /*设置输出*/
//写控制字
//0xff,0xff进入display test模式;0xff,0x00进入normal operation模式;0x0c,0x01进入shutdown模式;0x0b,0x07扫描极限8;0x0a,0xf5 intensity;0x09,0xff选择译码模式
write_byte_7219(0x0c,0x01,row1);
write_byte_7219(0x0f,0x00,row1);
write_byte_7219(0x09,0x0ff,row1);
write_byte_7219(0x0b,0x07,row1);
write_byte_7219(0x0a,0x04,row1);
write_byte_7219(0x0c,0x01,row2);
write_byte_7219(0x0f,0x00,row2);
write_byte_7219(0x09,0x0ff,row2);
write_byte_7219(0x0b,0x07,row2);
write_byte_7219(0x0a,0x04,row2);
}
void write_byte_7219(unsigned char address, unsigned char data,unsigned char indx)
{
unsigned char i;
if(indx) //写第一排数码管
{
S0_7219_CLR;
for (i=0x80;i>0;i/=2) //shift bit for masking
{
SCK_7219_CLR;
if (i & address) MOS_7219_SET; //masking value with i , write to SENSI-BUS
else MOS_7219_CLR;
SCK_7219_SET; //clk for SENSI-BUS
delay_us(5); //pulswith approx. 5 us
}
for (i=0x80;i>0;i/=2) //shift bit for masking
{
SCK_7219_CLR;
if (i & data) MOS_7219_SET; //masking value with i , write to SENSI-BUS
else MOS_7219_CLR;
SCK_7219_SET; //clk for SENSI-BUS
delay_us(5); //pulswith approx. 5 us
}
S0_7219_SET;
}
else //写第二排数码管
{
S1_7219_CLR;
for (i=0x80;i>0;i/=2) //shift bit for masking
{
SCK_7219_CLR;
if (i & address) MOS_7219_SET; //masking value with i , write to SENSI-BUS
else MOS_7219_CLR;
SCK_7219_SET; //clk for SENSI-BUS
delay_us(5); //pulswith approx. 5 us
}
for (i=0x80;i>0;i/=2) //shift bit for masking
{
SCK_7219_CLR;
if (i & data) MOS_7219_SET; //masking value with i , write to SENSI-BUS
else MOS_7219_CLR;
SCK_7219_SET; //clk for SENSI-BUS
delay_us(5); //pulswith approx. 5 us
}
S1_7219_SET;
}
}
void display_7219(unsigned indx)
{
uint8_t i;
if(indx)
{
for(i=0;i<=15;i=i+2)//写入控制字
{
write_byte_7219(led_row1[i],led_row1[i+1],indx);
}
}
else
{
for(i=0;i<=15;i=i+2)//写入控制字
{
write_byte_7219(led_row2[i],led_row2[i+1],indx);
}
}
}
//----------------------------------------------------------------------------------
int main(void)
//----------------------------------------------------------------------------------
{
init7219();
while(1)
{
display_7219(row1);
display_7219(row2);
for(int j=0;j<0xffff;j++) asm("NOP");
}
return 0;
}