用s12zvl的AN7口做AD口,硬件接的是一个光度传感器,控制PT3脚的PWM占空比,一直没有成功。请教一下各位大神
#include <hidef.h> /* forEnableInterrupts macro */
#include "deriva
tive.h" /* includeperipheral declarations */
typedef unsigned char bool;
#define ERR_OK 0;
#define ERR_NOTAVAIL 9U;
static volatile word RVL[1]
__attribute__ ((aligned (4)));
static volatile bool OutFlg;
static word RatioStore;
char BLUE;
byte* i;
static void SetRatio(
void)
{
PWMDTY1 = (byte)(((0xFFUL * (dword)RatioStore) + 0x8000UL) >> 0x10U); /* Calculate new value according to the given ratio */
}
byte PWM_BLUE_SetRatio8(byte Ratio)
{
RatioStore = (((word)Ratio << 8U) + 0x80U); /* Store new value of the ratio */
SetRatio(); /* Calculate and set up new appropriate values of theduty and period registers */
return ERR_OK; /* OK */
}
void PWM_BLUE_Init(void)
{
PWMCNT1 = 0x00;
PWMDTY1 = 0x9F;
PWMPER1 = 0xFF;
PWMPRCLK = 0x00;
PWMSCLA = 0x3B;
PWMCLK = 0x02;
PWME = 0x02U;
}
void ADC_Init(void)
{
ADC0CTL=0x8800;
ADC0TIM=0x07;
ADC0FMT=0x00;
ADC0CMD=0x00170000;
ADC0STS=0X01;
ADC0TIM=0X07;
ADC0CONIE=0x01;
ADC0CONIF=0X01;
ADC0CROFF1=0x00;
ADC0EIF=0x06;
ADC0EIE=0xFC;
ADC0IF=0xC0;
ADC0IE=0xC0;
}
byte ADC_GetValue8(byte *Values)
{
if (!OutFlg){ /* Is measured value(s) available? */
return ERR_NOTAVAIL; /* If no then error */
}
*Values = (byte)(RVL[0] >> 0x02U); /* Storeconversion data to user buffer */
return ERR_OK; /* OK */
}
void main(
void) {
PWM_BLUE_Init();
ADC_Init();
EnableInterrupts;
/* includeyour code here */
//for(;;)
//{
BLUE=ADC_GetValue8(i);
PWM_BLUE_SetRatio8(BLUE);
//}
for(;;) {
__RESET_WATCHDOG(); /* feeds the dog */
} /* loopforever */
/* pleasemake sure that you never leave main */
}
一周热门 更多>