==============================
void ultoa(unsigned char *str, unsigned long ul, unsigned char length) {
do {
str[length--]=(ul % 10) + '0';
ul = ul / 10;
} while (ul); //eliminates leading zeros
//} while (length); //keeps leading zeros
}
==============================
so a the following
ultoa (&vRAM[3], 123, 10);
will create a string of ' 123', starting at vRAM[3]. no trailing null, however.
I use a custom itoa to do that.
==============================
void ultoa(unsigned char *str, unsigned long ul, unsigned char length) {
do {
str[length--]=(ul % 10) + '0';
ul = ul / 10;
} while (ul); //eliminates leading zeros
//} while (length); //keeps leading zeros
}
==============================
so a the following
ultoa (&vRAM[3], 123, 10);
will create a string of ' 123', starting at vRAM[3]. no trailing null, however.
#include <avr/interrupt.h>
unsigned int flag=0;
unsigned char count=0;
unsigned int num=0;
//==================================================
const unsigned char seg[]={ 0xC0, // 0
0xF9, // 1
0xA4, // 2
0xB0, // 3
0x99, // 4
0x92, // 5
0x82, // 6
0xF8, // 7
0x80, // 8
0x90 // 9
};
//==================================================
//IO端口初始化
void PortInit(void)
{
DDRA=0XFF;
PORTA=0X00;
DDRB=0XFF;
PORTB=0XFF;
}
//Timer0初始化
void Timer0Init(void)
{
TCCR0 = 0x00; //stop
TCNT0 = 0x06; //set count
OCR0 = 0xFA; //set compare
TCCR0 = 0x03; //start timer
}
//==================================================
//定时器0溢出中断服务程序
ISR(TIMER0_OVF_vect)
{
TCNT0=0X06;
flag++;
count++;
switch(count)
{
case 1:if(num/1000){PORTA=0X01;PORTB=seg[num/1000];}else PORTB=0XFF;break;
case 2:if(((num/1000)+(num%1000/100))){PORTA=0X02;PORTB=seg[num%1000/100];}else PORTB=0XFF;break;
case 3:if(((num/1000)+(num%1000/100)+(num%100/10))){PORTA=0X04;PORTB=seg[num%100/10];}else PORTB=0XFF;break;
case 4:PORTA=0X08;PORTB=seg[num%10];break;
case 6:count=0;break;
default:break;
}
if (flag==100)
{
flag=0;
num++;
if(num==9999)
{
num=0;
}
}
}
//==================================================
//主函数
int main(void)
{
cli();
TIMSK = 0x01; //timer interrupt sources
PortInit();
Timer0Init();
sei();
//在这继续添加你的代码
while(1){
;
}
}
void LED_del_0(void)
{
unsigned char a,b,c;
a=(LED_data[4]+LED_data[3]);
b=(LED_data[4]+LED_data[3]+LED_data[2]);
c=(LED_data[4]+LED_data[3]+LED_data[2]+LED_data[1]);
CLI();
if(LED_data[4]==0) LED_data[4]=10;
if(a==0) LED_data[3]=10;
if(b==0) LED_data[2]=10;
if(c==0) LED_data[1]=10;
SEI();
}
用下面的精简一点,无论多少个数位,几句话就搞定
void LED_del_0(void)
{
unsigned char n;
for(n=4;n>1;n--) //显示5位数,从高位开始判断,最低位的0不管
{
if(LED_data[n]==0) LED_data[n]=10; //不知道上面的10代表什么,反正LED不显示就是了
else break; 遇到大于0的,退出循环
}
一周热门 更多>