|
|
发表于 2026-9-20 11:00:25
|
显示全部楼层
#include "config.h" //默认已包含stdio.h、intrins.h等头文件
////////////////////////////////////////
// 项目主函数
////////////////////////////////////////
void main(void)
{
SYS_Init();
while (1)
{
}
}
void SYS_Init(void)
{
EnableAccessXFR(); //使能访问扩展XFR
AccessCodeFastest(); //设置最快速度访问程序代码
AccessIXramSpeed(1); //设置访问内部XDATA的速度(如果不使能DMA,可将参数设置为0)
IAP_SetTimeBase(); //设置IAP等待参数,产生1us时基
PORT3_Init(); //P3口初始化
delay_ms(1);
USBLIB_Init(); //USB库初始化
delay_ms(1);
EnableGlobalInt(); //使能全局中断
USBLIB_WaitConfiged(); //等待USB完成配置
}
void delay_us(uint16_t us)
{
do
{
NOP(34); //(MAIN_Fosc + 500000) / 1000000 - 6
} while (--us);
}
////////////////////////////////////////
void delay_ms(uint16_t ms)
{
uint16_t i;
do
{
i = MAIN_Fosc / 6000;
while (--i);
} while (--ms);
}
void PORT3_Init(void)
{
SetP3nInitLevelHigh(PIN_ALL); //设置P3初始化电平
SetP3nQuasiMode(PIN_7 | PIN_6 | PIN_5 | PIN_4 | PIN_3 | PIN_2); //设置P3.7,P3.6,P3.5,P3.4,P3.3,P3.2为准双向口模式
SetP3nHighZInputMode(PIN_1 | PIN_0); //设置P3.1,P3.0为高阻输入模式
SetP3nManualMode(PIN_ALL); //设置P3手动配置端口模式
DisableP3nPullUp(PIN_ALL); //关闭P3内部上拉电阻
DisableP3nPullDown(PIN_ALL); //关闭P3内部下拉电阻
EnableP3nSchmitt(PIN_ALL); //使能P3施密特触发
SetP3nSlewRateNormal(PIN_ALL); //设置P3一般翻转速度
SetP3nDrivingNormal(PIN_ALL); //设置P3一般驱动能力
SetP3nDigitalInput(PIN_ALL); //使能P3数字信号输入功能
}
void USBLIB_Init(void)
{
usb_init(); //初始化USB模块
USB_SetIntPriority(3); //设置中断为最高优先级
set_usb_OUT_callback(USBLIB_OUT_Callback); //设置USB中断回调函数
set_usb_ispcmd("@STCISP#"); //设置USB不停电下载命令
}
void USBLIB_WaitConfiged(void)
{
while (DeviceState != DEVSTATE_CONFIGURED) //等待USB完成配置
WDT_Clear(); //清看门狗定时器 (防止硬件自动使能看门狗)
}
void USBLIB_OUT_Callback(void)
{
USB_SendData(UsbOutBuffer, OutNumber); //原路返回, 用于测试
} 这个是我用中断方式实现USB CDC 通讯, 没有问题! 记得用#include "AI8051U.H"
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <float.h>
#include <intrins.h>
#include "def.h"
#include "ai8051u_def.h"
#include "ai_usb.h" 才能成功! |
|