[求助]
M8电压表全部代码和仿真,顺便请教个问题
初学AVR,参考网上的源代码做了电压表,使用AD中断,但中断不能正常工作
[AVR AD CONVERTER] Result is not written to the ADC register because it has been locked.
一直这样提示
先谢谢各位看官了,小弟都折腾了好几天了,没有进展,希望各位多多指点
源代码工程和仿真工程
M8_AD.rar
(67.95 KB)
(下载次数: 28, 2012-10-18 22:17 上传)
源代码- #include
- #include
- #include
- #include
- #include
- #define uchar unsigned char
- #define uint unsigned int
- uchar Table[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};//共阴
- //uchar Table[10]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,};//共阳
- uchar Data[4]={0,0,0,0};
- static uint ADValue[16] = {0}; //AD采样值
- static uint g_nAdValue=0;//平均电压
- static ADTimes=0; //AD采样次数,第几次采样
- static tmp16; //AD采样和
- void Display(uchar *p) //间隔5ms显示
- {
- uchar i,sel=0x01;
- for(i=0;i<4;i++)
- {
- PORTC=sel;
- if(i==2)
- PORTD=0x7f-Table[p[i]]; //0111 1111
- else PORTD=0xff-Table[p[i]];
- _delay_ms(10);
- sel=sel<<1;
- }
- }
- /*
- void Display(uchar *p) //间隔5ms显示
- {
- uchar sel=0x01;
- for(uchar i=0;i<4;i++)
- {
- PORTC=~sel;
- if(i==2)
- PORTD= Table[p[i]] + 0x80; //0111 1111 1000 0000
- else
- PORTD = Table[p[i]];
- _delay_ms(10);
- sel=sel<<1;
- }
- }
- */
- void Process(uint i,uchar *p)
- {
- for(uchar j = 0; j < 4; j++)
- p[j] = 0;
- p[0]=i/1000;
- i=i%1000;
- p[1]=i/100;
- i=i%100;
- p[2]=i/10;
- i=i%10;
- p[3]=i;
- }
- void port_init()
- {
- DDRB=0xf0; //set port b as input port 1111 0000 the first 4 bits as output, the latter 4 bits as input
- PORTB=0xff;
- DDRC=0x0f; //设置C口为推挽1输出;
- PORTC=0x00;
- DDRD=0xff; //设置C口为推挽1输出;
- PORTD=0xff;
- PORTB=0x00;
- PORTD=0x00;
- }
- //********************************************************
- //函数名:Timer0_Init
- //输入:无
- //输出:无
- //描述:ATmega 8 Timer0 初始化,中断周期10ms
- //********************************************************
- void Timer0_Init(void)
- {
- TCCR0=0x04;//256分频,8M/1024,
- TCNT0=0X00;//计数器清零
- TIMSK|=0x01;//使能TOIE0中断
- TIFR|=0X01; //清楚中断标识位
- }
- //********************************************************
- //函数名:SIGNAL(SIG_ADC)
- //输入:无
- //输出:无
- //描述:AD测量中断服务程序
- //********************************************************
- SIGNAL(SIG_ADC)
- {
- ADCSRA|=(1<
- ADValue[ADTimes]=(ADCH<<8);
- ADValue[ADTimes]|=ADCL;
- //ADValue[ADTimes]|=(ADCH<<8);
- ADTimes++;
- if( ADTimes>=16)
- {
- ADTimes = 0;
- tmp16=0;
- for(char n=0;n<16;n++ )
- {
- tmp16+=ADValue[n];
- }
- g_nAdValue = tmp16/16;
- }
- }
- void adc_init()
- {
- ADMUX = 0B00000100; // INTERNAL Vref 2.56V select first ADC4;0000 0100
- // ADCSRA = 0B11000111; //0xc7
- ADCSRA=_BV(ADEN)|_BV(ADIE)|_BV(ADFR)|_BV(ADPS1)|_BV(ADPS0);
- ADCSRA|=_BV(ADSC);//自由模式开始转换
- }
- int main(void)
- {
- port_init();
- adc_init();
- _delay_ms(500); //延时待系统稳定;
- uint ada ,i,v;
- wdt_enable(WDTO_1S);
- sei();
- while(1)
- {
- cli();
- v= g_nAdValue;
- ada=(v+3)*30/128;
- Process(ada,Data);
- Display(Data);
- sei();
- wdt_reset();//喂狗
- }
- return 0;
- }