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

📄 clock.c

📁 8515的定时器/计数器的定时、计数、比较输出、PWM功能全验证源程序
💻 C
字号:
#include <iom8515v.h>
#include <macros.h>
#include <eeprom.h>

#define BIT(x)	           (1 << (x))                   //置位寄存器中的第x位
#define SETBIT(x, y)   	   (x |= y)
#define CLRBIT(x, y) 	   (x &= ~y)
#define CHKBIT(x, y) 	   (x & y)
#define SCL           BIT6
#define SDA           BIT7

#define BIT7 0x80
#define BIT6 0x40
#define BIT5 0x20
#define BIT4 0x10
#define BIT3 0x08
#define BIT2 0x04
#define BIT1 0x02
#define BIT0 0x01

unsigned char segtabel[16]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,
				0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};

unsigned char disp_buf[4];
unsigned char min;
unsigned char sec;
unsigned char dsec;
int mmm;

void delay_us(unsigned int useconds)                          //us级延时
{
	for(;useconds>0;useconds--);
}

void WDT_init(void)                                        //看门狗使能,溢出时间1.9s
{
	WDR();
	WDTCR=0x0f;
}

void WDT_off(void)                                         //关闭看门狗
{
	WDTCR=0x18;
	WDTCR=0x00;
}

/*void EEPROM_rite(unsigned int uAddr,unsigned char uData)   //写EPROM,16位地址,8位数据
{
	while(EECR&(1<<EEWE));
	EEAR=uAddr;
	EEDR=uData;
	EECR|=(1<<EEMWE);
	EECR|=(1<<EEWE);
}

unsigned char EEPROM_read(unsigned int uAddr)              //读EEPROM
{
	while(EECR&(1<<EEWE));
	EEAR=uAddr;
	EECR|=(1<<EERE);
	return EEDR;
}
*/
/*
void Start(void)
{
	DDRD|=0xc0;
	SETBIT(PORTD,SCL);
	SETBIT(PORTD,SDA);
	CLRBIT(PORTD,SDA);
}

void Stop(void)
{
	DDRD|=0xc0;
	SETBIT(PORTD,SCL);
	CLRBIT(PORTD,SDA);
	SETBIT(PORTD,SDA);
}
void TestAck(void)
{
	SETBIT(DDRD,SCL);
	CLRBIT(DDRD,SDA);                   //direct=input
	do{
		SETBIT(PORTD,SCL);
		CLRBIT(PORTD,SCL);
	}while (CHKBIT(PIND,SDA));
	SETBIT(DDRD,SDA);
}
void NoAck(void)
{
	SETBIT(DDRD,SCL);
	CLRBIT(DDRD,SDA);                   //direct=input
	do{
	SETBIT(PORTD,SCL);
	CLRBIT(PORTD,SCL);
	}while (!CHKBIT(PIND,SDA));
	SETBIT(DDRD,SDA);
}

void Write8Bit(unsigned char command)
{
	unsigned char i;
	DDRD|=0xc0;
	SETBIT(PORTD,SCL);
	CLRBIT(PORTD,SCL);
	delay_us(1);
	for(i=0;i<8;i++)
	   {if(command&0x80)
	         SETBIT(PORTD,SDA);
	    else CLRBIT(PORTD,SDA);
	    SETBIT(PORTD,SCL);
	    CLRBIT(PORTD,SCL);
	    command<<=1;
	   }
}
unsigned char Read8Bit(void)
{
	unsigned char temp=0;
	unsigned char i;
	SETBIT(DDRD,SCL);
	CLRBIT(DDRD,SDA);                    //direct=input
	for(i=8;i>0;i--)
	   {
	   SETBIT(PORTD,SCL);                   //falling edge(PD6=SCL)
	   CLRBIT(PORTD,SCL);
	   if(PIND&0x01)     temp|=0x01;
	   temp<<=1;
	   }
	 SETBIT(DDRD,SDA);
	 return temp;
}
/==========================================
               I2C总线写一个字节
===========================================/
void i2c_Write(unsigned char Wdata,unsigned char RomAddress)
{
	  Start();
	  Write8Bit(0xa0);
	  TestAck();
	  Write8Bit(RomAddress);
	  TestAck();
	  Write8Bit(Wdata);
	  TestAck();
	  Stop();
 	  delay_us(10000);
}
//==========================================
//          I2C总线读一个字节
//==========================================
unsigned char i2c_Read(unsigned char RomAddress)
      {
	   unsigned char temp;
	   Start();
	   Write8Bit(0xa0);
	   TestAck();
	   Write8Bit(RomAddress);
	   TestAck();
	   Start();
	   Write8Bit(0xa1);
	   TestAck();
	   temp=Read8Bit();
	   NoAck();
	   Stop();
	   return temp;
      }
*/
//==========================================
void sys_init(void)
{
	DDRA=0xff;                                         //LED位选
	DDRC=0xff;                                         //LED段选
	DDRD|=0xc0;
// 	TIMSK|=(1<<TOIE1);                               //触发定时器中断
	TIMSK=0x80;
	TCCR1B=0x00;
	TCNT1H=0xf3;
	TCNT1L=0xcb;
	TCCR1A=0x00;
	TCCR1B=0x04;
	GICR=0x40;                                         //开外部中断
	SEI();
	WDT_init();
	EEPROM_WRITE(0x0123,mmm);                          //等价于EEPROMwrite(0x123,0xe6)
}


void disp(unsigned char *dispbuff)
{
	unsigned char i,j;
	for(i=0;i<4;i++)
	{
		PORTA=(1<<i);
		PORTC=segtabel[*dispbuff];
		if(i==1) PORTC&=0x7f;                       //小数点
		else if(i==3) PORTC&=0x7f;
		else PORTC|=0x80;
		dispbuff+=1;
		//delay_1ms();
		delay_us(500);
	}
}
void main(void)
{
	MCUCR=3;                                           //上升沿触发INT0
	mmm=0xf5;                                          //待写入EEPROM的数据
	sys_init();
//	i2c_Write(0xc6,0x21);
//	i2c_Read(0x21);
	for(;;)
	{
		disp(disp_buf);
		WDR();
//		WDT_off();
	}
}

//每0.1秒显示器增1
#pragma interrupt_handler timer1_ovf_isr:7
void timer1_ovf_isr(void)
{
	TCNT1H=0xf3;
	TCNT1L=0xcb;
	dsec++;
	if (dsec>9)
	{
		dsec=0;
		sec++;
	}
	if (sec>59)
	{	sec=0;
		min++;
	}
	if(min==10)
	{
		DDRD|=0x04;                                //软件触发中断
		GICR|=0x40;
		PORTD&=~0x04;
		PORTD|=0x04;
	}
	disp_buf[0]=dsec;
	disp_buf[1]=sec%10;
	disp_buf[2]=sec/10;
	disp_buf[3]=min%10;
}

//将计数器清零,可由硬件或软件触发
#pragma interrupt_handler int0_isr:2
void int0_isr(void)
{
	unsigned char tmp,tmp1;
	TCCR1B=0x00;
	EEPROM_READ(0x123,tmp);                        //等价于tmp=EEPROMread(0x123);
//	i2c_Write(0xc6,0x21);
//	tmp=i2c_Read(0x21);
	tmp1=tmp%100;
	disp_buf[0]=tmp1%10;
	disp_buf[1]=tmp1/10;
	disp_buf[2]=tmp/100;
}

	

⌨️ 快捷键说明

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