⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 usb_main.c

📁 自己写的 USB数据采集的固件程序
💻 C
字号:
#include <c8051f320.h>               
#include "USB_REGISTER.h"
#include "USB_MAIN.h"
#include "USB_DESCRIPTOR.h"
int k=1;
int c=0;
int counter=1;
sfr16 TMR2RL   = 0xCC;                   // Timer2 reload value
sfr16 TMR2     = 0xCD;                   // Timer2 counter  低   
int n=0;  
int n1=0;
sbit Led1 = P2^2;        
int flag=1;
unsigned long  buffer[8]={0,0,0,0,0,0,0,0};
BYTE Out_Packet[8] = {0,0,0,0,0,0,0,0};   // Last packet received from host
BYTE In_Packet[8]  = {0,0,0,0,0,0,0,0};   // Next packet to sent to host
int a,b ; 
void main(void) 
{
   PCA0MD &= ~0x40;                       // Disable Watchdog timer

   Sysclk_Init();                         // Initialize oscillator
   Port_Init();                           // Initialize crossbar and GPIO
   Usb0_Init();                           // Initialize USB0
   Timer_Init();                          // Initialize timer2
   Adc_Init();                            // Initialize ADC

   while (1)
    if(flag)
   { In_Packet[0]=c;
     In_Packet[3]=a;
     In_Packet[4]=b;
	 In_Packet[3]=0;
	 flag=0;
     Led1 = ~Led1; 
   }
}

void Sysclk_Init(void)
{
#ifdef _USB_LOW_SPEED_

   OSCICN |= 0x03;                       // Configure internal oscillator for
                                         // its maximum frequency and enable
                                         // missing clock detector

   CLKSEL  = SYS_INT_OSC;                // Select System clock
   CLKSEL |= USB_INT_OSC_DIV_2;          // Select USB clock
#else
   OSCICN |= 0x03;                       // Configure internal oscillator for
                                         // its maximum frequency and enable
                                         // missing clock detector

   CLKMUL  = 0x00;                       // Select internal oscillator as 
                                         // input to clock multiplier

   CLKMUL |= 0x80;                       // Enable clock multiplier
   CLKMUL |= 0xC0;                       // Initialize the clock multiplier
   Delay(1);                              // Delay for clock multiplier to begin

   while(!(CLKMUL & 0x20));                // Wait for multiplier to lock
   CLKSEL  = SYS_INT_OSC;                // Select system clock  
   CLKSEL |= USB_4X_CLOCK;               // Select USB clock
#endif  /* _USB_LOW_SPEED_ */ 
}

void Port_Init(void)
{  
   P1MDIN   = 0x7F;                        // Port 1 pin 7 set as analog input
   P0MDOUT |= 0x0F;                        // Port 0 pins 0-3 set high impedence
   P1MDOUT |= 0x0F;                        // Port 1 pins 0-3 set high impedence
   P2MDOUT |= 0x0C; 
   P2SKIP=0X00;                       // Port 2 pins 0,1 set high impedence
   P1SKIP   = 0x80;                        // Port 1 pin 7 skipped by crossbar
   XBR0     = 0x00;						    
   XBR1     = 0x40;                        // Enable Crossbar
}

void Usb0_Init(void)
{
   POLL_WRITE_BYTE(POWER,  0x08);          // Force Asynchronous USB Reset
   POLL_WRITE_BYTE(IN1IE,  0x07);          // Enable Endpoint 0-2 in interrupts
   POLL_WRITE_BYTE(OUT1IE, 0x07);          // Enable Endpoint 0-2 out interrupts
   POLL_WRITE_BYTE(CMIE,   0x07);          // Enable Reset, Resume, and Suspend interrupts
#ifdef _USB_LOW_SPEED_
   USB0XCN = 0xC0;                         // Enable transceiver; select low speed
   POLL_WRITE_BYTE(CLKREC, 0xA0);          // Enable clock recovery; single-step mode
                                           // disabled; low speed mode enabled
#else                                      
   USB0XCN = 0xE0;                         // Enable transceiver; select full speed
   POLL_WRITE_BYTE(CLKREC, 0x80);          // Enable clock recovery, single-step mode
                                           // disabled
#endif /* _USB_LOW_SPEED_ */

   EIE1 |= 0x02;                           // Enable USB0 Interrupts
   EA = 1;                                 // Global Interrupt enable
                                           // Enable USB0 by clearing the USB Inhibit bit
   POLL_WRITE_BYTE(POWER,  0x01);          // and enable suspend detection
}

void Timer_Init(void)
{   TMR2CN  = 0x00;   
    CKCON  &= ~0xF0;  
	TMR2L     = 0x00;
    TMR2H     = 0x00;
    TMR2RLL   = 0xB0;
    TMR2RLH   = 0x3C;             
   ET2     = 1;                      
   TR2     = 1;                         
}

void Adc_Init(void)
{                 
   REF0CN  = 0x0E;                        // Enable voltage reference VREF
   AMX0P = 0x1E;                        // Positive input starts as temp sensor  
   AMX0N = 0x1F;                        // Single ended mode(negative input = gnd)
   
   ADC0CF  = 0xF8;                        // 0xFC  对齐方式 AR Period 0x1F, Right adjusted output

   ADC0CN  = 0xC0;                        // Continuous converion on timer 2 overflow
                                          // with low power tracking mode on
   EIE1   |= 0x08;                        // Enable conversion complete interrupt
}


void Timer2_ISR(void) interrupt 5
{ if(counter==0)
    {a=buffer[0]/n;
      b=buffer[1]/n;
   	c=k++;
	n1=n;
	if(k==0xff) k=1;
    buffer[0]=0;
    buffer[1]=0;
     n=0;
 	flag=1;
	AD0BUSY=1;
	counter=1;
	}
else	
  counter--;
     TF2H = 0; 
                           
}

//-------------------------
// Adc_ConvComplete_ISR
//-------------------------
// Called after a conversion of the ADC has finished
// - Updates the appropriate variable for either potentiometer or temperature sensor
// - Switches the Adc multiplexor value to switch between the potentiometer and temp sensor
//
void Adc_ConvComplete_ISR(void) interrupt 10
{

    buffer[1]+=ADC0L; 
    buffer[0]+=ADC0H;
    n++;
   AD0INT = 0;
}

void Delay(i)
{  
   int x,y;
   for(y=0;y<i;y++)
   {
   for(x = 0;x < 500;)
      x++;
	  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -