max7219控制8段数码管 spi接口
2016-08-21 来源:eefocus
#include
#define uint unsigned int
#define uchar unsigned char
#define CS_H PORTB|=(1<<4)
#define CS_L PORTB&=~(1<<4)
void SPI_MasterInit(void)
{
/* 设置MOSI 和SCK 为输出,其他为输入 */
DDRB = (1<<4)|(1<<5)|(1<<7);
/* 使能SPI 主机模式,设置时钟速率为fck/16 */
SPCR = (1<
void SPI_MasterTransmit(uchar cData)
{
/* 启动数据传输 */
SPDR = cData;
/* 等待传输结束 */
while(!(SPSR & (1<
void M7219_send(uchar b1,uchar b2)
{
CS_L;
SPI_MasterTransmit(b1);
SPI_MasterTransmit(b2);
CS_H;
}
void delay(uint ms)
{
uint i,j;
for(i=0;i
}
void main()
{
uchar i,j;
SPI_MasterInit();
//M7219_send(0x0f,0xff);
M7219_send(0x09,0xff);//手册说0x07 Code B decode for digits 7–0
//感觉有问题,直接写0xff了
M7219_send(0x0a,0x05);//Intensity Register
M7219_send(0x0b,0x07);//Display digits 0 1 2 3 4 5 6 7
M7219_send(0x0c,0x01);//开显示
while(1)
for(i=0;i<8;i++)
M7219_send(i+1,i);
}
上一篇:自制作并口ISP下载线