segdisp-16sn.c

来自「大量ATMEGA16实例, 都可以运行,包括驱动LCD1602, 上位机显示等多」· C语言 代码 · 共 138 行

C
138
字号
// Title:	SegDisp-16sn.c
// 			Function program for DEMOA-16sm utilizing 4x7-segment display

#include <iom16v.h>
#include <macros.h>

extern void port_init(void);
const char dig0 = 0x40, dig1 = 0x80, dig2=0x10, dig3=0x08, dot=0x20;
char segconv[]={0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f};
unsigned char BCD0, BCD1, BCD2, BCD3;
int count;
int led_index=0, time_over=0;
unsigned char swin3;

char pattern1[]= {0x01, 0x03, 0x07, 0x0F, 0x1f, 0x3f, 0x7f, 0xff, 0x7f,
						  0x3f, 0x1f, 0x0f, 0x07, 0x03, 0x01};
char pattern2[]= {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x40,
						  0x20, 0x10, 0x08, 0x04, 0x02, 0x01};
char pattern3[]= {0x01, 0x80, 0x02, 0x40, 0x04, 0x20, 0x08, 0x10, 0x10,
						  0x08, 0x20, 0x04, 0x40, 0x02, 0x80};
char pattern4[]= {0x11, 0x22, 0x44, 0x88, 0x44, 0x22, 0x11, 0x22, 0x44,
						  0x88, 0x44, 0x22, 0x11, 0x00, 0x00};
						  
void delay(int count)
{
 int i, j;
 for(i=count; i>0; i--)
    for(j=3; j>0; j--)
	   ;
} 

//*****************************************************************

void led_disp(void)
  {    
    if(time_over>=10)
	  {
	  swin3=PINC&0xc0;
	  swin3=swin3>>6;
	 	
	  if(swin3==0)
	  	{
	  	PORTD=pattern1[led_index];	  
	  	}
	  else if(swin3==0x01)
	    {
	  	PORTD=pattern2[led_index];	  
	  	}
	  else if(swin3==0x02)
	    {
	    PORTD=pattern3[led_index];	  
	    }
	  else
	    {
	    PORTD=pattern4[led_index];	  
	    }
	  led_index++;
	  if(led_index>14)
	     led_index=0;	
	  }	
	  swin3=~PINC&0x0f;
	delay(100+50*swin3);
  }

void BinToBCD(int data)
{
 BCD0=0;
 BCD1=0;
 BCD2=0;
 BCD3=0;
 if(data>=10000)
    data=0;
 if(data>=1000)
    {
    BCD3=data/1000;
	data=data%1000;
	}	
 if(data>=100)
    {
    BCD2=data/100;
	data=data%100;
	}	
	
 if(data>=10)
    {
    BCD1=data/10;
	data=data%10;
	}		
 BCD0=data;	
}

void dispseg(void)
{
 PORTB=~segconv[BCD0];
 PORTA=dig0;
 led_disp();
 PORTB=~segconv[BCD1];
 PORTA=dig1;
 led_disp();
 PORTB=~segconv[BCD2];
 PORTA=dig2;
 led_disp();
 PORTB=~segconv[BCD3];
 PORTA=dig3;
 led_disp();
 time_over++;
 if(time_over>10)
    time_over=0;
}

void dot_on(void)
{
 PORTB=~0x80;
 PORTA=dot; 
 delay(20);
}


//*****************************************************************
/*
void main(void)
{
 unsigned char outa=0x00, outb=0x00, outc=0x00, outd=0x00;
 unsigned char dswin;  
 port_init();
 while(1)
   {
    WDR();				  //Watchdog reset
	dswin=PINC;
	//dswin=1234;
    BinToBCD(dswin);
	dispseg();
	dot_on();
	PORTD=0x55;
	//PORTB=dswin;
    delay(5);
   }
}*/

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?