基本信息
源码名称:上位机通过usb控制下位机(上位机通过c8051f320 的usb控制LED)
源码大小:5.21M
文件格式:.zip
开发语言:C/C++
更新时间:2019-03-27
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
上位机通过c8051f320 的usb控制LED
// File best viewed using Tab Size of 4 Characters
// Author DM DATE 4-4-03
// Modified CS DATE 8-25-03
// This example illustrates usage of the USB_API.lib
// DO NOT locate code segments at 0x1400 to 0x4000
// These are used for non-voltile storage for transmitted data.
// Include files
#include <c8051f320.h> // Header file for SiLabs c8051f320
#include <stddef.h> // Used for NULL pointer definition
#include "USB_API.h" // Header file for USB_API.lib
#define uchar unsigned char
#define uint unsigned int
sbit clk_164=P1^7;
sbit clr_164=P2^0;
sbit ab_164=P1^6;
//uchar led_value=0;
//uchar code tab[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0xe7f,0xff,0x00};
BYTE Out_Packet[8]; // Last packet received from host
BYTE In_Packet[8] ; // Next packet to sent to host
void Port_IO_Init()
{
P1MDOUT = 0xF8; //1111 1000 高5位为推挽方式
P2MDOUT = 0x01; //P2.0为推挽方式
XBR1 = 0xC0; //交叉开关使能、、弱上拉禁止
}
void Oscillator_Init()
{
int i = 0;
if( ( CLKMUL & ( 1<<5 ) ) ) return;
CLKMUL = 0;
CLKMUL = 0x80; //时钟乘法器使能
for (i = 0; i < 20; i ); // Wait 5us for initialization
CLKMUL |= 0xC0; //初始化时钟乘法器
while ((CLKMUL & 0x20) == 0); //等待时钟乘法器准备好
CLKSEL = 0x02; //usbclk=48M sysclk=24M
}
void hc_164_out(uchar value) //74hc164输出一个字节函数
{
uchar i;
for(i=0;i<8;i )
{
clk_164=0;
ab_164=(value&0x80);
clk_164=1;
value<<=1;
}
clk_164=0;
}
void HC138_Sel( uchar x ) //38译码器选中0-7
{
x &= 0x07;
P1 |= ( 1<<3 ) | ( 1<<4 ) | ( 1<<5 );
P1 &= ( ( x<<3 ) | 0xC7 );
}
/*** [BEGIN] USB Descriptor Information [BEGIN] ***/
code const UINT USB_VID = 0x10C4; //16位卖主ID号
code const UINT USB_PID = 0xEA61; //16位产品ID
code const BYTE USB_MfrStr[] = {0x1A,0x03,'S',0,'i',0,'l',0,'i',0,'c',0,'o',0,'n',0,' ',0,'L',0,'a',0,'b',0,'s',0}; //描述厂商字符串 // Manufacturer String
code const BYTE USB_ProductStr[] = {0x10,0x03,'U',0,'S',0,'B',0,' ',0,'A',0,'P',0,'I',0}; // 描述产品字符串
code const BYTE USB_SerialStr[] = {0x0A,0x03,'1',0,'2',0,'3',0,'4',0}; //描述序列号字符串
code const BYTE USB_MaxPower = 15; // Max current = 30 mA (15 * 2) 最大电流
code const BYTE USB_PwAttributes = 0x80; // Bus-powered, remote wakeup not supported 总线供电
code const UINT USB_bcdDevice = 0x0100; // Device release number 1.00 用BCD码表示设备版本号
/*** [ END ] USB Descriptor Information [ END ] ***/
//-----------------------------------------------------------------------------
// Main Routine
//-----------------------------------------------------------------------------
void main(void)
{
PCA0MD &= ~0x40; // Disable Watchdog timer 关闭看门狗
Port_IO_Init();
Oscillator_Init(); //USB时钟为48M,系统时钟为4倍时钟乘法器除2 ,即24M
USB_Clock_Start(); // Init USB clock *before* calling USB_Init
USB_Init(USB_VID,USB_PID,USB_MfrStr,USB_ProductStr,USB_SerialStr,USB_MaxPower,USB_PwAttributes,USB_bcdDevice);
// CLKSEL |= 0x02; //USB时钟为48M,系统时钟为4倍时钟乘法器除2 ,即24M
// RSTSRC |= 0x02;//复位源寄存器 写:使能VDD监视器为复位源 读:最后一次复位是上电或VDD监视器复位
// Port_Init(); // Initialize crossbar and GPIO
USB_Int_Enable(); // Enable USB_API Interrupts
HC138_Sel(0); //选通Y0
USB_Int_Enable();
EA=1; //开总中断
while (1)
{
if (Out_Packet[0] == 0xAA)
hc_164_out(0x00); // 灯亮
if (Out_Packet[0] == 0x55)
hc_164_out(0xff); // 灯灭
}
}
// ISR for USB_API, run when API interrupts are enabled, and an interrupt is received */
void USB_API_TEST_ISR(void) interrupt 16
{
BYTE INTVAL = Get_Interrupt_Source(); // Determine type of API interrupts
if (INTVAL & TX_COMPLETE)
{
Block_Write(In_Packet, 8);
}
if (INTVAL & RX_COMPLETE)
{
Block_Read(Out_Packet, 8);
}
}