C8051F020 AD轉(zhuǎn)換和USB通信
config.c
#include "include.h"
// Peripheral specific initialization functions,
// Called from the Init_Device() function
void Timer0_Init (void)
{
TMOD= 0x01;
TH0= TH0_VALUE;
TL0= TL0_VALUE;
}
void ADC_Init()
{
AMX1SL= 0x00;//
ADC1CF= 0x21;//
ADC1CN= 0x80;//
REF0CN= 0x03;
}
//-------------------------------------------------------------------------------
void PORT_Reset(void)
{
// disable watchdog timer
// disable watchdog timer
WDTCN = 0xde;
WDTCN = 0xad;
//P0MDOUT|=0x10;
XBR0 = 0x04;// CP0EECI0EPCA0ME PCA0ME PCA0ME [UART0]SPI0EN [SMB0EN]
XBR1 = 0x14;// SYSCKET2EXET2EINT1ET1E[INT0E]T0ECP1E
XBR2 = 0x40;// WEAKPUD [XBARE] -T4EXET4E[UART1E] EMIFLE CNVSTE
P0MDOUT|=0x0;
P1MDIN=0x0;//Digital Input Mode
//P1MDOUT=0x0f;//Key Input Drain-out
//P1=0xff;//Key Input
//P2MDOUT=0x00;//CH372 WR RD CS A0 Output
P2MDOUT=0xff;
//P2=0xff;
P2=0xff;
P3MDOUT=0x00;//input
P3=0xff;
P74OUT=0;//P7 CH372 data
P7=0xff;
}//Port_Reset(void)
void SysClk_Reset (void)
{
uint i;// delay counter
//OSCICN = 0x87;//片內(nèi)晶振
WDTCN = 0xde;// disable watchdog timer
WDTCN = 0xad;
OSCXCN = 0x67;// start external oscillator with
// 11.0592MHz crystal
for (i=0; i <=255; i++) ;// XTLVLD blanking interval (>1ms)
while (!(OSCXCN & 0x80)) ;// Wait for crystal osc. to settle
OSCICN = 0x88;// select external oscillator as SYSCLK
// source and enable missing clock
// detector
}//void SysClk_Reset (void)
void Interrupts_Init()
{
//EIE1= 0x08;//AD0中斷
//IT01CF= 0x00;//P00為外部中斷0 低電平有效
IE= 0x00;//PS0 T0 enable
EIE2= 0x08;//ADC1 enable
EIP2= 0x08;//ADC1 high class
IT0= 1;//INT0 下降沿中斷
IT1= 1;
}
// Initialization function for device,
// Call Init_Device() from your main program
void init_var()
{
Flag8=0;
bfirst=0;
//cnt_send=0;
measurements=0;
sample_mode=0;
send_interval=1;
}
void Uart_Reset(void)
{
//串口0
SCON0=0x50;//[sm00 sm10] sm20 [ren0] tb80 rb80 ti0 ri0
//T2 for 波特率
//baud=SYSCLK/(32*(65536-(RCAP2H:RCAP2L)))
//4800: 0xffd8
// [ 9600: 0xffdc ]
//19200:0xffee
TH2=0xff;
TL2=0xb8;
RCAP2H=0xff;
RCAP2L=0xb8;
//TH2=0xff;
//TL2=0xdc;
//RCAP2H=0xff;
//RCAP2L=0xdc;
T2CON=0x34;//TF2 EXF2 [RCLK0] [TCLK0] EXEN2 [TR2] C/T2 CP/RL2
ClearCommSendBuffer();
}//void Uart_Reset(void);
void Init_Device(void)
{
SysClk_Reset();
PORT_Reset();
Timer0_Init();
ADC_Init();
Uart_Reset();
Interrupts_Init();
//init_var();
}
ch372.c
// ------------------------------------------------------------------------------------------------------
#include
#define ERR_SUCCESS0x00// 操作成功
#define ERR_PARAMETER0x10// 參數(shù)錯誤
#define ERR_UNSUPPORT0x80// 命令不支持
#define ERR_UNDEFINED0xFF// 未定義的錯誤
//#define THIS_FIRMWARE_VER0x10// 固件版本號
//#define THIS_APP_SYS_VER0x10// 應(yīng)用系統(tǒng)版本號
//#define THIS_APP_SYS_STR"RA50"http:// 應(yīng)用系統(tǒng)描述字符串
#define OP_CMD1// 命令操作碼
#define OP_DATA0// 數(shù)據(jù)操作碼
#define DELAY_TIMES2// 延時2微秒
#define DELAY_CNT_NUMS16*DELAY_TIMES// 延時2微秒所需的時鐘周期數(shù)(對于22.1184MHz晶振,每個微秒包含22.1184個時鐘周期)
//#define MY_USB_VENDOR_ID0x4348// USB設(shè)備制造商標識
//#define MY_USB_PRODUCT_ID0x5537// USB設(shè)備產(chǎn)品標識
sbitUSB_CS=P2^4;
sbitUSB_INT=P3^0;
sbitUSB_A0=P2^3;
sbitUSB_WR=P2^1;
sbitUSB_RD=P2^2;
sfrUSB_DATA=0x96;
//#define get_usb P7
//#define set_usb(dat1) P7=dat1