max7219控制8段數(shù)碼管 spi接口
#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)
{
/* 設(shè)置MOSI 和SCK 為輸出,其他為輸入 */
DDRB = (1<<4)|(1<<5)|(1<<7);
/* 使能SPI 主機(jī)模式,設(shè)置時(shí)鐘速率為fck/16 */
SPCR = (1<
void SPI_MasterTransmit(uchar cData)
{
/* 啟動(dòng)數(shù)據(jù)傳輸 */
SPDR = cData;
/* 等待傳輸結(jié)束 */
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);//手冊(cè)說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);
}
評(píng)論