打印
[菜农助学交流]

第五批 之四 AD+PWM

[复制链接]
1933|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
cnjxlxp|  楼主 | 2012-4-15 22:29 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
PWM, AD, IO, TI, ADC

#include <stdio.h>
#include "NUC1xx.h"
#include "Driver\DrvGPIO.h"
#include "Driver\DrvSYS.h"
#include "Driver\DrvUART.h"
#include "Driver\DrvADC.h"
#include "Driver\DrvPWM.h"

#define Run_Led 2 //2----LED1 3----LED2 4----LED3 5----LED4

void Init_System(void)
{
UNLOCKREG(x);
DrvSYS_SetOscCtrl(E_SYS_XTL12M, 1);
DrvSYS_Delay(5000);
LOCKREG(x);
}

void delay_ms(uint32_t count)
{
uint32_t i,j;
for(i=count;i>0;i--)
for(j=2395;j>0;j--);
}

void Init_Uart(void)
{
STR_UART_T param;
DrvSYS_SelectIPClockSource(E_SYS_UART_CLKSRC,0);
DrvGPIO_InitFunction(E_FUNC_UART0);
param.u32BaudRate = 9600;
param.u8cDataBits = DRVUART_DATABITS_8;
param.u8cStopBits = DRVUART_STOPBITS_1;
param.u8cParity = DRVUART_PARITY_NONE;
param.u8cRxTriggerLevel = DRVUART_FIFO_1BYTES;
param.u8TimeOut = 0;
DrvUART_Open(UART_PORT0,¶m);
}

void Init_PWM0(void)
{
S_DRVPWM_TIME_DATA_T sPt;

sPt.u8Mode = DRVPWM_AUTO_RELOAD_MODE;
sPt.u32Frequency = 1000;
sPt.u8HighPulseRatio =50;
sPt.i32Inverter = 0;

DrvPWM_Open();

DrvPWM_SelectClockSource(DRVPWM_TIMER0, DRVPWM_EXT_12M);

DrvPWM_SetTimerClk(DRVPWM_TIMER0, &sPt);

DrvPWM_SetTimerIO(DRVPWM_TIMER0, 1);

DrvGPIO_InitFunction(E_FUNC_PWM01);

DrvPWM_Enable(DRVPWM_TIMER0, 1); /* Enable PWM timer 0 function. */
}

int main (void)
{

uint16_t i32ConversionData;
// uint32_t i32ConversionData_Sum;
//uint8_t IsFirst = 1;

Init_System();

Init_Uart();

Init_PWM0();

DrvGPIO_Open(E_GPA,Run_Led, E_IO_OUTPUT);
DrvGPIO_ClrBit(E_GPA,Run_Led);

DrvGPIO_InitFunction(E_FUNC_ADC1);
DrvADC_Open(ADC_SINGLE_END, ADC_SINGLE_OP, 0x02, EXTERNAL_12MHZ, 3);

printf("\n");
printf("Driver version: %x\n", DrvADC_GetVersion());

DrvADC_EnableSelfCalibration();
while(DrvADC_IsCalibrationDone()==0);
DrvADC_DisableSelfCalibration();
DrvSYS_Delay(5000);
while(1)
{
DrvADC_StartConvert();

while(DrvADC_IsConversionDone()==0);

i32ConversionData = DrvADC_GetConversionData(1);

printf("%d\n",i32ConversionData);
DrvSYS_Delay(5000);
DrvSYS_Delay(5000);
delay_ms(1000);
PWMA->CMR0 =(PWMA->CNR0+1)*(i32ConversionData*100/4096)/100-1;
printf("PWMA->CMR0: %d\n", PWMA->CMR0);
DrvSYS_Delay(5000);
printf("PWMA->CNR0: %d\n", PWMA->CNR0);
}
}

AD PWM.rar

1.29 MB

相关帖子

发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

0

主题

46

帖子

2

粉丝