// 串行數(shù)碼管顯示 tlc549 AD轉(zhuǎn)換值
// 芯片 ATMEGA16L
// 時(shí)鐘 4MHz 內(nèi)部
// 采用系統(tǒng)自帶延時(shí)程序
#include
#include
#define hc164_da
#define hc164_clk PORTD.1 // 164時(shí)鐘線
#define CLK549 PORTD.4 // tlc549時(shí)鐘線, PD4輸出
#define DA
#define CS549 PORTD.6 // tlc549片選線 ,PD6輸出
void hc164_send_byte (unsigned char byte); // 164輸出子程序
void leddisplay (void); // 數(shù)碼管顯示子程序
unsigned char TLC549_ADC (void); //AD轉(zhuǎn)換子程序
unsigned char ledxs[8]={16,16,16,16,16,0,0,0}; // 數(shù)碼管顯示緩沖區(qū)
// AD轉(zhuǎn)換值 百位 十位 個(gè)位
flash unsigned char tab[]={0xb7,0x12,0x67,0x76,0xd2,0xf4,0xf5,0x16,0xf7,0xf6,0xd7,0xf1,0xa5,0x73,0xe5,0xc5,0,0xff};
//共陰極代碼 0-F, 全滅,全亮
void main()
{
unsigned char ad_value,temp,j;
j=1;while(--j); // 一個(gè)循環(huán)11個(gè)周期,4M晶振,延時(shí)2.75us
delay_ms(200);
DDRD = 0x53; // PD0、PD1(164驅(qū)動(dòng))置為輸出方式;
// PD4、PD6(tlc549時(shí)鐘和片選)置為輸出方式 ,PD5(tlc549數(shù)據(jù)線)置為輸入方式
while(1)
{
ad_value = TLC549_ADC(); //讀取AD轉(zhuǎn)換值
temp = ad_value/100;
ledxs[5] = temp; //求得AD轉(zhuǎn)換值百位
temp = ad_value%100;
ledxs[6] = temp/10; // 求得AD轉(zhuǎn)換值十位
ledxs[7] = temp%10; // 求得AD轉(zhuǎn)換值個(gè)位
leddisplay(); // 串行顯示
delay_ms(1000);
}
}
unsigned char TLC549_ADC(void)
{
unsigned char value=0;
unsigned char i,j;
CS549=1;
j=2;while(--j);
CLK549=0;
j=2;while(--j);
CS549=0; //芯片起始
j=2;while(--j); //等待延時(shí)
for(i=0;i<8;i++) //輸入采樣轉(zhuǎn)換時(shí)鐘 (預(yù)采樣)
{
CLK549=1;
j=1;while(--j);
CLK549=0;
j=1;while(--j);
}
CS549=1; //開(kāi)始轉(zhuǎn)換
j=10;while(--j); //等待轉(zhuǎn)換結(jié)束
CLK549=0;
j=2;while(--j);
CS549=0; //開(kāi)始讀取轉(zhuǎn)換結(jié)果
j=2;while(--j);
for(i=0;i<8;i++) // 讀采樣值
{
CLK549=1;
j=1;while(--j);
value<<=1;
value|=DA
CLK549=0;
j=1;while(--j);
}
CS549=1;
j=10;while(--j);
return(value); //返回轉(zhuǎn)換結(jié)果
}
void leddisplay() // 數(shù)碼管顯示子程序
{
unsigned char i;
for(i=0;i<8;i++)
{
hc164_send_byte (tab[ledxs[i]]);
delay_us(2);
}
}
void hc164_send_byte (unsigned char byte) // 164輸出子程序
{
unsigned char i;
for(i=0;i<8;i++)
{
hc164_da
hc164_clk = 1;
hc164_clk = 0;
}
}