下面是主程序:
//PIC24FJ64GA002的第7个程序
//LED亮度随电位器变化
//邵子扬 2008.8.21
#include <p24Fxxxx.h>
#define LED LATBbits.LATB12
unsigned char cnt = 0;
int main(void)
{
TRISB = 0x0FFF; //设置LED为输出RB15-RB12
LATB = 0xFFFF;
RPOR7bits.RP15R = 18; //RP15作为OC1输出
T2CON = 0;
TMR2 = 0;
PR2 = 1023;
OC1CON = 0x0006; //OC1设置为PWM
OC1RS = 1000;
T2CON = (1 << 15)|(0 << 5)|(0 << 4); //启动时钟,1分频
T3CON = 0;
TMR3 = 0;
PR3 = 144 - 1; //10ms
T3CON = (1 << 15)|(1 << 5)|(1 << 4); //启动时钟,256分频
AD1PCFG = ~(1 << 5);//AN5作为模拟输入
AD1CON1 = 0x8000;
AD1CON2 = 0x0000;
AD1CON3 = 0x0F00;
AD1CSSL = 0;
AD1CHS = 5;
AD1CON1bits.SAMP = 1;
while(1)
{
if(IFS0bits.T3IF)//每10ms改变一次PWM状态
{
IFS0bits.T3IF = 0;
cnt++;
if(cnt >= 100) //每秒改变一次LED状态
{
cnt = 0;
LED = !LED;
}
AD1CON1bits.SAMP = 0;
while(!AD1CON1bits.DONE);
OC1RS = ADC1BUF0; //设置OC1占空比
AD1CON1bits.SAMP = 1;
}
}
return 0;
}
shaoziyang 2008-8-25 20:35
用户124183 2008-8-25 18:48
用户461316 2008-8-25 14:05