#include "COMM/STC32G.h" //使用usb_hid之必须
#include "COMM/stc32_stc8_usb.h" //使用usb_hid之必须
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAIN_Fosc 24000000UL //定义主时钟频率

char *USER_DEVICEDESC = NULL;
char *USER_PRODUCTDESC = NULL;
char *USER_STCISPCMD = "@STCISP#"; //设置"自定义下载"命令
//函数声明区
void sys_init();
void delay_ms(u16 ms);
void main()
{
//变量定义必须在函数的开头处,定义在中间会有问题
int i=0;
sys_init(); //usb功能 + IO口初始化
usb_init(); //usb库的初始化
EA=1; //打开所有中断
while (1)
{
//USB串口调试段功能代码
if(DeviceState != DEVSTATE_CONFIGURED)
continue;
if (bUsbOutReady)
{
usb_OUT_done(); //接收应答(固定格式)
printf("hello-OK");
}
//业务逻辑部分
P40=0; //开通电源
for (i = 0; i < 8; i++)
{
P6 = 0xFF & ~(1 << i); //点亮LED
delay_ms(200);
P6 = 0xFF; //熄灭LED
delay_ms(500);
}
}
}
void sys_init()
{
WTST = 0; //设置程序指令延时参数,赋值为0可将CPU执行指令的速度设置为最快
EAXFR = 1; //扩展寄存器(XFR)访问使能
CKCON = 0; //提高访问XRAM速度
P0M1 = 0x00; P0M0 = 0x00; //设置为准双向口
P1M1 = 0x00; P1M0 = 0x00; //设置为准双向口
P2M1 = 0x00; P2M0 = 0x00; //设置为准双向口
P3M1 = 0x00; P3M0 = 0x00; //设置为准双向口
P4M1 = 0x00; P4M0 = 0x00; //设置为准双向口
P5M1 = 0x00; P5M0 = 0x00; //设置为准双向口
P6M1 = 0x00; P6M0 = 0x00; //设置为准双向口
P7M1 = 0x00; P7M0 = 0x00; //设置为准双向口
P3M0 = 0x00;
P3M1 = 0x00;
P3M0 &= -0x03;
P3M1 != 0x03;
//设置USB使用的时钟源
IRC48MCR = 0X80; //使能内部48M高速IRC
while (!(IRC48MCR & 0X01));
//使用CDC时要用下面两行,使用HID要禁用下面两行
USBCLK = 0x00;
USBCON = 0x90;
}
void delay_ms(u16 ms)
{
u16 i;
do{
i = MAIN_Fosc / 6000;
while(--i);
}while(--ms);
}