⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 atmega16_8led._c

📁 AVR单片机来驱动8位数码管
💻 _C
字号:
//ICC-AVR application builder : 2009-3-3 14:58:49
// Target : M16
// Crystal: 8.0000Mhz

#include <iom16v.h>
#include <macros.h>
#include "delay.h"
#define uchar unsigned char
#define uint  unsigned int
#define ulong  unsigned long

#define CLK_1  PORTA|=(1<<0)
#define CLK_0  PORTA&=~(1<<0)
#define RCLK_1  PORTA|=(1<<1) 
#define RCLK_0  PORTA&=~(1<<1)
#define DAT_1  PORTA|=(1<<2) 
#define DAT_0  PORTA&=~(1<<2) 

const char tab[17]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0};//0-9,
const char seg[9]={0x08,0x04,0x02,0x01,0x10,0x20,0x40,0x80,0x00};
const char off[8]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
const char on[8]={0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
uchar display_buff[8]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
uint i,number;


void port_init(void)
{
 PORTA = 0x00;
 DDRA  = 0x07;
}
//call this routine to initialize all peripherals
void init_devices(void)
{
 //stop errant interrupts until set up
 CLI(); //disable all interrupts
 port_init();

 MCUCR = 0x00;
 GICR  = 0x00;
 TIMSK = 0x00; //timer interrupt sources
 SEI(); //re-enable interrupts
 //all peripherals are now initialized
}

void BINtoBCD(ulong count)
{ 
  uint temp,i;
  temp=count;
  for (i=0;i<8;i++)
  {
   temp=count%10;
   display_buff[i]=tab[temp];
   count=count/10; 
   }
}
//发送一个字节
void write_595 (uchar data)
{
 uchar i,temp;
 temp=data;
 for (i=0;i<8;i++)
 {
  CLK_0;
  if (temp&0x80)DAT_1;
  else DAT_0;
  temp<<=1;
  CLK_1;
 }
}

void out_595 ()
{
 RCLK_0;
 RCLK_1;
}

//发送并显示四个字节
void display ()
{
 uchar i;
 for (i=0;i<4;i++)
 {
  write_595 (seg[i]);
  write_595 (display_buff[i]);
  out_595 ();
  delay_nms (2);
 }
}

void display_off ()
{
 uchar i;
 for (i=0;i<4;i++)
 {
  write_595 (seg[i]);
  write_595 (off[i]);
  out_595 ();
  delay_nms (2);
 }
}

void display_on ()
{
 uchar i;
 for (i=0;i<4;i++)
 {
  write_595 (seg[i]);
  write_595 (on[i]);
  out_595 ();
  delay_nms (2);
 }
}

void main ()
{
 init_devices();
 number=9990;
 while (1)
 {
  number++;
  BINtoBCD(number);
  for (i=0;i<125;i++) 
  display ();
  if (number>=9999) number=0;
 }
} 

⌨️ 快捷键说明

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