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

📄 30miao.c

📁 用C语言实现单片机的30S
💻 C
字号:
#include <c8051f000.h>                 // SFR declarations
#include<intrins.h>
  // Reload value for Timer2

void Timer2_Init (void);               // Timer2 initialization routine
sfr16 RCAP2 = 0xCA;                    // Timer2 Reload Register
sfr16 TMR2 = 0xCC;                     // Timer2 Register
sbit rd     = P1^7;
sbit re     = P1^5;
sbit wr		= P1^6;
sbit addr0	= P1^0;
sbit addr1	= P1^1;
sbit addr2	= P1^2;
sbit addr3	= P1^3;
sbit addr4	= P1^4;
sbit data0	= P2^0;
sbit data1	= P2^1;
sbit data2	= P2^2;
sbit data3	= P2^3;
sbit lcd_en = P3^0;
int n,m,l;   unsigned char led=0xfe;
int hour=0,minute=0,second=0,percent=0,percent1=0; 
void write (int addr, int datain);
unsigned int read();
void OSCILLATOR_Init (void);           
void PORT_Init (void);
void Timer2_Init(void);
void Timer1_Init(void);


void Timer2_ISR (void) interrupt 5
{

	percent++;
	if(percent==50)
	{write(0,led );  led=~led;led|=0xfe;}
	
	if(percent==100) 
	{
		percent=0;
		second++; 
		
		if(second%10==0) {write(8,0);}
		else {write(8,0);}
	}
	if(second==30){second=0;}

	write(1,second%10);
	write(2,second/10);  

    TF2 = 0;                       // Reset Interrupt
}




  
  
  void main (void)
{
   
   WDTCN = 0xde;                       // Disable watchdog timer
   WDTCN = 0xad;
   OSCILLATOR_Init ();                 // Initialize Oscillator
   PORT_Init();                        // Initialize Port I/O
   lcd_en=0;	
   Timer2_Init (); 
                   // Initialize the Timer2
	OSCICN = 0x08; 
	write(0,15);  
    EA = 1;  
   	while (1);
                                     // end of while(1)
}                                      // end of main()

unsigned int read()
{
   unsigned int data_rd;
   PRT2CF  &= 0xf0;
   P2=0xff;
   rd=0;
   data_rd=P2;
   data_rd &=0x0f;
   rd=1;
   PRT2CF  |= 0xff; 
   return data_rd;
}

void OSCILLATOR_Init (void)
{
   OSCICN |= 0x03;                     // Configure internal oscillator for
   OSCXCN=0xff;                       // its maximum frequency (24.5 Mhz)
}

void PORT_Init (void)
{
   PRT1CF  |= 0xff;                    
   PRT2CF  |= 0xff; 
                                    
   XBR0    = 0x00;
   XBR1    = 0x00;
   XBR2    = 0x40;                     // Enable crossbar and enable
}
void write (int addr, int datain)
{
    char pp;
    PRT2CF  |= 0xff; 
    pp=addr;
	pp|= 0xA0;
	P1 =pp;
	P2=datain;
	wr=1;	
	wr=0;
}

void Timer2_Init(void)
{
   CKCON =0x00;                   // Timer2 uses SYSCLK/12
   RCAP2 = 60927;;              // Timer2 reload value
   TMR2 = RCAP2;                       // Initialize the timer
   T2CON = 0x04;                       // Enable Timer2 in Reload Mode
   ET2 = 1;                            // Timer2 interrupt enabled
}


⌨️ 快捷键说明

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