本帖最后由 whtwhtw 于 2019-3-2 09:33 编辑
四、代码
编译器用的CodeVisionAVR
定时器0中断用作指示灯LED1的反转定时;
定时器1中断用于PWM输出
AD使用查询模式,每次循环检测滑动电阻器输出电压值,通过判断ADC值修改定时器1的输出比较寄存器的值OCR1AL、OCR1AH。
程序主要内容为:
#include <mega8.h>
#include <delay.h>
#include "ASCII_FONT.h"
#include "yj_init.h"
#include "AD.h"
#include "stdlib.h"
//#include "stdio.h"
// Declare your global variables here
unsigned int plus,show_plus;
unsigned int adc_int_data;
unsigned char show_adcdata[16];
unsigned char show_plusdata[16];
float adc2_data;
// Timer 0 overflow interrupt service routine
interrupt [TIM0_OVF] void timer0_ovf_isr(void)
{
// Reinitialize Timer 0 value
TCNT0=0xD8;
PORTC.3=~PORTC.3;
// Place your code here
}
// Timer1 overflow interrupt service routine
interrupt [TIM1_OVF] void timer1_ovf_isr(void)
{
// Place your code here
show_plus=plus;
if(show_plus<=270)
plus=270; //由于使用内部RC震荡器精度问题,和舵机动作范围受供电电压影响;脉宽0.5ms-2.5ms对应的比较寄存器值在250-1250才对。
else if(show_plus>=1225) //实测脉宽在低于0.5ms和大于2.5ms时候舵机会震颤,所以这里进行了范围压缩
plus=1225;
else
;
OCR1AH=plus>>8;
OCR1AL=plus&0xff;
}
具体程序如下
mage8_PWM.zip
(143.3 KB)
|