本来是在ICC下写好的渐变色LED程序,也运行很好,因后面要用到在线硬件仿真,所以改用gcc,定时测试正常,各变量变化没问题,就是结果,led颜色变化不再是渐变而是跳动,应该是while里出现问题,请问下各位大仙怎么处理?主要程序如下:
// Target : M16
// Crystal: 8.0000Mhz
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
//--------DEFINE------------------------
#define RLED 0XF5
#define GLED 0XF6
#define BLED 0XFC
#define shudu 5 //LED渐变速度调整
#define dengji 1 //LED变化等级调整
volatile unsigned char Rout,Bout,Gout;
volatile unsigned int num;
volatile unsigned int ji;
//----------------------------------------------------
void RGBpwm(unsigned char Rou,unsigned char Gou,unsigned char Bou);
void RGBpwmchange(void);
//----------------------------------------------------
void port_init(void)
{
PORTB = 0x04;
DDRB = 0xFB;
}
//TIMER1 initialize - prescale:8
// WGM: 0) Normal, TOP=0xFFFF
// desired value: 1mSec
// actual value: 1.000mSec (0.0%)
void timer1_init(void)
{
TCCR1B = 0x00; //stop
TCNT1H = 0xFC; //setup
TCNT1L = 0x18;
OCR1AH = 0x03;
OCR1AL = 0xE8;
OCR1BH = 0x03;
OCR1BL = 0xE8;
ICR1H = 0x03;
ICR1L = 0xE8;
TCCR1A = 0x00;
TCCR1B = 0x02; //start Timer
}
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
cli(); //disable all interrupts
port_init();
timer1_init();
MCUCR = 0x00;
GICR = 0x00;
TIMSK = 0x04; //timer interrupt sources
sei(); //re-enable interrupts
//all peripherals are now initialized
}
//------------------------------------
void RGBpwm(unsigned char Rou,unsigned char Gou,unsigned char Bou)//3个局部变量,些部分为带参数函数
{
if(Rou!=0)//如果红灯变量不等于0就亮红灯
{
PORTB|=RLED;//红灯亮
_delay_us(Rou);//红灯亮起时间
PORTB&=~RLED;//红灯灭
}
if(Gou!=0)//如果红灯变量不等于0就亮红灯
{
PORTB|=GLED;;//绿灯亮
_delay_us(Gou);//绿灯亮起的时间
PORTB&=~GLED;//绿灯灭
}
if(Bou!=0)//如果红灯变量不等于0就亮红灯
{
PORTB|=BLED;;//绿灯亮
_delay_us(Bou);//兰灯亮起的时间
PORTB&=~BLED;//绿灯灭
}
}
//--------------------------------------------
void RGBpwmchange(void)
{
if(num==shudu)//如果速度变量等于定时计数器变量就执行下面大括号语句
{
num=0;//将计数器清0
ji++;//将亮度等级控制变量加1
if(ji<20)
{
Rout=255;//红灯亮起变量为255
Gout=255;//绿灯亮起变量为255
Bout=255;//兰灯亮起变量为255
..................省略部分语句......................
}
}
//------------------------------------
ISR(TIMER1_OVF_vect)
{
//TIMER1 has overflowed
TCNT1H = 0xFC; //reload counter high value
TCNT1L = 0x18;//reload counter low value
num++;
RGBpwmchange();
}
//-----------------------------------------
int main(void)
{
init_devices();
//insert your functional code here...
num=0;
ji=0;
Rout=255;
Gout=255;
Bout=255;
while(1)
{
RGBpwm(Rout,Gout,Bout);
}
}
|