本帖最后由 cpu51 于 2014-8-6 12:25 编辑
我用OC0B输出一个PWM,但发现有中断,PA7始终为0.,在中断中将PA7置高高置低都可以改变.不知道哪里需设置.另外PRR0编译出错,,未定义,,不知道哪问题.PRR0是什么?
#include <_iotx4v.h>
#include <macros.h>
#define POWER_OUT_UN PORTA&=~(1<<PA2) //置低电平
#define POWER_OUT_EN PORTA|=(1<<PA2) //置高电平
unsigned int s_voltage,b_voltage;
unsigned char x;
void port_init(void)
{
PORTA = 0x70;
DDRA = 0xF5; //定义输出 输入
//1111 0101
PORTB = 0x00;
DDRB = 0xFB; //定义输出 输入
// 1111 1011
}
//**********************************************************
void delay(unsigned int ms)
{
unsigned int i;
for(i=0;i<ms;i++)
{
_NOP();_NOP();_NOP();_NOP();_NOP();_NOP();_NOP();
WDR();
}
}
//**********************************************************
void delay_ms(unsigned int s)
{
unsigned int i;
for(i=0;i<s;i++)
{
delay(4000);
_NOP();_NOP();_NOP();_NOP();_NOP();_NOP();_NOP();
_NOP();_NOP();_NOP();_NOP();_NOP();_NOP();_NOP();
}
}
//*******************************************************************
//adc
//*******************************************************************
unsigned int read_adc(unsigned char ad_channel)
{
ADCMUX=0x00; //AVCC作为vref,转换结果右对齐,ADC0口 0-1023
ADCMUX|=ad_channel;//选择ADC通道
ADCSRA|=1<<ADSC;//启动ADC转换 //???不同
ACSR=(1<<ACD);//关闭模拟比较器
//ADCSRA=0xc7; //adc使能 ,开始单次转换 128分频
// delay_s(10);
while(0==(ADCSRA&(1<<ADIF)));//等待转换完成
ADCSRA|=1<<ADIF;//清零标志
return(ADC);//返回测到的电压值
}
//*******************************************************************
//ADC initialize
// Conversion time: 52uS
//*******************************************************************
void adc_init(void)
{
ADCSRA = 0x00; //disable adc
ADCMUX = 0x00;
ACSR = 0x80;
ADCSRA = 0x04;
ADCSRA = 0x82;
}
//TIMER0 initialize - prescale:256
// WGM: CTC
// desired value: 1KHz
// actual value: 0.977KHz (-2.4%)
void timer0_init(void)
{
TIMSK0=0x05;
TIFR0=0x00;
TCCR0A=0x02;
TCCR0B=0x04;
TCNT0=0xE1;
OCR0A=0x1F;
OCR0B=0x1F;
GTCCR=0x00;
}
#pragma interrupt_handler timer0_compb_isr:iv_TIM0_COMPB
void timer0_compb_isr(void)
{
// Timer/Counter0 Compare Match B
}
#pragma interrupt_handler timer0_ovf_isr:iv_TIM0_OVF
void timer0_ovf_isr(void)
{
TCNT0 = 0xE1;
// Timer/Counter0 Overflow
}
//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 ;
GIMSK = 0x00 ;
PCMSK0 = 0x00;
PCMSK1 = 0x00;
TIMSK0 = 0x05;
TIMSK1 = 0x00;
//PRR0 = 0x00 ;
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
main()
{
init_devices();
adc_init();
delay_ms(40);
while(1)
{
;
}
|