基于RL-RTX的,建了两个任务。一个是计算ADC1和VBG电压然后通过串口上报。任务2是USB的轮询函数
KEY1是鼠标水平移动,KEY2是鼠标垂直移动
初始化
#include "NUC1xx.h"
#include "drvsys.h"
#include "drvgpio.h"
#include "drvuart.h"
#include "drvadc.h"
#include "drvusb.h"
#include <rtl.h>
#include "HIDSys.h"
#include "HID_API.h"
static void ClockInit( void )
{
UNLOCKREG();
DrvSYS_SetOscCtrl( E_SYS_XTL12M, ENABLE );
DrvSYS_SelectPLLSource( E_SYS_EXTERNAL_12M );
DrvSYS_Open(48000000);
DrvSYS_SelectSysTickSource( 3 );
}
static void UartInit( void )
{
STR_UART_T sParam;
sParam.u32BaudRate = 115200;
sParam.u8cDataBits = DRVUART_DATABITS_8;
sParam.u8cStopBits = DRVUART_STOPBITS_1;
sParam.u8cParity = DRVUART_PARITY_NONE;
sParam.u8cRxTriggerLevel = DRVUART_FIFO_1BYTES;
DrvSYS_SelectIPClockSource(E_SYS_UART_CLKSRC, INTERNAL_HCLK);
DrvUART_Open (UART_PORT0, &sParam);
DrvGPIO_InitFunction(E_FUNC_UART0_RX_TX);
}
static void ADCInit( void )
{
DrvADC_Open(ADC_SINGLE_END, ADC_SINGLE_CYCLE_OP, (1 << 1) | (1 << 7), EXTERNAL_12MHZ, 11); //Set adc parameter
DrvADC_ConfigADCChannel7( INTERNAL_BANDGAP );
DrvGPIO_InitFunction(E_FUNC_ADC1);
DrvADC_EnableSelfCalibration(); //start calibration
while(DrvADC_IsCalibrationDone() == 0)
{
;
}
}
void USBD_Init( void )
{
//int32_t i32Ret = 0;
GPIOB->IEN = (1 << 15) | (1 << 14);
NVIC_EnableIRQ(EINT1_IRQn);
NVIC_EnableIRQ(EINT0_IRQn);
DrvUSB_Open((void *)DrvUSB_DispatchEvent);
// i32Ret = DrvUSB_Open((void *)DrvUSB_DispatchEvent);
//// if(i32Ret != 0)
// {
// return i32Ret;
// }
/* Disable USB-related interrupts. */
_DRVUSB_ENABLE_MISC_INT(0);
/* Enable float-detection interrupt. */
_DRVUSB_ENABLE_FLDET_INT();
/* Start HID and install the callback functions to handle in/out report */
HID_Init();
/* Enable USB-related interrupts. */
_DRVUSB_ENABLE_MISC_INT(INTEN_WAKEUP | INTEN_WAKEUPEN | INTEN_FLDET | INTEN_USB | INTEN_BUS);
/* Enter power down to wait USB attached */
// PowerDown();
}
void SystemInit( void )
{
ClockInit();
UartInit();
ADCInit();
// USBD_Init();
}
/* 重定向PRINTF函数到串口1*/
int putchar(int __c)
{
if(DrvUART_Write(UART_PORT0, (unsigned char *)__c, 1))
{
return 1;
}
else
{
return 0;
}
}
主函数
#include "NUC1xx.h"
#include <rtl.h>
#include "drvgpio.h"
#include "bsp.h"
#include "drvuart.h"
#include "drvadc.h"
#include "drvusb.h"
#include <stdio.h>
#include "HIDSys.h"
#include "HID_API.h"
extern void USBD_Init( void );
__task void usbtask( void )
{
USBD_Init();
/* Poll and handle USB events. */
for( ;; )
{
E_DRVUSB_STATE eUsbState;
eUsbState = DrvUSB_GetUsbState();
if (eUsbState == eDRVUSB_DETACHED)
{
break;
}
if(eUsbState & eDRVUSB_SUSPENDED)
{
/* Enter suspend here */
// PowerDown();
}
else
{
/* Set HID IN report for interrupt IN transfer */
HID_SetInReport();
}
os_dly_wait(2);
}
}
__task void ledtask( void )
{
for( ;; )
{
unsigned int i;
float j;
DrvADC_StartConvert(); //start first convert
os_dly_wait( 200 );
i = DrvADC_GetConversionData( 1 );
j = (float)i * 3.3 / 4095;
printf("The ADC Channel 1 Voltage:%1.3f V\n", j);
i = DrvADC_GetConversionData( 7 );
j = (float)i * 3.3 / 4095;
printf("The VBG Voltage:%1.3f V\n", j);
}
}
__task void taskinit( void )
{
os_tsk_create(ledtask, 1); //创建任务
os_tsk_create(usbtask, 2);
os_tsk_delete_self(); //删除自己
for( ;; )
{
}
}
int main( void )
{
// usbd_init(); // USB Initialization
// usbd_connect(__TRUE); // USB Connect
os_sys_init( taskinit ); //启动任务调度器
while( 1 )
{
}
}
|