📄 benkesheji.c
字号:
//------------------------------------------------------------------------------------
// Blinky.c
//------------------------------------------------------------------------------------
// Copyright (C) 2005 Silicon Laboratories, Inc.
//
// AUTH: BW
// DATE: 4 SEP 01
//
// This program flashes the green LED on the C8051F020 target board about five times
// a second using the interrupt handler for Timer3.
// Target: C8051F02x
//
// Tool chain: KEIL Eval 'c'
//
//------------------------------------------------------------------------------------
// Includes
//------------------------------------------------------------------------------------
#include <c8051f020.h> // SFR declarations
//------------------------------------------------------------------------------------
// 16-bit SFR Definitions for 'F02x
//------------------------------------------------------------------------------------
sfr16 DP = 0x82; // data pointer
sfr16 TMR3RL = 0x92; // Timer3 reload value
sfr16 TMR3 = 0x94; // Timer3 counter
sfr16 ADC0 = 0xbe; // ADC0 data
sfr16 ADC0GT = 0xc4; // ADC0 greater than window
sfr16 ADC0LT = 0xc6; // ADC0 less than window
sfr16 RCAP2 = 0xca; // Timer2 capture/reload
sfr16 T2 = 0xcc; // Timer2
sfr16 RCAP4 = 0xe4; // Timer4 capture/reload
sfr16 T4 = 0xf4; // Timer4
sfr16 DAC0 = 0xd2; // DAC0 data
sfr16 DAC1 = 0xd5; // DAC1 data
//------------------------------------------------------------------------------------
// Global CONSTANTS
//------------------------------------------------------------------------------------
#define uint unsigned int
#define uchar unsigned char
#define SYSCLK 2000000 // approximate SYSCLK frequency in Hz
#define T2_Freq 1000
sbit LED = P2^6; // green LED: '1' = ON; '0' = OFF
sbit CS1 = P2^5;
sbit CS2 = P2^4;
sbit ADCS =P0^2; //ADC0832 chip seclect
sbit ADDI =P0^0; //ADC0832 data in
sbit ADDO =P0^1; //ADC0832 data out
sbit ADCLK =P0^3; //ADC0832 clock signal
sbit DIG9=P0^4;
sbit DIG10=P0^5;
sbit DIG11=P0^6;
sbit DIG12=P0^7;
sbit BUT1=P2^0;
sbit BUT2=P2^1;
sbit BUT3=P2^2;
sbit BUT4=P2^3;
unsigned char code tab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x0,0x0};//数码管共阳级显示0~9,不显示
uint ad_0832_1,ad_0832_2,ad_data1,ad_data2,ad_data3,ad_data4,ad_data5,ad_data6;
uchar channel=0;
uchar num=4;
//------------------------------------------------------------------------------------
// Function PROTOTYPES
//------------------------------------------------------------------------------------
void PORT_Init (void);
void Timer2_Init (int counts);
void Timer2_ISR (void);
void display(void);
void delayms(unsigned int loop);
uchar Adc0832(unsigned char channel);
void Delay(unsigned char x);
//------------------------------------------------------------------------------------
// MAIN Routine
//------------------------------------------------------------------------------------
void main (void) {
// disable watchdog timer
WDTCN = 0xde;
WDTCN = 0xad;
PORT_Init ();
Timer2_Init (SYSCLK / 12 / T2_Freq ); // Init Timer3 to generate interrupts
// at a 10Hz rate.
EA = 1; // enable global interrupts
LED=0;
while (1)
{
if(num==1)
{
P4=tab[1];
P3=0xfe;
}
if(num==2)
{
P4=tab[2];
P3=0xfe;
}
if(num==3)
{
P4=tab[3];
P3=0xfe;
}
if(num==4)
{
display();
}
CS1=1;
CS2=1;
P1=ad_0832_1*20/21;
CS1=0;
delayms(1);
CS1=1;
CS2=1;
P1=ad_0832_2*20/21;
CS2=0;
delayms(1);
CS1=1;
CS2=1;
if(ad_0832_1<52||ad_0832_2<52)
{
LED=1;
}
if(ad_0832_1>204||ad_0832_2>204)
{
LED=1;
}
if((ad_0832_1>=52&&ad_0832_1<=204)&&(ad_0832_2>=52&&ad_0832_2<=204))
{
LED=0;
}
}
}
//------------------------------------------------------------------------------------
// PORT_Init
//------------------------------------------------------------------------------------
//
// Configure the Crossbar and GPIO ports
//
void PORT_Init (void)
{
XBR2 = 0x40; // Enable crossbar and weak pull-ups
P1MDOUT |= 0xff; // enable DI,CS,CLK as push-pull output
P2MDOUT |= 0xf0; // enable P2.6 (LED),CS1,CS2 as push-pull output
P74OUT=0x03; //enable P4-P7 as a push-pull output
}
//------------------------------------------------------------------------------------
// Timer2_Init
//------------------------------------------------------------------------------------
//
// Configure Timer3 to auto-reload and generate an interrupt at interval
// specified by <counts> using SYSCLK/12 as its time base.
//
void Timer2_Init (int counts)
{
T2CON = 0x00; // Stop Timer3; Clear TF3;
CKCON &= 0xdf; // use SYSCLK/12 as timebase
RCAP2 = -counts; // Init reload values
T2 = 0xffff; // set to reload immediately
ET2 = 1; // enable Timer2 interrupts
TR2 = 1; // start Timer2
}
//------------------------------------------------------------------------------------
// Interrupt Service Routines
//------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------
// Timer2_ISR
//------------------------------------------------------------------------------------
// This routine changes the state of the LED whenever Timer3 overflows.
//
void Timer2_ISR (void) interrupt 5
{
static unsigned int T2times=0;
static unsigned int ButScan=0;
TF2=0; // clear TF2
T2times++;
ButScan++;
if(T2times<=1000)
{
channel=0;
}
if(T2times>1000&&T2times<=2000)
{
channel =1;
}
if(T2times>2000)
{
T2times=0;
}
if(ButScan>10)
{
if(BUT1==0)
{
num=1;
}
if(BUT2==0)
{
num=2;
}
if(BUT3==0)
{
num=3;
}
if(BUT4==0)
{
num=4;
}
ButScan=0;
}
}
void delayms(unsigned int loop)
{ unsigned int i,j; //loop 为执行空指令的次数,改变它可一改变延时时长
for(j=0;j<50;j++)
for(i=0;i<loop;i++); //循环执行空指令loop次,达到延时目的
}
void Delay(unsigned char x)
{
unsigned char i;
for(i=0;i<x;i++); //延时,脉冲一位持续的时间
}
/************
读ADC0832函数
************/
uchar Adc0832(unsigned char channel)
{
uchar i=0;
uchar j=0;
uchar dat=0;
uchar ndat=0;
if(channel==0)channel=2;
if(channel==1)channel=3;
ADDI=1;
Delay(1);
ADCS=0;//拉低CS端
Delay(1);
ADCLK=1;//拉高CLK端
Delay(1);
ADCLK=0;//拉低CLK端,形成下降沿1
Delay(1);
ADDI=(channel>>1)&0x01;
Delay(1);
ADCLK=1;//拉高CLK端
Delay(1);
ADCLK=0;//拉低CLK端,形成下降沿2
Delay(1);
ADDI=channel&0x01;
Delay(1);
ADCLK=1;//拉高CLK端
Delay(1);
ADCLK=0;//拉低CLK端,形成下降沿3
ADDI=1;
Delay(1);
dat=0;
for(i=0;i<8;i++)
{
dat|=ADDO;//收数据
ADCLK=1;
Delay(1);
ADCLK=0;//形成一次时钟脉冲
Delay(1);
dat<<=1;
if(i==7)dat|=ADDO;
}
for(i=0;i<8;i++)
{
j=j|ADDO;//收数据
ADCLK=1;
Delay(1);
ADCLK=0;//形成一次时钟脉冲
Delay(1);
j=j<<7;
ndat=ndat|j;
if(i<7)ndat>>=1;
}
ADCS=1;//拉高CS端
ADCLK=0;//拉低CLK端
ADDO=1;
return(dat); //return ad data
}
void display(void)
{
ad_0832_1=Adc0832(0);
ad_0832_2=Adc0832(1);
ad_data1=(ad_0832_1*49/25)/100; //the highest bit, 255*49/25=4.99V 百位
ad_data2=(ad_0832_1*49/25)/10%10; //十位
ad_data3=(ad_0832_1*49/25)%100%10; //个位
ad_data4=(ad_0832_2*49/25)/100; //the highest bit, 255*49/25=4.99V 百位
ad_data5=(ad_0832_2*49/25)/10%10; //十位
ad_data6=(ad_0832_2*49/25)%100%10; //个位
P3=0x00;//通道二显示
P4=tab[ad_data6];
P3=0xfe;
delayms(1);
P3=0xff;
P4=tab[ad_data5];
P3=0xfd;
delayms(1);
P3=0xff;
P4=tab[ad_data4];
P3=0xfb;
P4=P4&0x7f;
delayms(1);
P3=0xff;
P4=0xbf;
P3=0xf7;
delayms(1);
P3=0xff;
P4=tab[2];
P3=0xef;
delayms(1);
P3=0xff;//通道二显示结束
P4=tab[ad_data3];//通道一显示
P3=0x7f;
delayms(1);
P3=0xff;
P4=tab[ad_data2];
DIG9=0;
DIG10=1;
DIG11=1;
DIG12=1;
delayms(1);
P0=P0|0xf0;
P4=tab[ad_data1];
P4=P4&0x7f;
DIG9=1;
DIG10=0;
DIG11=1;
DIG12=1;
delayms(1);
P0=P0|0xf0;
P4=0xbf;
DIG9=1;
DIG10=1;
DIG11=0;
DIG12=1;
delayms(1);
P0=P0|0xf0;
P4=tab[1];
DIG9=1;
DIG10=1;
DIG11=1;
DIG12=0;
delayms(1);
P0=P0|0xf0;//通道一显示结束
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -