:2.534KB : :1 :2022-10-06 16:46:38
usb示例代码(pic16f690)如果开发者对于本文件有需要的可以参考。
#include
__CONFIG(0X3BE4);
//关闭看门狗,选择高速晶振,上电延时复位,掉电复位使能,代码保护
//#include "tab1.h"
#include "tab2.h"
#include "dianzhen12864.h"
#include "AD.h"
unsigned char ReadEE(unsigned char addr);
void WriteEE(unsigned char addr,unsigned char data); //写EEPROM
void get_AD();
uchar hour=0; //时分秒
uchar min=0;
uchar sec=0;
uint mah;//容量 mah表示
uint mahYS;//容量 mah表示
uchar valL;
uchar valH;
unsigned long int mas=0;//ma秒,3600mas=1mah
bit GL=0;
#define YX RA2
#define SET RB4
#define CC RB5//CC
#define BG RA5
uchar ad1,ad2,ad3,ad4;
uchar i1,i2,i3,i4,i5,i6,i7;
void delay(uint x);
void init();
void timer1_init();
uchar counter=0;//计数器
void timer1_init()
{
GIE=1;
PEIE=1;
PIE1=1;
T1CON=0B00110101;
// T1CKPS0=1;T1CKPS1=1; //1:8
// TMR1CS=0;
TMR1IE=1;
TMR1ON=1;
TMR1H=0xfb;
TMR1L=0x1e;
}
/***************************/
//如果充电电流小于10ma,则认为充电停止
/*****************************/
void interrupt ISR(void)
{
if(TMR1IF==1)
{
TMR1IF=0;
TMR1H=0xfb;
TMR1L=0x1e;
if( counter>=99)
{
counter=0;
YX=~YX;
GL=1;
}
}
}
/**********main***********/
void main()
{
delay(100);
init();
delay(100);
InitLCD();
delay(100);
clear_screen();
/*
DisplayString1608(1, 20,"-" );
DisplayString1608(5, 28,"U" );
DisplayString1608(5, 36,"S" );
DisplayString1608(5, 44,"B" );
DisplayString1608(5, 52,"t" );
DisplayString1608(5, 60,"e" );
DisplayString1608(5, 68,"s" );
DisplayString1608(5, 76,"t" );
//DisplayString1608(1, 84,"-" );
*/
timer1_init();
YX=0;
delay(10);
YX=1;
delay(1000);
YX=0;
delay(100);
valL=ReadEE(0x02);
delay(10);
valH=ReadEE(0x03);
delay(10);
mah=valL valH*256;
delay(10);
mahYS=mah;
sec=ReadEE(0x04);
delay(10);
min=ReadEE(0x05);
delay(10);
hour=ReadEE(0x06);
delay(10);
DisplayString1608(7, 36,"V" );
DisplayString1608(7, 96,"m" );
DisplayString1608(7, 104,"A" );
DisplayString1608(7, 112,"h" );
DisplayString1608(1, 36,"A" );
DisplayString1608(1, 104,"W" );
DisplayString1608(1, 112,"h" );
while(1)
{
get_AD();
i1=hour/100;
i2=hour%100/10;
i3=hour%10;
i4=min/10;
i5=min%10;
i6=sec/10;
i7=sec%10 ;
WriteEE(0x04,sec);
WriteEE(0x05,min);
WriteEE(0x06,hour);
DisplayNumber1608(3, 0,i1);
DisplayNumber1608(3, 8,i2 );
DisplayNumber1608(3, 16,i3 );
DisplayString1608(3, 24,":");
DisplayNumber1608(3, 28,i4 );
DisplayNumber1608(3, 36,i5 );
DisplayString1608(3, 44,":" );
DisplayNumber1608(3, 48,i6 );
DisplayNumber1608(3, 56,i7 );
if((SET==0)&&(CC==1))
{
delay(600);
if(SET==0)
{BG=~BG;}
}
}
}
/*************************************/
void delay(unsigned int x)
{
uchar a,b;
for(a=x;a>0;a--)
for(b=110;b>0;b--);
}
void init()
{
ANSEL=0X30; //AN4-5
ANSELH=0X00; //AN10-AN11
delay(10);
TRISB=0x30;
PORTB=0XFF;
TRISA=0x00;
PORTA=0x00;
delay(10);
TRISC=0x03;
PORTC=0x00;
ADCInit();
}
void get_AD()
{
static float temp;
uint wh;
uint temp2;
uint k,j;
uchar t1,t2,t3,t4,t5;
k=ADCAve(AN4,100);
delay(10);
temp=k*0.0195;
//ad4=(int)temp/10; //暂存A/D转换结果
ad1=(int)temp; //个位
ad2=((int)(temp*10)-ad1*10) ; //小数点第一位
ad3=((int)(temp*100)-ad1*100-ad2*10); //小数点第二位
if(ad1>=10)
{DisplayNumber1608(7, 0, 1);}
else{DisplayNumber1608(7, 0, 0);}
// delay(10);
DisplayNumber1608(7, 8, ad1);
//delay(100);
DisplayString1608(7,16,".");
DisplayNumber1608(7, 20,ad2 );
DisplayNumber1608(7,28,ad3 );
delay(50);
j=ADCAve(AN5,120);
delay(30);
//temp=j*0.018;
temp2=(j*13);
temp2=temp2-26; //暂存A/D转换结果
ad1=temp2/1000; //个位
ad2=temp2%1000/100; //小数点第一位
ad3=temp2%100/10; //小数点第二位
ad4=temp2%10; //小数点第三位
/**************************/
if(SET==0)
{
delay(300);
if(CC==0)
{ mah=0;
mahYS=0;
}
delay(800);
if(CC==0)
{
sec=0;
min=0;
hour=0;
mas=0;
}
}
if(CC==0)
{
delay(1000);
if(CC==0)
{wh=0;}
}
valL=mah%256;
valH=mah/256;
//mah=mahYS;
WriteEE(0x02,valL);
delay(1);
WriteEE(0x03,valH);
/****************************/
if(temp2>30)
{
if(GL==1)
{
GL=0;
mas =temp2;
mah=mas/3600;
delay(1);
wh=(temp*mah);
delay(1);
mah =mahYS;
// mahYS=0;
sec ;
if(sec==60)
{
sec=0;
min ;
if(min==60)
{
min=0;
hour ;
}
}
}
}
/*
// mahYS=0;
valL=mah%256;
valH=mah/256;
//mah=mahYS;
WriteEE(0x02,valL);
delay(1);
WriteEE(0x03,valH);
delay(1);
*/
t5=mah/10000;
t4=mah%10000/1000;
t3=mah%1000/100;
t2=mah%100/10 ;
t1=mah%10;
DisplayNumber1608(7, 56,t5);
DisplayNumber1608(7, 64,t4 );
DisplayNumber1608(7, 72,t3 );
DisplayNumber1608(7, 80,t2);
DisplayNumber1608(7, 88,t1 );
/******************Wh**********************/
//wh=temp*mah;
t5=wh/10000;
t4=wh%10000/1000;
t3=wh%1000/100;
t2=wh%100/10 ;
//t1=wh%10;
DisplayNumber1608(1, 64,t5 );
DisplayNumber1608(1, 72,t4 );
DisplayString1608(1,80,".");
DisplayNumber1608(1, 84,t3);
DisplayNumber1608(1, 92,t2 );
//DisplayNumber1608(1, 96,t1);
/************************************************/
// DisplayString1608(5, 0, "0");
DisplayNumber1608(1, 0, ad1);
delay(10);
DisplayString1608(1,8,".");
DisplayNumber1608(1, 12, ad2);
DisplayNumber1608(1, 20,ad3 );
DisplayNumber1608(1,28,ad4 );
//delay(100);
// delay(10);
}
//读EEPROM
unsigned char ReadEE(unsigned char addr) //读EEPROM
{
unsigned char num;
while(RD==1); //上一次读操作是否完成
EEADR=addr; //0x01 //EEPROM地址为00H
EEPGD=0; //指向EEPROM数据储存器
RD=1;
do{;} //开始读
while(RD==1); //等待读操作完成
num=EEDATA;
//读出
return(num); //返回读出的数
}
//写EEPROM
void WriteEE(unsigned char addr,unsigned char data) //写EEPROM
{
while(WR==1); //上一次写操作是否完成
EEADR=addr;//0X02 //EEPROM地址
EEDATA=data; //0X05 //准备写入EEPROM的数据
EEPGD=0; //指向EEPROM数据储存器
WREN=1; //使能写操作
GIE=0; //禁止中断
EECON2=0x55; //设置通用参数
EECON2=0xAA; //设置通用参数
WR=1;
GIE=1;
do{;} //开始写
while(WR==1); //等待写操作完成
if(EEIF==1)
{
EEIF=0;
}
WREN=0; //禁止写操作
}
02-02驱动键鼠的硬件USB通讯
10-19STM32F03 USB+PCB+封装库
10-16VC操作查找USB的代码
10-16USBVIEW USB 数据的比对
10-16MCU usb通讯demo
10-15USB U盘试验(HOST)示例代码