《AVR单片机嵌入式系统原理与应用实践》AVR Studio编译系统定时、中断代码
#include<avr/io.h>#include<avr/interrupt.h>
unsigned char led_7={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
unsigned charposition ={0x20,0x10,0x08,0x04,0x02,0x01};
unsigned char time;
unsigned char dis_buff;
unsigned char time_counter;
unsigned char posit;
unsigned int point_on,time_1s_ok;
void display(void)
{
PORTC =0x00;
PORTA =led_7];
if(point_on && (posit==2||posit==4)) PORTA |=0x80;
PORTC =position;
if(++posit>=6) posit = 0;
}
//定时器0初始化中断-比较匹配CTC模式4M/16分频
//ISR (TIMER0_COMP_vect)
ISR(SIG_OUTPUT_COMPARE0)
{
TCNT0 =0x00;
display();
if(++time_counter>=500)
{
time_counter =0;
time_1s_ok =1;
}
}
void time_to_disbuffer(void)
{
char i,j=0;
for(i=0;i<=2;i++)
{
dis_buff =time%10;
dis_buff =time/10;
}
}
void main(void)
{
PORTA =0x00;
DDRA =0xFF;
PORTC =0xC0;
DDRC =0x3F;
time=23;time=58;time=55;
posit=0;
time_to_disbuffer();
//T0初始化
TCCR0 =0x00;
TCNT0 =0x00;
OCR0 =0x7C;
TIMSK =0x02;
sei();
while(1)
{
if(time_1s_ok =1)
{
time_1s_ok=0;
point_on=~point_on;
if(++time>=60)
{
time=0;
if(++time>=60)
{
time=0;
if(++time>=24) time=0;
}
}
time_to_disbuffer();
}
}
}
此代码是《AVR单片机嵌入式系统原理与应用实践》中8.4节移植过来的 #include <avr/io.h>
#include <avr/delay.h>
unsigned char led_7={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
unsigned char position={0x20,0x10,0x08,0x04,0x02,0x01};
unsigned char time; // 时、分、秒计数
unsigned char dis_buff; // 显示缓冲区,存放要显示的6个字符的段码值
unsigned char time_counter; // 1秒计数器
unsigned int point_on; // 秒显示标志
void display(void) // 扫描显示函数,执行时间12ms
{
unsigned char i;
for(i=0;i<=5;i++)
{
PORTA = led_7];
if (point_on && ( i==2 || i==4 )) PORTA |= 0x80; // (1)
PORTC = position;
_delay_ms(2); // (2)
PORTC = 0x00; // (3)
}
}
void time_to_disbuffer(void) // 时间值送显示缓冲区函数
{
unsigned char i,j=0;
for (i=0;i<=2;i++)
{
dis_buff = time % 10;
dis_buff = time / 10;
}
}
int main(void)
{
PORTA=0x00; // PORTA初始化
DDRA=0xFF;
PORTC=0xC0; // PORTC初始化
DDRC=0x3F;
time = 11; time =34; time = 52; // 时间初值23:58:55
time_to_disbuffer();
while (1)
{
display(); // 显示扫描,执行时间12ms
if (++time_counter >= 40)
{
time_counter = 0; // (4)
point_on = ~point_on; // (5)
if (++time >= 60)
{
time = 0;
if (++time >= 60)
{
time = 0;
if (++time >= 24) time = 0;
}
}
time_to_disbuffer();
}
_delay_ms(13); // 延时13ms,可进行其它处理(6)
}
}
此代码《AVR单片机嵌入式系统原理与应用实践》中6.5节移植过来的 本人是电子爱好者,AVR单片机是看过马老师的书和mega16资料自学的,之前也没有学过单片机,不对地方请鞭策 回复【楼主位】starlu163 星星
#include<avr/io.h>
#include<avr/interrupt.h>
unsigned char led_7={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
unsigned charposition ={0x20,0x10,0x08,0x04,0x02,0x01};
unsigned char time;
unsigned char dis_buff;
unsigned char time_counter;
unsigned char posit;
unsigned int point_on,time_1s_ok;
void display(void)
{
portc =0x00;
porta =led_7[dis_buff[posi......
-----------------------------------------------------------------------
//T0初始化
TCCR0 =0x03; //TCCR0 =0x00; 此处修改
TCNT0 =0x00;
OCR0 =0x7C;
TIMSK =0x02;
页:
[1]