-
-
glorfindel | 当前状态:离线
总积分:58 2024年可用积分:0
注册时间: 2009-01-06
最后登录时间: 2009-01-06
-
达人来帮忙改一下这个程序
glorfindel 发表于 2009/1/6 19:24:04 874 查看 0 回复 [上一主题] [下一主题]
手机阅读
板子是ARM9的AT91SAM9261
板子上有2排共8盏灯,用C语言编写。程序的要求是编写出来使得8盏灯按照1.0ms, 1.1ms, 1.2ms, 1.3ms, 1.4ms, 1.5ms, 1.6ms 1.7ms的时间间隔不断地闪烁(要求有渐亮和渐灭) 其中需要使用pulse width modulation(脉冲带宽调制)原理,每一个PWM周期是20ms。板子上还有3个按钮 一个是reset 还有一个要实现中断功能。现在有以下半成品,但是运行起来有问题,8个灯的闪烁过程太短,肉眼几乎无法分辨,而且没有中断按钮功能。希望有达人能帮忙修改!谢谢
#include "ex.h"
int x,i,a,c;
int n=0;
/* example of IRQ */
void __attribute__ ((interrupt ("IRQ"))) irq ()
{
/* once called -- IRQ becomes disabled up util return */
*INT_RAW_P &= ~INT_MASK_TIMER; /* reset IRQ request */
time(void)
{
c=1;//标志位置1//}
}
void delay(void)
{
if (*TIMER_P<=256)
{
*TIMER_COMPARE_P = *TIMER_P + 1;//写入比较器//
else *TIMER_COMPARE_P=0;
IRQ_ENABLE;//开中断//
while (1)//等待中断//
do
{if (c=1)break;//判断中断//
}
c=0;//标志位置0//
x++;//奇偶标志位//
}}}
void pwm(void)
{int n,i,a=0;
for(n=0;n<=20;n++)
{for(i=0;i<=20;i++)
{if (i<=n)
{delay()
//delay for 1ms//
a++;//计算时间//
if(a%1000<=500)*LEDS_P|=0x01;
else *LEDS_P&=0xFE;//输出判断//
if(a%1100<=550)*LEDS_P|=0x02;
else *LEDS_P&=0xFD;
if(a%1200<=600)*LEDS_P|=0x04;
else *LEDS_P&=0xFB;
if(a%1300<=650)*LEDS_P|=0x08;
else *LEDS_P&=0xF7;
if((a%2==1)&&(a%1400<=700))*LEDS_P|=0x10;
else *LEDS_P&=0xEF;
if((a%2==1)&&(a%1500<=750))*LEDS_P|=0x20;
else *LEDS_P&=0xDF;
if((a%2==0)&&(a%1600<=800))*LEDS_P|=0x40;
else *LEDS_P&=0xBF;
if(a%2==0)&&(a%1700<=850))*LEDS_P|=0x80;
else *LEDS_P&=0xF7;
}
else
*LEDS_P &= 0;//置0//
}}}
void _attribute_((interrupt("FIQ")))
{HALT_P=0x00
}
/* example of main */
int main ()
{
*LEDS_P &= 0;
*TIMER_COMPARE_P = *TIMER_P + 128;
IRQ_ENABLE; /* IRQ enable, this is different from unmasking*/
*INT_ENABLE_P |= INT_MASK_TIMER; /* IRQ input unmask */
*INT_ENABLE_P |= INT_MASK_BUTTON2;
while (1);
}
——————————————————
其中的ex.h定义如下
/* _P stands for pointer */
#define LEDS_P (int*)0x10000000
/* ST1..3 are buttons */
#define STATE_BUTTONS_P (int*)0x10000004
#define STATE_MASK_ST1 0x08
#define STATE_MASK_ST2 0x80
#define STATE_MASK_ST3 0x40
#define TIMER_P (int*)0x10000008
#define TIMER_COMPARE_P (int*)0x1000000C
#define INT_RAW_P (int*)0x10000018
#define INT_ENABLE_P (int*)0x1000001C
#define INT_MASK_TIMER 0x01
#define INT_MASK_BUTTON2 0x40
#define INT_MASK_BUTTON3 0x80
/* write anything to this address in order to stop */
#define HALT_P (int*)0x10000020
/* you need these to implement a "shortcut" return from a nested interrupt */
/* Extended assembler language */
#define RESTORE_SP(X) \
asm ( "mov sp, %0"\
:\
: "r" (X));
#define SAVE_SP(X) \
asm ( "mov %0, sp"\
: "=r" (X));
#define RESTORE_LR(X) \
asm ( "mov lr, %0"\
:\
: "r" (X));
#define SAVE_LR(X) \
asm ( "mov %0, lr"\
: "=r" (X));
/* Enable/disable interrupts -- it is different from (un)masking int. inputs! */
#define IRQ_ENABLE \
asm ( "mrs r2, cpsr\n\t"\
"bic r2, r2, #0x80\n\t"\
"msr cpsr_c, r2"\
: /* no output registers */\
: /* no input registers */\
: /* clobber list */ "r2");
#define IRQ_DISABLE \
asm ( "mrs r2, cpsr\n\t"\
"orr r2, r2, #0x80\n\t"\
"msr cpsr_c, r2"\
: /* no output registers */\
: /* no input registers */\
: /* clobber list */ "r2");
#define FIQ_ENABLE \
asm ( "mrs r2, cpsr\n\t"\
"bic r2, r2, #0x40\n\t"\
"msr cpsr_c, r2"\
: /* no output registers */\
: /* no input registers */\
: /* clobber list */ "r2");
#define FIQ_DISABLE \
asm ( "mrs r2, cpsr\n\t"\
"orr r2, r2, #0x40\n\t"\
"msr cpsr_c, r2"\
: /* no output registers */\
: /* no input registers */\
: /* clobber list */ "r2");