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

📄 main._c

📁 一个关于AVR单片机的例程
💻 _C
字号:
// ICC-AVR application builder : 2007-2-14 21:34:00
// Target : M16
// Crystal: 7.3728Mhz
// 工程DEMO 七段数码管显示演示

#include "config.h"

extern const led_data[16]; //声明外部led显示数据,led_data在led.c中
uint16 countdata;

void port_init(void)
{
 PORTA = 0x00;
 DDRA  = 0x00;
 PORTB = 0x00;
 DDRB  = 0x00;
 PORTC = 0x00; //m103 output only
 DDRC  = 0x00;
 PORTD = 0x00;
 DDRD  = 0x00;
}
/***************************************************************/
//定时器1用于一秒刷新 countdata 的值,达到从1-9999计秒的功能
//TIMER1 initialize - prescale:1024
// WGM: 0) Normal, TOP=0xFFFF
// desired value: 1Sec
// actual value:  1.000Sec (0.0%)
void timer1_init(void)
{
 TCCR1B = 0x00; //stop
 TCNT1H = 0xE3; //setup
 TCNT1L = 0xE1;
 OCR1AH = 0x1C;
 OCR1AL = 0x1F;
 OCR1BH = 0x1C;
 OCR1BL = 0x1F;
 ICR1H  = 0x1C;
 ICR1L  = 0x1F;
 TCCR1A = 0x00;
 TCCR1B = 0x05; //start Timer
}

#pragma interrupt_handler timer1_ovf_isr:9
void timer1_ovf_isr(void)
{
 //TIMER1 has overflowed
 TCNT1H = 0xE3; //reload counter high value
 TCNT1L = 0xE1; //reload counter low value
 countdata++;
 if(countdata==9999) countdata=0;
}


//call this routine to initialize all peripherals
void init_devices(void)
{
 //stop errant interrupts until set up
 CLI(); //disable all interrupts
 port_init();
 led_init(); //7段数码管显示
 
 MCUCR = 0x00;
 GICR  = 0x00;
 TIMSK = 0x00; //timer interrupt sources
 SEI(); //re-enable interrupts
 //all peripherals are now initialized
}

/**************************************************/
//只有综合应用才用到本函数
//call this routine to initialize all peripherals
void init_devices2(void)
{
 //stop errant interrupts until set up
 CLI(); //disable all interrupts
 port_init();
 led_init(); //7段数码管显示
 timer1_init(); //定时器1用于更新countdata的值,从1到9999变化
 
 MCUCR = 0x00;
 GICR  = 0x00;
 TIMSK = 0x04; //注意这里允许定时器一溢出
 SEI(); //re-enable interrupts
 //all peripherals are now initialized
}
/******************************************************************************/
//以下显示模式只保留一个,查看效果
/******************************************************************************/

//综合应用例子

void main(void)
{
 init_devices2(); //比init_devices()多了
 while(1)
 {
 display(countdata,0); //普通模式,显示countdata的值,countdata将在timer1定时一秒溢出时改变
 }
}

/*
//用第二种思路显示一个浮点数,有点闪烁感
void main(void)
{
 init_devices();
 while(1)
 {
 display_float2(1.234);
 }
}
*/

/*
//用第一种思路显示一个浮点数,耗费大量(mega16 19%)空间和cpu时间
void main(void)
{
 init_devices();
 while(1)
 {
 display_float(1.234);
 }
}
*/

/*
//最常用的显示,调用display显示 1-9999 中的数
void main(void)
{
 init_devices();
 while(1)
 {
// display(567,0); //普通模式
 display(567,1); //补零模式
 }
}



/*
//从0到F依次显示,一秒变换一次
void main(void)
{
 unsigned char i=0;
 init_devices();
 led_contrl_port |= (1<<led_a)|(1<<led_b)|(1<<led_c)|(1<<led_d); //选中对应位
 while(1)
 {
  led_port=led_data[i];
  delay_1s();
  i++;
  if(i==15) i=0;
 }
}
*/

/*
//一位一位的显示
void main(void)
{
 init_devices();
 while(1)
 {
  display_one(0,1);
  delay_ms(6); //6ms为经验值
    led_contrl_port &=~ (1<<0);
  display_one(1,2);
  delay_ms(6);
    led_contrl_port &=~ (1<<1);
  display_one(2,3);
  delay_ms(6);
    led_contrl_port &=~ (1<<2);
  display_one(3,4);
  delay_ms(6);
    led_contrl_port &=~ (1<<3);
 }
}
*/

⌨️ 快捷键说明

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