|
![](static/image/common/ico_lz.png)
楼主 |
发表于 2008-4-18 15:18:56
|
显示全部楼层
怪了,可能是程序问题,整理了一下程序,想贴上来,结果重编译后仿真出来LED显示0,1234脚电平也不会轮流高低变化了,但烧进开发板正常,晕了,帮忙看看是不是程序什么地方不规范,造成差异?
编程环境:AVR Studio 4.13 SP2
/* 中断方式 A/D 转换测试程序 MCU:atmega16 时钟:4MHz 外部晶振 */
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#define uchar unsigned char
#define uint unsigned int
//常量定义
#define AREF_Value 2560
const uchar led_7[10] PROGMEM = {0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
const uchar position[4] PROGMEM ={0xfe,0xfd,0xfb,0xf7};
uint dis_buff[4]={0,0,0,0},posit;
uchar time_2ms_ok;
//端口初始化
void PORT_Init()
{
DDRA=0x0f; //PA0~PA3 ----> LED 共阴控制
PORTA=0x0f;
DDRB=0xff; //闲置端口,设为输出
PORTB=0xff;
DDRC=0xff; // LED显示控制,PC0~PC7 ----> a、b、c、d、e、f、g、p
PORTC=0x00;
DDRD=0xff; //闲置端口,设为输出
PORTD=0xff;
}
void adc_to_disbuffer(uint adc)
{
uchar i;
for (i=0;i<=3;i++)
{
dis_buff=adc % 10;
adc /= 10;
}
}
void display(void) // 4位LED数码管动态扫描函数
{
PORTA |= 0x0f;
PORTC = pgm_read_byte(&led_7[dis_buff[posit]]);
if (posit==3) PORTC |= 0x80;
PORTA &= pgm_read_byte(&position[posit]);
if (++posit >=4 ) posit = 0;
}
// A/D 转换完成中断
ISR(ADC_vect )
{
uint g_nAdValue;
volatile uint adc_v;
g_nAdValue=ADCL;
g_nAdValue|=(uint)(ADCH<<8);
adc_v = (unsigned long)g_nAdValue*AREF_Value/1024;
adc_to_disbuffer(adc_v);
}
//Timer 0 比较匹配中断服务
ISR(TIMER0_COMP_vect)
{
time_2ms_ok = 1;
}
int main(void)
{
PORT_Init(); //IO端口初始化
// T/C0 初始化
// 内部时钟,64分频(4M/64=62.5KHz),CTC模式
TCCR0 = (1<<WGM01)|(0<<WGM00)|(0<<CS02)|(1<<CS01)|(1<<CS00);
//计数器置0
TCNT0=0x00;
//T/C0计数器输出比较寄存器
OCR0=0x7C; // OCR0 = 0x7C(124),(124+1)/62.5=2ms
// 允许T/C0比较中断
TIMSK = 1<<OCIE0;
//内部2.56V 参考电压,7 通道
ADMUX =(1<<REFS1)|(1<<REFS0)|(0<<MUX4)|(0<<MUX3)|(1<<MUX2)|(1<<MUX1)|(1<<MUX0);
//使能ADC,中断允许,自由模式, 时钟:ck/32
ADCSRA =(1<<ADEN)|(1<<ADATE)|(1<<ADIE)|(1<<ADIF)|(1<<ADPS2)|(0<<ADPS1)|(1<<ADPS0);
//T/C0预分频器复位
//SFIOR&=0x1F;
SFIOR = 1<<PSR10;
// 选择T/C0比较匹配中断为ADC触发;
SFIOR |= (0<<ADTS2)|(1<<ADTS1)|(1<<ADTS0);
sei();
while(1)
{
if (time_2ms_ok)
{
display(); // LED扫描显示
time_2ms_ok = 0;
}
}
} |
|