新聞中心

EEPW首頁(yè) > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > atmega16控制MAX7219在數(shù)碼管顯示

atmega16控制MAX7219在數(shù)碼管顯示

作者: 時(shí)間:2016-11-23 來源:網(wǎng)絡(luò) 收藏
#include

#include

本文引用地址:http://butianyuan.cn/article/201611/320228.htm

#define uchar unsigned char
#define uint unsigned int

uchar QIAN=0,BAI=0,SHI=0,GE=0;

void delay_ms(uint ms)
{
uint i,j;
for(i=ms;i>0;i--)
for(j=1141;j>0;j--);
}


void System_Init(void)
{
DDRB=0XFF;
PORTB=0XFF;
}

void BCD_Change(uint data)
{
uint TEN=0; //注意變量的類型
TEN=data;
QIAN=TEN/1000; //0x03e8=1000
TEN%=1000;
BAI=TEN/100;
TEN%=100;
SHI=TEN/10;
TEN%=10;
GE=TEN;
}

void MAX7219_SendData(uchar addr,uchar data)//先發(fā)送高位在發(fā)送低位
{
uchar i=0,j=0,a=0,temp=0;
PORTB&=0Xbf; //拉低LOAD
while(i<16)//LOAD/cs端在第16 個(gè)時(shí)鐘的上升沿同時(shí)或之后,下個(gè)時(shí)鐘上升沿之前變?yōu)楦唠娖?,否則數(shù)據(jù)將會(huì)丟失
{
if(i<8) //判斷傳送地址還是傳送數(shù)據(jù)
temp=addr;
else
temp=data;
for(j=8;j>=1;j--)
{
a=temp;
a=a>>2; //將數(shù)據(jù)右移2為,將待傳送的數(shù)據(jù)最高位移位至bit5上,方便在PB5口上輸出
a=a&0x20; //取出BIT5上的數(shù)據(jù)的高低位
PORTB&=0XDF; //0XDF 1101 1111
PORTB|=a; //將數(shù)據(jù)的最高位輸出
temp<<=1; //數(shù)據(jù)左移一位,傳送次高位數(shù)據(jù),一次循環(huán)8次傳送完畢
PORTB&=0x7f; //拉低CLK
PORTB|=0x80; //拉高CLK,上升沿?cái)?shù)據(jù)傳輸
}
i+=8; //判斷語句
}
PORTB|=0X40; //LOAD=1,PB6=1;
}

void MAX7219_SendData_(uchar addr,uchar data)
{
uchar i=0,j=0,temp=0;
PORTB&=0XBF; //LOAD=0,PB6=0;
for(i=0;i<8;i++) //傳送地址
{
temp=addr;
PORTB&=0X7F; //SLK=0,數(shù)據(jù)在時(shí)鐘上升沿傳入
if((temp&0x80)==0x80) //判斷高位為1還是0,若為1在PB7上輸出1,否則輸出0
PORTB|=0X20;
else
PORTB&=0XDF;
PORTB|=0X80; //SLK=1,數(shù)據(jù)在時(shí)鐘上升沿傳入
addr=addr<<1; //數(shù)據(jù)左移一位,傳送次高位數(shù)據(jù),一次循環(huán)8次傳送完畢
}
for(j=0;j<8;j++) //傳送數(shù)據(jù)
{
temp=data;
PORTB&=0X7F; //SLK=0,數(shù)據(jù)在時(shí)鐘上升沿傳入
if((temp&0x80)==0x80)//判斷高位為1還是0,若為1在PB7上輸出1,否則輸出0
PORTB|=0X20;
else
PORTB&=0XDF;
PORTB|=0X80; //SLK=1,數(shù)據(jù)在時(shí)鐘上升沿傳入
data=data<<1; //數(shù)據(jù)左移一位,傳送次高位數(shù)據(jù),一次循環(huán)8次傳送完畢
}
PORTB|=0X40; //LOAD=1,PB6=1;
}

void MAX7219_Init(void)
{
MAX7219_SendData_(0x0c,0x01);//掉電模式:0x01表示正常操作/0x00表示睡眠模式
MAX7219_SendData_(0x0f,0x00);//顯示測(cè)試模式:0x00表示正常操作/0x01表示顯示測(cè)試模式
MAX7219_SendData_(0x09,0xff);//譯碼模式:0x00表示不譯碼/0x01、0x0f、0xff表示不同的譯碼方式,0xff為7段譯碼
MAX7219_SendData_(0x0b,0x07);//掃描設(shè)置:0x07表示8個(gè)數(shù)碼管
MAX7219_SendData_(0x0a,0x07);//顯示亮度:范圍是0~f,這里設(shè)置為7
}

void Display_time(void)
{
MAX7219_SendData_(0x01,1);
MAX7219_SendData_(0x02,1);
MAX7219_SendData_(0x03,0x0a);
MAX7219_SendData_(0x04,2);
MAX7219_SendData_(0x05,6);
MAX7219_SendData_(0x06,0x0a);
MAX7219_SendData_(0x07,3);
MAX7219_SendData_(0x08,0);
}

void Display_date(void)
{

MAX7219_SendData(0x01,QIAN);
MAX7219_SendData(0x02,BAI);
MAX7219_SendData(0x03,SHI);
MAX7219_SendData(0x04,0X80|GE);
MAX7219_SendData(0x05,1);
MAX7219_SendData(0x06,0x80|2);
MAX7219_SendData(0x07,2);
MAX7219_SendData(0x08,8);
}

void main(void)
{
System_Init();
MAX7219_Init();
BCD_Change(2010);
while(1)
{
Display_time();
delay_ms(2000);
Display_date();
delay_ms(2000);
}
}

PROTUES7.5方針圖



評(píng)論


技術(shù)專區(qū)

關(guān)閉