|
程序是小程序,写的很菜,请高手们指正一下,以增加我学GCC路上的信心!今天打算把串口加上去,用功能码来查询当前的计数值,和设置向上向下清零等操作。源程序如下:
/*项目名称:双向计时器,计时范围0-9999s 采用动态扫描
晶振:内部1Mhz 调试方法:JTAG+实验板 采用AVRSTUDIO4.12+WINAVR GCC
PA口对应数码管的七段 PB0-PB3对应数码管的位控制
日期:2005-12-1晚20:03分
*/
#include <avr/io.h>
#include <avr/signal.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <avr/delay.h>
#define set_led1 PORTB|=0x01 //宏定义,个位选通位
#define clr_led1 PORTB&=~(1<<PB0) //关掉个位
#define set_led2 PORTB|=(1<<PB1) //选通十位
#define clr_led2 PORTB&=~(1<<PB1) //关掉十位
#define set_led3 PORTB|=(1<<PB2) //选通百位
#define clr_led3 PORTB&=~(1<<PB2) //关掉百位
#define set_led4 PORTB|=(1<<PB3) //选通千位
#define clr_led4 PORTB&=~(1<<PB3) //关掉千位
unsigned char count,direction; //定义时间计数及方向寄存器
unsigned int timer; //计时寄存器
const unsigned char table[] PROGMEM={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff}; //FLASH存储区显示数据
void disp(unsigned int data){ //显示程序,并将INT数转换成BCD码再表查送显示
unsigned char temp;
temp=data/1000; //求千位
if(temp) data-=1000*temp;
PORTA=pgm_read_byte(table+temp); //将千位送显示
set_led4;
_delay_ms(1);
clr_led4;
temp=data/100; //求百位
if(temp) data-=100*temp;
PORTA=pgm_read_byte(table+temp); //将百位送显示
set_led3;
_delay_ms(1);
clr_led3;
temp=data/10; //求十位
if(temp) data-=10*temp;
PORTA=pgm_read_byte(table+temp); //将十位送显示
set_led2;
_delay_ms(1);
clr_led2;
PORTA=pgm_read_byte(table+data); //余数为个位,直接查表送显示
set_led1;
_delay_ms(1);
clr_led1;
}
void load_timer_val(){
TCNT0=12; //250ms
}
void init(){
DDRA=0xff;
DDRB=0xff;
DDRD=0b00011111;
PORTD=0b11100000;
PORTA=0xff;
PORTB=0x10;
TCCR0=5; //FSYS/1024分频,普通定时模式250MS
TIMSK=1; //允计计数中断
direction=1; //上电复位时默认向上计数
load_timer_val();
}
SIGNAL(SIG_OVERFLOW0){ //计数器0中断程序
count++;
if(count>4)
{
if(direction){
count=0; //向上计数,最大为9999
timer++;
if(timer>9999) timer=0;
}
else {
count=0; //向下计数,大于65535时,自动为0
timer--;
if(timer==65535) timer=9999;
}
load_timer_val();
}
}
int main(){
init(); //初始化设置
sei(); //开中断
while(1){
disp(timer);
if(bit_is_clear(PIND,PD7)){ //PD7为向上计数设置按键
_delay_ms(2);
loop_until_bit_is_set(PORTD,PD7);
direction=1;
}
if(bit_is_clear(PIND,PD6)){ //PD6为向下计数设置按键
_delay_ms(2);
loop_until_bit_is_set(PORTD,PD6);
direction=0;
}
if(bit_is_clear(PIND,PD5)){ //PD5为数据清零按键
_delay_ms(2);
loop_until_bit_is_set(PORTD,PD5);
timer=0;
}
}
return 0;
} |
阿莫论坛20周年了!感谢大家的支持与爱护!!
月入3000的是反美的。收入3万是亲美的。收入30万是移民美国的。收入300万是取得绿卡后回国,教唆那些3000来反美的!
|