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

📄 main.c

📁 一个关于AVR单片机的例程
💻 C
字号:
/*********************************************************************************************************
**--------------文件信息--------------------------------------------------------------------------------
**文   件   名: main.c
**创   建   人: 张兴成
**最后修改日期: 2007年6月4日
**描   述: T0定时2ms 数码管显示
**芯   片:M16
**晶   振:7.3728M
**------------------------------------------------------------------------------------------------------
*********************************************************************************************************/

/*
PA 0~7 接数码管a~dp 数码管A接VCC
*/


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

volatile unsigned int i=0;
volatile unsigned char j=0;
const unsigned char seg_7[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
void port_init(void)
{
PORTA = 0x00;
DDRA = 0xFF;
PORTB = 0x00;
DDRB = 0x0F;
PORTC = 0x00; //m103 output only
DDRC = 0x00;
PORTD = 0x00;
DDRD = 0x00;
}

//TIMER0 initialize - prescale:64
// WGM: Normal
// desired value: 2mSec
// actual value: 1.997mSec (0.2%)
void timer0_init(void)
{
TCCR0 = 0x00; //stop
TCNT0 = 0x1A; //set count
OCR0 = 0xE6; //set compare
TCCR0 = 0x03; //start timer
}

#pragma interrupt_handler timer0_ovf_isr:iv_TIMER0_OVF
void timer0_ovf_isr(void)
{
TCNT0 = 0x1A; //reload counter value
i++; 
}

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

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

void main(void)
{
init_devices();
while(1)
{
if(i==500)
{
i=0; 
PORTA = seg_7[j];
j++;
if(j==10) j=0;
}
}
}

⌨️ 快捷键说明

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