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

📄 1sled.txt

📁 定时1sLED显示的程序实例 单片机进阶教程的扩展程序
💻 TXT
字号:
//ICC-AVR application builder : 2006-11-24 11:46:03
// Target : M16
// Crystal: 7.3728Mhz

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

const led_table[16]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};// 0~~f
//定义LED的数据表
typedef unsigned char uint8;
uint8 i;

void port_init(void)
{
PORTA = 0x00;
DDRA = 0xFF;
PORTB = 0x00;
DDRB = 0x00;
PORTC = 0x00; //m103 output only
DDRC = 0x00;
PORTD = 0x00;
DDRD = 0x00;
}

//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_compa_isr:7
void timer1_compa_isr(void)
{
//compare occured TCNT1=OCR1A
}

#pragma interrupt_handler timer1_compb_isr:8
void timer1_compb_isr(void)
{
//compare occured TCNT1=OCR1B
}

#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
i++;
if(i==16) i=0; //等于F时,恢复零
PORTA=led_table[i]; //这里使LED变化
}

//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
timer1_init();

MCUCR = 0x00;
GICR = 0x00;
TIMSK = 0x1C; //timer interrupt sources /*定时器使用中断规则设定*/
SEI(); //re-enable interrupts
//all peripherals are now initialized
}

void main(void)
{
init_devices();
while(1)
;
}

⌨️ 快捷键说明

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