c8051f320学习,单片机不外乎时钟、IO、串口、USB等外设用法

 
时钟
IO(输入、输出,如何配置)
  IO
 
数字和模拟资源可以通过25个I/O 引脚(C805 1F3 2 0 ),每个端口引脚都可以被定义为 1  通用I/O(GPIO)或 0 模拟输入
所有端口I/O 都耐5V电压
端口I/O 单元可以被配置为 漏极开路或推挽方式,口输出方式寄存器PnMDOUT 中设置,n =  0,1, 2, 3
P1MDOUT |= 0x0F;//0000  1111     置P1的0-3口为1即推挽输出方式
P2MDOUT |= 0x0C;//0000 1100 对应 P2.2、P2.3推挽输出
 
寄存器XBR0 和XBR1 用于为数字I/O 资源分配物理I/O 引脚
 
PnMDIN 选择所有端口引脚的输入方式(模拟或数字)
 
注意:为使端口P0、P1和P2. 0  ~  P2 .3 工作在标准端口I/O 输入/输出方式,交叉开关必
须被使能。当交叉开关被禁止时,端口输出驱动器被禁止。P2.4 ~  P2.7 和P3.0 总是作为标准
GPIO 使用
 
 XBR1 |= ( 1<<6 );



USB时钟
 
C8051F 320/ 1 有一个 可编程内部振荡器、一个外部振荡器驱动电路和一个4 倍时钟乘法器
可以通过对OSCICN 和OSCICL 寄存器编程来使能/ 禁止内部振荡器和调节其输出频率(如图
13.1 所示)。系统时钟(SYSCLK )可以来自内部振荡器、外部振荡器电路或4 倍时钟乘法器
二分频。USB 时钟(USBCLK )可以来自内部振荡器、外部振荡器电路或4 倍时钟乘法器。
表13.3 给出了内部振荡器的电气特性
 
一旦内部振荡器的频率被修改,则它不能再被用作USB 时钟
4 倍时钟乘法器允许使用12 M Hz 振荡器产生全速USB 通信所需要的48MHz 时钟
用CLKMUL寄存器配置4 倍时钟乘法器。配置和使能4 倍时钟乘法器的步骤如下:
1 .通过向寄存器CLKMUL写0x00来复位时钟乘法器。
2 .用MULSEL位选择时钟乘法器的输入。
3 .用MULEN位使能时钟乘法器(CLKM UL | = 0x80 )。
4 .延时大于5 µs 。
5 .用MULINIT位初始化时钟乘法器(CLKMUL |  = 0 x C 0 )。
6 .查询等待MULRDY => ‘1 ’。
 
//都是一样的乘法寄存器配置
void Clock_Init( void )
{
     INT8U i;
     if( ( CLKMUL & ( 1<<5 ) ) ) return;//如乘法寄存器锁定,则返回
     CLKMUL = 0;
     CLKMUL |= ( 1<<7 );//时钟乘法器使能位
     for( i = 0; i < 250; i ++ );//延迟
     CLKMUL |= 0xC0;//1100 0000 ,初始化时钟乘法器
     for( i = 0; i < 250; i ++ );
     while( !( CLKMUL & ( 1<<5 ) ) );//时钟乘法器稳定后,MULRDY的读出值为‘1 ’
     CLKSEL = ( 2<<0 );
}

串口
 
void Port_Init (void)
{
   XBR1     = 0x40;                    // Enable crossbar and weak pullups
    XBR0      = 0x01;
     P0MDOUT |= 0x10;                    // Set TX pin to push-pull
}
 
