?? serialdac.c
字號:
/*****************************************/
/* Copyright (c) 2005, 通信工程學(xué)院 */
/* All rights reserved. */
/* 作 者:戴 佳 */
/*****************************************/
#include "SerialDAC.h"
/* 延時(shí)t毫秒 */
void delay(uint t)
{
uint i;
while(t--)
{
/* 對于12M時(shí)鐘,約延時(shí)1ms */
for (i=0;i<125;i++)
{}
}
}
/* 向MAX7219寫入字節(jié)(8位)*/
void SendChar (uchar ch)
{
uchar i,temp;
_nop_();
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;
_nop_();
SendChar(addr);
_nop_();
SendChar(num);
_nop_();
LOAD=1; // 鎖存進(jìn)相應(yīng)寄存器
}
/* MAX7219初始化 */
void InitDisplay (void)
{
WriteWord (ScanLimit,ScanDigit); // 設(shè)置掃描界限
WriteWord (DecodeMode,DecodeDigit); // 設(shè)置譯碼模式
WriteWord (Intensity,IntensityGrade); // 設(shè)置亮度
WriteWord (ShutDown,NormalOperation); // 設(shè)置為正常工作模式
}
/* 起始條件子函數(shù) */
void start(void)
{
SDA = 1;
SCL = 1;
_nop_();
SDA = 0;
_nop_();
}
/* 停止條件子函數(shù) */
void stop(void)
{
SDA = 0;
SCL = 1;
_nop_();
SDA = 1;
_nop_();
}
/* 應(yīng)答子函數(shù) */
void ack(void)
{
SDA = 0;
_nop_();
SCL = 1;
_nop_();
SCL = 0;
}
/* 發(fā)送數(shù)據(jù)子程序,ch為要發(fā)送的數(shù)據(jù) */
void send(uchar ch)
{
uchar BitCounter = 8; //位數(shù)控制
uchar tmp; //中間變量控制
do
{
tmp = ch;
SCL = 0;
if ((tmp&0x80)==0x80) //如果最高位是1
SDA = 1;
else
SDA = 0;
SCL = 1;
tmp = ch<<1; //左移
ch = tmp;
BitCounter--;
}
while(BitCounter);
SCL = 0;
}
/* 串行DA轉(zhuǎn)換子函數(shù) */
void DACOut(uchar ch)
{
start(); // 發(fā)送啟動信號
send(0x58); // 發(fā)送地址字節(jié)
ack();
send(0x00); // 發(fā)送命令字節(jié)
ack();
send(ch); // 發(fā)送數(shù)據(jù)字節(jié)
ack();
stop(); // 結(jié)束一次轉(zhuǎn)換
}
/* 主函數(shù) */
void main(void)
{
InitDisplay (); // MAX7219初始化
WriteWord(DisplayTest,TestMode); // 開始顯示測試,點(diǎn)亮所有LED
delay(2000); // 延時(shí)約2s
WriteWord (DisplayTest,TextEnd); // 退出顯示測試模式
while(1)
{
uchar i,j;
/* 對數(shù)字0~255進(jìn)行數(shù)模轉(zhuǎn)換,并用數(shù)碼管顯示正在轉(zhuǎn)換的數(shù)字(二進(jìn)制) */
for (i=0;i<=255;i++)
{
delay(1000); // 間隔約1s
InitDisplay();
for (j=0;j<=7;j++)
DisBuffer[j]=((i>>j)&0x01);
WriteWord (Digit0,DisBuffer[0]);
WriteWord (Digit1,DisBuffer[1]);
WriteWord (Digit2,DisBuffer[2]);
WriteWord (Digit3,DisBuffer[3]);
WriteWord (Digit4,DisBuffer[4]);
WriteWord (Digit5,DisBuffer[5]);
WriteWord (Digit6,DisBuffer[6]);
WriteWord (Digit7,DisBuffer[7]);
DACOut(i); // 調(diào)用串行DA轉(zhuǎn)換子函數(shù)
}
delay(2000); // 延時(shí)2s
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -