今天是采用TLC548做的电压表:
TLC549.pdf
(166.63 KB)
下图是proteus仿真截图:
以下是Atmel Studio6.2的程序
/*
* GccApplication24.c
*
* Created: 2014-9-18 21:08:34
* Author: Administrator
*/
#include <avr/io.h>
#include <avr/interrupt.h>
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
#define SS 4//PORTB.4
#define dv 196 //5V*1000/255
uchar dat_in;
uint adc_v;
uchar tab[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8, //共阳极LED 0~F的段码
0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E};
void delay(uint k) //延时约0.1ms
{
uchar m,n;
for(m=0;m<k;m++)
{
for(n=0;n<114;n++);
}
}
//interrupt [SPI_STC] void spi_isr(void)
ISR(SPI_STC_vect)
{
dat_in=SPDR;
PORTB |= 1<<SS;//SS=1;
adc_v=(ulong)dat_in*dv/10;
}
void display(void)
{
uchar val1,val2,val3,val4;
val1=adc_v/1000;
val2=(adc_v/100)%10;
val3=(adc_v/10)%10;
val4=adc_v%10;
PORTD=0x01;
PORTC=tab[val1];
//PORTC.7=0; //小数点
PORTC &= ~(1<<7);
delay(10);
PORTD=0x02;
PORTC=tab[val2];
delay(10);
PORTD=0x04;
PORTC=tab[val3];
delay(10);
PORTD=0x08;
PORTC=tab[val4];
delay(10);
}
//interrupt [TIM0_COMP] void timer0_isr(void)
ISR(TIMER0_COMP_vect)
{
PORTB &=~(1<<SS); //SS=0; // 每隔2ms选通TLC548
SPDR=0x00; // 启动SPI
}
int main(void)
{
DDRB=0xBF;
PORTB=0x4F;
DDRC=0xFF;
PORTC=0xFF;
DDRD=0xFF;
PORTD=0xFF;
TCCR0=0x0B;
OCR0=0x7C;
TIMSK=0x02;
SPCR=0xD3; //SPE=1,SPE=1,MSTR=1,SPR1=1,SPR0=1,使能SPI 主机模式,设置时钟速率为fck/128
SPSR=0x00;
//#asm("sei")
sei();
while (1)
{
display();
}
}
|