今天捣鼓了一下MAX7219.
DIN:PA10
LOAD:PB03
CLK:PB02
代码:
#include "samd21.h"
#define uchar unsigned char
#define uint unsigned int
#define CLK_0 PORT->Group[1].OUTCLR.reg = PORT_PB02
#define CLK_1 PORT->Group[1].OUTSET.reg = PORT_PB02
#define LOAD_0 PORT->Group[1].OUTCLR.reg = PORT_PB03
#define LOAD_1 PORT->Group[1].OUTSET.reg = PORT_PB03
#define DIN_0 PORT->Group[0].OUTCLR.reg = PORT_PA10
#define DIN_1 PORT->Group[0].OUTSET.reg = PORT_PA10
#define NoOp 0x00 //空操作寄存器
#define Digit0 0x01 // 数码管1寄存器
#define Digit1 0x02 // 数码管2寄存器
#define Digit2 0x03 // 数码管3寄存器
#define Digit3 0x04 // 数码管4寄存器
#define Digit4 0x05 // 数码管5寄存器
#define Digit5 0x06 // 数码管6寄存器
#define Digit6 0x07 // 数码管7寄存器
#define Digit7 0x08 // 数码管8寄存器
#define DecodeMode 0x09 // 译码模式寄存器
#define Intensity 0x0a // 亮度寄存器
#define ScanLimit 0x0b // 扫描位数寄存器
#define ShutDown 0x0c // 低功耗模式寄存器
#define DisplayTest 0x0f // 显示测试寄存器
#define ShutdownMode 0x00 // 低功耗方式
#define NormalOperation 0x01 // 正常操作方式
#define ScanDigit 0x07 // 扫描位数设置,显示8位数码管
#define DecodeDigit 0xff // 译码设置,8位均为BCD码
#define IntensityGrade 0x0a // 亮度级别设置
#define TestMode 0x01 // 显示测试模式
#define TextEnd 0x00 // 显示测试结束,恢复正常工作模式
/*****************************************************************************
* Function implementation - global ('extern') and local ('static')
******************************************************************************/
uchar DisBuffer[8]={0,0,0,0,0,0,0,0}; // 显示缓存区
//******************延时t毫秒**************************************
void delay(uint t)
{
uint i;
while(t--)
{
/* 对于12M时钟,约延时1ms */
for (i=0;i<625;i++)
{}
}
}
//*************向MAX7219写入字节(8位)********************
void SendChar (uchar ch)
{
uchar i,temp;
delay(1);
for (i=0;i<8;i++)
{
temp=ch&0x80;
ch=ch<<1;
if(temp)
{
DIN_1;
CLK_0;
CLK_1;
}
else
{
DIN_0;
CLK_0;
CLK_1;
}
}
}
//**************向MAX7219写入字(16位)*****************************
void WriteWord (uchar addr,uchar num)
{
LOAD_0;
delay(1);
SendChar(addr);
delay(1);
SendChar(num);
delay(1);
LOAD_1; // 锁存进相应寄存器
}
//*********************** MAX7219初始化 ******************
void InitDisplay (void)
{
WriteWord (ScanLimit,ScanDigit); // 设置扫描界限
WriteWord (DecodeMode,DecodeDigit); // 设置译码模式
WriteWord (Intensity,IntensityGrade); // 设置亮度
WriteWord (ShutDown,NormalOperation); // 设置为正常工作模式
}
void SystemInit()
{
//do nothing
}
void LED_Init(void)
{
PORT->Group[1].DIRSET.reg = PORT_PB10;
PORT->Group[1].OUTSET.reg = PORT_PB10;
PORT->Group[0].DIRSET.reg = PORT_PA10;
PORT->Group[1].DIRSET.reg = PORT_PB02;
PORT->Group[1].DIRSET.reg = PORT_PB03;
}
void LED_ON(void)
{
PORT->Group[1].OUTCLR.reg = PORT_PB10;
}
void LED_OFF(void)
{
PORT->Group[1].OUTSET.reg = PORT_PB10;
}
void configure_osc8m(SYSCTRL_OSC8M_Type osc8m)
{
osc8m.bit.ENABLE = 1;
SYSCTRL->OSC8M = osc8m;
}
void configure_osc8m_8m(void)
{
SYSCTRL_OSC8M_Type osc8m;
osc8m = SYSCTRL->OSC8M;
osc8m.bit.PRESC = SYSCTRL_OSC8M_PRESC_0_Val;
osc8m.bit.RUNSTDBY = 0;
osc8m.bit.ONDEMAND = 1;
configure_osc8m(osc8m);
}
void configure_dfll(SYSCTRL_DFLLCTRL_Type dfllctrl, SYSCTRL_DFLLMUL_Type dfllmul, SYSCTRL_DFLLVAL_Type dfllval)
{
SYSCTRL->DFLLCTRL.reg = dfllctrl.reg & ~SYSCTRL_DFLLCTRL_ONDEMAND;
while (!(SYSCTRL->PCLKSR.reg & SYSCTRL_PCLKSR_DFLLRDY)) {
/* Wait for DFLL sync */
}
SYSCTRL->DFLLMUL.reg = dfllmul.reg;
SYSCTRL->DFLLVAL.reg = dfllval.reg;
dfllctrl.bit.ENABLE = 1;
/* Write full configuration to DFLL control register */
SYSCTRL->DFLLCTRL.reg = dfllctrl.reg;
}
void configure_dfll_open_loop(void)
{
SYSCTRL_DFLLCTRL_Type dfllctrl;
SYSCTRL_DFLLMUL_Type dfllmul;
SYSCTRL_DFLLVAL_Type dfllval;
dfllctrl.bit.LLAW = 0;
dfllctrl.bit.STABLE = 0;
dfllctrl.bit.QLDIS = 0;
dfllctrl.bit.CCDIS = 0;
dfllctrl.bit.ONDEMAND = 1;
dfllctrl.bit.MODE = 0;
dfllval.bit.COARSE = (*((uint8_t*)(0x806020+7)))>>2;//0x1f / 4; /* Midpoint */
dfllval.bit.FINE = (*((uint32_t*)(0x806020+8))) & 0x3FF;//0xff / 4; /* Midpoint */
dfllmul.bit.CSTEP = 1;
dfllmul.bit.FSTEP = 1;
dfllmul.bit.MUL = 6; /* Multiply 8MHz by 6 to get 48MHz */
configure_dfll(dfllctrl, dfllmul, dfllval);
NVMCTRL->CTRLB.bit.RWS = 2;
}
void configure_gclk(GCLK_GENCTRL_Type genctrl, GCLK_GENDIV_Type gendiv)
{
while (GCLK->STATUS.reg & GCLK_STATUS_SYNCBUSY) {
/* Wait for synchronization */
};
/* Select the correct generator */
GCLK->GENDIV.bit.ID = gendiv.bit.ID;
/* Write the new generator configuration */
while (GCLK->STATUS.reg & GCLK_STATUS_SYNCBUSY) {
/* Wait for synchronization */
};
GCLK->GENDIV = gendiv;
while (GCLK->STATUS.reg & GCLK_STATUS_SYNCBUSY) {
/* Wait for synchronization */
};
genctrl.bit.GENEN = GCLK->GENCTRL.bit.GENEN;
GCLK->GENCTRL = genctrl;
}
void configure_gclk_gen_dfll(void)
{
/* Change system clock to DFLL */
GCLK_GENCTRL_Type genctrl;
GCLK_GENDIV_Type gendiv;
genctrl.reg = 0;
gendiv.reg = 0;
genctrl.bit.ID = GCLK_CLKCTRL_GEN_GCLK0_Val;
gendiv.bit.ID = GCLK_CLKCTRL_GEN_GCLK0_Val;
genctrl.bit.SRC = GCLK_SOURCE_DFLL48M;
genctrl.bit.DIVSEL = 0;
genctrl.bit.IDC = 0;
genctrl.bit.OOV = 0;
genctrl.bit.RUNSTDBY = 0;
genctrl.bit.OE = 0;
gendiv.bit.DIV = 0;
configure_gclk(genctrl, gendiv);
}
int main(void)
{
long int i;
configure_osc8m_8m();
configure_dfll_open_loop();
configure_gclk_gen_dfll();
LED_Init();
delay(1500);
InitDisplay (); // MAX7219初始化
WriteWord(DisplayTest,TestMode); // 开始显示测试,点亮所有LED
delay(1500); // 延时约1.5s
WriteWord (DisplayTest,TextEnd); // 退出显示测试模式
WriteWord (Digit0,0);
WriteWord (Digit1,1);
WriteWord (Digit2,2);
WriteWord (Digit3,3);
while(1)
{
LED_ON();
for(i=5000000;i>0;i--);
LED_OFF();
for(i=5000000;i>0;i--);
}
}
效果图:
MAX7219模块是我自己做的,原理图:
|