实例介绍
【实例简介】
上位机通过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); } }
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论