void UART0_Init (void)
{
   SCON0 = 0x10;               // 0x0001 0000      8位UART  无  停止位的逻辑电平被忽略 UART0接收允许
                                    //    SCON0: 8-bit variable bit rate
                                       //        level of STOP bit is ignored
                                       //         RX enabled
                                       //        ninth bits are zeros
                                       //        clear RI0 and TI0 bits
//配置 T1M 、 SCA1:0
   if (SYSCLK/BAUDRATE/2/256 < 1) {
      TH1 = -(SYSCLK/BAUDRATE/2);
      CKCON |=  0x08;                  // T1M = 1; SCA1:0 = xx
   } else if (SYSCLK/BAUDRATE/2/256 < 4) {
      TH1 = -(SYSCLK/BAUDRATE/2/4);
      CKCON &= ~0x0B;                  // T1M = 0; SCA1:0 = 01
      CKCON |=  0x01;
   } else if (SYSCLK/BAUDRATE/2/256 < 12) {
      TH1 = -(SYSCLK/BAUDRATE/2/12);
      CKCON &= ~0x0B;                  // T1M = 0; SCA1:0 = 00
   } else if (SYSCLK/BAUDRATE/2/256 < 48) {
      TH1 = -(SYSCLK/BAUDRATE/2/48);
      CKCON &= ~0x0B;                  // T1M = 0; SCA1:0 = 10
      CKCON |=  0x02;
   } else {
      while (1);                       // Error.  Unsupported baud rate
   }

   TL1 = TH1;                          // init Timer1
   TMOD &= ~0xf0;                      // TMOD: timer 1 in 8-bit autoreload
   TMOD |=  0x20;
   TR1 = 1;                            // START Timer1
   TI0 = 1;                            // Indicate TX0 ready
}

ADC
用配置器来配置:
ADC0CN = 0x02;                      // ADC0 disabled, normal tracking,
                                       // conversion triggered on TMR2 overflow ADC0 用时钟2来触发
 
EIE1 |= 0x08;                       // enable ADC0 conversion complete int. 中断配置
 
 
在ADCMUX设置那里选P2.4,和GND。 引脚配置
AMX0P = 0x0C;                       // ADC0 positive input = P2.4
   AMX0N = 0x1F;                       // ADC0 negative input = GND
                                       // i.e., single ended mode
 
P2MDIN    = 0xEF;//P2.4设置为模拟输入,portinit处
 
还要串口配置和时钟2

I2C
void SMBus_Init(void);
void SMBus_ISR(void);

转载于:https://www.cnblogs.com/zkp2010/p/5510766.html

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
-------------------------------------------------- USB Bulk Example Readme File Copyright (C) 2005 Silicon Laboratories, Inc. -------------------------------------------------- This release contains the following components: * USB Bulk Driver Example * USB Bulk Firmware Example * Host Application (F32x_BulkFileTransfer.exe) * Host Application Source Last updated: 25 MAY 2005 Running the Example on the C8051F32x target board ------------------------------------------------------------ 1) Connect the hardware: a) Connect the target board to a PC through a Debug Adapter. b) Install shorting blocks at the following positions: J3[1-2] (P2.0 Switch), J3[3-4] (P2.1 Switch), J3[5-6] (P2.2 LED), J3[7-8] (P2.3 LED), J9 (P0.2), J10 (P0.3) and J13 (P1.7 Potentiometer). c) Connect a USB cable between the target device and the host PC. d) To power the target board from an wall-powered source, place a shorting block at J2 and NO shorting block at J11. Next, connect a DC power adapter to the target board at P1. e) To power the target board from the USB bus, place a shorting block on the target board at J11 and NO shorting block on J2. 2) Build and download the Bulk Example firmware: a) Open the Silicon Laboratories IDE (v1.72 or later). b) Open the Connection Options window by selecting Options-> Connection Options... in the IDE menus. In the Debug Interface section, select C2. c) Connect to the target C8051F32x device (Alt+C, or IDE menu Debug->Connect). d) From the Project menu, select "Open Project" and browse to the Bulk Example Firware project "BulkFirmware.wsp", located by default in the "Silabs\MCU\Examples\C8051F32x\USB_Bulk\Firmware" directory. e) Build the project (F7, or through IDE menu Project->Build/Make Project). f) Download the firmware (Alt+D). 3) Run the Bulk Example firmware: a) In the IDE, Run the firmware (F5, or IDE menu Debug->Go). b) When Windows detects the device and the driver installation wizard opens: a. Choose "Search for a suitable driver for my device", and click Next. b. Check the box "Specify a location", and click Next. c. Browse to directory ..\USB_Bulk and select the file SilabsBulk.inf, and click OK. d. Follow the dialogue to finish driver installation. c) Once the firmware has been downloaded and is running, the IDE is no longer needed. Disconnect from the target device with the IDE and press the Reset button on the Target Board. 4) Run the Bulk Example host application: a) Run the F32x_BulkFileTransfer.exe application located by default in the "Silabs\MCU\Examples\C8051F32x\USB_Bulk" directory. b) The F32x_BulkFileTransfer application will display the current USB devices connected to the PC. Select the device to be used for the file transfer. c) Select file names for Transferring or Receiving data.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值